// JavaScript Document

$(document).ready( function() {
	$(window).load(function() { 
							$("#loadingWarning").fadeOut("fast");
							$(".externalGalleriesHolder").load("external.html #externalGalleries", function() {
																											//alert("Load callback");
																											});
					});
});

var gArray = new Array()
gArray[0] = new imgGallery(1, 16, false);
gArray[1] = new imgGallery(2, 12, false);
gArray[2] = new imgGallery(3, 16, false);
gArray[3] = new imgGallery(4, 16, false);
gArray[4] = new imgGallery(5, 16, false);
gArray[5] = new imgGallery(6, 16, false);
var curGalleryIdx = 0;
var curGallery = gArray[curGalleryIdx];


function imgGallery(idNumber, numImages, isVisible) {
	this.idNumber = idNumber;
	this.numImages = numImages; 
	this.isVisible = isVisible;
	this.curIdx = 0;
	this.nextIdx = this.curIdx + 1;
	this.prevIdx = numImages - 1;
	this.finalIdx = numImages - 1;
}

imgGallery.prototype.debug = function() {
	alert("Gallery info, idNumber: " + this.idNumber + "\n" +
		  "numImages: " + this.numImages + "\n" +
		  "isVisible: " + this.isVisible + "\n" + 
		  "curIdx: " + this.curIdx + "\n" + 
		  "nextIdx: " + this.nextIdx + "\n" + 
		  "prevIdx: " + this.prevIdx + "\n" + 
		  "finalIdx: " + this.finalIdx);
}

imgGallery.prototype.handleThumbClick = function(clickedThumb) {
		var clickedIdx = $(this.selectorStr() + " li").index(clickedThumb);
		$(".galleryList li").removeClass("selected");
		$(this.selectorStr() + " li:eq(" + clickedIdx + ")").addClass("selected");
		this.setCurIdx(clickedIdx);
		hashStr = "gallery" + this.idNumber + "/image" + (clickedIdx+1);
		window.location.hash = hashStr;
		$(".mainImageList li.current").fadeOut("slow").removeClass("current");
		$(".mainImageList li").removeClass("current");
		var selStr = "#galleryImages" + this.idNumber + " li:eq(" + clickedIdx + ")"
		$(selStr).fadeIn("slow").addClass("current");
		return false;
}

imgGallery.prototype.setCurIdx = function(newIdx) {
	this.curIdx = newIdx;
	var prevIdx = newIdx - 1;
	if(prevIdx < 0) { prevIdx = this.finalIdx; }
	this.prevIdx = prevIdx;
	
	var nextIdx = newIdx + 1;
	if(nextIdx > this.finalIdx) { nextIdx = 0; }
	this.nextIdx = nextIdx;
}

imgGallery.prototype.selectorStr = function() {
	return "#gallery" + this.idNumber;	
}

function parseHash(hash) {
	var gPrefix = "gallery";
	var iPrefix = "image";
	if(hash && hash.length > 0) {
		var hArray = hash.split("/");
		if(hArray.length == 2) {
			var gStr = hArray[0];
			var iStr = hArray[1];
			if(gStr && gStr.length > gPrefix.length) {
				gStr = gStr.substring(gPrefix.length+1); // need +1 because of prefix #
				if(gStr != parseInt(gStr)) {
					gStr = "1";
				}
				//alert("gStr: " + gStr); 
			}
			else { gStr = "1"; }
			if(iStr && iStr.length > iPrefix.length) {
				iStr = iStr.substring(iPrefix.length);
				if(iStr != parseInt(iStr)) {
					iStr = "1";
				}
				//alert("iStr: " + iStr);
			}
			else { iStr = "1"; }
		}
		else { 
			gStr = "1";
			iStr = "1";
		}
	  var gIdx = parseInt(gStr) - 1;
	  if(gIdx > gArray.length || gIdx < 0) { gIdx = 0; }
	  var imgIdx = parseInt(iStr) - 1;
	  if(imgIdx >= curGallery.numImages || imgIdx < 0) { imgIdx = 0; }
	  /*var selStr = "#gallery" + curGallery.idNumber + " li:eq(" + imgIdx + ")";
	  curGallery.handleThumbClick($(selStr));*/
	  //alert("gallery number: " + gStr + " image index: " + imgIdx);
	  showGallery(gIdx, imgIdx);
	}
	
}

function thumbClicked() {
	curGallery.handleThumbClick(this);
	return false;
}

function nextClicked() {
	var selStr = "#gallery" + curGallery.idNumber + " li:eq(" + curGallery.nextIdx + ")";
	curGallery.handleThumbClick($(selStr));
	return false;
}
function prevClicked() {
	var selStr = "#gallery" + curGallery.idNumber + " li:eq(" + curGallery.prevIdx + ")";
	curGallery.handleThumbClick($(selStr));
	return false;
}

function gSubNavClicked() {
	var prefix = "subNav";
	var curIdx = $(this).parent().attr("id").substring(prefix.length) - 1;
	$("#subNav li").removeClass("selected");
	$(this).parent().addClass("selected");
	showGallery(curIdx, 0);
	return false;
}

function showGallery(curIdx, imgIdx) {
	$("#subNav li").removeClass("selected");
	$("#subNav" + (curIdx+1)).addClass("selected");
	$("#gallery" + (curGalleryIdx+1)).fadeOut("slow");
	curGallery = gArray[curIdx];
	curGalleryIdx = curIdx;
	$("#gallery" + (curGalleryIdx+1)).fadeIn("slow");
	curGallery.handleThumbClick($("#gallery" + curGallery.idNumber + " li:eq(" + imgIdx + ")"));
	//$.history.load(hash);
	return false;
}

//var $j = jQuery.noConflict();
	//var $ = {};
	// PageLoad function
	// This function is called when:
	// 1. after calling $.historyInit();
	// 2. after calling $.historyLoad();
	// 3. after pushing "Go Back" button of a browser
	function pageload(hash) {
		// hash doesn't contain the first # character.
		//alert(hash);
		if(hash) {
			// restore ajax loaded state
			parseHash(hash);
		} else {
			
		}
	}