// JavaScript Document

function hpPhotoGallery(){
	$(function(){
		function hpPhotoGalleryInit(){
			$("a[hpphoto]").unbind("click",hpPhotoGalleryOpen);
			$("a[hpphoto]").bind("click",hpPhotoGalleryOpen);
		}
		function hpPhotoGalleryOpen(){
					if($("#hpPhotoGallery").length){ $("#hpPhotoGallery").remove(); $("#hpPhotoGallery_close").remove(); }
					var gal = $(this).attr("hpphoto");
					var ind = $(this).index("a[hpphoto='"+gal+"']");
					
					var wh = parseInt($(window).height())-100;
					var caseDiv = '<a href="javascript:;" id="hpPhotoGallery_close"></a><div id="hpPhotoGallery" style="height:'+wh+'px;"><a href="javascript:;" id="hpPhotoGallery_back"></a><div id="hpPhotoGallery_photoarea_case"><ul id="hpPhotoGallery_photoarea"></ul></div><a href="javascript:;" id="hpPhotoGallery_go"></a></div>';
					$("body").append(caseDiv);
					$("#hpPhotoGallery").hide();
					$("#hpPhotoGallery_go").css("height",wh+"px");
					$("#hpPhotoGallery_back").css("height",wh+"px");
					$("#hpPhotoGallery_photoarea_case").css("height",wh+"px").css("width",(parseInt($("#hpPhotoGallery").width())-120)+"px");
					$("#hpPhotoGallery_photoarea").css("height",wh+"px");
	
					var LarguraTotal = 0;
					$("a[hpphoto='"+gal+"']").each(function(){
						LarguraTotal+=parseInt($("#hpPhotoGallery_photoarea_case").css("width"));
						var src = $(this).attr("href");
						if(src.search(/youtube.com/)!=-1){
							src = src.split("/");
							src = src[src.length-1];
							src = src.split("=");
							src = src[src.length-1];
							$("#hpPhotoGallery_photoarea").append('<li><div class="hpPhotoGallery_frame"><object style="height: 100%; width: 100%"><param name="movie" value="http://www.youtube.com/v/'+src+'?version=3"><param name="allowFullScreen" value="true"><param name="allowScriptAccess" value="always"><embed src="http://www.youtube.com/v/'+src+'?version=3" type="application/x-shockwave-flash" allowfullscreen="true" allowScriptAccess="always" width="100%" height="100%"></object></div></li>');
						}else{
							$("#hpPhotoGallery_photoarea").append('<li><img src="'+src+'" border="0" alt="" /></li>');
						}
					});
					$("#hpPhotoGallery_photoarea li:eq("+ind+")").attr("atual");
					$("#hpPhotoGallery_photoarea li").css("width",$("#hpPhotoGallery_photoarea_case").css("width"));
					$("#hpPhotoGallery_photoarea").css("width",LarguraTotal+"px");
					
					$("#hpPhotoGallery_photoarea").find("img,.hpPhotoGallery_frame").each(function(){
						var eu = $(this);
						eu.hide();
						var wid=0;
						if(eu.attr("class") == "hpPhotoGallery_frame"){
								wid = parseInt(eu.height());
								if(wid > (wh-20)){
									eu.attr("height",(wh-20));
								}
								eu.css("marginTop",((wh-parseInt(eu.height()))/2)+"px").fadeIn(500);
						}else{
							eu.load(function(){
								wid = parseInt(eu.height());
								if(wid > (wh-20)){
									eu.attr("height",(wh-20));
								}
								eu.css("margin",((wh-parseInt(eu.height()))/2)+"px 0 0 0").fadeIn(500);
							});
						}
					});
					hpPhotoGalleryChecker();
					hpPhotoGalleryGoto(ind);
					$("#hpPhotoGallery_go").click(function(){hpPhotoGalleryGo(1);});
					$("#hpPhotoGallery_back").click(function(){hpPhotoGalleryGo(-1);});
					$("#hpPhotoGallery_close").click(function(){hpPhotoGalleryClose();});
					
					$(document).bind("keydown",hpPhotoGalleryKey);
					$("#hpPhotoGallery").fadeIn(500);
			return false;
		}
		hpPhotoGalleryInit();
		function hpPhotoGalleryGoto(num){
			var nova = $("#hpPhotoGallery_photoarea li:eq("+num+")");
			
			/*var nova = $("#hpPhotoGallery_photoarea li[atual]").next("li");*/
			if(nova.length){
				$("#hpPhotoGallery_photoarea li[atual]").removeAttr("atual");
				nova.attr("atual","true");
				var myw = parseInt(nova.index())*parseInt($("#hpPhotoGallery_photoarea_case").css("width"));
				 $("#hpPhotoGallery_photoarea").stop().animate({marginLeft:"-"+myw+"px"},800);
			}
			hpPhotoGalleryChecker();
		}
		function hpPhotoGalleryGo(num){
			var nova;
			if(num==1) nova = $("#hpPhotoGallery_photoarea li[atual]").next("li");
			else if(num==-1) nova = $("#hpPhotoGallery_photoarea li[atual]").prev("li");
			if(nova.length){
				$("#hpPhotoGallery_photoarea li[atual]").removeAttr("atual");
				nova.attr("atual","true");
				var myw = parseInt(nova.index())*parseInt($("#hpPhotoGallery_photoarea_case").css("width"));
				 $("#hpPhotoGallery_photoarea").stop().animate({marginLeft:"-"+myw+"px"},800);
			}
			hpPhotoGalleryChecker();
		}
		function hpPhotoGalleryKey(event){
			if(event.keyCode==39) hpPhotoGalleryGo(1);
			else if(event.keyCode == 37)  hpPhotoGalleryGo(-1);
			else if(event.keyCode == 27)  hpPhotoGalleryClose();
		}
		function hpPhotoGalleryChecker(){
			if(!$("#hpPhotoGallery_photoarea li[atual]").prev("li").length) $("#hpPhotoGallery_back").fadeTo(500,0.2);
			else $("#hpPhotoGallery_back").fadeTo(500,1);
			if(!$("#hpPhotoGallery_photoarea li[atual]").next("li").length)$("#hpPhotoGallery_go").fadeTo(500,0.2);
			else $("#hpPhotoGallery_go").fadeTo(500,1);
		}
		function hpPhotoGalleryClose(){
			$(document).unbind("keydown",hpPhotoGalleryKey);
			$("#hpPhotoGallery_close").remove();
			$("#hpPhotoGallery").fadeOut(500,function(){$("#hpPhotoGallery").remove();})
		}
	});
}
hpPhotoGallery();
