 // imgGalleryWindow Controller
function imgGalleryWindow(imagesDataSet)
{
	var imagesDS = imagesDataSet;
	var windowObj = this;

	// Window Propertes - Used for resizing
	var documentHeight = 0; //YAHOO.util.Dom.getDocumentHeight();
	var lastWindowWidth = 0;
	var lastWindowHeight = 0;
	var currentWindowWidth = null;
	var currentWindowHeight = null;
	var targetWindowWidth = null;
	var targetWindowHeight = null;
	var defaultWindowTop = 40;
	var defaultWindowBottom = 40;
	var windowTop = defaultWindowTop;
	var windowMargin = 14;
	var windowStatus = "hidden";	// hidden,resizing,visible
	var resizeWindowTimeout = null;


	// Required dom objects
	var imgGalleryModalCover = null;
	var imgGalleryContainer = null;
	var imgGalleryWindow = null;
	var imgGalleryWindowHeader = null;
	var imgGalleryWindowHeaderLabel = null;
	var imgGalleryHeaderImageInfo = null;
	var imgGalleryWindowClose = null;
	var imgGalleryHeaderGalleryInfo = null;
	var imgGalleryWindowBottom = null;
	var imgGalleryWindowContent = null;
	var imgGalleryWindowImage = null;
	var imgGalleryWindowFooter = null;
	var imgGalleryWindowFooterLeftArrow = null;
	var imgGalleryWindowFooterRightArrow = null;

	//YAHOO.util.Event.addListener(window, "load", verifyRequiredObjects);
	//verifyRequiredObjects();

	var observer = new Object();
	observer.onPostUpdate = function(notifier, data)
	{
		verifyRequiredObjects();
	};

	Spry.Data.Region.addObserver("imgGalleryWindow", observer);
	


	var regionObserver = new Object();
	regionObserver.onPostUpdate = function(notifier, data)
	{
		if (documentHeight == 0)
		{
			documentHeight = YAHOO.util.Dom.getDocumentHeight();	
		}

		
		imgGalleryWindowHeader = YAHOO.util.Dom.get("imgGalleryWindowHeader");
		imgGalleryWindowHeaderLabel = YAHOO.util.Dom.get("imgGalleryWindowHeaderLabel");
		imgGalleryHeaderImageInfo = YAHOO.util.Dom.get("imgGalleryHeaderImageInfo");
		imgGalleryWindowClose = YAHOO.util.Dom.get("imgGalleryWindowClose");
		imgGalleryHeaderGalleryInfo = YAHOO.util.Dom.get("imgGalleryHeaderGalleryInfo");
		imgGalleryWindowBottom = YAHOO.util.Dom.get("imgGalleryWindowBottom");
		imgGalleryWindowContent = YAHOO.util.Dom.get("imgGalleryWindowContent");
		imgGalleryWindowImage = YAHOO.util.Dom.get("imgGalleryWindowImage");
		imgGalleryWindowFooter = YAHOO.util.Dom.get("imgGalleryWindowFooter");
		imgGalleryWindowFooterLeftArrow = YAHOO.util.Dom.get("imgGalleryWindowFooterLeftArrow");
		imgGalleryWindowFooterRightArrow = YAHOO.util.Dom.get("imgGalleryWindowFooterRightArrow");
	};
	
	Spry.Data.Region.addObserver("imgGalleryWindow", regionObserver);




	this.nextImg = function(e)
	{
		if (e)
		{
			var rowCount = imagesDS.getRowCount();
			var curRowNumber = imagesDS.getCurrentRowNumber();
			var nextRowNumber = 0;
			
			if (curRowNumber + 1 != rowCount)
			{
				nextRowNumber = curRowNumber + 1;	
			}
			
			var rows = imagesDS.getData();
		
			windowObj.setImg(rows[nextRowNumber]["ds_RowID"]);
			
			return false;
		}
	}


	this.prevImg = function(e)
	{
		if (e)
		{
			var rowCount = imagesDS.getRowCount();
			var curRowNumber = imagesDS.getCurrentRowNumber();
			var prevRowNumber = rowCount - 1;
			
			if (curRowNumber - 1 > -1)
			{
				prevRowNumber = curRowNumber - 1;	
			}
			
			var rows = imagesDS.getData();
	
			windowObj.setImg(rows[prevRowNumber]["ds_RowID"]);
			
			return false;
		}
	}

	this.setImg = function(ds_RowID)
	{
		if (documentHeight != YAHOO.util.Dom.getDocumentHeight())
		{
			documentHeight = YAHOO.util.Dom.getDocumentHeight();
		}
		
		if (currentWindowWidth != null)
		{
			lastWindowWidth = currentWindowWidth;
			lastWindowHeight = currentWindowHeight;
		}

		imagesDS.setCurrentRow(ds_RowID);

		showWindow();
		var row = imagesDS.getCurrentRow();

		resizeWindowStart(row["width"], row["height"]);
	}

	this.hideImg = function(e)
	{
		if (e)
		{
			if (YAHOO.util.Event.getTarget(e).id == "imgGalleryContainer" || YAHOO.util.Event.getTarget(e).id == "imgGalleryWindowClose")
			{
				hideWindow();
			}
		}
	}








	function setWindowStatus(status)
	{
		if (YAHOO.util.Dom.hasClass(imgGalleryWindow, "hidden"))
		{
			YAHOO.util.Dom.removeClass(imgGalleryWindow, "hidden");
		}
		if (YAHOO.util.Dom.hasClass(imgGalleryWindow, "resizing"))
		{
			YAHOO.util.Dom.removeClass(imgGalleryWindow, "resizing");
		}
		if (YAHOO.util.Dom.hasClass(imgGalleryWindow, "visible"))
		{
			YAHOO.util.Dom.removeClass(imgGalleryWindow, "visible");
		}


		if (status == "hidden")
		{
			YAHOO.util.Dom.addClass(imgGalleryWindow, "hidden");
			windowStatus = "hidden";
		}
		else if (status == "resizing")
		{
			YAHOO.util.Dom.addClass(imgGalleryWindow, "resizing");
			windowStatus = "resizing";
		}
		else if (status == "visible")
		{
			YAHOO.util.Dom.addClass(imgGalleryWindow, "visible");
			windowStatus = "visible";
		}
	}

	function setWindowSize(width, height)
	{
		var fullWindowWidth = width + windowMargin;
		
		imgGalleryWindow.style.width = fullWindowWidth + "px";
		imgGalleryWindowHeader.style.width = fullWindowWidth + "px";
		imgGalleryWindowFooter.style.width = width + "px";
		imgGalleryWindowContent.style.width = width + "px";
		imgGalleryWindowContent.style.height = height + "px";
	}

	function showWindow()
	{
		showModal();
		showWindowContainer();
		setWindowStatus("resizing");
		updateWindowVerticalPosition();
		YAHOO.util.Event.addListener(window, "scroll", updateWindowVerticalPosition);
	}

	function hideWindow()
	{
		if (resizeWindowTimeout != null)
		{
			clearTimeout(resizeWindowTimeout);
			resizeWindowTimeout = null;
		}
		
		setWindowStatus("hidden");
		hideWindowContainer();
		hideModal();
		YAHOO.util.Event.removeListener(window, "scroll", updateWindowVerticalPosition);
	}

	function updateWindowVerticalPosition()
	{
		var maxWindowTop = documentHeight - Number(imgGalleryWindow.offsetHeight) - defaultWindowBottom;
		var viewportTop = getDocumentScrollTop();
		var newTop = defaultWindowTop + viewportTop;

		// If the image window maximum top can be greater than 0
		// AND
		// the viewport is below the maxWindowTop
		if (maxWindowTop > 0 && viewportTop > maxWindowTop)
		{
			newTop = maxWindowTop;
		}


		// If Image Window is larger than the page
		if (Number(imgGalleryWindow.offsetHeight) > documentHeight)
		{
			newTop = defaultWindowTop;
		}


		// if a change is needed
		if (newTop != windowTop)
		{
			imgGalleryWindow.style.top = newTop + "px";
			windowTop = newTop;
		}
	}

	function setModalHeight()
	{
		imgGalleryModalCover.style.height = getCoverHeight();
	}

	function setModalStatus(status)
	{
		if (YAHOO.util.Dom.hasClass(imgGalleryModalCover, "visible"))
		{
			YAHOO.util.Dom.removeClass(imgGalleryModalCover, "visible");
		}
		if (YAHOO.util.Dom.hasClass(imgGalleryModalCover, "hidden"))
		{
			YAHOO.util.Dom.removeClass(imgGalleryModalCover, "hidden");
		}
		
		if (status == "hidden")
		{
			YAHOO.util.Dom.addClass(imgGalleryModalCover, "hidden");
		}
		else
		{
			YAHOO.util.Dom.addClass(imgGalleryModalCover, "visible");
		}
	}

	function showModal()
	{
		setModalStatus("visible");

		var fade = new Spry.Effect.Fade("imgGalleryModalCover", {duration: 1, from: 50, to: 50, toggle: false});
		fade.start();

		setModalHeight();


		var listeners = YAHOO.util.Event.getListeners(window, "resize");
		if (listeners == null)
		{
			YAHOO.util.Event.addListener(window, "resize", onViewportResize);
		}
		else
		{
			var located = false;
			for (var key in listeners)
			{
				if (String(listeners[key].fn) == String(showModal))
				{
					located = true;
					break;
				}
			}
			
			if (!located)
			{
				YAHOO.util.Event.addListener(window, "resize", onViewportResize);
			}
		}
	}

	function hideModal()
	{
		YAHOO.util.Event.removeListener(window, "resize", onViewportResize);
		imgGalleryModalCover.style.height = "0px";
		setModalStatus("hidden");
	}

	function setWindowContainerHeight()
	{
		imgGalleryContainer.style.height = getCoverHeight();
	}

	function setWindowContainerStatus()
	{
		if (YAHOO.util.Dom.hasClass(imgGalleryContainer, "visible"))
		{
			YAHOO.util.Dom.removeClass(imgGalleryContainer, "visible");
		}
		if (YAHOO.util.Dom.hasClass(imgGalleryContainer, "hidden"))
		{
			YAHOO.util.Dom.removeClass(imgGalleryContainer, "hidden");
		}
		
		if (status == "hidden")
		{
			YAHOO.util.Dom.addClass(imgGalleryContainer, "hidden");
		}
		else
		{
			YAHOO.util.Dom.addClass(imgGalleryContainer, "visible");
		}
	}

	function showWindowContainer()
	{
		setWindowContainerStatus("visible");
		setWindowContainerHeight();
	}

	function hideWindowContainer()
	{
		imgGalleryContainer.style.height = "0px";
		setWindowContainerStatus("hidden");
	}

	function showImg()
	{
		YAHOO.util.Dom.addClass(imgGalleryWindowContent, "visible");
	}

	function hideImg()
	{
		YAHOO.util.Dom.removeClass(imgGalleryWindowContent, "visible");
	}

	function getCoverHeight()
	{
		var viewportHeight = YAHOO.util.Dom.getViewportHeight();
		var bodyHeight = documentHeight;
		var windowBottom = windowTop;
		var coverHeight = "100%";

		if (currentWindowHeight != null)
		{
			windowBottom += Number(imgGalleryWindow.offsetHeight);
			windowBottom += defaultWindowBottom;
		}

		if (viewportHeight < bodyHeight)
		{
			coverHeight = bodyHeight + "px";	
		}

		if (viewportHeight < windowBottom && bodyHeight < windowBottom)
		{
			coverHeight = windowBottom + "px";	
		}

		return coverHeight;
	}

	function resizeWindowStart(imgWidth, imgHeight)
	{
		var minWidth = null;
		var newWidth = imgWidth;
		var newHeight = imgHeight;
		var totalWindowWidth = windowMargin + lastWindowWidth;


		// Get Minimum width needed for the Header Label
		if (imgGalleryWindowHeaderLabel.offsetWidth)
		{
			minWidth = imgGalleryWindowHeaderLabel.offsetWidth;
			if (minWidth > newWidth)
			{
				newWidth = minWidth;	
			}
		}


		// Hide Image as we resize
		hideImg();

		// Set Window sizes to last loaded content, if any
		setWindowSize(lastWindowWidth, lastWindowHeight)


		// If currentWindowWidth not already set
		if (currentWindowWidth == null)
		{
			currentWindowWidth = Number(String(imgGalleryWindowContent.style.width).substr(0, String(imgGalleryWindowContent.style.width).length - 2));
			currentWindowHeight = Number(String(imgGalleryWindowContent.style.height).substr(0, String(imgGalleryWindowContent.style.height).length - 2));
		}


		targetWindowWidth = newWidth;
		targetWindowHeight = newHeight;

		resizeWindow();
	}

	function resizeWindow()
	{
		var newWindowWidth = 0;
		var newWindowHeight = 0;
		var distance = 0;
		var movement = 0;
		var splitAmount = 6;

		if (currentWindowWidth != targetWindowWidth)
		{
			distance = Math.abs(targetWindowWidth - currentWindowWidth);
			movement = Math.ceil(distance / splitAmount);

			if (currentWindowWidth < targetWindowWidth)
			{
				newWindowWidth = currentWindowWidth + movement;
			}
			else
			{
				newWindowWidth = currentWindowWidth - movement;
			}
		}
		else
		{
			newWindowWidth = currentWindowWidth;
		}


		if (currentWindowHeight != targetWindowHeight)
		{
			distance = Math.abs(targetWindowHeight - currentWindowHeight);
			movement = Math.ceil(distance / splitAmount);

			if (currentWindowHeight < targetWindowHeight)
			{
				newWindowHeight = currentWindowHeight + movement;
			}
			else
			{
				newWindowHeight = currentWindowHeight - movement;
			}
		}
		else
		{
			newWindowHeight = currentWindowHeight;
		}


		// Resize Window
		setWindowSize(newWindowWidth, newWindowHeight)


		currentWindowWidth = newWindowWidth;
		currentWindowHeight = newWindowHeight;


		if (currentWindowWidth != targetWindowWidth || currentWindowHeight != targetWindowHeight)
		{
			resizeWindowTimeout = setTimeout(resizeWindow, 4);
		}
		else
		{
			resizeWindowTimeout = null;
			onWindowResized();
		}
	}

	function onWindowResized()
	{
		var row = imagesDS.getCurrentRow();
		
		setWindowStatus("visible");

		imgGalleryWindowImage.style.width = row["width"] + "px";
		imgGalleryWindowImage.style.height = row["height"] + "px";
	
		var fade = new Spry.Effect.Fade("imgGalleryWindowImage", {duration: 1502, from: 0, to: 100, toggle: false, setup: setImgOn});
		fade.start();

		setModalHeight();
		setWindowContainerHeight();
		setClickListeners();
	}

	function onViewportResize()
	{
		setModalHeight();
		setWindowContainerHeight();
		updateWindowVerticalPosition();
	}

	function setImgOn()
	{
		// Fixes Image flicker
		setTimeout(showImg, 2);	
	}

	function setClickListeners()
	{
		var clickListeners = YAHOO.util.Event.getListeners("imgGalleryWindowFooterLeftArrow", "click");
		if (clickListeners == null)
		{
			YAHOO.util.Event.addListener("imgGalleryWindowFooterLeftArrow", "click", windowObj.prevImg);
		}

		clickListeners = YAHOO.util.Event.getListeners("imgGalleryWindowFooterRightArrow", "click");
		if (clickListeners == null)
		{
			YAHOO.util.Event.addListener("imgGalleryWindowFooterRightArrow", "click", windowObj.nextImg);
		}

		clickListeners = YAHOO.util.Event.getListeners("imgGalleryWindowClose", "mouseup");
		if (clickListeners == null)
		{
			YAHOO.util.Event.addListener("imgGalleryWindowClose", "mouseup", windowObj.hideImg);
		}

		clickListeners = YAHOO.util.Event.getListeners("imgGalleryContainer", "mouseup");
		if (clickListeners == null)
		{
			YAHOO.util.Event.addListener("imgGalleryContainer", "mouseup", windowObj.hideImg);
		}
	}

	function getObjectPosition(obj)
	{
		var x = 0;
		var y = 0;
		var position = new Object();
		
		if (obj.offsetParent)
		{
			x = obj.offsetLeft;
			y = obj.offsetTop;
			
			while (obj = obj.offsetParent)
			{
				x += obj.offsetLeft;
				y += obj.offsetTop;
			}
		}
		
		position.x = x;
		position.y = y;
		
		return position;
	}

	function getDocumentScrollTop(doc)
	{
		doc = doc || document;
		return Math.max(doc.documentElement.scrollTop, doc.body.scrollTop);
	}

	function verifyRequiredObjects()
	{
		imgGalleryModalCover = YAHOO.util.Dom.get("imgGalleryModalCover");
		imgGalleryContainer = YAHOO.util.Dom.get("imgGalleryContainer");
		imgGalleryWindow = YAHOO.util.Dom.get("imgGalleryWindow");
		imgGalleryWindowHeader = YAHOO.util.Dom.get("imgGalleryWindowHeader");
		imgGalleryWindowHeaderLabel = YAHOO.util.Dom.get("imgGalleryWindowHeaderLabel");
		imgGalleryHeaderImageInfo = YAHOO.util.Dom.get("imgGalleryHeaderImageInfo");
		imgGalleryWindowClose = YAHOO.util.Dom.get("imgGalleryWindowClose");
		imgGalleryHeaderGalleryInfo = YAHOO.util.Dom.get("imgGalleryHeaderGalleryInfo");
		imgGalleryWindowBottom = YAHOO.util.Dom.get("imgGalleryWindowBottom");
		imgGalleryWindowContent = YAHOO.util.Dom.get("imgGalleryWindowContent");
		imgGalleryWindowImage = YAHOO.util.Dom.get("imgGalleryWindowImage");
		imgGalleryWindowFooter = YAHOO.util.Dom.get("imgGalleryWindowFooter");
		//imgGalleryWindowFooterLeftArrow = YAHOO.util.Dom.get("imgGalleryWindowFooterLeftArrow");
		//imgGalleryWindowFooterRightArrow = YAHOO.util.Dom.get("imgGalleryWindowFooterRightArrow");

		if (
			!imgGalleryModalCover ||
			!imgGalleryContainer ||
			!imgGalleryWindow ||
			!imgGalleryWindowHeader ||
			!imgGalleryWindowHeaderLabel ||
			!imgGalleryHeaderImageInfo ||
			!imgGalleryWindowClose ||
			!imgGalleryHeaderGalleryInfo ||
			!imgGalleryWindowBottom ||
			!imgGalleryWindowContent ||
			!imgGalleryWindowImage ||
			!imgGalleryWindowFooter
			)
		{
			alert("Missing required DOM elements for gallery window.");
		}
	}
}


