var loopIndex = 0; 
var currentIndx = 0;

$(function() {
	
	$.urlParam = function(name){
	      var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);
				if (results) {
					return results[1];
				} else {
					return 0;
				}
	  	}


	     if ($.urlParam('targetgallery')) {
			target = $.urlParam('targetgallery');
		} else {
			target = "0";
		}
		
	target++;
		
	$("<ul></ul>").appendTo("#contentLeft");
	 $.ajax({
		 type: "GET",
		 url: "/xml/photoTour.xml",
		 dataType: "xml",
		 error: function() {
					alert("There has been an error.");
					},
		 success: function(xml) {
			 $(xml).find("gallery").each(function(){
					var galleryName = $(this).attr("name");
					var galleryId = $(this).attr("id");
				 $("<li></li>").html(galleryName).attr("id", galleryId).appendTo("#contentLeft ul");
			 }); 
			loadGallery("gallery#g"+ target); 
		
			$("#contentLeft ul li#g"+target).addClass("currentGallery");
			$("#contentLeft ul li").click(
				function() {
					if($(this).hasClass("currentGallery")==false) {	
						$.idle(null, 1);
						$("#contentLeft ul li").removeClass("currentGallery");
						$(this).addClass("currentGallery");
						var gallId = $(this).attr("id");
						var whatGallery = "gallery#" + gallId;
						$("#frame ul").html("");
						$("#slideHolder").html("");
						loadGallery(whatGallery); 
						$("#frame ul").css("left", "0");
					}  else {
						return false;
					}
				}
			);
		 }
	 });//end ajax
			function loadGallery(whatGallery) {
				
			$("#contentRight h2").html("Loading");
			$.ajax({
				type: "GET",
				url: "/xml/photoTour.xml",
				dataType: "xml",
				success: function(xml) {
					$(xml).find(whatGallery).each(function(){
						var galleryName = $(this).attr("name");
						var images = $(this).find("image");
						$(images).each(function() {
						var imageId= $(this).attr("id");
						var altText = $(this).attr("alt")
						var thumbs = $(this).find("thumb");
						//});
						$(thumbs).each(function() {
						var thumbPaths = $(this).attr("src");
						$("#contentRight h2").html(galleryName);
						$("<li></li>").html($("<img />").attr("src", thumbPaths).attr("alt", altText)).addClass(imageId).appendTo("#frame ul");
						$("#frame ul li:first").css("border-color", "#fff");
						$("p#playTour").css("backgroundPosition", "0px 0px");
						loopIndex = 0;
						currentIndx = 0;
						$("p#photoText").html($("#frame ul li img:first").attr("alt"));
					});					
					var mainImages = $(this).find("main");
					
					$(mainImages).each(function() {
						var mainPaths = $(this).attr("src");
						$("<img />").attr("src", mainPaths).attr("id", imageId).attr("alt", altText).appendTo("#slideHolder");
						$("#mainImage img#loader").show();
						$("#slideHolder img").hide();
						
		})
						$.preload( $('#slideHolder img'),  { onFinish: function() {	
																$("#mainImage img#loader").hide();
																$("#slideHolder img:first").fadeIn(1000);
																$.idle(loop, 6000);																		
																  }
						
						
					});
					
					
				});				
			 }); // end xml find	

			 
			 
			 
			 $("#frame ul li").click (
				function() {	
					currentIndx = $("#frame ul li").index(this);
					hideImages();
					showImage(this);
					$(this).css("border-color", "#fff");
					$("p#playTour").css("backgroundPosition", "0px -14px");
				}
			);
			function hideImages() {		
				$("#slideHolder img").fadeOut("slow");
				$("#frame ul li").css("border-color", "#000");
			}
			
			function showImage(whatImage) {
				//$("#frame ul li").unbind("click"); 
				var source = $(whatImage).attr("class");
				var target = document.getElementById(source);
				$(target).fadeIn("slow");
				var text = $(target).attr("alt");
				$("p#photoText").html(text);
			}
			$("p#next").click(function() {
		forward();		
});

			$("p#prev").click(function() {
					backward();		
			});
			function forward() {
				$("p#next").unbind("click");
				$.idle(null, 1); 
	
				var slideHolder = $("#slideHolder img"); 
				var allThumbs = $("#frame ul"); 
				var thumbs = $("#frame ul li");
				
				var thumbNumber = (thumbs.length)/3.01;
				var rndThumbNumber = Math.floor(thumbNumber);
				var thumbImg  = $("#frame ul li img"); 
				var pos = $("#frame ul").css("left"); 
				var totThumbs = (thumbs.length)/3.01;
				var finalFrame = Math.floor(totThumbs)*3;
				if(rndThumbNumber > 0) {
				$("p#playTour").css("backgroundPosition", "0px -14px");
				$(thumbs).css("border-color", "#82664E");
					if((loopIndex != rndThumbNumber*3) && ((currentIndx > (finalFrame-1)) == false)) {				
						 currentIndx = (loopIndex+3);
						var numPos = ((parseFloat(pos))-630) + "px";
						loopIndex = loopIndex+3;
					} else {
						loopIndex=0;
						currentIndx = 0;
						numPos = 0
					}
						$(allThumbs).animate(
									{ left: numPos },
									1000, function() {			
						$(thumbs[currentIndx]).css("border-color", "#fff"); 
						$(slideHolder).hide();
						$(slideHolder[currentIndx]).fadeIn("slow");
						var text = $(thumbImg[currentIndx]).attr("alt");
						$("p#photoText").html(text);
						$("p#next").bind("click", forward);
						});
						
				} else {
					return false;
				}
	

}		


			
function backward() {
	$("p#prev").unbind("click");
	$.idle(null, 1);
	
	$(thumbs).css("border-color", "#82664E");
		var slideHolder = $("#slideHolder img"); 
		var allThumbs = $("#frame ul"); 
		var thumbs = $("#frame ul li");
		var thumbNumber = (thumbs.length)/3.01;
		var rndThumbNumber = Math.floor(thumbNumber);
		var prevNumb = (thumbs.length)/3.01;
		var rndPrevNumb = Math.floor(prevNumb);
		var lastFrame = rndPrevNumb*3;
			var thumbImg  = $("#frame ul li img"); 
			var pos = $("#frame  ul").css("left"); 
		if(rndThumbNumber > 0) {	
		$("p#playTour").css("backgroundPosition", "0px -14px");		
			if(pos == "0px" || pos == "0pt") {
				var numPos = (-630 * rndPrevNumb) +"px";
				loopIndex = rndPrevNumb*3;
				currentIndx = thumbs.length-1;
			} else {
					var numPos = ((parseFloat(pos))+630) + "px";
					currentIndx = loopIndex-3;
					loopIndex = loopIndex-3;
			}
			$(allThumbs).animate(
						{ left: numPos },
						1500, function() {
			 
			$(thumbs[currentIndx]).css("border-color", "#fff"); 
				$(slideHolder).hide();
				$(slideHolder[currentIndx]).fadeIn("slow");
				var text = $(thumbImg[currentIndx]).attr("alt");
				$("p#photoText").html(text);
				$("p#prev").bind("click", backward);
			});
		} else {
			return false;
		}
	}	
		 } 			
	 });//end ajax
}
	
});
	 



