/**************************************************************************************************
* Goldfish JavaScript Picture Gallery (works only with jQuery)
* This javascript is created by Goldfish from Fishbeam Software: http://www.fishbeam.com
* All rights reserved. © 2008 Yves Pellot
**************************************************************************************************/

//Declare image loader
var zoomGalleryImageLoader;

//Add events to thumbnails and widget
$(document).ready(function(){
	var galleryItems=$('.zoomGallery');
	for(var i=0; i<galleryItems.length; i++) {
		var item=$(galleryItems[i]);
		item.parent().attr("href", "JavaScript:openImage('"+item.parent().attr("href")+"')");
	}
	$("body").append('<div id="zoomGalleryOuter"><img src="" id="zoomGalleryImage" alt="" /><div id="zoomGalleryImageFrame"><div id="zoomGalleryTop"></div><img src="'+pathToZoomGallery+'top_right.png" id="zoomGalleryTopRight" alt="" /><div id="zoomGalleryLeft"></div><div id="zoomGalleryRight"></div><img src="'+pathToZoomGallery+'bottom_left.png" id="zoomGalleryBottomLeft" alt="" /><div id="zoomGalleryBottom"></div><img src="'+pathToZoomGallery+'bottom_right.png" id="zoomGalleryBottomRight" alt="" /><img src="'+pathToZoomGallery+'widget.png" id="zoomGalleryWidget" alt="" /></div></div>');
	$("#zoomGalleryWidget").click(function(){
		closeImage(function(){});
	});
});

//Load image
function openImage(url) {
	closeImage(function(){
		var pageSizes=getPageSize();
  	var pageScroll=getPageScroll();
  	
  	//Load image and prepare
		showSpinner(function(){
      zoomGalleryImageLoader=new Image();
    	zoomGalleryImageLoader.onload=function(){
			 $("#zoomGalleryImage").attr("src", url);
			 $("#zoomGalleryImage").css({marginLeft: (zoomGalleryImageLoader.width/2)+"px", marginTop: (zoomGalleryImageLoader.height/2)+"px", width: "0px", height: "0px"});	
			 $("#zoomGalleryImageFrame").css({width: (zoomGalleryImageLoader.width-16)+"px", height: (zoomGalleryImageLoader.height-16)+"px"});
			 $("#zoomGalleryOuter").css({width: (zoomGalleryImageLoader.width+24)+"px", height: (zoomGalleryImageLoader.height+24)+"px", left: Math.max(0, (pageScroll[0]+(pageSizes[2]-zoomGalleryImageLoader.width-24)/2))+"px", top: Math.max(0, (pageScroll[1]+(pageSizes[3]-zoomGalleryImageLoader.height-24)/2))+"px"});

				//Show image
				hideSpinner(function(){
					$("#zoomGalleryOuter").show(1, function(){
						$("#zoomGalleryImage").animate({width: zoomGalleryImageLoader.width+"px", height: zoomGalleryImageLoader.height+"px", marginLeft: 0, marginTop: 0, opacity: "show"}, 1000, function(){
							$("#zoomGalleryImageFrame").fadeIn("fast");
						});
					});
				});
    	 
    	};
    	zoomGalleryImageLoader.src=url;
    });
  });
}

//Close image
function closeImage(callback) {
	if($("#zoomGalleryOuter").css("display")!="none") {
		$("#zoomGalleryImageFrame").fadeOut("fast", function(){
			$("#zoomGalleryImage").animate({width: "0px", height: "0px", marginLeft: (zoomGalleryImageLoader.width/2)+"px", marginTop: (zoomGalleryImageLoader.height/2)+"px", opacity: "hide"}, 1000, function(){
				$("#zoomGalleryOuter").hide(1, callback);
			});
		});
	}
	else {
		callback();
	}
}

/**
/ THIRD FUNCTION
* getPageSize() by quirksmode.com
*
* @return Array Return an array with page width, height and window width, height
*/
function getPageSize() {
  var xScroll, yScroll;
  if (window.innerHeight && window.scrollMaxY) {	
    xScroll = window.innerWidth + window.scrollMaxX;
    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
    if(document.documentElement.clientWidth){
      windowWidth = document.documentElement.clientWidth; 
    } else {
      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 = xScroll;		
  } else {
    pageWidth = windowWidth;
  }
  arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
  return arrayPageSize;
}
/**
/ THIRD FUNCTION
* getPageScroll() by quirksmode.com
*
* @return Array Return an array with x,y page scroll values.
*/
function getPageScroll() {
  var xScroll, yScroll;
  if (self.pageYOffset) {
    yScroll = self.pageYOffset;
    xScroll = self.pageXOffset;
  } else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
    yScroll = document.documentElement.scrollTop;
    xScroll = document.documentElement.scrollLeft;
  } else if (document.body) {// all other Explorers
    yScroll = document.body.scrollTop;
    xScroll = document.body.scrollLeft;	
  }
  arrayPageScroll = new Array(xScroll,yScroll) 
  return arrayPageScroll;
}
