//<![CDATA[

/**
slow down expansion rate
add width to img targets when build
noscript - enable js - you're missing out!
add animation speeds & opacity %s as properties
error img transparent background for fade?
scroll thingy moves too far to the right with few images
make rel=lightbox optional when writing gallery
caption breaks if no caption
move fgpadding to param

/**
 * fishGallery 1.0b
 * 
 * A fisheye lens picture gallery written in jQuery
 * Copyright (c) 2008 Andrew Weeks
 * 
 * http://www.poleexercise.co.uk/dev/fishgallery/
 * 
 * 
 * The fisheye part of the gallery is based on
 * Fisheye menu compoment : Interface Elements for jQuery
 * Copyright (c) 2006 Stefan Petre
 * 
 * http://interface.eyecon.ro
 * 
 */

/**
 * @name fishGallery
 * @description A fisheye lens picture gallery
 * @param Hash hash A hash of parameters
 * @option Integer fishWidth Maximum extra width for each image (over width) (px)
 * @option Integer width Minimum width for each image (px)
 * @option Integer margin Distance between image (px)
 * @option Integer proximity Distance from image at which fisheye starts to take effect (px)
 * @option String loadImg Image to display while main picture is loading (URL)
 * @option String errorImg Image to display if main picture cannot be loaded (URL)
 *
 * @type jQuery
 * @cat Um...?
 * @author Andrew Weeks
 */

