var current = 1;
var total = 16;
var hlItem;
var hl;
var work = [];
var displaying;
var slide;

$(document).ready(function(){
	$(this).bind("contextmenu", function(e) {
                e.preventDefault();
    });


	$("#theme").click(function(){
			if (!$('body').hasClass("black")){
				$('body').addClass("black")
			}else{
				$('body').removeClass("black")
			}
	})
	$(".menu a").click(function(){
		var self = this;
		$("#video").html("")
		
		$("#bio").hide();
		$("#contact").hide();
		$("#slideshow, .sub-menu").hide();
		$("#work, #work-album").hide()
		$(".menu a").removeClass("selected")
		$(this).addClass("selected")
		$(".sub-menu a").fadeOut("fast")
		
		$("#slideshow div").remove();
		clearInterval(slide)
		if ($(this).hasClass("bio")){
			$("#bio").fadeIn();
			
		}else if($(this).hasClass("contact")){
			$("#contact").fadeIn();
			
		}else{

		
		$.getJSON("ajax.php?load-category="+$(this).attr("data-id"), function(data) {
			
			$(".sub-menu a").remove();
			
			if ($(self).attr("data-id")=="motion"){
				//alert('d')
				$.each(data, function(key, val) {
					$(".sub-menu").append('<a href="javascript:void(0)" data-id="'+val[0]+'" class="motion" data-vimeo="'+val[3]+'">'+val[1]+'<span>'+val[2]+'</span></a>')
			  	});
			}else{
			$.each(data[0], function(key, val) {
				$(".sub-menu").append('<a href="javascript:void(0)" data-id="'+val[0]+'">'+val[1]+'<span>'+val[2]+'</span></a>')
		  	});
			$.each(data[1], function(key, val) {
				$("#slideshow").append('<div><img src="/assets/'+val+'/feature.jpg" /></div>')
		  	});
			
			slide = setInterval("fadeFeature()", 4000)
			
			if ($(self).hasClass("album")){
				$("#slideshow").delay(1000).fadeIn();
			}
			
		}
			
			$(".sub-menu").show()
			
		
			
			fadeInSubMenu()

		});
	}
	})
	$(".sub-menu a").live("click",function(){
		var self = this
		$("#video").hide();
		$("#slideshow").fadeOut("fast", function(){
			$(".sub-menu a").removeClass("selected")
			$(self).addClass("selected")
			if ($(self).hasClass("motion")){
				$("#work, #controls, #work table, #protect").hide()
				$("#work").fadeIn()
				
				var vimeo_html = '<iframe src="http://player.vimeo.com/video/'+$(self).attr("data-vimeo")+'?title=0&amp;byline=0&amp;portrait=0&amp;color=ffffff&amp;autoplay=1" width="800" height="450" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>';
				
				$("#video").html(vimeo_html).fadeIn();
				
			}else{
				$("#work table, #protect").show();
				getAlbum($(self).attr("data-id"))
				$("#print").attr("href","http://juliehealy.com/tcpdf/print/?i="+$(self).attr("data-id"))			
			}
		})
	})


	$("#next").click(function(){
		if (current<total){
			current++;
			updateCurrent()
			$("#work-container img").fadeOut(function(){
				displaying++
				displayImage(displaying)
			})
		}
	})
	
	$("#prev").click(function(){
		if (current>1){
			current--;
			updateCurrent()
			$("#work-container img").fadeOut(function(){
				displaying--
				displayImage(displaying)
			})
		}
	})
	$("#all").click(function(){
		$("#work").fadeOut(function(){
			$("#work-album a").hide()
			$("#work-album").show();	
			fadeInAlbum()
		})
	
	})

	$("#work-album a").live("mouseover", function(){
		hlItem = this;
		clearTimeout(hl)
		hl = setTimeout("highlight()",50)
	}).live("click",function(){
		var i = this;
		$("#work-album").fadeOut('fast', function(){
			//$("#work").fadeIn("fast")
			$('#feature-image').hide();
			$("#work, #controls").hide().fadeIn()
			displayImage($("#work-album a").index(i)+1)
			current=$("#work-album a").index(i)+1;
			updateCurrent()
		});
	})
	$(window).resize(function() {
	// 	setUpScreen()
	});
	$("#loading").click(function(){
		fadeOutSplashScreen()
	})
	setTimeout("fadeOutSplashScreen()", 4000)
	//setUpScreen()
	
})
function fadeOutSplashScreen(){
	$("#loading").fadeOut(function(){
		$(this).remove();
		$('body').removeClass('loading');
	});
}
function setUpScreen(){
	var documentWidth = $(document).width();
	var documentHeight = $(document).height();
	var featureWidth = documentWidth - 420 - 50
	$("#work").css("width", featureWidth)
	$("#work").css("height", $(document).height()-100)
	$("#work-container").css("height", documentHeight - 60 - 100)
	$("#feature-image").css("max-height", documentHeight - 60 - 100 - 40)
	$("#feature-image").css("max-width", featureWidth)

}
function fadeInSubMenu(){
	$(".sub-menu a:hidden:first").fadeIn("slow", function(){
		fadeInSubMenu()
	})
}
function fadeInAlbum(){
	$("#work-album a:hidden:first").fadeIn("fast", function(){
		fadeInAlbum()
	})
}
function highlight(){
	$(hlItem).fadeTo(200,1)		
	$("#work-album a").not(hlItem).fadeTo(10,0.5)
}
function updateCurrent(){
	$("#current").html(current)
	$("#total").html(total)
	
}
function getAlbum(id){
	$("#work, #work-album").hide()
	$('#feature-image').hide();
	work = [];
    
	$.getJSON("ajax.php?load-album="+id, function(data) {
		if (data){
		
		$("#work, #controls").hide().fadeIn()
			
		$("#work-album a").remove();
		var x = 0;
		$.each(data, function(key, val) {
			work.push(val);
			var imgCode = '<a href="javascript:void(0)"';
	   		x++;
			if (x%4==0){
				imgCode = imgCode + 'style="margin-right: 0;"';
			}
			imgCode = imgCode + '><img src="assets/'+val+'/173/thumb.jpg" /></a>';
			$("#work-album").append(imgCode)
	  	});
		total = x
		current = 1
		updateCurrent()
		displayImage(1) // Load first image
	}
	});	
		
}

function displayImage(id){
	displaying=id;
	$('#feature-image').attr('src', 'assets/'+work[displaying-1]+'/image.jpg').imgpreload(function(){
		$('#feature-image').fadeIn()
	});
}


function fadeFeature(){
	$("#slideshow div:visible:last").fadeOut(function(){
		if ($("#slideshow div:visible").size()==1){
			$("#slideshow div").show()
		}
	});
}
