/* By Mohd Farhan bin Nor */

var numberOfGallerySets = 0;
var currentGallerySet = 1;

function scrollThumbnails(thumbnailSetID) {
	jQuery(".theThumbnailContainer").fadeOut("fast", function() {
		var firstImage = thumbnailSetID+" .theThumb1";
		var firstImageURL = jQuery(""+firstImage+"").attr("href");
		displayTheImage2(firstImageURL,thumbnailSetID);
	});
}

function displayTheImage(theImageSRC) {
	jQuery(".theLargeImage #theLargeImageBox").fadeOut("fast", function() { 
		var theImageHTML = '<img src="'+theImageSRC+'" id="largeImageID" onload="slideTheImageDown(\'#theLargeImageBox\');" \/>';
		jQuery(".theLargeImage #theLargeImageBox").html(theImageHTML);
		//setCenter("#exGalleryContainer");
		//resizeOverlay();
	});
}

function displayTheImage2(theImageSRC,thumbnailSetID) {
	jQuery(".theLargeImage img").fadeOut("normal", function() { 
		var theImageHTML = '<img src="'+theImageSRC+'" id="largeImageID" onload="slideTheImageDown(\'#theLargeImageBox\');" \/>';
		jQuery(".theLargeImage #theLargeImageBox").html(theImageHTML);
		jQuery(""+thumbnailSetID+"").fadeIn("slow");
		//setCenter("#exGalleryContainer");
		//resizeOverlay();
	});
}

function slideTheImageDown(imageID) {
	var theUserAgent = navigator.userAgent;
	if(theUserAgent.search("AppleWebKit")==-1 ) {
		if(theUserAgent.search("MSIE")==-1 ) {
			if(jQuery("#theLargeImageBox img").attr("height")<=500) {
			resizeLargeImage(jQuery("#theLargeImageBox img").attr("height"))
			}
			else {
			resizeLargeImage(500);
			}
		}
		
	}
	jQuery(".theLoading").css("display","none");
	jQuery(""+imageID+"").fadeIn("normal", function() {
		if(jQuery("#theLargeImageBox img").attr("height")>500) {
			jQuery("#theLargeImageBox img").css("height","500px");
		}
		setCenter("#exGalleryContainer");
		resizeOverlay();	
	});
	
}

function displayTheGallery(galleryURL) {
	runGalleryOverlay();
	jQuery("#exGalleryOverlay").click( function() {
		hideGallery();
	});
	var randCode = Math.floor(Math.random()*100);
	jQuery("#exGalleryContainer").load(galleryURL+"?randcode="+randCode, function() {
		setCenter("#exGalleryContainer");
		jQuery("#exGalleryContainer").fadeIn("normal", function() {
			jQuery("#thumbnailSetContainer").hover(function() {
				jQuery("#thumbNext").fadeIn("fast");
				jQuery("#thumbPrev").fadeIn("fast");
			}, function() {
				jQuery("#thumbNext").fadeOut("fast");
				jQuery("#thumbPrev").fadeOut("fast");

			});	
			setCenter("#exGalleryContainer");
		});
	});
}

function runGalleryOverlay() {
	var wholeH = 0;
	wholeH = document.body.clientHeight;
	var wholeW = document.body.clientWidth;
	jQuery("#exGalleryOverlay").css("width", wholeW+"px");
	jQuery("#exGalleryOverlay").css("height", wholeH+"px");
	jQuery("#exGalleryOverlay").fadeIn("fast");
	var theUserAgent = navigator.userAgent;
	if(theUserAgent.search("MSIE 6")!=-1) {
		jQuery("#exGalleryOverlay").animate({opacity: 0.8},1);
	}
}

function resizeOverlay() {
	var wholeH = 0;
	wholeH = document.body.clientHeight;
	var wholeW = document.body.clientWidth;
	jQuery("#exGalleryOverlay").css("width", wholeW+"px");
}

