/*
 * Funke Web Solutions
 * http://www.funke.com.au
 * Copyright (c) 2007 Funke Web Solutions
 *
 * $Id: jquery-funkefotos.js 515 2007-03-13 04:40:40Z robbie $
 * $LastChangedBy: robbie $
 *
 * Client: Wall Candy
 * Project: Wall Candy Web Site
 * Website: http://www.wallcandy.com.au
 *
 * $LastChangedDate: 2007-03-13 15:40:40 +1100 (Tue, 13 Mar 2007) $
 * $LastChangedRevision: 515 $
 * 
 */

jQuery.fn.funkefotos = function(siteURL) {
	var funkefotoLoadingImage = siteURL + "images/gallery/gallery-loader.gif";
	var funkefotoCancelImage = siteURL + "images/gallery/gallery-cancel.jpg";
	var funkefotoCloseImage = siteURL + "images/gallery/preview-close-button.jpg";
	var funkefotoThumbnailIdentifier = "_thumb";
	var funkefotoFullSizeIdentifier = "";

	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).appendTo("body");
	
	$("#funkefotoContainer").hide();
	$("#funkefotoBg").hide();
	$("#funkefotoLoading").hide();
	$("#funkefotoCancel").hide();
	$("#funkefotoBg").css("height", document.documentElement.offsetHeight + "px");
	
	$("#funkefoto").load(function() {	
			$("#funkefotoLoading").hide();
			$("#funkefotoCancel").hide();
	});	
	
	$(this).click(function(){

		var continuefunkefoto = true;
		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(){					
			tempWidth = preLoadImage.width / 2;
			tempHeight = preLoadImage.height / 2;
			containerWidth = preLoadImage.width;
			
			$("#funkefotoContainer").css("width", containerWidth + "px");
			$("#funkefotoContainer").css("marginLeft", "-" + tempWidth + "px");
			$("#funkefotoContainer").css("marginTop", "-" + tempHeight + "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();				
		$("#funkefotoLoading").show();
		$("#funkefotoCancel").show();

		preLoadImage.src = tempImageURLArray.join("/") + "/" + tempImageName;
	});
	
	$("#funkefotoClose").click(function() {
		$("#funkefotoLoading").hide();
		$("#funkefotoBg").hide();
		$("#funkefotoContainer").fadeOut();		
	});
}

// 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;
	}
}