/*Global pour l'ajax*/
var requesting = false;
	
$(document).ready(function(){ 
	initPostComments();
	initScroll();
	initAnchor();
	initMenu();
	initLastPost();
	initCenter();	
	initNavKeyboard();   
	if($.browser.msie && $.browser.version < 7){
	   DD_belatedPNG.fix('.png_bg');
	}                 
});
$(window).load(function(){
	initWidth();
});

      


function initPostComments(){
	$(".commentForm").hide();
	$(".addComment a").live('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 initWidth(){
	var newWidth = 0;
	$("#content > div").each(function(){
		//largeur
		var w = $(this).width();
		
		//padding left
		var pL = 0;
		if(parseInt($(this).css("paddingLeft"))){
			pL = parseInt($(this).css("paddingLeft"));
		}        
		     
		//padding right
		var pR = 0;
		if(parseInt($(this).css("paddingRight"))){
			pR = parseInt($(this).css("paddingRight"));
		}
		     
		//margin left
		var mL = 0;
		if(parseInt($(this).css("marginLeft"))){
			mL = parseInt($(this).css("marginLeft"));
		}
		     
		//margin right
		var mR = 0;
		if(parseInt($(this).css("marginRight"))){
			mR = parseInt($(this).css("marginRight"));
		}
		     
		//border left
		var bL = 0;
		if(parseInt($(this).css("borderLeftWidth"))){
			bL = parseInt($(this).css("borderLeftWidth"));
		}
		     
		//border right
		var bR = 0;
		if(parseInt($(this).css("borderLeftWidth"))){
			bR = parseInt($(this).css("borderLeftWidth"));
		}
		
		newWidth += (w+pL+pR+mL+mR+bL+bR);
	});    
	$("#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);
	    $("#content > div").each(function(){
	    	var offset = $(this).offset();
	    	if(offset.left > -$(this).width()){
		    	if(document.location.hash != "#"+$(this).attr("id")){
		    		var tmpId = $(this).attr("id");
		    		var date = new Date();
		    		$(this).attr("id", "tmpId"+date.getTime()); 
					document.location.replace("#"+tmpId);
		    		$(this).attr("id", tmpId);
					initNavigation();       
				}
	    		return false;
			}
	    });
		if($("#wrapper").scrollLeft() + parseInt($(window).width()) == $("#content").width()){
			if(!requesting){ 
				loadPost();
			}			
		}		
	});
}

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

function initMenu(){   
	$("#menu span, #header span, #navigation span, #loading").css("opacity",0);
	// on mouse over
	$("#header a").hover(
		function () {
			// animate opacity to full
			$("span", this).stop().animate({
				opacity: 1
			}, 500);
			return false;
		},
		// on mouse out
		function () {
			// animate opacity to null
			$("span", this).stop().animate({
				opacity: 0
			}, 500);
			return false;
	});
	
	$("#header .help a").fancybox({
		'overlayShow'	:	true,
		'margin' : 0,
		'padding' : 0,
		'overlayOpacity' : 0.9,
		'overlayColor' : '#333',
		'onComplete' : startFancy,
		'onClosed' : stopFancy
	});
		
		
	$("#header h1 a, #menu li:not(.help) 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;
	});
			  	
	$("#header #navigation a").click(function(event){
		event.preventDefault();
      	if($(this).attr("href") != ""){
      		if($(this).attr("href") != "#"){      
			  	var nextDiv = $(this).attr("href");
			  	var offset = $(nextDiv).position();
			  	if(Math.abs($("#wrapper").scrollLeft() - offset.left) > $(window).width() && $("#wrapper").scrollLeft() - $("#content").width() + $(window).width() != 0 && $(this).parent("li").attr("id") != "prev"){
				  	newScroll =  $(window).width()/2 + $("#wrapper").scrollLeft();
				  	$("#wrapper").scrollTo({left:newScroll}, 400, {axis:'x', onAfter:initNavigation});
				} else {                                        
					$("#wrapper").scrollTo(nextDiv, 500,{axis:'x', onAfter:replaceAnchor});
				}
			} else if($("#wrapper").scrollLeft() - $("#content").width() + $(window).width() != 0){
				newScroll =  $(window).width()/2 + $("#wrapper").scrollLeft();
			  	$("#wrapper").scrollTo({left:newScroll}, 400, {axis:'x', onAfter:initNavigation});                        
			} 
			if($("#wrapper").scrollLeft() - $("#content").width() + $(window).width() == 0){
				loadPost();
			} 
		}
		return false;
	});
			
			
			
	$("#lastPosts ul li").click(function(event){ 
		event.preventDefault();
		$("#wrapper").scrollTo($("a", this).attr("href"), 500,{axis:'x', onAfter:replaceAnchor});
   		return false;
	});
		
				
}


function startFancy(){  
	$("embed, object").css("visibility", "hidden");
}
function stopFancy(){ 
	$("embed, object").css("visibility", "visible");
}

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

function initLastPost(){ 
	$('#lastPosts ul').kwicks({  
		min : 45,
		spacing:5,
		isVertical : true  
	});	  
}
function initCenter(){
	$("#liens li").each(function(){
		$("p.image", this).height($("p", this).eq(1).height());
		$("p.image a", this).center({
			horizontal : 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", "#").show();	                                  	
	}
	if($("#wrapper").scrollLeft() - $("#content").width() + $(window).width() == 0){             
		$("#next a").hide();
		loadPost();
	}
}

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;
    }
 });
}






function loadPost(){  

	if(!requesting){  
			
		requesting = true;

		var _class = $("#content > div").last().attr("class");
		var temp = new Array();
		temp = _class.split(' ');
		temp = temp[0].split('-');
		_id = temp[1];	
		$("#loading").fadeTo(500, 1);    
		$.ajax({
			url:"wp-admin/admin-ajax.php",
			type:"POST",
			data:"action=my_special_action&postId=" + _id,	
			success:function(results){   
				if(results != 0 && results != -1){
					//on peut refaire une requête
					requesting = false;
					//on ajoute les résultats à la suite
					$("#content").append(results);          
					//Le div qu'on vient de placer
					var lastDiv = $("#content > div").last(); 
					//on cache le formulaire
					lastDiv.children(".commentForm").hide();  
					//mise à jour de la largeur
					var prevWidth = $("#content").width();
					initWidth();
					//On met à jour l'url   
		    		var tmpId = lastDiv.attr("id");
		    		var date = new Date();
		    		lastDiv.attr("id", "tmpId"+date.getTime()); 
					document.location.replace("#"+tmpId);
		    		lastDiv.attr("id", tmpId); 
					//on scrolle jusqu'au nouveau post
					var newScroll = prevWidth - $(window).width() + 320;
					$("#wrapper").delay(100).scrollTo(newScroll, 500, {axis:'x', onAfter:initNavigation});
				}
				$("#loading").fadeTo(500, 0);
			}
		});
	}
}