var debugMode;

function quickInit(){
	$("#header").css("margin-top",($(window).height()-375)/2+"px");
	$("#thumbs").hide();
	$("#fullview").hide();
}

$(document).ready(function(){

	if (jQuery.browser.mozilla){
		debugMode=true;
	}


	checkHash();

	//categories navigation
	$("#mainnav a").click(function(){
		var category = parseInt($(this).attr("rel"));
		selectCategory(category);
	});

	//general links navigation
	$("#secondarynav a").click(function(){
		selectCategory(-1);
		showContent($(this).attr("href"));
		return false;
	});


	//header display
	$("#header a:not(#shiri)").click(function(){
		var artworkParams = $(this).attr("rel").split(",");
		var category = parseInt(artworkParams[0]);
		var artwork = artworkParams[1];
		selectCategory(category);
		setTimeout(function(){
				selectArt(artwork);
			},0);
	});
	$("#header #shiri").click(function(){
		showContent("me.htm");
		return false;
	});


	//Thumbnails click
	$(".thumb").click(function(){
		var artid = $(this).attr("artid");
		selectArt(artid);
	});


	$("#browse-close").click(function(){
		$("#fullview").slideUp("slow");
		return false;
	});

	//Scroll-to-top click

	$("#scrolltop").click(function(){
		positionMode("top");
	});

});








function selectCategory(category){

	if (category>=0){//-1 means just close the thumbs and unmarking the category
		positionMode("nav");
	}

	$("#thumbs").slideUp("fast",function(){
		if (category<0){
			//unmark all
			$(".thumb").removeClass("selected");

		}else{
			//show all
			if (category==0){
				$(".thumb").show();

			//show category
			}else{
				$(".thumb:not([type*='"+category+"'])").hide();
				$(".thumb[type*='"+category+"']").show();
			}
			//open thumbs strip back
			$("#thumbs").slideDown("fast");
		}

	});


	$("#mainnav a").removeClass("selected");
	$("#mainnav a[rel*='"+category+"']").addClass("selected");

	//pageTracker._trackPageview('/select category/'+category)

	return false;
}


function positionMode(modeName){
	switch (modeName){
		case "top":
			$("html,body").animate({scrollTop:0}, 800);
			selectCategory(-1);
			$("#fullview").slideUp("fast");
			break;

		case "nav":
			//$("#scrolltop").fadeIn('slow');
			$("html,body").animate({scrollTop:$("#nav").offset().top-20}, 800);
			//$("#scrolltop").show();
			break;

		case "content":
			$("html,body").animate({scrollTop:$("#fullview").offset().top-20}, 800);
			$("#scrolltop").show();
			break;
			}

}

function selectArt(artid){
	var contentPath= "fullview.php?artid=" + artid;
	$(".thumb").removeClass("selected");
	$(".thumb[artid='"+artid+"']").addClass("selected");
	showContent(contentPath);
	//pageTracker._trackPageview('/select artwork/'+artid)
}


function showContent(url){

	if (debugMode) console.log("url: "+url);

	//positionMode("nav");
	//url="test"+Math.ceil(3*Math.random())+".htm"; //TEMPORARY!!!!!!!!
	$("#fullview").slideUp("fast",function(){
		//$("#fullview #content").empty();
		$("#fullview #content").load(url,function(){
			$("#fullview").slideDown("fast");
		});

	});
}

function checkHash(){
	if (location.hash.length<2)return;
	var hashString = location.hash.toString().replace("#","");
	//var categoryNum = parseInt($("#mainNav a[href*='#"+hashString+"']").attr("rel"));
	selectCategory(hashString);

}
