//
// swapGalleryImages and related functions.
//
//    Description:
//			* Called from iframe full of thumbnails, that when moused over, displays corresponding
//				fullsize image in parent window complete with loading animation.
//			* Purpose is to correctly display loading animimation placeholder for images that are yet to complete loading.
//
//    Variables:
//			document.aGalleryImages: Array holding objects of fullsized images to be displayed.
//			document.iGalleryThisImage: index for aGallerImages that corresponds to the fullsize image of the currently "moused over" thumbnail.
//			document.MM_src: Array, Dreamweavers global for swapimage/swapimagerestore.
//			oThis: reference to thumbnail image in the page that triggered the mouseover event.
//			oThis.iFullsize: index of aGalleryImages for corresponding fullsized image to be displayed.
//			x: reference to image that is "swapped" to display fullsized pics, pointed to by sImageTarget.

function preloadGalleryImage(sImageTarget,sImageURL,oThis) {
	if ( !document.aGalleryImages ) {
		document.aGalleryImages = new Array;
	}
				
	var oTempImage = new Image;
	var iTempIndex,x;
	x=top.document.getElementById(sImageTarget);	// image to swap.
	if (x) {
			var bFound = false;
			i=0;
			while ((!bFound) && (i< document.aGalleryImages.length)){
				if (sImageURL == document.aGalleryImages[i].src){
					oThis.iFullsize = i;
					bFound = true;
				}
				else{
					i++;
				}
			}
			if (!bFound){
				iTempIndex = (document.aGalleryImages.push(oTempImage))-1;
				oThis.iFullsize = iTempIndex;
				document.aGalleryImages[iTempIndex].onload = function(){finishSwap(x,oThis);};
				document.aGalleryImages[iTempIndex].src = sImageURL;
			}
		//console.log("Preload: %s",document.aGalleryImages[iTempIndex].src);
		//debugger;
		
	}
}

function swapGalleryImage(sImageTarget, sImageURL, oThis) {
	// compatible with Dreamweavers swapimages/swapimagerestore functions.
	// ie. still using MM_sr

	var i,j=0,x,a=swapGalleryImage.arguments;
	document.MM_sr=new Array;
	x=top.document.getElementById(sImageTarget);	// image to swap.

	if (x!=null){
		document.MM_sr[j++]=x;
		if(!x.oSrc){
			x.oSrc=x.src;
		}
		if (document.aGalleryImages){
			var bFound = false;
			i=0;
			while ((!bFound) && (i< document.aGalleryImages.length)){
				if (sImageURL == document.aGalleryImages[i].src){
					document.iGalleryThisImage = i;
					bFound = true;
				}
				else{
					i++;
				}
			}
			if (bFound){
				// image has already been requested, it should be loading and it's callback set.
				if (document.aGalleryImages[document.iGalleryThisImage].complete){
					//preloader doesn't set oThis.iFullsize...
					if (!oThis.iFullsize) {
						oThis.iFullsize =document.iGalleryThisImage;
					}
					
					// image has completed loading. Lets not leave the swap up to the callback, lets do it now.
					finishSwap(x,oThis);
				}
				else {
					// image is still loading.
					// make sure the loading anim is still displayed while the mouse is over this image.
					showGalleryLoading();
				}
			}
			else{
				// Image not already requested...
				// Add to aGalleryImages and set up callback, then start loading.
				var oTempImage = new Image;
				
				document.iGalleryThisImage = (document.aGalleryImages.push(oTempImage))-1;
				oThis.iFullsize =document.iGalleryThisImage;	// add fullsize picture's index to the thumbnail's image object
				document.aGalleryImages[document.iGalleryThisImage].onload = function(){finishSwap(x,oThis);};
				showGalleryLoading();
				document.aGalleryImages[document.iGalleryThisImage].src = sImageURL;
			}
		}
		else {
			//First Run .. Swap with loading anim
			// create needed globals.
			document.aGalleryImages = new Array;
			var oTempImage = new Image;
			document.iGalleryThisImage = 0;
			
			document.iGalleryThisImage = (document.aGalleryImages.push(oTempImage))-1;
			oThis.iFullsize = document.iGalleryThisImage;	// add fullsize picture's index to the thumbnail's image object
			document.aGalleryImages[document.iGalleryThisImage].onload = function(){finishSwap(x,oThis);};
			showGalleryLoading();
			document.aGalleryImages[document.iGalleryThisImage].src = sImageURL;
		}
	}
}

function clearGallery(){	// called when loading new gallery, free up the fullsize images.
	//console.log("clearGallery::");
	if (document.aGalleryImages) {
		for (var i=0; i < document.aGalleryImages.length; i++){
			document.aGalleryImages[i].onload = null;
		}
		document.iGalleryThisImage = null;
		document.aGalleryImages = null;
	}
}
function showGalleryLoading(){
	$("#loader").css("visibility", "visible");
}

function hideGalleryLoading(){
	$("#loader").css("visibility", "hidden");
}

function finishSwap(img,oThat){	// hide loading anim, and display fullsized image
	// triggered by onload events therefore have to account for mouse moving on after initiating load.
	// so only complete swap if we're (still) over the right thumbnail.
	//**firebug:debugging 
	//console.log("finishSwap: %s: iFullsize:%s:%d, iGalleryThisImage:%d", oThat,oThat.iFullsize, oThat.src.substr(oThat.src.length-15), document.iGalleryThisImage);
	if (oThat.iFullsize == document.iGalleryThisImage){
		hideGalleryLoading();
		img.src = document.aGalleryImages[oThat.iFullsize].src;
	}
}

function galleryImgRestore() {
	hideGalleryLoading();
  var i,x,a=document.MM_sr;
	for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++){
		x.src=x.oSrc;
		//writeConsole("ImgRestore::" + x.oSrc + ":" + document.iGalleryThisImage);
	}
	// document.iGalleryThisImage, refers to the fullsize image of the thumbnail the mouse is currently over,
	// now that the mouse is "mousing out" of the thumbnail, it makes sense that the mouse is not over any thumbnails right now...
	document.iGalleryThisImage = null;
}