
function activate_button(selector,a) {	
	$("#menu li a.active").each(function(){
	$(this).removeClass("active");
	$(this).stop().animate(
	{backgroundPosition:"(6px 0px)"}, 
	{duration:360});						  
	});	
	$(selector).addClass("active");
	$(selector).stop().animate(
	{backgroundPosition:"(6px "+a+")"}, 
	{duration:360})	
}

function button(selector,a,b) {
	if($(selector).hasClass("active")) {
		$(selector).css( {backgroundPosition: "6px "+b+""} )
		}
	else {
		$(selector).css( {backgroundPosition: "6px 0px"} )}
	
	$(selector) 
	.mouseover(function(){
		if($(this).hasClass("active")) {}
		else {
		$(this).stop().animate(
			{backgroundPosition:"(6px "+a+")"}, 
			{duration:180})
		}
		})
	.mouseout(function(){
		if($(this).hasClass("active")) {}
		else {
		$(this).stop().animate(
			{backgroundPosition:"(6px 0px)"}, 
			{duration:360});}
		})
	

}

function ajaxize(hash) {
	$("#footer").hide();
	var hash_array = hash.split("-");
	var category = hash_array[0];			
	hash = hash.replace('-', '/');	
	
	if(hash) href = hash;
	else var href = $(this).attr("href");
	
	activate_button("#menu-"+category,"-32px");
	$("#footer").fadeOut("fast");
	$(".content").fadeOut("normal",function() {
		$("#loader").fadeIn("normal");
		$.post(href,{ajax:true},function(data){	
			if(data!='null') {
				$("#loader").fadeOut("normal",function(){
					$("#container").html(data);
					$(".content").hide();
					$(".content").fadeIn("normal");
					$("#footer").fadeIn("fast");
					historySet("work_history");
					lazy();
					zoom();
					caption();
					setTitle();
				});
			}
		});
		
	});
	return false;	
}

function pageload(hash) {
		if(hash) {
			if($.browser.msie) {
				hash = encodeURIComponent(hash);
			}
			ajaxize(hash);
		} else {
			ajaxize("web");
		}
	}
	
function historyInit() {
	$.historyInit(pageload);
	var hash = location.hash.replace(/\?.*$/, '');
	if(hash) {}
	else {
	//ajaxize("web");
	}	
}

function historySet(rel) {
		$("a[rel='"+rel+"']").click(function(event){
			event.stopPropagation(); 
			var href = $(this).attr("href");
			var hash_array = href.split("/")
			hash_array = hash_array.reverse();
			
			if(rel == "work_history") {
				if(hash_array[0] == "") var hash = hash_array[2]+"-"+hash_array[1];
				else var hash = hash_array[1]+"-"+hash_array[0];
			}
			else {
				if(hash_array[0] == "") var hash = hash_array[1];
				else var hash = hash_array[0];
			}
			
			hash = hash.replace(/^.*#/, '');
			$.historyLoad(hash);
			return false;
		});
}

function lazy() {
	 $("img.lazy").lazyload({ 
    	//placeholder : "../img/gray.gif",
    	effect : "fadeIn" 
		});
}


function caption() {
	$('.project_thumb').hover(function(){
					$(this).removeAttr("title");
					var top_position = 145;
					
					var clearQueue = true;						
					var gotoEnd= true;
					$(".cover_txt", this).stop(clearQueue , gotoEnd);
					
					$(".cover", this).stop().animate({top:top_position+"px"},{queue:false,duration:160});
			$(".cover_txt", this).fadeIn("slow");
														 
				}, function() {
					
					$(".cover", this).stop().animate({top:'115px'},{queue:false,duration:950});
						$(".cover_txt", this).fadeOut("fast");
						
						 
														 
					
				});
}


function scrollTotop() {
	$("#totop").click(function(){
		$.scrollTo({top:'0px', left:'0px'}, 800, {easing:'easeOutQuad'} );
		return false;
	});
}

function zoom() {
	$("a.zoom").click(function(){
		var img_src = $(this).attr("href");
		if($("img", this).hasClass("zoom")) {
			var semi_src = $("img", this).attr("original");
			$("img", this).attr("src",semi_src);
			$("img", this).removeClass("zoom");
		}
		else {
		$("img", this).attr("src",img_src);
		$("img", this).removeAttr("width");
		$("img", this).removeAttr("height");
		$("img", this).addClass("zoom");
		$("img", this).removeClass("lazy");
		}
	$.scrollTo($("img", this), 600, {offset: {top:-80, left:0} }, {easing:'easeOutQuad'} );
	return false;
	});
}

function setTitle() {
	var pageTitle = $("#pageTitle").html();
	document.title = pageTitle;
}


$(document).ready(function() {	
						  
	button("#menu-web","-16px","-32px");
	button("#menu-id","-16px","-32px");
	button("#menu-misc","-16px","-32px");
	button("#menu-contact","-16px","-32px");	
	$("#menu li a").removeAttr("title");	
	
	historyInit();
	historySet("history");
	historySet("work_history");
	lazy();
	zoom();
	caption();
	scrollTotop();
});