$(document).ready(function(){
	initWidth();
	initScroll();
	initAnchor();
	initMenu();
	initLastPost();
	initPng();
	initCenter();	
	initPostComments();
	initNavigation();
	initNavKeyboard();
});


function initWidth(){
	var newWidth = 0;
	$("#content > div").each(function(){
		newWidth += $(this).width()+15;
	});
	$("#content").width(newWidth);
}	


function initScroll(){						   
	//SCROLL HORIZONTAL
	var deplace=$("#wrapper").scrollLeft();
	$(document).mousewheel(function(event, delta) {
	   var deplace=0;
		delta/=-1;
		deplace=delta*100;
		var curScroll = $("#wrapper").scrollLeft();
		var newScroll = curScroll+deplace;
		if(newScroll<0){
			newScroll=0;
		} else if(newScroll>$("#content").width()){
		  newScroll=$("#content").width();
		}
		$("#wrapper").scrollLeft(newScroll);
	});
}

function initAnchor(){
	var anchorUrl = document.location.hash.toString();
	if(anchorUrl!= ""){
		$("#wrapper").scrollTo(anchorUrl, 500,{axis:'x', onAfter:replaceAnchor});
	}else {
		anchorUrl = "#lastPosts";
		$("#wrapper").scrollTo(anchorUrl, 500,{axis:'x', onAfter:replaceAnchor});
	}
}

function initMenu(){

		$("ul#menu li a span, #header h1 a span").css("opacity",0);
		// on mouse over
		$("ul#menu li a").hover(
			function () {
				// animate opacity to full
				$("span", this).stop().animate({
					opacity: 1
				}, "fast");
				return false;
			},
			// on mouse out
			function () {
				// animate opacity to nill
				$("span", this).stop().animate({
					opacity: 0
				}, "fast");
				return false;
		});
		// on mouse over
		$("#header h1 a").hover(
			function () {
				// animate opacity to full
				$("span", this).stop().animate({
					opacity: 1
				}, "slow");
				return false;
			},
			// on mouse out
			function () {
				// animate opacity to nill
				$("span", this).stop().animate({
					opacity: 0
				}, "slow");
				return false;
		});
		
		
		
	$("#header a").click(function(event){
      if($(this).attr("href") != "" && $(this).attr("href") != "#"){
			  event.preventDefault();
			  $("#wrapper").scrollTo($(this).attr("href"), 500,{axis:'x', onAfter:replaceAnchor});
			}
			return false;
	});
			
	$("#lastPosts ul li").click(function(event){
	  event.preventDefault();
	  $("#wrapper").scrollTo($("a", this).attr("href"), 500,{axis:'x', onAfter:replaceAnchor});
   	return false;
	});
		
				
}

function replaceAnchor(hash){
	document.location.replace(hash);
	initNavigation();
}

function initLastPost(){
	$('#lastPosts ul').kwicks({  
		min : 45,
		spacing:5,
		isVertical : true  
	});	  
}

function initPng(){
	DD_belatedPNG.fix('.png_bg');
}

function initCenter(){
	$("#liens li").each(function(){
		$("p.image", this).height($("p", this).eq(1).height());
		$("p.image a", this).center({
			horizontal : false
		});
	});
}


function initPostComments(){
	$(".commentForm").hide('slide', {direction: 'left'}, 0);
	$(".addComment a").click(function(event){
		event.preventDefault();
		$(this).parent().parent(".postComments").next(".commentForm").show('slide', {direction: 'left'}, 500);
		$(this).hide('slide', {direction: 'right'}, 500);
      return false;
	});
}


function initNavigation(){
	var anchorUrl = document.location.hash.toString();
	var prevAnchor = $(anchorUrl).prev("div").attr("id");
	if(prevAnchor != undefined){
		$("#prev a").attr("href", "#"+prevAnchor).show();
	}else {
		$("#prev a").attr("href", "#").hide();
	}
	var nextAnchor = $(anchorUrl).next("div").attr("id");
	if(nextAnchor != undefined){
		$("#next a").attr("href", "#"+nextAnchor).show();
	}else {
		$("#next a").attr("href", "#").hide();
	}
}

function initNavKeyboard(){
 $(document).keydown(function(event){
    if ((event.keyCode == 39) || (event.keyCode == 40)) {
       $('#next a').click();
       return false;
    } else if ((event.keyCode == 37) || (event.keyCode == 38)) {
       $('#prev a').click();
       return false;
    }
 });
}