var itemperpage = 5;
var totalpages;

var currentsection;
var currentpage = 0;
var currentitem;

var showInfo = false;
var sections = ["recentwork","download"];
var recentitems = [];
var downloaditems = [];

var recentHash = '';
var mark = /\?/;


function checkHash() {
		if ((window.location.hash!=recentHash)) {
			recentHash = window.location.hash;
			var hash = window.location.hash;
			if(hash.match(mark)){
				var splitHash = hash.split(/\?/);
				var section = splitHash[0];
				var item = splitHash[1];
				directTo(section, item);
			}else{
				var section = hash;
				directTo(section);
			}
		}
}

function directTo(section, item){
	section = section.replace(/#/,'');
 	

 	var sectionIndex = jQuery.inArray(section, sections);
 	
 	if (sectionIndex == -1){
 		section = "recentwork";
 		sectionIndex = 0;
 	}
 	if (section != currentsection){
 		display(section);
 	}
 	

 	 
 	$("#menuList li").removeClass('active');
 	$("#menuList li").eq(sectionIndex).addClass('active');
 	
 	if(item && item != currentitem){
		loadItem = "#" + item;
		$(".indicator").remove();
		
		html = '<a href="#'+section+'" onclick="javascript:unexpand(\''+item+'\');"><img src="images/overlay.png" class="indicator" alt="" /></a>';
		
		if(section == "download"){
		
			var itemIndex = jQuery.inArray(item, downloaditems);
			$("#downloads li").removeClass('active');
			$("#downloads li").eq(itemIndex).addClass('active');	
			$("#downloads li").eq(itemIndex).after(html);
			
			page = Math.floor(itemIndex/itemperpage);
			
			if (currentpage != page){
				if (page == -1){
					page = 0;
				}
				currentpage = page;
				position = (480*-1*page)+"px";
				
				$("#downloads ul.previews").animate({
					left: position
				},500);

				navCheck();
			}
		}else if(section == "recentwork"){
		
			
			var itemIndex = jQuery.inArray(item, recentitems);

			$("#recentworks li").removeClass('active');
			$("#recentworks li").eq(itemIndex).addClass('active');	
			$("#recentworks li").eq(itemIndex).after(html);
			
		}
		$(".details").hide();
		$(loadItem).fadeIn(300);
		currentitem = item;
	}else{
		currentitem = "";
	}
}


$(document).ready(function(){
	$(".details").hide();
	currentsection = "recentwork";
	
	$("#downloads ul li a").each(function(){
		var itemName = this.getAttribute('href');
		itemName = itemName.replace(/#download\?/,'');
		downloaditems.push(itemName);
	})
	$("#recentworks ul li a").each(function(){
		var itemName = this.getAttribute('href');
		itemName = itemName.replace(/#recentwork\?/,'');
		recentitems.push(itemName);
	})	
	checkHash();
	setInterval(checkHash, 500);	

	totalpages = Math.ceil($("#downloads ul li").length/itemperpage);



	$("h1 a").mouseover(function(){
		$("h1 a").animate({
			left: "0px"
		},400);
	});
	$("h1 a").mouseout(function(){
		if (!showInfo){
			$("h1 a").animate({
				left: "-60px"
			},400);
		}
	});
	$("h1").click(function(){
		if (showInfo){
			$("#header").animate({
				top: "100px"
			},400);
			showInfo = false;
		}else{
			$("#header").animate({
				top: "0px"
			},400);	
			showInfo = true;
		}
	});
	$("#menuList li a.sLink").click(function(){
		var selected = $(this).parent();
		$("#menuList li").removeClass('active');
		selected.addClass('active');
	});
	$(".details").mouseover(function(){
		var selected = $(this).parent();
		$(".actionBar").fadeIn(500);
	});
	$("#page").click(function(){
		$(".actionBar").fadeOut(500);
	});
	
	$("#btnNext").click(function(){
		if (currentpage < totalpages-1){
		$("#downloads ul.previews").animate({
			left: "-=480px"
		},500);
		currentpage += 1;
		navCheck();
		}
	});
	$("#btnPrev").click(function(){
		if (currentpage > 0){
			$("#downloads ul.previews").animate({
				left: "+=480px"
			},500);
			currentpage -= 1;
			navCheck();
		}
	});
	$(".section").hide();
	$(".rwBox").hide();
	$(".dlBox").hide();
	$(".actionBar").hide();
	$(".section:first").show();
	navCheck();
});

function display(id){
	$(".rwBox").hide();
	$(".details").hide();
	$(".section").hide(1);
	$("#recentworks li").removeClass('active');
	$("#downloads li").removeClass('active');
	$(".indicator").remove();
	$("#"+id).fadeIn(600);
	currentsection = id;
}
function unexpand(item){
	if (item == currentitem){
		$("#recentworks li").removeClass('active');
		$("#downloads li").removeClass('active');
		$(".details").fadeOut(500);
		$(".indicator").remove();
		currentitem = "";
	}
}
function navCheck(){
	if (currentpage == 0){
		$("#btnPrev").animate({
			opacity: "0"
		},500);
	}else if (currentpage > 0){
		$("#btnPrev").animate({
			opacity: ".8"
		},500);			
	}
	if (currentpage >= totalpages-1){
		$("#btnNext").animate({
			opacity: "0"
		},500);					
	}else if (currentpage < totalpages-1){
		$("#btnNext").animate({
			opacity: ".8"
		},500);
	}
}
