/**** NEMROD - jQuery JS ****/
var numberOfPics = 0;
var whichOneClicked = 0;
var nextOne = 0;
var prevOne = 0;


function displayNav(itemClickedId)
{
       alert(itemClickedId);     
}
function openDiv(what)
{
		var hache	=	what;
                var dataUrl	=	hache.split('/');
                var whichSection=       dataUrl[0].replace("#","");
		var whichOne    =	dataUrl[0].replace("#","#section_");
		var itemId	=	dataUrl[1];
		
                $(whichOne).slideDown("fast");
                $("#title_section_"+whichSection).addClass('section-cat-open');
                $("#"+dataUrl[1]).slideDown("fast");
		
                
                if ( $('.bg-actif').length > 0 )
		{
                    var itemOpenId	=	$('.bg-actif').attr('id').replace("item_", "");
                    $('.bg-actif').removeClass('bg-actif');
                    $("#close_"+itemOpenId).css('display','none');
                    $("#"+itemOpenId).slideUp();
                    document.location.hash.replace('/' + itemOpenId, '');
		}
		
                $("#item_"+itemId).addClass('bg-actif');
                
                if ( $("#"+itemId).html() == "" )
		{
                    $("#"+itemId).html('<img src="'+ajaURL+'images/ajax-loader-2.gif" class="ajax-loader" alt="Chargement en cours" />');
                    $("#"+itemId).addClass('opened');
                    $("#"+itemId).css('display','block');
                    
                   
                     
                    
                    
                    $.post(ajaURL+"single.php",
                        {action: 'getDetail', article_id: itemId},
                            function(data){
                                //document.location = siteUrl + "#post_"+itemId;
                                $("#"+itemId).html(data);
                                var destination = $("#item_"+itemId).offset().top;
                     $('html,body').animate({scrollTop: destination},10);
                            }
		    );
		}
                else
                {
                    if ( (itemId != "undefined") && (itemId != undefined) && (itemId != "") )
                    {
                                $("#"+itemId).addClass('opened');
                                $("#"+itemId).css('display','block');
                                document.location.hash = document.location.hash + '/' + itemId;
                                //document.location = siteUrl + "#post_"+itemId;
                    }
                    
                }
}


