var aig_isOpen = false;
jQuery.fn.aImageGallery = function(options, images){
		
	var settings = jQuery.extend({
		onClick : ig_imageSwap,
		onMouseover : ig_highlight,
		loadInto : '#background',
		width : 400,
		fitWidth : true,
		scroll : false,
		hoverArea : 0.15, // max .5
		maxSpeed : 1.4,
		defaultImage : 0
		}, options);
	
	var aigGallery = jQuery(this);
	
	aigGallery.wrap('<div id="aigContainer"></div>');
	
	var aigContainer = jQuery(this).parent();
	
	jQuery(settings.loadInto).one('load', function(){
		wideImage = imgBox.children('#aigImage_' + settings.defaultImage).width() > imgBox.children('#aigImage_' + settings.defaultImage).height() ? true : false;
		jQuery(this).css({
			width : wideImage === true ? '100%' : ((jQuery('#menu_practice').position().left + jQuery('#menu_practice').width()) / jQuery('#container').width() * 100) + '%'
			});
		});
	//alert(jQuery(settings.loadInto).attr('src'));

	// empty the container before starting, to make sure there's nothing in there
	aigGallery.empty();
	aigContainer.width(settings.width);
	aigGallery.css({
		overflow : 'hidden'
		});
	
	// create the image box
	aigGallery.append('<div id="aigImageBox"></div>');
	var imgBox = aigGallery.children('#aigImageBox');
	
	var width = 0;
	for(var i in images){
		var img = images[i];
		if(img.defaultImage ===	 true){
			settings.defaultImage = i;
			}
		imgBox.append('<img src="' + (img.thumb ? img.thumb : img.src) + '" large="' + img.src + '" id="aigImage_' + i + '" />');
		var thisImage = jQuery('#aigImage_' + i);
		var old_h = img.h; //$.browser.msie ? img.h : thisImage.height();
		var old_w = img.w; //$.browser.msie ? img.w : thisImage.width();
		thisImage.attr({
			fullHeight : old_h,
			fullWidth : old_w
			});

		thisImage.height('100%');
		thisImage.width((thisImage.height() / old_h) * old_w);
		//alert(old_w / old_h + ' ' + thisImage.width() / thisImage.height());
		
		var spacer = $('<img>')
		.attr({
			src : 'images/gallery_spacer.gif',
			'class' : 'gallerySpacer',
			fullHeight : 37,
			fullWidth : 8
			})
		.css('height', '100%')
		.appendTo(imgBox);
		spacer.width((spacer.height() / 37) * 8);

		thisImage.bind('mouseover', typeof(img.onMouseover) == 'function' ? img.onMouseover : settings.onMouseover);
		thisImage.bind('click', typeof(img.onClick) == 'function' ? img.onClick : function(){
			wideImage = jQuery(this).width() > jQuery(this).height() ? true : false;
			/* jQuery(settings.loadInto).css({
				width : wideImage === true ? '100%' : ((jQuery('#menu_practice').position().left + jQuery('#menu_practice').width()) / jQuery('#container').width() * 100) + '%'
				});
			if(wideImage === false && jQuery.browser.safari){
				jQuery(settings.loadInto).width(jQuery(settings.loadInto).width() + 1);
				}
			*/
			jQuery(settings.loadInto).loadImage({
				imageSrc : jQuery(this).attr('large'),
				callback : wideImage === true ? function(){ return true; } : function(){ $('#content').show(); },
				isWide : wideImage
				});
			//attr('src', jQuery(this).attr('large'));
			});
		width += thisImage.width() + spacer.width();
		}
	jQuery(settings.loadInto).attr('src', images[settings.defaultImage].src);
	
	//alert(width + ', ' + aigGallery.width());
	
	//aigGallery.after('<div id="aigLeftEnd"><img src="images/bg_gallery_left_open.png" width="100%" height="100%" /></div>');
	aigGallery.after('<div id="aigLeftEnd"></div>');
	aigGallery.after('<div id="aigLeftMiddle"><img src="images/bg_gallery_left_prev2.png" width="100%" height="100%" /></div>');
	aigGallery.before('<div id="aigRightEnd"><img src="images/bg_gallery_right_next.png" width="100%" height="100%" /></div>');
	
	var aigLeft = jQuery('#aigLeftEnd');
	var aigLeftM = jQuery('#aigLeftMiddle');
	var aigRight = jQuery('#aigRightEnd');
	var _leftWidth = ieLeftWidth; //$.browser.msie ? ieLeftWidth : aigLeft.width();
	var _leftHeight = ieLeftHeight; //$.browser.msie ? ieLeftHeight : aigLeft.height();
	var _rightWidth = ieRightWidth; //$.browser.msie ? ieRightWidth : aigRight.width();
	var _rightHeight = ieRightHeight; //$.browser.msie ? ieRightHeight : aigRight.height();
	
	//$('#status').html(_leftHeight + ', ' + _leftWidth + ', ' + aigLeft.height() + ', ' + aigContainer.width());
	
	aigLeft.height('100%');
	var temp23 = aigLeft.height() / _leftHeight;
	//aigLeft.width((((_leftWidth * temp23) / aigContainer.width()) * 100) + '%');
	aigLeft.width(0);

	aigLeftM.height('100%');
	//var temp24 = aigLeftM.height() / _rightHeight;
	aigLeftM.width(0);
	//aigLeftM.width((((_rightWidth * temp24) / aigContainer.width()) * 100) + '%');

	aigRight.height('100%');
	aigRight.width(0);
	
	jQuery('#aigImageBox :last').after('<div class="aigClear"></div>');
	imgBox.children('img').each(function(){
		imgBox.width(imgBox.width() + $(this).width());
		//alert($(this).width() + ' ' + imgBox.width());
		});
		
	aigLeft.bind('open', function(){
		aigRight.width((((_rightWidth * (aigRight.height() / _rightHeight)) / aigContainer.width()) * 100) + '%');
		aigLeftM.width((((_rightWidth * (aigLeftM.height() / _rightHeight)) / aigContainer.width()) * 100) + '%');
		//jQuery('#status').html((((_rightWidth * (aigRight.height() / _rightHeight)) / aigContainer.width()) * 100) + '%');
			var chh = aigGallery.height();
			var ghh = imgBox.height();
			imgBox.css({
				marginTop : ((chh - ghh) / 2) + 1
				});
		
		var new_width = aigContainer.width() - (aigLeft.width() + aigLeftM.width() + aigRight.width() + 1);
		if(imgBox.width() < new_width){ new_width = imgBox.width(); }
		else{ new_width = ((((aigContainer.width() - (aigLeft.width() + aigLeftM.width() + aigRight.width() + 1)) / aigContainer.width()) * 100) + '%'); }
		
		aigGallery.stop().animate({ 'width' : new_width }, 500, function(){
			//$('#status').html((chh - ghh) / 2);
			});
		
		$(this).unbind('click').click(function(){ $(this).trigger('close'); }).children('img').attr('src', 'images/bg_gallery_left_close.png');
		
		aig_isOpen = true;
	
		if(jQuery.browser.safari){
			resizeBox(true);
			imgBox.aScroll({ direction : 'left', mousePos : { pageX : 1, pageY : 1 } });
			}
		})
	.bind('close', function(){
		aigRight.width(0);
		aigLeftM.width(0);
		aigGallery.stop().animate({ 'width' : 0 }, 500, function(){
			aig_isOpen = false;
			if(jQuery.browser.safari){
				resizeBox(true);
				imgBox.aScroll({ direction : 'left', mousePos : { pageX : 1, pageY : 1 } });
				}
			});
		$(this).unbind('click').click(function(){ $(this).trigger('open') }).children('img').attr('src', 'images/bg_gallery_left_open.png');;
		})
	.click(function(){ $(this).trigger('open'); });
	
	aigRight.click(typeof(settings.nextClick) == 'function' ? settings.nextClick : function(){ return true; });
	aigLeftM.click(typeof(settings.prevClick) == 'function' ? settings.prevClick : function(){ return true; });
	
	if(imgBox.width() > (aigContainer.width() - aigLeft.width() - aigRight.width())){
		aigGallery.mousemove(function(e){
			var x = e.pageX - jQuery(this).parent().position().left - jQuery(this).position().left - (jQuery('#container').position().left - (jQuery('#container').width() / 2));
			var y = e.pageY - jQuery(this).parent().position().top - jQuery(this).position().top - (jQuery('#container').position().top - Math.floor(jQuery('#container').height() / 2));
			var c_width = jQuery(this).width();
			var c_height = jQuery(this).height() + 10;
			
			if(x / c_width < settings.hoverArea){
				imgBox.aScroll({ direction : 'left', mousePos : e });
				}
			else if(x / c_width > (1 - settings.hoverArea)){
				imgBox.aScroll({ direction : 'right', mousePos : e });
				}
			else{
				//jQuery('#status').html('stop');
				imgBox.stop(true);
				}
			});
	
		aigGallery.mouseout(function(e){
			var x = e.pageX - jQuery(this).parent().position().left - jQuery(this).position().left - (jQuery('#container').position().left - (jQuery('#container').width() / 2));
			var y = e.pageY - jQuery(this).parent().position().top - jQuery(this).position().top - (jQuery('#container').position().top - Math.floor(jQuery('#container').height() / 2));
			var c_width = jQuery(this).width();
			var c_height = jQuery(this).height();
			//jQuery('#status').html('mx: ' + x + ', my: ' + y + ', width: ' + c_width + ', height: ' + c_height);
			if(x > 0 && x < c_width && y > 0 && y < c_height){
				return true;
				}
			imgBox.stop(true);
			});
		}
	};

