// JavaScript Document
var paths = new Array(), titles = new Array(), descs = new Array(), orientations = new Array(), index = 0,
	windowWidth = $(window).width(),
	windowHeight = $(window).height();
var imgs = new Array(paths.length);

$(document).ready(function(){
	var maxi = 0, mini = 0, h = window.screen.availHeight;
	
	if (h <= 770){
		maxi = 425; mini = 283;
	}else if (h <= 800){ 
		maxi = 454; mini = 302;
	}else if (h <= 900){
		maxi = 509; mini = 339;
	}else if (h <= 1024){
		maxi = 590; mini = 393;
	}else if( h <= 1050){
		maxi = 607; mini = 401;
	}else if( h <= 1080){
		maxi = 626; mini = 413;
	}else {
		maxi = 704; mini = 464;
	}		   
	//functionality used for the navigation
	$('ul li ul').hide();
	$('ul li ul li ul').hide();
	var hidden = true;
	
	$('ul li:first').click(function(){
		if(hidden){
			$(this).children("ul").slideDown(700);
			$(this).css("margin-bottom", "10px");
			hidden = false;
		}else{
			$(this).children("ul").slideUp(300);
			$(this).css("margin-bottom", "0px");
			hidden = true;
		}
		
		return false;
	});
	
	var hide = true;
	$('ul li ul li').click(function(){
		if($(this).find("a").css("font-size") !== "11px"){
			if($(this).children("ul").css("display") === "none"){
				$('ul li ul li').children("ul").slideUp(300);
				$(this).children("ul").slideDown(700);
				hide = false;
			}else{
				$(this).children("ul").slideUp(300);
				//document.cookie = "page=; expires=-1";
				hide = true;
			}
			return false;
		}
		var d = new Date();
		var tomo = new Date(d.getDate()+1);
		if(navigator.userAgent.match(/Internet Explorer/) != null){
			document.cookie = "page=" + $(this).parent().parent().attr('id') + "expires=" + tomo.toGMTString();
		}else{
			document.cookie = "page=" + $(this).parent().parent().attr('id');
		}
		window.location = $(this).children("a").attr("href");
		if($(this).children("a").attr("href").match("video") != null){
			
		}else if((window.location.href.match("gallery") != null)){
			window.location.reload();	
		}
				
	});
	
	//******************************
	//this can probably go
	//call for loading the json for the gallery pages
	if(document.getElementById("leftTwo") != undefined){
		index = 0;
		var s = window.location.href.split("#");
		s[1] = s[1].replace("%20", " ");
		s[1] = s[1].replace("%20", " ");
		
		loadJson2(s[1]);
		
	}
	
	//*********************************
	if(window.location.href.match("video") != null){
		$('#four').parent("ul").slideDown(700);
		$('#four').children("ul").slideDown(700);
		$(this).css("margin-bottom", "10px");
		hide = false;
		hidden = false;
	}
	
	if(window.location.href.match("gallery") != null){
		var page = getCookie("page");
		$('#' + page).parent("ul").slideDown(700);
		$('#' + page).children("ul").slideDown(700);
		
		$(this).css("margin-bottom", "10px");
		hide = false;
		hidden = false;
	}
	
	//load the main galley images
	if(document.getElementById("left") != undefined){
		index = 0;
		var s = window.location.href.split("#");
		var p = s[1].split(".");
		$('#'+p[0]).addClass("current");
		loadJson(s[1]);
		
		//toggles for the thumbs and images switch
		var firstTimeToggle = true;
		$('#toggleThumbs').click(function(){
			if(firstTimeToggle){
				loadThumbs();
				firstTimeToggle = false;
			}
			
			if($('#thumbs').css("display") == "none"){
				$('#left').addClass("hidePanel");
				$('#thumbs').removeClass("hidePanel");
				$('#toggleThumbs img').attr("src", "images/toggle_image.png");
				
			}else{
				$('#thumbs').addClass('hidePanel');
				$('#left').removeClass('hidePanel');
				$('#toggleThumbs img').attr("src", "images/toggle_thumbs.png");
				
			}
			return false;
		});
		
		
		//gallery navigation back button
		$('#back').click(function(){
			//console.log("back");
			index--;
			if(index < 0){
				index = paths.length-1;
			}
			$('#galleryImg').fadeOut("slow", function(){
				$('#loading').fadeIn("slow", function(){
					loadImages();									  
				});										  
			});
			
			return false;
		});
		
		//gallery navigation forward button
		$('#forward').click(function(){
			
			index++;
			if(index+1 > paths.length){
				index = 0;
			}
			$('#galleryImg').fadeOut("slow", function(){
				$('#loading').fadeIn("slow", function(){
					loadImages();									  
				});										  
			});
			
			return false;
		});
		
	
	//function to display an image click on in the thumbs display
	$('#thumbs a img').live("click" ,function(){
		var id = $(this).attr("id");
		id = id.replace("thumbs","");
		index = id;
		loadImages();
		if($('#thumbs').css("display") == "none"){
			$('#left').addClass("hidePanel");
			$('#thumbs').removeClass("hidePanel");
			$('#toggleThumbs img').attr("src", "images/toggle_image.png");
			
		}else{
			$('#thumbs').addClass('hidePanel');
			$('#left').removeClass('hidePanel');
			$('#toggleThumbs img').attr("src", "images/toggle_thumbs.png");
			
		} 
		return false;
	});
}
	if(document.getElementById("videoImage") != undefined){
		//$('#videoImage').attr("width", (windowWidth * .37) + "px");
		//$('#videoImage').attr("height", (windowHeight  * .47) + "px");
		$('#leftThree').css("height", (maxi) + "px");
		$('#videoImage').css("margin-top", "8%");
		$('#videoImage').css("margin-left", "0%");
		$('#content').attr('height', "100%");	
	}
	
	
	
	//funtion to load the images for the galleries
	function loadJson(file){
		try{
			
			$.getJSON("imagesJSON/" + file, null, function(json){
				$.each(json.images.image, function(i,imgDetails){	
					
					paths[i] = imgDetails.path;
					titles[i] = imgDetails.title;
					descs[i] = imgDetails.description;
					orientations[i] = imgDetails.orientation;	
				
					if(orientations[index] === "portrait"){
						$('#galleryImg').attr("width", mini + "px");
						$('#galleryImg').attr("height", maxi + "px");
						$('#galleryImg').css("margin-left", "7.5%");
						$('#galleryImg').css("margin-top", "0");
					}else{
						$('#galleryImg').attr("width", maxi + "px");
						$('#galleryImg').attr("height", mini + "px");
						$('#left').css("height", (maxi) + "px");
						$('#galleryImg').css("margin-top", "8%");
						$('#galleryImg').css("margin-left", "0%");
					}		
					
					
					$('#galleryImg').attr("src", paths[index]);
					$('#galleryImg').attr("alt", titles[index]);
					$('#content').attr('height', "100%");
					$('#titleText').html(titles[index]);
					$('#descText').html(descs[index]);
				});
				
			});
			
		}catch(e){
			return false;
		}finally{
			loadImages();
		}
	}
	
	//****************************************
	//second load json function
	//****************************************
	
	function loadJson2(s){
		var count = 0;
		
		try{
			
			$.getJSON("imagesJSON/images.json", null, function(json){
				$.each(json.images.image, function(i,imgDetails){	
					paths[i] = imgDetails.path;
					titles[i] = imgDetails.title;
					descs[i] = imgDetails.description;
					orientations[i] = imgDetails.orientation;	
					if(titles[i] == s){
						count = i;
					}
					
					if(orientations[count] === "portrait"){
						$('#galleryImg').attr("width", mini + "px");
						$('#galleryImg').attr("height", maxi + "px");
						$('#galleryImg').css("margin-left", "7.5%");
						$('#galleryImg').css("margin-top", "0");
					}else{
						$('#galleryImg').attr("width", maxi + "px");
						$('#galleryImg').attr("height", mini + "px");
						$('#left').css("height", (maxi) + "px");
						$('#galleryImg').css("margin-top", "8%");
						$('#galleryImg').css("margin-left", "0%");
					}		
					
					//$('#galleryImg').load(paths[index])
					$('#galleryImg').attr("src", paths[count]);
					$('#galleryImg').attr("alt", titles[count]);
					$('#content').attr('height', "100%");
					$('#titleText').html(titles[count]);
					$('#descText').html(descs[count]);
				});
					
			});
			
			
		}catch(e){
			return false;
		}finally{
			//loadImages();
		}
	}
	
	function loadThumbs(){
		for(var i = 0; i < paths.length; i++){
			
			var s = "<a href=\"#\"><img src=\"" + paths[i] + "\" class=\"smallImg\" title=\"" + titles[i] + "\" alt=\"" + titles[i] + "\" id=\"thumbs" + i + "\"";
			if(orientations[i] === "portrait"){
				s += "height=\"113\" width=\"75\" /></a>";
			}else{
				s += "height=\"75\" width=\"113\" /></a>";
			}
			
			var d = document.getElementById('thumbs').innerHTML += s;
			
		}
	}
	
	
	function getCookie(name) {
		//alert(document.cookie);
		var c = document.cookie.split(";");
		//alert(c.length);
		if(c.length > 0){
			for(var i = 0; i < c.length; i++){
				if(c[i].match("page") != null){
					var co = c[i].split("=");
					//alert(co[1]);
					return co[1];
				}
			}
		}
	}
	
	function loadImages(){
		try{
			//console.log("in images");
			//$('#galleryImg').load(paths[index])
			$('#galleryImg').attr("src", paths[index]);
			$('#galleryImg').attr("alt", titles[index]);
			$('#content').attr('height', "100%");
			$('#titleText').html(titles[index]);
			$('#descText').html(descs[index]);
			if(orientations[index] === "portrait"){
				//alert(orientations[index]);
				$('#galleryImg').attr("width", mini + "px");
				$('#galleryImg').attr("height", maxi + "px");
				$('#galleryImg').css("margin-left", "7.5%");
				$('#galleryImg').css("margin-top", "0");
				document.getElementById("galleryImg").height = maxi;
				document.getElementById("galleryImg").width = mini;
			}else{
				//alert(orientations[index]);
				$('#galleryImg').attr("width", maxi + "px");
				$('#galleryImg').attr("height", mini + "px");
				$('#left').css("height", (maxi) + "px");
				$('#galleryImg').css("margin-top", "8%");
				$('#galleryImg').css("margin-left", "0%");
				document.getElementById("galleryImg").height = mini;
				document.getElementById("galleryImg").width = maxi;
			}		
			
			$('#loading').fadeOut("slow",function(){
				$('#galleryImg').fadeIn('slow');
			});
			//console.log("paths" + paths + ":::index: " + index);
			//console.log(paths[index]);		
			return true;
		}catch(e){
			//Console.log("Error: " + e);
			alert(e);
		}
	}
});