$(document).ready(function() {

	if ( document.location.hash != "" )
	{
		openDiv(document.location.hash);
	}
	
        $('.hrefNews').click(function(){
                window.document.location = $(this).attr('href');
                window.document.location.reload();
        });
	
	
	
	// ---> OUVERTURE DES CATEGORIES
	$(".section-title").click( function() {
		var whichOnePrevious	=	( $(".section-cat-open").length > 0 ) ? $(".section-cat-open").children("span").attr("id") : "" ;
		var whichOne			=	$(this).children("span").attr("id");
		
		// -> Si le cliqué est l'ancien actif, on ferme seulement
		if ( whichOnePrevious == whichOne )
		{
			$("#section_" + whichOnePrevious).slideUp();
			$(this).removeClass('section-cat-open');
                        document.location.hash.replace("#"+whichOne, '');
		}
		else
		{
			// -> Sinon, on ferme l'ouvert (si il yen a 1 )
			if ( whichOnePrevious != "" )
			{
				$("#section_" + whichOnePrevious).slideUp();
				$(".section-cat-open").removeClass('section-cat-open');
				document.location.hash.replace("#"+whichOnePrevious, '');
				
				//= document.location.hash + "#"+whichOne;
			}
			
			// -> et on ouvre le nouveau
				$(this).addClass('section-cat-open');
                                
                                var destination = $(this).offset().top;
                                //alert(destination);
                                $('html,body').animate({scrollTop: destination},1);
                                
                                $("#section_" + whichOne).slideDown("fast");
				document.location.hash = "#" + whichOne;
				
		}
		
		
			
	});
	
	$(".section-title-no-js").click( function() {
		var daLocation = $(this).attr('rel');
		document.location = daLocation;
		
	});
	$(".item-no-js").click( function() {
		var daLocation = $(this).children(".title").children("a").attr('rel');
		document.location = daLocation;
	});
	
	// ---> OUVERTURE DES POSTS
	$(".item").click( function() {
		
		var itemClickedId	=	$(this).attr('id').replace("item_", "");
		var destination         =       0; 
                // -> Si celui sur lequel on clique est déjà ouvert on le ferme
			if ( $("#"+itemClickedId).css('display') == 'block' )
			{
				$("#"+itemClickedId+" .postOpen").css('display', 'none');
                                $("#"+itemClickedId).slideUp();
				$(this).removeClass('bg-actif');
				$("#close_"+itemClickedId).css('display','none');
                                
                                var prevHash = document.location.hash;
                                document.location.hash =  prevHash.replace('/' + itemClickedId, '');
			}
			else
			{
				// -> Sinon, si il y en a un ouvert, on le ferme
				if ( $('.bg-actif').length > 0 )
				{
					
                                        var itemOpenId	=	$('.bg-actif').attr('id').replace("item_", "");
					$("#"+itemOpenId+" .postOpen").css('display', 'none');
                                        $('.bg-actif').removeClass('bg-actif');
					$("#close_"+itemOpenId).css('display','none');
					$("#"+itemOpenId).slideUp('700', function() {
                                                                var destination = $("#item_"+itemClickedId).offset().top;
                                                                $('html,body').animate({scrollTop: destination},1);
                                                });
                                        
                                        var prevHash = document.location.hash;
                                        location.hash =  prevHash.replace('/' + itemOpenId, '');
                                }
				
				
				$(this).addClass('bg-actif');
                                $("#"+itemClickedId+" .postOpen").css('display', 'block');
                                $("#close_"+itemClickedId).css('display','block');
				// -> on charge via AJAX les data dans le nouveau
                                
					//alert($(this).offsetTop());
                     if ( $("#"+itemClickedId).html() == "" )
					{
						$("#"+itemClickedId).html('<img src="'+ajaURL+'images/ajax-loader-2.gif" class="ajax-loader" alt="Chargement en cours" />');
						$("#"+itemClickedId).addClass('opened');
						$("#"+itemClickedId).css('display','block');
                                                
                                                var destination = $("#item_"+itemClickedId).offset().top;
                                                $('html,body').animate({scrollTop: destination},1);
		
						$.post(ajaURL+"single.php",
							{action: 'getDetail', article_id: itemClickedId},
							function(data){
								$("#"+itemClickedId).html(data);
                                numberOfPics = 0;
                                whichOneClicked = 0;
                                
							}
						);
					}
					else
					{
						// -> Si il a déjà été ouvert auparavant, on ne passe pas par AJAX
						$("#"+itemClickedId).addClass('opened');
                                                $("#"+itemClickedId).slideDown("fast");
                                                
					}
                                                
                                        location.hash = location.hash + '/' + itemClickedId;
			}
		
	});
	
	$(".item").mouseover( function() {
		if ( $(this).hasClass('bg-actif') == false )
		{
			var itemHoverId	=	$(this).attr('id').replace("item_", "prev_");
			if ( $("#"+itemHoverId).length > 0 )
			{
				$("#"+itemHoverId).css('display', 'block');
			}
			$(this).addClass('item_hover');
		}
	});
	$(".item").mouseout( function() {
		if ( $(this).hasClass('item_hover') )
		{
			$(this).removeClass('item_hover');
		}
		var itemHoverId	=	$(this).attr('id').replace("item_", "prev_");
		if ( $("#"+itemHoverId).length > 0 )
		{
			$("#"+itemHoverId).css('display', 'none');
		}
	});
        
	
	
	
});

