//JQUERY READY DOCUMENT
$(document).ready(function() 
{	
	$('#logForm').css('width' , '0px');
	//CAROUSSEL
	$("div.carousel").carousel( { 
		autoSlide: true,
		autoSlideInterval: 5000,
		loop: true,
		animSpeed: "slow",
		direction: "vertical"
	});
	
	$('#connectBt').toggle(
		function () {
			$("#loginerror").html('');
			$('#logForm').animate( {
				width: '450px'
			}, 600 );
		}
		, 
		function () {
			$('#logForm').animate( {
				width: '0px'
			}, 600 );
		}
	);
	//CACHE LES DIV DE PROPOSITION D'UN AUTRE JV
	$("div[id^=another_jv]").hide();
	
	//AFFICHAGE DU MENU DES COMPTES PERSO
	//$("div[id=profil_menu]").hide();
	
	//AFFICHAGE DU MENU DE FILTRE DES JNVP
	$("div[id=form_filter]").hide();
	
	//CACHE LA DIV DE COMMENTAIRE
	$('div.jnvpBlock div.commentZone').hide();
	
	//CACHE ET AFFICHE LA DIV DE MODIFICATION D'UN COMMENTAIRE
	$("div[id^=edit_one_com]").hide();
	
	//TRIER LES JNVP PAR DATE,VOTES,COMS HOME PAGE
	$("a[id=datesort]").click(function(){ 
		var url = document.location.href;
		var cat = getParamValue('cat',url);
		if(cat!="0"){window.location.replace("index.php?page=1&tri=date&cat="+cat);}
		else{window.location.replace("index.php?page=1&tri=date");}
	});
	$("a[id=votesort]").click(function(){ 
		var url = document.location.href;
		var cat = getParamValue('cat',url);
		if(cat!="0"){window.location.replace("index.php?page=1&tri=vote&cat="+cat);}
		else{window.location.replace("index.php?page=1&tri=vote");}
	});
	$("a[id=comsort]").click(function(){ 
		var url = document.location.href;
		var cat = getParamValue('cat',url);
		if(cat!="0"){window.location.replace("index.php?page=1&tri=com&cat="+cat);}
		else{window.location.replace("index.php?page=1&tri=com");}
	});
	//TRIER LES JNVP PAR CATEGORIES HOME PAGE
	$("a[id^=cat]").mouseover(function() {
		var id=$(this).attr('id').substring(3);
		document.getElementById('imgcat'+id).src = 'images/croix_bleu.png';
		document.getElementById('cat'+id).className = 'catchecked';
	});
	$("a[id^=cat]").mouseout(function() {
		var id=$(this).attr('id').substring(3);
		document.getElementById('imgcat'+id).src = 'images/croix_noire.png';
		document.getElementById('cat'+id).className = 'catunchecked';
	});
	$("a[id^=cat]").click(function () {
		window.location.replace("index.php?page=1&cat="+$(this).attr('id'));
	});

	//TRIER LES JNVP PAR DATE,VOTES,COMS JNVP CHERCHE SON JV PAGE
	$("a[id=jnvpdatesort]").click(function(){ 
		var url = document.location.href;
		var cat = getParamValue('cat',url);
		if(cat!="0"){window.location.replace("jnvp.php?page=1&tri=date&cat="+cat);}
		else{window.location.replace("jnvp.php?page=1&tri=date");}
	});
	$("a[id=jnvpvotesort]").click(function(){ 
		var url = document.location.href;
		var cat = getParamValue('cat',url);
		if(cat!="0"){window.location.replace("jnvp.php?page=1&tri=vote&cat="+cat);}
		else{window.location.replace("jnvp.php?page=1&tri=vote");}
	});
	$("a[id=jnvpcomsort]").click(function(){ 
		var url = document.location.href;
		var cat = getParamValue('cat',url);
		if(cat!="0"){window.location.replace("jnvp.php?page=1&tri=com&cat="+cat);}
		else{window.location.replace("jnvp.php?page=1&tri=com");}
	});
	//TRIER LES JNVP PAR CATEGORIES JNVP CHERCHE SON JV PAGE
	$("a[id^=jnvpcat]").click(function () {
		var id=$(this).attr('id').substring(4);
		window.location.replace("jnvp.php?page=1&cat="+id);
	});

	//TRIER LES JNVP PAR DATE,VOTES,COMS LIST JNVP PAGE
	$("a[id=listjnvpdatesort]").click(function(){ 
		var url = document.location.href;
		var cat = getParamValue('cat',url);
		var id = getParamValue('id',url);
		if(cat!="0"){window.location.replace("list_jnvp.php?page=1&id="+id+"&tri=date&cat="+cat);}
		else{window.location.replace("list_jnvp.php?page=1&id="+id+"&tri=date");}
	});
	$("a[id=listjnvpvotesort]").click(function(){ 
		var url = document.location.href;
		var cat = getParamValue('cat',url);
		var id = getParamValue('id',url);
		if(cat!="0"){window.location.replace("list_jnvp.php?page=1&id="+id+"&tri=vote&cat="+cat);}
		else{window.location.replace("list_jnvp.php?page=1&id="+id+"&tri=vote");}
	});
	$("a[id=listjnvpcomsort]").click(function(){ 
		var url = document.location.href;
		var cat = getParamValue('cat',url);
		var id = getParamValue('id',url);
		if(cat!="0"){window.location.replace("list_jnvp.php?page=1&id="+id+"&tri=com&cat="+cat);}
		else{window.location.replace("list_jnvp.php?page=1&id="+id+"&tri=com");}
	});
	//TRIER LES JNVP PAR CATEGORIES LIST JNVP PAGE
	$("a[id^=listjnvpcat]").click(function () {
		var id=$(this).attr('id').substring(8);
		var url = document.location.href;
		var nom = getParamValue('id',url);
		window.location.replace("list_jnvp.php?page=1&id="+nom+"&cat="+id);
	});
	
	//TRIER LES JNVP PAR DATE,VOTES,COMS FOUND JNVP PAGE
	$("a[id=foundjnvpdatesort]").click(function(){ 
		var url = document.location.href;
		var cat = getParamValue('cat',url);
		if(cat!="0"){window.location.replace("jnvp_found.php?page=1&tri=date&cat="+cat);}
		else{window.location.replace("jnvp_found.php?page=1&tri=date");}
	});
	$("a[id=foundjnvpvotesort]").click(function(){ 
		var url = document.location.href;
		var cat = getParamValue('cat',url);
		if(cat!="0"){window.location.replace("jnvp_found.php?page=1&tri=vote&cat="+cat);}
		else{window.location.replace("jnvp_found.php?page=1&tri=vote");}
	});
	$("a[id=foundjnvpcomsort]").click(function(){ 
		var url = document.location.href;
		var cat = getParamValue('cat',url);
		if(cat!="0"){window.location.replace("jnvp_found.php?page=1&tri=com&cat="+cat);}
		else{window.location.replace("jnvp_found.php?page=1&tri=com");}
	});
	//TRIER LES JNVP PAR CATEGORIES FOUND  JNVP PAGE
	$("a[id^=foundjnvpcat]").click(function () {
		var id=$(this).attr('id').substring(9);
		var url = document.location.href;
		window.location.replace("jnvp_found.php?page=1&cat="+id);
	});
	
	//TRIER LES ARTICLES PAR DATE,VOTES,COMS 
	$("a[id=articledatesort]").click(function(){ 
		window.location.replace("live_campaign.php?page=1&tri=date");
	});
	$("a[id=articlevotesort]").click(function(){ 
		window.location.replace("live_campaign.php?page=1&tri=vote");
	});
	$("a[id=articlecomsort]").click(function(){ 
		window.location.replace("live_campaign.php?page=1&tri=com");
	});
	
	//LIENS VERS LES ARTICLES
	$("div[id^=article_resume]").click(function () {
		var id=$(this).attr('id').substring(14);
		window.location.replace("articles.php?id="+id);
	});
	
	//LIEN POUR SOUMETTRE DES PHOTOS
	$("#send_photo").click(function(){
		window.location.replace("submit_photo.php");
	});
	
	//TRIER LES PHOTOS PAR DATE,VOTES
	$("a[id=photodatesort]").click(function(){ 
		window.location.replace("gallery.php?page=1&tri=date");
	});
	$("a[id=photovotesort]").click(function(){ 
		window.location.replace("gallery.php?page=1&tri=vote");
	});
	
	//TRIER LES EDITOS PAR DATE,VOTES,COMS 
	$("a[id=editodatesort]").click(function(){ 
		window.location.replace("edito.php?page=1&tri=date");
	});
	$("a[id=editovotesort]").click(function(){ 
		window.location.replace("edito.php?page=1&tri=vote");
	});
	$("a[id=editocomsort]").click(function(){ 
		window.location.replace("edito.php?page=1&tri=com");
	});
	
	//S'ABONNER A UN FIL DE COM JNVP
	$("p[id^=jnvp_abonn]").click(function () {
		var pos = getScrollPosition(0)[1];
		var id=$(this).attr('id').substring(10);
		$.post("lib/ajax/ajax.php",
		{'abonn': "1",
		 'jnvp':"1",
		 'id': id
		},
	   function success(data)
	   {
			$("p[id=jnvp_abonn"+id+"]").hide();
			$("p[id=jnvp_desabonn"+id+"]").show();
			window.scrollTo(0,pos);
	   });
	});
	
	//SE DESABONNER A UN FIL DE COM JNVP
	$("p[id^=jnvp_desabonn]").click(function () {
		var pos = getScrollPosition(0)[1];
		var id=$(this).attr('id').substring(13);
		$.post("lib/ajax/ajax.php",
		{'desabonn': "1",
		 'jnvp':"1",
		 'id': id
		},
	   function success(data)
	   {
			$("p[id=jnvp_abonn"+id+"]").show();
			$("p[id=jnvp_desabonn"+id+"]").hide();
			window.scrollTo(0,pos);
	   });
	});
	
	//S'ABONNER A UN FIL DE COM ARTICLE
	$("p[id^=article_abonn]").click(function () {
		var pos = getScrollPosition(0)[1];
		var id=$(this).attr('id').substring(13);
		$.post("lib/ajax/ajax.php",
		{'abonn': "1",
		 'article':"1",
		 'id': id
		},
	   function success(data)
	   {
			$("p[id=article_abonn"+id+"]").hide();
			$("p[id=article_desabonn"+id+"]").show();
			window.scrollTo(0,pos);
	   });
	});
	
	//SE DESABONNER A UN FIL DE COM ARTICLE
	$("p[id^=article_desabonn]").click(function () {
		var pos = getScrollPosition(0)[1];
		var id=$(this).attr('id').substring(16);
		$.post("lib/ajax/ajax.php",
		{'desabonn': "1",
		 'article':"1",
		 'id': id
		},
	   function success(data)
	   {
			$("p[id=article_abonn"+id+"]").show();
			$("p[id=article_desabonn"+id+"]").hide();
			window.scrollTo(0,pos);
	   });
	});
	
	//VOTER POUR UN EDITO
	/*$("a[id^=voter_edito]").click(function () {
		var pos = getScrollPosition(0)[1];
		var id=$(this).attr('id').substring(11);
		$.getJSON("lib/ajax/ajax.php?vote=1&edito=1&id="+id,
		function (json)
		{
			$("a[id=voter_edito"+id+"]").remove();
			$("span[id=jaime"+id+"]").show();
			data = eval(json);
			document.getElementById('nb_votes_edito'+id).firstChild.nodeValue = data[0].nb_vote;
			window.scrollTo(0,pos);
		});
	});*/
	
	//S'ABONNER A UN FIL DE COM EDITO
	$("p[id^=edito_abonn]").click(function () {
		var pos = getScrollPosition(0)[1];
		var id=$(this).attr('id').substring(11);
		$.post("lib/ajax/ajax.php",
		{'abonn': "1",
		 'edito':"1",
		 'id': id
		},
	   function success(data)
	   {
			$("p[id=edito_abonn"+id+"]").hide();
			$("p[id=edito_desabonn"+id+"]").show();
			window.scrollTo(0,pos);
	   });
	});
	
	//SE DESABONNER A UN FIL DE COM EDITO
	$("p[id^=edito_desabonn]").click(function () {
		var pos = getScrollPosition(0)[1];
		var id=$(this).attr('id').substring(14);
		$.post("lib/ajax/ajax.php",
		{'desabonn': "1",
		 'edito':"1",
		 'id': id
		},
	   function success(data)
	   {
			$("p[id=edito_abonn"+id+"]").show();
			$("p[id=edito_desabonn"+id+"]").hide();
			window.scrollTo(0,pos);
	   });
	});
	
	
	
	$('div.themaList').hide();
	$('#themalink').click(function() {
		$('#themalink').toggleClass('arrowFirst');
		$('#themalink').toggleClass('arrowActive');
		$('div.themaList').slideToggle();
		return false;
	});
	$('div.themaBox ul li a').click(function() {
		$('div.themaBox ul li a').removeClass('activeFilter');
		$(this).addClass('activeFilter');
		//$('#jnvpjvList h2 em').html($(this).attr('title'));
		return false;
	});
	$('div.themaList ul li a').click(function() {
		$('div.themaList ul li a').removeClass('activeThema');
		$(this).addClass('activeThema');
		//$('#jnvpjvList h2 span').html($(this).html());
		return false;
	});
	
	/* This code is executed after the DOM has been completely loaded */
	
	var totWidth=0;
	var positions = new Array();
	
	$('#slides .slide').each(function(i){
		
		/* Traverse through all the slides and store their accumulative widths in totWidth */
		
		positions[i]= totWidth;
		totWidth += $(this).width();
		
		/* The positions array contains each slide's commulutative offset from the left part of the container */
		
		if(!$(this).width())
		{
			alert("Please, fill in width & height for all your images!");
			return false;
		}
		
	});
	// Mini-galerie via jCarousel
	$(window).load( function() {
		var limiteclip = $('#carouselGalery li').width() ;
		
		$('#carouselGalery li').each( function() {
			var img = $(this).children('img');
			var imgbaselarge = img.width();
			var imgbasehaut = img.height();
			if ( imgbaselarge >= imgbasehaut ) {
				img.height(limiteclip);
				var newhaut = img.width();
				var marge = parseInt( (newhaut-limiteclip) /2 );
				img.css('margin-left', '-'+marge+'px');
			}
			if ( imgbaselarge < imgbasehaut  ) {
				img.width(limiteclip);
				var newlarge = img.height();
				var marge = parseInt( (newlarge-limiteclip) /2 );
				img.css('margin-top', '-'+marge+'px');
			}
		});
	});
	
	$("#carouselGalery").carousel( {
		autoSlide: true,
		autoSlideInterval: 3000,
		loop: true,
		animSpeed: "slow",
		buttonPrevHTML : null
	});
	
	//*CONTROLE DES LONGEURS DES SOUMISSIONS DES JNVP/JV*/
	$('.countChar').jqEasyCounter({
		'maxChars': 250,
		'maxCharsWarning': 240,
		'msgFontColor': '#fff',
		'msgFontFamily': 'Arial',
		'msgFontSize': '10px',
		'msgTextAlign': 'left',
		'msgWarningColor': '#F00',
		'msgAppendMethod': 'insertBefore'
	});
	//*CONTROLE DES LONGEURS DES SOUMISSIONS DES COMMENTAIRES*/
	$('.countCharCom').jqEasyCounter({
		'maxChars': 1023,
		'maxCharsWarning': 1000,
		'msgFontColor': '#fff',
		'msgFontFamily': 'Arial',
		'msgFontSize': '10px',
		'msgTextAlign': 'left',
		'msgWarningColor': '#F00',
		'msgAppendMethod': 'insertBefore'
	});
});

