/*
 * Funke Web Solutions
 * http://www.funke.com.au
 *
 * $Id$
 * $LastChangedBy$
 *
 * Client: Mark Hayman
 * Project: Havana II
 * Website: http://www.sunseeker75.com.au
 *
 * $LastChangedDate$
 * $LastChangedRevision$
 *
 */

jQuery.fn.funkefotos = function() {
	
	// sets all variables and constants
	
	var funkefotoLoadingImage = "images/gallery-loader.gif";
	var funkefotoCancelImage = "images/gallery-cancel.gif";
	var funkefotoCloseImage = "images/gallery-close.gif";
	var funkefotoThumbnailIdentifier = "_tn";
	var funkefotoFullSizeIdentifier = "";
	
	// inserts and hides all funkefoto markup into the start of the body element 
	// and sets all required styles to markup
	
	var fgHTML = "<div id='funkefotoBg'>" +
					"<img id='funkefotoLoading' src='" + funkefotoLoadingImage + "' alt='Loading'/>" +
					"<img id='funkefotoCancel' src='" + funkefotoCancelImage + "' alt='Cancel' />" +
				 "</div>" +
				 "<div id='funkefotoContainer'>" +		 
					"<img id='funkefoto' src='' />" +
					"<img id='funkefotoClose' src='" + funkefotoCloseImage + "' alt='close' />" +			
					"<p></p>" +						
				 "</div>";
				
	$(fgHTML).prependTo("body");
	
	$("#funkefotoBg").hide();
	$("#funkefotoBg").css("position", "absolute");
	$("#funkefotoBg").css("width", " 100%");
	$("#funkefotoBg").css("top", "0");
	$("#funkefotoBg").css("left", "0");
	$("#funkefotoBg").css("cursor", "pointer");
	$("#funkefotoBg").css("z-index", "10");
	
	$("#funkefotoContainer").hide();	
	$("#funkefotoContainer").css("position", "absolute");
	$("#funkefotoContainer").css("left", "50%");
	$("#funkefotoContainer").css("z-index", "20");
	
	$("#funkefoto").css("clear", "both");
	
	$("#funkefotoLoading").hide();
	$("#funkefotoLoading").css("position", "absolute");
	$("#funkefotoLoading").css("left", "50%");
	$("#funkefotoLoading").css("cursor", "pointer");

	$("#funkefotoCancel").hide();
	$("#funkefotoCancel").css("position", "absolute");
	$("#funkefotoCancel").css("left", "50%");
	$("#funkefotoCancel").css("cursor", "pointer");	

	$("#funkefotoBg").css("height", document.documentElement.offsetHeight + "px");
	
	$(this).hover(function(){
		$(this).addClass("hover");
	}, function() {
		$(this).removeClass("hover");	
	});
	
	// hides the loading and cancel screens once the funke foto has loaded
	
	$("#funkefoto").load(function() {	
			$("#funkefotoLoading").hide();
			$("#funkefotoCancel").hide();
	});	
	
	// sets onclick function to all selected elements
	
	$(this).click(function(){
		// gets page attributes / dimensions
		var arrayPageSize = getPageSize();
		var arrayPageScroll = getPageScroll();
		
		// continuteFunkeFoto is check later to see if the user has clicked the
		// cancel button on page load
		var continuefunkefoto = true;
		
		// prepares the preloading of the selected image
		var preLoadImage = new Image();
		
		var tempImageName = $(this).attr("src");
		var tempImageURLArray = new Array();
		
		tempImageURLArray = tempImageName.split("/");
		tempImageName = tempImageURLArray.pop();
		tempImageName = tempImageName.replace(funkefotoThumbnailIdentifier, funkefotoFullSizeIdentifier);
	
		var tempCaption =  $(this).attr("title");
		var tempAlt =  $(this).attr("alt");
		
		preLoadImage.onload=function(){	

			//calculates the center of the screen for the Funkfe Foto
			containerLeftMargin = preLoadImage.width / 2;
			containerTopMargin = (arrayPageScroll[1] + ((arrayPageSize[3] - 35 - preLoadImage.height) / 2));
			containerWidth = preLoadImage.width;

			$("#funkefotoContainer").css("width", containerWidth + "px");
			$("#funkefotoContainer").css("marginLeft", "-" + containerLeftMargin + "px");
			$("#funkefotoContainer").css("marginTop", containerTopMargin + "px");					
			$("#funkefotoContainer p").html(tempCaption);													
			
			$("#funkefoto").attr("src", preLoadImage.src);
			
			if(continuefunkefoto) {
				$("#funkefotoContainer").fadeIn();
			}	
		}
		
		$("#funkefotoCancel").bind("click", function() {
			continuefunkefoto = false;
			$("#funkefotoBg").hide();
			preLoadImage.src = null;
		});
		
		$("#funkefotoBg").show();
		$("#funkefotoBg").css("height", arrayPageSize[1] + "px");
	
		// centers positioning for the loading and cancel image
		
		$("#funkefotoLoading").show();
		loadingLeftMargin = $("#funkefotoLoading").width() / 2;
		loadingTopMargin = (arrayPageScroll[1] + ((arrayPageSize[3] - 35 - $("#funkefotoLoading").height()) / 2));
		$("#funkefotoLoading").css("marginTop", loadingTopMargin + "px");
		$("#funkefotoLoading").css("marginLeft", "-" + loadingLeftMargin + "px");
		

		$("#funkefotoCancel").show();		
		cancelLeftMargin = $("#funkefotoCancel").width() / 2;
		cancelTopMargin = (arrayPageScroll[1] + $("#funkefotoLoading").height() + ((arrayPageSize[3] - 35 - $("#funkefotoCancel").height()) / 2));
		$("#funkefotoCancel").css("marginTop", cancelTopMargin + "px");
		$("#funkefotoCancel").css("marginLeft", "-" + cancelLeftMargin + "px");
		
		preLoadImage.src = tempImageURLArray.join("/") + "/" + tempImageName;
	});
	
	$("#funkefotoBg").click(function() {
		$("#funkefotoLoading").hide();
		$("#funkefotoBg").hide();
		$("#funkefotoContainer").fadeOut();		
	});
	
	$("#funkefotoClose").click(function() {
		$("#funkefotoLoading").hide();
		$("#funkefotoBg").hide();
		$("#funkefotoContainer").fadeOut();		
	});
}

// Pauses Code Execution for a set amount of time.
// Code from http://www.faqts.com/knowledge_base/view.phtml/aid/1602

function pause(numberMillis) {
	var now = new Date();
	var exitTime = now.getTime() + numberMillis;
	while (true) {
		now = new Date();
		if (now.getTime() > exitTime)
			return;
	}
}

//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
// As seen in Lightbox JS: Fullsize Image Overlays 
// by Lokesh Dhakar - http://www.huddletogether.com
//

function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
// As seen in Lightbox JS: Fullsize Image Overlays 
// by Lokesh Dhakar - http://www.huddletogether.com
//
function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}
