if(!console) var console = {log: function(){}};

var ppGallery = {
	galleries: [// this array defines the categories and the directories that the images must be in
		// directories are determined by making the string all lower case and removing spaces so that
		// Special Occasions becomes specialoccasions
		"Infants",
		"Children",
		"Families",
		"Pets",
		"Special Occasions"
	],
	heroes: [// this array determines which photo should show up as the hero initially. the length of this array
			 //should be the same as galleries.
			 1,1,1,1,1
	],
	loadedGalleries: [],
	init: function(){
		ppGallery.preloadThumbs();
		ppGallery.buildGalleryCategories();
		ppGallery.bindThumbs();
		ppGallery.initNextPrev();
		ppGallery.bindNav();
		if(window.location.hash && !!($(window.location.hash).length)){
			$(window.location.hash).click();
		} else {
			$("#catNav a:first").click();
		}
		
	},
	preloadThumbs: function(){
		$.each(ppGallery.galleries,function(n,gallery){
			gallery = ppGallery.fixCatName(gallery);
		  	for(var i = 1; i<9; i++) {
				$("#hiddenImageContainer").append(jQuery("<img>").attr("src", "img/portrait/"+gallery+"/"+i+"_t.jpg"));
			}
		});
		
	},
	loadGallery: function(gallery, heroId){
		gallery = gallery ? gallery : ppGallery.galleries[0];
		var gName = ppGallery.fixCatName(gallery)
		ppGallery.setThumbnails(gName,heroId);
		ppGallery.preloadImages(gName);
		
	},
	setThumbnails: function(gallery,heroId){
		if($.support.tbody){
			$("#photoShadow").fadeOut(500)
		}
		var fadeId = $.support.tbody ? "#slideshow ul .img-shadow" : "#slideshow ul img";
		var thumbs = $(fadeId);
/*		for(var i=1;i<9;i++){
			var t = $(thumbs[i-1])*/
			thumbs.EffectChain({effect:"fadeTo", fadeToOpacity: 0, duration:.1,onComplete:function(){
										var images = $("#slideshow ul img").get();
										$.each(images,function(i,img){
											img.src = "img/portrait/"+gallery+"/"+(i+1)+"_t.jpg";
											$(img).removeAttr("width");
										});
										thumbs.EffectChain({effect:"fadeTo", fadeToOpacity: 1,duration:.25, onComplete: function(){ppGallery.setCurrentPhoto("img/portrait/"+gallery+"/"+(heroId>=1?heroId:1) +".jpg");}});
									  }})
		//}
		
//		$("#slideshow ul img").each(function(i,img){img.parentNode.style.marginLeft = (116-$(img).width())/2 + "px"});
	},
	preloadImages: function(gallery) {
		if($.inArray(gallery,ppGallery.loadedGalleries) >= 0){
			return;
		}
		ppGallery.loadedGalleries.push(gallery);
	  for(var i = 1; i<9; i++)
	  {
		$("#hiddenImageContainer").append(jQuery("<img>").attr("src", "img/portrait/"+gallery+"/"+i+".jpg"));
	  }
	},
	bindThumbs: function(){
		var thumbs = $("#slideshow ul img");
		
		thumbs.bind("click",function(evt){
			evt.preventDefault();
			var newSrc = this.src.replace("_t","");
			ppGallery.setCurrentPhoto(newSrc);
		});
		
		thumbs.bind("load",function(){
			this.parentNode.style.marginLeft = (116-$(this).width())/($.support.tbody ? 2 : 4) + "px";
		});
		
		$("#thePhoto").bind("load",function(){
			this.parentNode.style.marginLeft = (487-$(this).width())/($.support.tbody ? 2 : 4) + "px";
			var w = $(this).width();
			var h = $(this).height();
			console.log("thePhoto.load",w,h);
			$("#heroContainer").css({marginTop: w>h ? 60 : 0});
		});
	},
	
	bindNav: function(){
		$("#portraitGalleryNav a").bind("click",function(evt){
			evt.preventDefault();
			console.log($(this).attr("galleryId"),$($(this).attr("galleryId")));
			$("#"+$(this).attr("galleryId")).click();
		});
	},
	
	insertCategory: function(index,catName) {
		$("#catNav").append($("<li>").append($("<a>").html(catName).attr({href:"#", id:ppGallery.fixCatName(catName), heroId: ppGallery.heroes[index]})));
	},
	
	fixCatName: function(catName){
		return catName.replace(" ","").toLowerCase();
	},
	
	buildGalleryCategories: function(){
		$.each(ppGallery.galleries,ppGallery.insertCategory);
		$("#catNav a").bind("click",ppGallery.categoryClick);
		$("#catNav li:first").css("paddingLeft",0);
		
	},
	categoryClick: function(evt){
		evt.preventDefault();
		console.log("categoryClick",$(this).attr("heroId"));
		$("#catNav a").removeClass("current");
		$(this).addClass("current");
		
		ppGallery.loadGallery(this.id,$(this).attr("heroId"));
	},
	initNextPrev: function(){
		$("#nextPhoto,#prevPhoto").bind("click",ppGallery.navigatePhotos);
	},
	setCurrentPhoto: function(url){
		var fadeId = $.support.tbody ? "#photoShadow" : "#photoShadow,#thePhoto";
		$(fadeId).fadeOut(500,function(){
			$("#thePhoto").attr("src",url);
			$(fadeId).fadeIn(500);
		});
	},
	navigatePhotos: function(evt){
		evt.preventDefault();
		var step = this.id == "nextPhoto" ? 1 : -1;
		var src = $("#thePhoto").attr("src");
		var currentPhotoIndex = src.substr(src.lastIndexOf("/")+1,1)*1;
		var newPhotoIndex = (currentPhotoIndex + step)%8;
		newPhotoIndex = newPhotoIndex === 0 ? 8 : newPhotoIndex;
		var newSrc = src.replace(currentPhotoIndex+".jpg",newPhotoIndex+".jpg");
		ppGallery.setCurrentPhoto(newSrc)
/*	$("#thePhoto").fadeOut(500,function(){
			$(this).attr("src",newSrc);
			var w = $(this).width();
			var h = $(this).height();
			$("#heroContainer").css({height: h, width: w, marginTop: w>h ? 60 : 0});
			$(this).fadeIn(500);
		});*/
	}
}

$(ppGallery.init);