function moveSlideNext(postId) {
   
       // -> On recupere le nombre d'image et le cliqué
              numberOfPics	=	$("#imageList-"+postId).children('.thumb').length;
              whichOneClicked	=	$("#imageList-"+postId).children('.activeTh').attr('id').replace('th_'+postId+'_','');
       // on cherche le suivant
              nextOne	= parseInt(whichOneClicked) + 1;
	
    
              //$('#jsDebug').html("#th_"+postId+'_'+whichOneClicked+'---<br />');
       // si le suivant est defini on l'affiche
       
              if (nextOne <= numberOfPics )
              {
                  $("#currentPics_"+postId).html(nextOne);
                  $("#th_"+postId+'_'+whichOneClicked).removeClass('activeTh');
                  $("#th_"+postId+'_'+nextOne).addClass('activeTh');
              }
              else
              {
                  $("#currentPics_"+postId).html('1');
                          // Sinon on revient au premier de la liste
                  $("#th_"+postId+'_'+whichOneClicked).removeClass('activeTh');
                  $('#th_'+postId+'_1').addClass('activeTh');
              }
    

}
function moveSlidePrev(postId) {

       // -> On recupere le nombre d'image et le cliqué
              numberOfPics	=	$("#imageList-"+postId).children('.thumb').length;
              whichOneClicked	=	$("#imageList-"+postId).children('.activeTh').attr('id').replace('th_'+postId+'_','');
   
       // on cherche le suivant
              prevOne	= parseInt(whichOneClicked) - 1;
	
       // si le suivant est defini on l'affiche
              if (prevOne > 0 )
              {
                     $("#currentPics_"+postId).html(prevOne);
                     $("#th_"+postId+'_'+whichOneClicked).removeClass('activeTh');
                     $("#th_"+postId+'_'+prevOne).addClass('activeTh');
              }
              else
              {
                     $("#currentPics_"+postId).html(numberOfPics);
                     // Sinon on revient au premier de la liste
                     $("#th_"+postId+'_'+whichOneClicked).removeClass('activeTh');
                     $("#th_"+postId+'_'+numberOfPics).addClass('activeTh');
              }

}


// -> Ouverture / fermeture div
function openClose(what)
{
	
	if ( $("#"+what).css('display') == 'none' )
	{
		$("#"+what).css('display', 'block');
	}
	else
	{
		$("#"+what).css('display','none');
	}
	
}

// -> Remplissage / vidage champs formulaire
function clearfield(field) {
	if ( $("."+field).attr('value') == field )
        {
                $("."+field).attr('value','');
        }
	
}


// -> insertion commentaire
function check(artId) {
	var insert=false;
	
	if ( $("#nom"+article_id).attr('value') == "" ) {
		alert ('Le champ [PSEUDO] est vide !');
	} else if ($("#msg"+article_id).attr('value') == "") {
		alert ('Le champ [MESSAGE] est vide !');
	} else insert=true
	
	
	if (insert) {
		var article_id 	= $(".article_id").attr('value');
		var nom        	= $("#nom"+artId).attr('value');
		var email      	= $("#email").attr('value');
		var site     	= $("#site").attr('value');
		var msg        	= $("#msg"+artId).attr('value');
		var zone_a_charger = "comment_insert"+article_id;
		//alert(article_id + '--' + nom + '--' + email + '--' + msg + '--' + site);
		$.post(ajaURL+"comment_insert.php",
			{email: email, site: site, msg: msg, article_id: article_id, nom: nom},
			function(data){
				$("#"+zone_a_charger).html(data);
			}
		);
	}
}

function goDirect(item) {
       window.document.location = item.attr('href');
       window.document.location.reload();
}


jQuery.fn.anchorAnimate = function(settings) {

 	settings = jQuery.extend({
		speed : 1100
	}, settings);	
	
	return this.each(function(){
		var caller = this
		$(caller).click(function (event) {	
			event.preventDefault()
			var locationHref = window.location.href
			var elementClick = $(caller).attr("href")
			
			var destination = $(elementClick).offset().top;
			$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination}, settings.speed, function() {
				window.location.hash = elementClick
			});
		  	return false;
		})
	})
}

function backNav_js(newURL)
{
	window.location.href = newURL;
}