$(function() {       
	
	var moz = (typeof document.implementation != "undefined") && (typeof
document.implementation.createDocument != "undefined");             
	if (moz) {
		var home = "0pt";  
} else {
		var home = "0px";  
}

	                      

loop =  function(){    
	var slideHolder = $("#slideHolder img"); 
	var thumbs = $("#frame ul li"); 
	var allThumbs = $("#frame ul"); 
		if(thumbs.length == 1) {
			return false;
		} else {
			for (var i=0; i < thumbs.length; i++) {     
				$(slideHolder[i]).fadeOut("slow");
				$(thumbs[i]).css("border-color", "#000");
			}         
			if (currentIndx < thumbs.length-1) {
				currentIndx=currentIndx+1;
				var targImg = $(thumbs[currentIndx]).attr("class");
				var quelImage = "#slideHolder img#" + targImg;
				$(quelImage).fadeIn("slow");
				var text = $(slideHolder[currentIndx]).attr("alt");
				$("p#photoText").html(text);
				$(thumbs[currentIndx]).css("border-color", "#fff");  
				
				
				
				
			} else {
				currentIndx=0;  
			$(slideHolder[currentIndx]).fadeIn("slow"); 
			$(thumbs[currentIndx]).css("border-color", "#fff"); 				
			}					
			if(currentIndx == 0) {
				$.idle(null, 1);
				$(allThumbs).animate(
						{ left: home },
						1000);				 
			} else if(currentIndx % 3 == 0) {
				$.idle(null, 1);
				$(allThumbs).animate(
						{ left: (((currentIndx/3) * -630) + "px")  },
						1000);				 
			}
			$.idle(loop, 6000); 
		}		
}  

		


	
$("p#playTour").click(
	function() {
		if($(this).css("backgroundPosition") == "0px -14px") {
		loop();
			$(this).css("backgroundPosition", "0px 0px");
		} else {
			$.idle(null, 1);
			$(this).css("backgroundPosition", "0px -14px");
		}
	}
);
	
	
});