jQuery.iFishGallery = 
{
	title: "Pole Exercise",
	fading: false,
	fadeSpeed: 20,
	power: { value: 0, end: 0, change: 0, min: 0, max: 1, step: 0.1 },
	opacity: { value: 0.6, end: 1, change: 0, min: 0.6, max: 1, step: 0.05 },
	activeImg: 0,
	pointer: { x: 0, y: 0 },
	fishTemper: 1.5,
	popupViewer: "thickbox",
	setOpacity: false,
	useOpacity: true,
	seenImg: new Array(),
	
	init: function(options)
	{
		return this.each(function()
		{
			fishGallery = jQuery.iFishGallery;
			
			fishGallery.cache = new Image();
			fishGallery.cache.loadingImg = options.loadingImg;
			fishGallery.cache.errorImg = options.errorImg;

			// Stop unsightly flash
			var cacheError = new Image();
			cacheError.src = fishGallery.cache.errorImg;

			fishGallery.makeGallery(this);

			fishGallery.items = jQuery(".FishGallery .FishItem");				
			fishGallery.frame = jQuery(".FishGallery .Frame");
			fishGallery.box = jQuery(".FishGallery .Box");

			fishGallery.items.width = options.width;
			fishGallery.items.fishWidth = options.fishWidth;
			fishGallery.items.fishHeight = options.fishWidth + options.width;
			fishGallery.items.top = options.fishWidth / 2;
			fishGallery.items.margin = options.margin * 2;
			fishGallery.items.proximity = options.proximity;

			fishGallery.first = fishGallery.items[0];
			fishGallery.last = fishGallery.items[fishGallery.items.length - 1];
			
			fishGallery.frame.fgPadding = 30;
			
			fishGallery.box.fgOffsetLeft = 0;
			fishGallery.box.fgLiveLeft = 0;
			fishGallery.box.fgMoveStep = parseInt(fishGallery.frame.css("width")) - fishGallery.items.width - options.margin - fishGallery.frame.fgPadding * 2;
			fishGallery.box.fgWidth = (fishGallery.items.width + options.margin) * (fishGallery.items.length + 1) + options.margin + fishGallery.frame.fgPadding;
			fishGallery.box.fgLiveWidth = fishGallery.box.fgWidth;
			// Generous safety margin
			fishGallery.box.css("width", (fishGallery.box.fgWidth + fishGallery.items.fishWidth * 10) + "px");
			
			fishGallery.positionGallery();
			
			jQuery(".FishGallery .ImgFrame").click(function()
			{
				if (fishGallery.cache.fgError) return false;

				if (fishGallery.popupViewer == "thickbox")
				{
					tb_show(this.title, this.href, false);
					return false;
				}
				else if (fishGallery.popupViewer == "lightbox")
				{
					myLightbox.start(this);
					return false;
				}
			});

			jQuery(window).resize(function()
			{
				fishGallery.positionGallery();
			});
			
			jQuery(".FishItem a").click(function()
			{
				var link = jQuery(this);
				var bigLink =  jQuery(this).next("a");
				// Image to display in gallery
				fishGallery.cache.fgSrc = link.attr("href");
				fishGallery.cache.fgCaption = link.attr("title");
				// Image for Thickbox / Lightbox
				fishGallery.cache.fgHref = bigLink.attr("href");
				fishGallery.cache.fgTitle = bigLink.attr("title");

				jQuery(".FishGallery .SelectedImg").removeClass("SelectedImg");
				link.children("img").addClass("SelectedImg");
				
				if (fishGallery.cache.fgSrc != jQuery(".FishGallery .BigImg#img" + fishGallery.activeImg).attr("src"))
				{
					jQuery(".FishGallery .LoadingImg").show();
				}
				fishGallery.cache.src = fishGallery.cache.fgSrc;
				
				// Opera doesn't trigger the load event if the image has been loaded before.
				if (jQuery.browser.opera && fishGallery.seenImg[fishGallery.cache.fgSrc] != undefined)
				{
					fishGallery.changeImg(false);
				}
				return false;
			});

			jQuery(fishGallery.cache).load(function() { fishGallery.changeImg(false); }).error(function() {	fishGallery.changeImg(true); });

			//jQuery(document).mousemove(fishGallery.fishMouseMove);
			jQuery(".InfluenceBox").mousemove(fishGallery.fishMouseMove);

			jQuery(window).load(function() { jQuery(".FishGallery .FishItem:first a:first").triggerHandler("click"); });

			jQuery(".FishGallery #arrowLeft").click(function() { fishGallery.moveLeft(); });

			jQuery(".FishGallery #arrowRight").click(function() { fishGallery.moveRight(); });
			
			jQuery(".FishGallery .FishArrow").hover(function()
			{
				fishGallery.fishFadeDown(false);
			}, function() 
			{
				fishGallery.fishFadeUp(false);
			});
			
			jQuery(".FishGallery .InfluenceBox").hover(function()
			{
				fishGallery.fishFadeUp(true);
			}, function()
			{
				fishGallery.fishFadeDown(true);
			});
		})
	},
	
	makeGallery: function(gallery)
	{
		// Go 'live'
		jQuery(gallery).addClass("JQuery")
		
		// Create the gallery
		.append("<div class=\"Top\">" + fishGallery.title + "</div>"
			+ "<div class=\"Bottom\"></div>"
			+ "<a class=\"ImgFrame thickbox\">"	//onclick=\"myLightbox.start(this); return false;\" rel=\"lightbox\"
			+ "<img class=\"LoadingImg\" src=\"" + fishGallery.cache.loadingImg + "\" />"
			+ "<div class=\"ImgBox\"><img class=\"BigImg\" id=\"img0\" style=\"display: none;\" />" + (jQuery.browser.msie && jQuery.browser.version < 7 ? "<img class=\"Spacer\" src=\"" + fishGallery.cache.loadingImg + "\" />" : "<span class=\"Spacer\">&nbsp;</span>") +  "</div>"
			+ "<div class=\"ImgBox\"><img class=\"BigImg\" id=\"img1\" style=\"display: none;\" />" + (jQuery.browser.msie && jQuery.browser.version < 7 ? "<img class=\"Spacer\" src=\"" + fishGallery.cache.loadingImg + "\" />" : "<span class=\"Spacer\">&nbsp;</span>") +  "</div>"
			+ "</a>"
			+ "<div class=\"Caption\"></div>"
			+ "<div class=\"InfluenceBox\">"
			+ "<div class=\"FishArrow\" id=\"arrowLeft\"></div>"
			+ "<div class=\"FishArrow\" id=\"arrowRight\"></div>"
			+ "<div class=\"Frame\">"
			+ "<div class=\"Box\">"
			+ "</div></div></div>");
		
		// Move the thumbnails inside the gallery
		jQuery(".FishGallery .Box").append(jQuery(".FishGallery .FishItem"));
		
		// sort selectedimg load default add title to second a if blank
		jQuery(".FishGallery .FishItem").each(function()
		{
			var link = jQuery(this).children("a:first");
			var bigLink = jQuery(this).children("a:last");
			var title;

			this.fgExtraWidth = 0;
			jQuery(this).css("position", "absolute");
			bigLink.css("display", "none").attr("title", title = bigLink.attr("title") == undefined ? link.attr("title") : title);
		});
	},

	changeImg: function(error)
	{
		fishGallery.cache.fgError = error;
		jQuery(".FishGallery .LoadingImg").hide();
		jQuery(".FishGallery .Caption").html(fishGallery.cache.fgCaption);
		
		if (fishGallery.cache.fgSrc != jQuery(".FishGallery .BigImg#img" + fishGallery.activeImg).attr("src"))
		{
			if (jQuery.browser.opera && !error)
			{
				fishGallery.seenImg[fishGallery.cache.fgSrc] = true;
			}
			jQuery(".FishGallery .BigImg#img" + fishGallery.activeImg).fadeOut(500);
			jQuery(".FishGallery .BigImg#img" + (fishGallery.activeImg = (fishGallery.activeImg + 1) % 2)).attr("src", error ?  fishGallery.cache.errorImg : fishGallery.cache.fgSrc).fadeIn(500);
			jQuery(".FishGallery .ImgFrame").attr({ href: error ? "" : fishGallery.cache.fgHref, title: fishGallery.cache.fgTitle }).css("cursor", error ? "default" : "pointer");
		}
	},
	
	fishFadeUp: function(changeOpacity)
	{
		with (fishGallery)
		{
			if (changeOpacity)
			{
				fishGallery.fishFade(power.step, power.min, power.max, opacity.step, opacity.min, opacity.max);
			}
			else
			{
				fishGallery.fishFade(power.step, power.min, power.max, 0, 1, 1);
			}
		}
	},
	
	fishFadeDown: function(changeOpacity)
	{
		with (fishGallery)
		{
			if (changeOpacity)
			{
				fishGallery.fishFade(-power.step, power.max, power.min, -opacity.step, opacity.max, opacity.min);
			}
			else
			{
				fishGallery.fishFade(-power.step, power.max, power.min, 0, 1, 1);
			}
		}
	},
	
	fishFade: function(powerChange, powerStart, powerEnd, opacityChange, opacityStart, opacityEnd)
	{
		fishGallery.power.change = powerChange;
		fishGallery.power.end = powerEnd;
		fishGallery.opacity.change = opacityChange;
		fishGallery.opacity.end = opacityEnd;

		if (!fishGallery.fading)
		{
			fishGallery.power.value = powerStart;
			fishGallery.fading = true;
			fishGallery.opacity.value = opacityStart;
			
			var timer = setInterval(function() 
			{
				fishGallery.power.value = fishGallery.power.change > 0 ? Math.min(fishGallery.power.end, fishGallery.power.value + fishGallery.power.change) : Math.max(fishGallery.power.end, fishGallery.power.value + fishGallery.power.change);
				fishGallery.opacity.value = fishGallery.opacity.change > 0 ? Math.min(fishGallery.opacity.end, fishGallery.opacity.value + fishGallery.opacity.change) : Math.max(fishGallery.opacity.end, fishGallery.opacity.value + fishGallery.opacity.change);
				fishGallery.fishMouseMove({ type: "fgfading", pageX: fishGallery.pointer.x, pageY: fishGallery.pointer.y });
				
				if (((fishGallery.power.change > 0 && fishGallery.power.value >= fishGallery.power.end) || (fishGallery.power.change < 0 && fishGallery.power.value <= fishGallery.power.end))
				&& ((fishGallery.opacity.change > 0 && fishGallery.opacity.value >= fishGallery.opacity.end) || (fishGallery.opacity.change < 0 && fishGallery.opacity.value <= fishGallery.opacity.end)))
				{
					fishGallery.fading = false;
					clearInterval(timer);
				}
			}, fishGallery.fadeSpeed);
		}
	},
	
	fishMouseMove: function(e)	//reuseLiveLeft
	{
		var pointer = fishGallery.pointer = fishGallery.getPointer(e);
		var toAdd = 0;
		var item;
		var posy = Math.pow(pointer.y - fishGallery.box.fgTop - fishGallery.items.fishHeight / 2, 2);
		// Set opacity every other mousemove - helps IE in particular
		var setOpacity = fishGallery.useOpacity && (fishGallery.setOpacity = !fishGallery.setOpacity);
		
		for (var i = 0; i < fishGallery.items.length; i++)
		{
			item = fishGallery.items[i];
			
			/*if (offLeft + item.fgLeft < 0)	// or too far the other side
			{
				// make sure it's hidden
				item.fgDisplay = false;
				/*item.fgWidth = fishGallery.config.stdWidth;
				item.fgTop = fishGallery.config.stdTop;* /
			}
			else
			{
				item.fgDisplay = true;*/

			distance = Math.sqrt(Math.pow(pointer.x - fishGallery.box.fgLeft - item.fgLeft - item.fgWidth / 2, 2) + posy);
			distance = distance < 0 ? 0 : distance;
			distance = distance > fishGallery.items.proximity ? fishGallery.items.proximity : distance;
			distance = fishGallery.items.proximity - distance;
			
			extraWidth = fishGallery.power.value < 0.01 ? 0 : fishGallery.items.fishWidth * distance * fishGallery.power.value / fishGallery.items.proximity;
			item.fgExtraWidth = extraWidth;
			
			// This is last time's box offset - assuming continuous so avoid 2 loops
			//xxxnoliveleft item.fgLeft = (el.fisheyeCfg.itemWidth + el.fisheyeCfg.itemMargin) * i + toAdd + fishGallery.box.fgLiveLeft + fishGallery.frame.fgPadding;
			item.fgLeft = (fishGallery.items.width + fishGallery.items.margin) * i + toAdd + fishGallery.frame.fgPadding;
			item.fgWidth = fishGallery.items.width + item.fgExtraWidth;
			item.fgTop = (fishGallery.items.fishHeight - item.fgWidth) / 2;
			
			/*xxxnoliveleft item.style.left = item.fgLeft + "px";
			item.style.width = item.fgWidth + "px";
			item.style.top = item.fgTop + "px";*/
			if (setOpacity)
			{
				if (jQuery.browser.msie)
				{
					item.style.filter = "alpha(opacity=" + (60 * fishGallery.opacity.value + 40 * distance * fishGallery.power.value / fishGallery.items.proximity) + ")";
				}
				/*else if (jQuery.browser.mozilla && jQuery.browser.version < 1.5)	// Yes??
				{
					item.style.mozOpacity = 0.6 * fishGallery.opacity.value + 0.4 * distance * fishGallery.power.value / fishGallery.items.proximity;
				}*/
				else
				{
					item.style.opacity = 0.6 * fishGallery.opacity.value + 0.4 * distance * fishGallery.power.value / fishGallery.items.proximity;
				}
			}
			toAdd += item.fgExtraWidth;
		}
		
		fishGallery.box.fgLiveLeft =  -toAdd * (pointer.x - fishGallery.box.fgLeft) / fishGallery.frame.fgWidth / fishGallery.fishTemper;
		fishGallery.box.fgLiveWidth = fishGallery.box.fgWidth - fishGallery.box.fgLiveLeft;
							
		for (i = 0; i < fishGallery.items.length; i++)
		{
			item = fishGallery.items[i];
			item.fgLeft += fishGallery.box.fgLiveLeft;

			item.style.left = item.fgLeft + "px";
			item.style.width = item.fgWidth + "px";
			item.style.top = item.fgTop + "px";
		}
	},			

	moveLeft: function()
	{
		fishGallery.moveBox(fishGallery.box.fgMoveStep);
	},
	
	moveRight: function()
	{
		fishGallery.moveBox(-fishGallery.box.fgMoveStep);
	},
	
	moveBox: function(distance)
	{
		if ((distance < 0 && fishGallery.last.fgLeft + fishGallery.last.fgWidth + fishGallery.box.fgOffsetLeft <= fishGallery.frame.fgWidth)
		|| (distance > 0 && fishGallery.first.fgLeft + fishGallery.box.fgOffsetLeft - fishGallery.box.fgLiveLeft >= 0)) return;

		distance = distance < 0 ? Math.max(distance, fishGallery.frame.fgWidth - fishGallery.box.fgWidth - fishGallery.box.fgOffsetLeft - fishGallery.frame.fgPadding - fishGallery.items.margin) : Math.min(distance, -fishGallery.box.fgOffsetLeft);
		fishGallery.box.fgLeft += distance;
		fishGallery.box.fgOffsetLeft += distance;
		
		jQuery(document).unbind("mousemove", fishGallery.fishMouseMove);

		fishGallery.box.animate({ left: fishGallery.box.fgOffsetLeft }, 350, function() { jQuery(document).mousemove(fishGallery.fishMouseMove); });
	},	
	
	positionGallery: function()
	{
		var widthPx = fishGallery.items.width + "px";
		var topPx = fishGallery.items.top + "px";
		var boxOffset = fishGallery.box.offset();

		fishGallery.box.fgTop = boxOffset.top;
		fishGallery.box.fgLeft = boxOffset.left;
		fishGallery.frame.fgWidth = parseInt(fishGallery.frame.css("width"));
		fishGallery.frame.fgLeft = fishGallery.frame.offset().left;
		
		fishGallery.items.each(function(n)
		{
			this.fgWidth = fishGallery.items.width;
			this.fgLeft = (fishGallery.items.width + fishGallery.items.margin) * n + fishGallery.frame.fgPadding;
			this.style.width = widthPx;
			this.style.left = this.fgLeft + "px";
			this.style.top = topPx;
			
			if (fishGallery.useOpacity)
			{
				if (jQuery.browser.msie)
				{
					this.style.filter = "alpha(opacity=" + (60 * fishGallery.opacity.value) + ")";
				}
				else
				{
					this.style.opacity = 0.6 * fishGallery.opacity.value;
				}
			}
		});
	},
	
	// Function taken from iutil.js : Interface Elements for jQuery by Stefan Petre : http://interface.eyecon.ro
	getPointer: function(e)
	{
		var x = e.pageX || (e.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft)) || 0;
		var y = e.pageY || (e.clientY + (document.documentElement.scrollTop || document.body.scrollTop)) || 0;
		return { x: x, y: y };
	}
};
	
jQuery.fn.FishGallery = jQuery.iFishGallery.init;

//]]>