function hideGallery() {
	jQuery("#exGalleryContainer").fadeOut("normal", function() {
		jQuery("#exGalleryContainer").html("");
		jQuery("#exGalleryOverlay").fadeOut("normal");
	});
}

function setCenter(theID) {
		
	   var theWidth = jQuery(""+theID+"").width();
	   var theHeight = jQuery(""+theID+"").height();	

	   var intH = 0, intW = 0;

	    if(self.innerHeight) {
	       intH = window.innerHeight;
	       intW = window.innerWidth;
	    }
	    else {
		if(document.documentElement && document.documentElement.clientHeight) {
		    intH = document.documentElement.clientHeight;
		    intW = document.documentElement.clientWidth;
		}
		else {
		    if(document.body) {
			intH = document.body.clientHeight;
			intW = document.body.clientWidth;
		    }
		}
	    }

	intH = document.body.clientHeight;
		
	var intWindow = document.documentElement.clientHeight;
	
	if(intWindow==0) {
		intWindow = intH;
	}
	
	var intScroll = document.documentElement.scrollTop;
	
	var theUserAgent = navigator.userAgent;
	if(theUserAgent.search("AppleWebKit")!=-1) {
		intWindow = window.innerHeight;
		intScroll = document.body.scrollTop;
	}	
	
	var xCenter = (intW/2)-(theWidth/2);
	xCenter = parseInt(xCenter);
	var yCenter = intScroll + (intWindow/2) - (theHeight/2);
	yCenter = parseInt(yCenter);
	//if(theHeight>intWindow) {
		//yCenter = 0;
	//}

	
	jQuery(""+theID+"").css("top",yCenter+"px");
	jQuery(""+theID+"").css("left",xCenter+"px");
	
	//alert(xCenter+" "+yCenter+" "+document.body.clientHeight);

}

function resizeLargeImage(theNewHeight) {
	jQuery(".theLargeImage").height(theNewHeight);
}

function initExiledGallery() {
	var theGallery = '<div id="exGalleryContainer"> </div>';
	var theOverlay = '<div id="exGalleryOverlay" class="transparent_class"> </div>';
	jQuery("body").prepend(theOverlay);
	jQuery("body").prepend(theGallery);	
}

function initSets(noOfSet) {
	numberOfGallerySets = noOfSet;
	jQuery("#thumbNext").click(function() {
		nextGallerySet();
	});
	jQuery("#thumbPrev").click(function() {
		prevGallerySet();
	});	
}

function nextGallerySet() {
	if(currentGallerySet==numberOfGallerySets) {
		currentGallerySet=1;
		scrollThumbnails("#theThumbnailSet"+currentGallerySet);
	}
	else {
		currentGallerySet=currentGallerySet+1;
		scrollThumbnails("#theThumbnailSet"+currentGallerySet);
	}
}

function prevGallerySet() {
	if(currentGallerySet==1) {
		currentGallerySet=numberOfGallerySets;
		scrollThumbnails("#theThumbnailSet"+currentGallerySet);
	}
	else {
		currentGallerySet=currentGallerySet-1;
		scrollThumbnails("#theThumbnailSet"+currentGallerySet);
	}
}

function displayTheVideo(videoURL) {
	runGalleryOverlay();
	setCenter("#videoBox");
	jQuery("#videoBox").fadeIn(4000, function() {
		displayVideo(videoURL);
	});
}

function hideVideoBox() {
	jQuery("#videoBox").fadeOut("normal", function() {
		jQuery("#videoBoxContent").html("");
		jQuery("#exGalleryOverlay").fadeOut("normal");
	});
}

function displayVideo(videoId) {
	var s1 = new SWFObject('player.swf','player','640','480','9');
	s1.addParam('allowfullscreen','true');
	s1.addParam('allowscriptaccess','always');
	s1.addParam('flashvars','file='+videoId+'&autostart=true');
	s1.write('videoBoxContent');
}