jQuery.fn.aScroll = function(options){
	var settings = jQuery.extend({
		speed : 0.08,
		direction : 'left',
		loop : false,
		mousePos : { pageX : 0, pageY : 0 }
		}, options);
	
	var x = settings.mousePos.pageX - jQuery(this).parent().position().left - 191;
	var y = settings.mousePos.pageY - jQuery(this).parent().position().top - 41;
	
	var rightEnd = (jQuery(this).width() - jQuery(this).parent().width()) * -1;
	var d = jQuery(this).position().left;
	d = d < 0 ? d * -1 : d;
	var distance = settings.direction == 'left' ? d : jQuery(this).width() - jQuery(this).parent().width() - d;
	var speed = settings.speed;
	var time = distance / speed;
	//jQuery('#status').html('distance: ' + distance + 'px, speed: ' + speed + 'px/ms, time: ' + time + 'ms');
	
	if(settings.direction == 'left'){
		jQuery(this).animate({ left : 0 }, time, 'linear');
		}
	else if(settings.direction == 'right'){
		jQuery(this).animate({ left : rightEnd }, time, 'linear');
		}
	return jQuery(this);
	};

jQuery.fn.loadImage = function(options){
	var settings = jQuery.extend({
		imageSrc : '',
		callback : function(){ return true; },
		isWide : false
		}, options);
	//$('#status').html(settings.imageSrc);

	var $this = $(this);
	var $parent = $this.parent();
	
	if ($("#downloadLink").length>0) {
		if (wideImage) {
			$("#downloadLink").css({'zIndex':-1});
		} else {
			$("#downloadLink").css({'zIndex':10});
		}
	}
		
	//$parent.children('#content, #background, #downloadLink').css({ display : 'none' });
	$this.fadeOut(600, function(){
		$(this).css({
			width : settings.isWide === true ? '100%' : ((jQuery('#menu_practice').position().left + jQuery('#menu_practice').width()) / jQuery('#container').width() * 100) + '%'
		});				
		$this.one('load', function(){
			$(this).fadeIn(600, function(){
				if(!settings.isWide){
					$('#content').fadeIn(600);
				}
				$loading.fadeOut(600, function(){
					settings.callback();
				});
			});
		});
	});

	$('#aLoading').fadeOut(600, function(){
		$('#aLoading').remove();
	});
	$('#content').hide()
	$parent.append('<div id="aLoading" style="visibility: hidden;"><b><img src="images/loading.gif" /></b></div>');
	var $loading = $parent.children('#aLoading');
	//$loading.css({
	//	fontSize : '150%'
	//	});
	$loading.width($loading.children('b').width());
	$loading.css({
		position : 'absolute',
		top : '50%',
		left : '50%',
		opacity : 0,
		visibility : 'visible',
		marginTop : '-' + ($loading.height() / 2) + 'px',
		marginLeft : '-' + ($loading.width() / 2) + 'px'
		});
	$loading.animate({opacity : 1}, 600, function(){
		$this.attr('src', settings.imageSrc);
	});
};