$(document).ready(function(){
	galleryBrowser.init();
});

galleryBrowser = {
	init: function()
	{
		gal_items = [];
		images = $(".browserImage");
		popupStatus = 0;
		popupCurrentItem = null;
		videoPlaying = 0
		for (var i=0; i < images.length; i++) {
			gal_items[i] = images[i];
		}
		this.hooks();
	},
	hooks: function ()
	{
		$("a.browserImage").click(function(e) {
			e.preventDefault();
			var id = galleryBrowser.arraySearch(gal_items, $(this).attr("href"));
			galleryBrowser.show(id);
		});
		$("#navPrevious").click(function(e) {
			e.preventDefault();
			galleryBrowser.previous();
		});
		$("#navNext").click(function(e) {
			e.preventDefault();
			galleryBrowser.next();
		});
		$("#galleryPopupClose").click(function(){
			galleryBrowser.hide();
		});
		$("#galleryPopupBackground").click(function(){
			galleryBrowser.hide();
		});
		$(document).keypress(function(e){
			if(e.keyCode==27 && popupStatus==1){
				galleryBrowser.hide();
			}
		});
		window.onscroll = function() {
			if (popupStatus == 1) {
				galleryBrowser.centerPopup();
			}
		};
	},

	show: function(itemNum)
	{
		//itemNum = galleryBrowser.arraySearch(gal_items, href);
		if (typeof(itemNum) == "undefined") {
			return;
		}
		if (popupCurrentItem == itemNum && popupStatus == 1) {
			return;
		}
		popupCurrentItem = itemNum;
		theObject = gal_items[itemNum];
		href = theObject.href;


		if (typeof(href) != "string") {
			return;
		}
		$.get("/gallery_log.php?href="+href);
		if (href.match(/.*\.avi|mp4|flv/)) {
			// is video
			$("#imageView").hide();
			$("#moviePlayer").show();
			$f("moviePlayer", strBase+"/js/flowplayer-3.0.7.swf", {
				clip: {
					url: href
				}
			});
			videoPlaying = 1;
			var width = 600;
		}
		else {
			$("#moviePlayer").hide();
			arrClasses = theObject.className.split(' ');
			var width = 0;
			var height = 0;
			for (var i=0; i < arrClasses.length; i++) {
				str = arrClasses[i];
				if (str.search('theWidth') != -1) {
					arrSplit = str.split('|');
					width = arrSplit[1];
				}
				else if (str.search('theHeight') != -1) {
					arrSplit = str.split('|');
					height = arrSplit[1];
				}
			}
			if (width != 0 && height != 0) {
				strExtra = ' width="' + width + '" height="' + height + '"';
			}
			$("#imageView").html('<img src="' + href + '"' + strExtra + ' />');
			$("#imageView").show();
		}
		if (width) {
			if (width < 160) {
				width = 160;
			}
			$("#galleryPopup").width(width);
		}
		if(popupStatus==0){
			$("#galleryPopupBackground").css({
				"opacity": "0.7"
			});
			$("#galleryPopupBackground").fadeIn("slow");
			$("#galleryPopup").fadeIn("slow");
			popupStatus = 1;
		}
		this.centerPopup();
		galleryBrowser.drawNavigation(itemNum, gal_items.length);
	},
	previous: function()
	{
		galleryBrowser.show(popupCurrentItem-1);
	},
	next: function()
	{
		galleryBrowser.show(popupCurrentItem+1);
	},
	arraySearch: function(theArray, theValue)
	{
		if (typeof(theArray) != "object" || theArray.length < 1) {
			return;
		}
		for (i=0; i < theArray.length; i++) {
			if (theArray[i].href == theValue) {
				return i;
			}
		}
		return;

	},
	hide: function()
	{
		if (popupStatus == 1) {
			galleryBrowser.killVideo();
			$("#galleryPopupBackground").fadeOut("slow");
			$("#galleryPopup").fadeOut("slow");
			popupStatus = 0;
		}

	},
	killVideo: function()
	{
		if (videoPlaying == 1) {
			$("#moviePlayer").hide();
			if ($f() != undefined) {
				$f().stop();
				$f().unload();
			}
			videoPlaying = 0;
		}

	},
	drawNavigation: function(currentItem, totalItems)
	{
		$("#navNext").css('visibility', 'hidden');
		$("#navPrevious").css('visibility', 'hidden');

		if ((currentItem+1) < totalItems) {
			$("#navNext").css('visibility', '');
		}
		if (currentItem > 0) {
			$("#navPrevious").css('visibility', '');
		}
		$("#navDescription").html((currentItem+1) + ' of ' + (totalItems));
	},

	centerPopup: function()
	{
		$("#galleryPopup #galleryNavigation").width(1);
		var windowWidth = $(window).width();
		var windowHeight = $(window).height();
		var navWidth = 0;
		if ($("#moviePlayer").css('display') == "none") {
			navWidth = $("#imageView").width();
		}
		else if($("#imageView").css('display') == "none") {
			navWidth = $("#moviePlayer").width();
		}
		$("#galleryPopup #galleryNavigation").width(navWidth);
		var popupHeight = $("#galleryPopup").height();
		var popupWidth = $("#galleryPopup").width();
		var scrolledY = document.documentElement.scrollTop;
		$("#galleryPopup").css({
			"position": "absolute",
			"top": scrolledY+(windowHeight/2)-(popupHeight/2),
			"left": (windowWidth/2)-(popupWidth/2),
			"zIndex": 5000
		});
		$("#galleryPopupBackground").height($(document).height()-2);
		$("#galleryPopupBackground").width($(document).width());
	}
}