//*************************************************************************//
function getnowdate()
{
	var currentTime = new Date();
	var month = currentTime.getMonth() + 1;
	if (month < 10){month = "0" + month;}
	var day = currentTime.getDate();
	if (day < 10){day = "0" + day;}
	var year = currentTime.getFullYear();
	var hours = currentTime.getHours();
	var minutes = currentTime.getMinutes();
	if (minutes < 10){minutes = "0" + minutes;}
	var now = day + "/" + month + "/" + year + " \340 " + hours + "h" + minutes;
	return now;
}
function jumpto(id)
{
	if((obj = document.getElementById(id)) && obj != null)
	{
		var test=obj.offsetTop-150;
		setTimeout('window.scrollTo(0, '+test+')',1);
		//window.scrollTo(0, obj.offsetTop-150);
	}
}
function getScrollPosition()
{
	return Array((document.documentElement && document.documentElement.scrollLeft) || window.pageXOffset || self.pageXOffset || document.body.scrollLeft,(document.documentElement && document.documentElement.scrollTop) || window.pageYOffset || self.pageYOffset || document.body.scrollTop);
}

function open_another_jv(id)
{
	$("div[id=another_jv"+id+"]").slideToggle("fast");
	jumpto('one_jnvp'+id);
}
function close_another_jv(id)
{
	$("div[id=another_jv"+id+"]").slideToggle("fast");
	jumpto('one_jnvp'+id);
}
function toggle_filter_cat()
{
	$("div[id=form_filter]").slideToggle("fast");
	var src = (document.getElementById('filter_cat_fleche').src === "images/dbl_fleche_bas.png")
				? "images/dbl_fleche_droite.png" 
				: "images/dbl_fleche_bas.png";
	document.getElementById('filter_cat_fleche').src=src;
}
function toggle_menu_perso()
{
	$("div[id=stat]").slideToggle("fast");
	$("div[id=param]").slideToggle("fast");
	$("div[id=infos]").slideToggle("fast");
}
function toggle_menu_admin()
{
	$("div[id=profil_menu]").slideToggle("fast");
}
function open_comment_jnvp(id)
{
	$("div[id=comment_this_jnvp"+id+"]").slideToggle("fast");
	jumpto('comment_this_jnvp'+id);
}
function close_comment_jnvp(id)
{
	$("div[id=comment_this_jnvp"+id+"]").slideToggle("fast");
	jumpto('comment_this_jnvp'+id);
}
function go_compage_elab(id_destination,id_jnvp)
{
	$("div[id^=com_content]").hide();
	$("a[id^=paging"+id_jnvp+"]").removeClass('paging_jnvp_current');
	$("div[id=com_content"+id_destination+id_jnvp+"]").show();
	$("a[id=paging"+id_jnvp+id_destination+"]").addClass('paging_jnvp_current');
}
function getParamValue(param,url)
{
	var u = url == undefined ? document.location.href : url;
	var reg = new RegExp('(\\?|&|^)'+param+'=(.*?)(&|$)');
	if(u.match(reg))
	{
	matches = u.match(reg);
	return matches[2] != undefined ? decodeURIComponent(matches[2]).replace(/\+/g,' ') : '';
	}
	else
	{
		return 0;
	}
}

function edit_comment(id,id_jnvp)
{
	$("div[id=one_com"+id+"]").hide();
	$("div[id=edit_one_com"+id+"]").show();
	jumpto('one_jnvp'+id_jnvp);
}
function cancel_edit_comment(id,id_jnvp)
{
	$("div[id=one_com"+id+"]").show();
	$("div[id=edit_one_com"+id+"]").hide();
	jumpto('one_jnvp'+id_jnvp);
}
function effacerError()
{
    document.getElementById('loginerror').innerHTML = '';
}

