// VERSION: 1.0 
/*
 * Made by Matt Thiévent for eliom.eu
 * 
/*
Description:


Usage:

*/
var currentPage			= null;
var posXactuel 			= 0;
var precedentHash		= null;
var mobilebugtxton		= false;
var faq_actuel			= 1;
var faq_nombre			= 13;
var faq_pos				= 0;
var faq_width			= 600;
var oneFaqActive		= true;
var lastPos 			= 0;
var newPos 				= 0;
var desactiveOutconnect	= false;

var mouseMenu			= true;

//history
var namePage = ["null","accueil","projet","telecharger","annonceurspro","contact","aide","apropos"];

var locationPage = {
        put: function(hash, win) {
            (win || window).location.hash = encodeURIComponent(hash);
        },
        get: function(win) {
            var hash = ((win || window).location.hash).replace(/^#/, '');
            return $.browser.fx ? hash : decodeURIComponent(hash);
        }
};
	

//CHARGEMENT AUTOMATIQUE 
$(document).ready(function()
{	 
	//initialisation
	$('#apropos_cache').fadeOut(0);
	checkIfConnected();
	setCurrentPage(1);
	
/*	var diff = (900-document.body.clientHeight);
	var coeff = 10;
	
	$('#limitscroll').css('height',300+"px");
	$('#staticbuttons').draggable({
		axis: 'y',
		containment: '#limitscroll',
		drag: function(event, ui) {
			newPos = parseInt($('#staticbuttons').css('top').replace('px',''));
			var posheader = parseInt($('#header').css('margin-top').replace('px',''));
			if(newPos > lastPos){
				if((posheader-coeff)<-diff){
					$('#header').css('margin-top',-diff+'px');				}
				else
					$('#header').css('margin-top',(posheader-coeff)+'px');	
			}
			else if(newPos < lastPos){
				if((posheader+coeff)>0)
					$('#header').css('margin-top','0px');
				else
					$('#header').css('margin-top',(posheader+coeff)+'px');			
			}
			lastPos = newPos;
			
		},
		stop: function(event, ui) {}

	});	*/
		
		
	//GESTION DE L'HISTORIQUE
	//on va directement a la bonne page selon le hash
	if(locationPage.get()!=""){
		var namelocation = locationPage.get();
		if(namelocation.indexOf("openprofil") != -1){
			namelocation = namelocation.replace("openprofil","");
			showDirectProfilBloc();
		}
		var recupPos	= getPosition(namePage,namelocation);
		goto_page(recupPos,0,false);
		setCurrentPage(recupPos);
	}
	
	//checker les retour arrière ou retour avant du navigateur
	setInterval("checkChangeUrl()",500);
	//FIN DE LA GESTION DE L'HISTORIQUE
	
	authorizeScroll();
	displayTweet();
	
	//ACTION ZONE CONNECTION
	$('#icon-header').rotate({maxAngle:0,minAngle:-90,
	bind:
	[
	{"mouseover":function(){
		if(mouseMenu){
			if(document.cookie.indexOf('CONNECTED') != -1)
				showProfilBloc();
			else
				showConnectBloc();
		}
	}}
	]
	});
	
	$('#header').click(function() {	
		if(mouseMenu && !desactiveOutconnect){
			if(document.cookie.indexOf('CONNECTED') != -1)
				hideProfilBloc();
			else
				hideConnectBloc();
		}
	});
	
			
	$('#main').click(function() {
		if(mouseMenu && !desactiveOutconnect){
			if(document.cookie.indexOf('CONNECTED') != -1)
				hideProfilBloc();
			else
				hideConnectBloc();
		}
	});
	
	$('#btnconnect2').mouseover(function() {
		if(mouseMenu){
			if(document.cookie.indexOf('CONNECTED') != -1)
				showProfilBloc();
			else
				showConnectBloc();
		}
	});
	
	
	
	$('#bloc-connect').mouseover(function() {
		desactiveOutconnect			= false;
	});
	
	$('#btnconnect2, #createaccount').click(function() {
		desactiveOutconnect			= true;
		if($('#bloc-connect').css('height') != "0px"){
			if(document.cookie.indexOf('CONNECTED') != -1)
				hideProfilBloc();
			else
			
				hideConnectBloc();
			
		}
		else{
			if(document.cookie.indexOf('CONNECTED') != -1)
				showProfilBloc();
			else
				showConnectBloc();
		}
	});
	
	
	$('#btninscription').click(function(){
			$('#verticalconnect').stop().animate({top: '2px'}, 200);		
			$('#cache-pasencoreinscrit').stop().fadeOut(0);		
			$('#cache-pasencoreinscrit').show();	
			$('#cache-pasencoreinscrit').stop().fadeIn(1000);
			$('#bloc-connexInscrip').css('cursor','pointer');
			$('#connect-fleche').show();
	});
		
	$('#bloc-connexInscrip').click(function(){
			if($('#verticalconnect').css('top') == "2px"){
				$('#verticalconnect').stop().animate({top: '-220px'}, 200);		
				$('#cache-pasencoreinscrit').stop().fadeOut(1000, function(){$('#cache-pasencoreinscrit').hide();});
				$('#bloc-connexInscrip').css('cursor','normal');
				$('#connect-fleche').hide();
			}
	});
	
	//FIN DE ACTION ZONE CONNECTION
	
	$('#drag').draggable({
		axis: 'x',
		start: function(event, ui) {
			posXactuel = parseInt($('#drag').css('left').replace("px",""));
			$('#drag').stop();
			$('#drag').css('cursor','pointer');
			$('#keyboard').stop().animate({opacity:0},100);
		},
		stop: function(event, ui) {dragRepose();}

	});
	
	$("#flashplayer").mouseover(function(){
		$('#drag').draggable( "disable" ); 
	});
	$("#flashplayer").mouseout(function(){
		$('#drag').draggable( "enable" ); 
	});
	
	$(document).keydown(function(event) {
		if (event.keyCode == '37') {
			if(currentPage>1){
				$('#keyboard').animate({opacity:0},0);
				setCurrentPage(currentPage-1);
				goto_page(currentPage,2000,true);
			}
		}
		else if (event.keyCode == '39') {
			if(currentPage<7){
				$('#keyboard').animate({opacity:0},0);
				setCurrentPage(currentPage+1);
				goto_page(currentPage,2000,true);
			}
		}
	});

	

	
	$('#bloc-logo').click(function() {
	});
	
	
	//ACTION MENU PRINCIPAL
	
	//clique sur le menu
	$('.menuaction').click(function() {		   
		setCurrentPage(parseInt($(this).attr('id').replace('goPage','')));
		goto_page(currentPage,2000,false);
	});
	
	//rollover sur le menu
	$('.menuaction').mouseover(function() {		   
	  $(this).attr('src', $(this).attr('src').replace(/_doff/,'_on'));
	});
	
	//rollout sur le menu
	$('.menuaction').mouseout(function() {
		var self = this;
	  //on ajoute un interval
	   var timer = window.setInterval(function(){ $(self).attr('src', $(self).attr('src').replace(/_on/,'_doff')); window.clearInterval(timer) }, 150);
	});
	//FIN ACTION MENU PRINCIPAL
	
	
	//ACTION FAQ
	$('#faqnnaviugerplus').click(function() {
		if(faq_actuel<faq_nombre){
			faq_pos -= faq_width;
			$('#faqslide').stop().animate({left: faq_pos+"px"}, 1000,"easeInOutQuint");	
			faq_actuel++;
			
			var faq_actuel_txt = faq_actuel;
			if(faq_actuel_txt < 10){
				faq_actuel_txt = "0"+faq_actuel_txt;	
			}
			($('#faqpageactuel').text(faq_actuel_txt));
		}

	});
	
	$('#faqnnaviugermoins').click(function() {
		if(faq_actuel>1){
			faq_pos += faq_width;
			$('#faqslide').stop().animate({left: faq_pos+"px"}, 1000,"easeInOutQuint");	
			faq_actuel--;
			
			var faq_actuel_txt = faq_actuel;
			if(faq_actuel_txt < 10){
				faq_actuel_txt = "0"+faq_actuel_txt;	
			}
			($('#faqpageactuel').text(faq_actuel_txt));
		}

	});	
	
	//FIN ACTION FAQ
	
	
	//ACTION PAGE LE PROJET
	$('#btn-projetplus').click(function() {
		$('#animtextprojet').stop().animate({top: '-386px'}, 1000,"easeInOutBack", function(){
			var linkretour			= $('#btn-projetretour >img').attr('src');
			var linkretour			= linkretour.replace('_off','_on');
			$('#btn-projetretour >img').attr('src', linkretour);
			$('#btn-projetplus').css('display','none');

			$('#btn-projetretour').css('display','block');
		});
		
	});
	
	
	$('#btn-projetretour').click(function() {
		$('#animtextprojet').stop().animate({top: '0px'}, 1000,"easeInOutBack", function(){
			var linkretour			= $('#btn-projetplus >img').attr('src');
			var linkretour			= linkretour.replace('_off','_on');
			$('#btn-projetplus >img').attr('src', linkretour);
			$('#btn-projetretour').css('display','none');

			$('#btn-projetplus').css('display','block');
		});
		
	});
	
	//ACTION SUR LE MOBILE ANDROID
	$('#diapo').slideshow({timeout:5500,fadetime:2000});
	$('#htc_eliom').mouseover(function() {
		if(!mobilebugtxton){
			if($('#animhtc').css('top') == "-309px"){
				$('#animhtc').stop().animate({top: '0px'}, 700);
			}
			else if($('#animhtc').css('top') == "0px"){
				$('#animhtc').stop().animate({top: '-309px'}, 700);
			}
		}
	});	
		//gestion du bug de survol
	$('#animhtctxt').mouseover(function() {
		mobilebugtxton = true;
	});
	$('#animhtc').mouseleave(function() {
		mobilebugtxton = false;				   
	});
	
	//FIN ACTION SUR LE MOBILE ANDROID
	//FIN ACTION PAGE LE PROJET
	
	
	
	//ACTION PAGE A PROPOS
	$('#btn-mentions').click(function() {
		$.ajax({
  			url: "include-mentions.php",
  			success: function(html){
				resetAllBtnAide("#btn-mentions-img");
				$('#aproposdynamique').jScrollPaneRemove();
				$('#aproposdynamique').css('padding-right','12px');
				$('#apropos_cache').fadeIn(800, function(){
					$("#aproposdynamique").html(html);
					$('#apropos_cache').fadeOut(1500, function(){$('#aproposdynamique').css('padding-right','12px');$('#aproposdynamique').jScrollPane();
				});

				})
  			}
		});
	});	
	$('#btn-charte').click(function() {
		$.ajax({
  			url: "include-charte.php",
  			success: function(html){
				resetAllBtnAide("#btn-charte-img");
				$('#aproposdynamique').jScrollPaneRemove();
				$('#aproposdynamique').css('padding-right','12px');
				$('#apropos_cache').fadeIn(800, function(){
					$("#aproposdynamique").html(html);
					$('#apropos_cache').fadeOut(1500, function(){$('#aproposdynamique').css('padding-right','10px');$('#aproposdynamique').jScrollPane();
				});
				})
  			}
		});
	});	
	$('#btn-conditions').click(function() {
		$.ajax({
  			url: "include-conditions.php",
  			success: function(html){
				resetAllBtnAide("#btn-conditions-img");
				$('#aproposdynamique').jScrollPaneRemove();
				$('#aproposdynamique').css('padding-right','12px');
				$('#apropos_cache').fadeIn(800, function(){
					$("#aproposdynamique").html(html);
					$('#apropos_cache').fadeOut(1500, function(){$('#aproposdynamique').css('padding-right','10px');$('#aproposdynamique').jScrollPane();
				});
				})
  			}
		});
	});	
	$('#btn-credits').click(function() {
		$.ajax({
  			url: "include-credits.php",
  			success: function(html){
				resetAllBtnAide("#btn-credits-img");
				$('#aproposdynamique').jScrollPaneRemove();
				$('#apropos_cache').fadeIn(800, function(){
					$("#aproposdynamique").html(html);
					$('#apropos_cache').fadeOut(1500, function(){$('#aproposdynamique').css('padding-right','12px');$('#aproposdynamique').jScrollPane();});
				})
  			}
		});
	});	
	//FIN ACTION PAGE A PROPOS
	
});

function resetAllBtnAide(btntoOn){
	$('#btn-credits-img').attr('src', $('#btn-credits-img').attr('src').replace('_click','_off'));
	$('#btn-conditions-img').attr('src', $('#btn-conditions-img').attr('src').replace('_click','_off'));
	$('#btn-charte-img').attr('src', $('#btn-charte-img').attr('src').replace('_click','_off'));
	$('#btn-mentions-img').attr('src', $('#btn-mentions-img').attr('src').replace('_click','_off'));
	//btn on
	$(btntoOn+"").attr('src', $(btntoOn+"").attr('src').replace('_off','_click'));
	$(btntoOn+"").attr('src', $(btntoOn+"").attr('src').replace('_on','_click'));
	
	
}
function testMac(){
	alert("ok");	
}

function showProfilBloc(){
	$('#bloc-connect').stop().animate({height: '566px'}, 1000,"easeInOutQuint");
	$("#icon-header").rotateAnimation(-90);	
}
function showDirectProfilBloc(){
	$('#bloc-connect').stop().animate({height: '566px'}, 0);
	$("#icon-header").rotateAnimation(-90);	
}

function hideProfilBloc(){
	$('#bloc-connect').stop().animate({height: '0px'}, 1000	);
	$("#icon-header").rotateAnimation(0);
}

function showConnectBloc(){
	$('#bloc-connect').stop().animate({height: '238px'}, 1000,"easeInOutQuint");
	$('#cache-bloc-connexInscrip').stop().animate({opacity: 0}, 1000, function(){$('#cache-bloc-connexInscrip').hide();});
	$("#icon-header").rotateAnimation(-90);	
	$("#state-pasencoreinscrit").show();
	$("#state-inscrit").hide();
	$("#inscriptionerreur").html("");
}

function hideConnectBloc(){
	$('#bloc-connect').stop().animate({height: '0px'}, 1000,function(){
		//on remet sur la connecion
		if($('#verticalconnect').css('top') == "2px"){
			$('#verticalconnect').css('top','-220px');		
			$('#pasencoreinscrit').fadeIn(0);
			$('#cache-pasencoreinscrit').stop().fadeOut(0);
			$('#cache-pasencoreinscrit').hide();
			$('#bloc-connexInscrip').css('cursor','normal');
			$('#connect-fleche').hide();
		}																 
	});
	$('#cache-bloc-connexInscrip').show();
	$('#cache-bloc-connexInscrip').stop().animate({opacity: 1}, 1000);
	$("#icon-header").rotateAnimation(0);

}



function setCurrentPage(pageIndex){
	if(currentPage!=null)
		$("#goPage"+currentPage).attr('src', $("#goPage"+currentPage).attr('src').replace('.','_doff.'));
	$("#goPage"+pageIndex).attr('src', $("#goPage"+pageIndex).attr('src').replace(/_on/,''));
	$("#goPage"+pageIndex).attr('src', $("#goPage"+pageIndex).attr('src').replace(/_doff/,''));
	
	currentPage			= pageIndex;
	
}
function goto_page(pageIndex, speed, iskeyboard){
	if(!oneFaqActive){	
			displayOnlyOneFAQ();
	}	

	//change url
    locationPage.put(namePage[pageIndex]);
	precedentHash = locationPage.get(); 
	var pageSize		= 1012;
	var pagePos			= - ((pageSize * pageIndex) - pageSize);
	
	if(!iskeyboard){
		$('#keyboard').stop().animate({opacity:0},100).delay(speed).animate({opacity:1},200);
	}
	
	
	$('#drag').stop().animate({left: pagePos}, speed,"easeOutQuad",function(){
		if(pageIndex == 6){
			displayAllFAQ();
		} 																		
	});
	
}
/**
 *
 */
function dragRepose(){
	$('#drag').css('cursor','default');
	 var posX = parseInt($('#drag').css('left').replace("px",""));
	 
	 var diff = posXactuel-posX;
	 
	 
	 
	 if(diff >= 100  && currentPage < 7){
			setCurrentPage(currentPage+1);
		 	goto_page(currentPage,1000,false);
	 }
	 else if(diff <= -100 && currentPage > 1){
			setCurrentPage(currentPage-1);
		 	goto_page(currentPage,1000,false);
	 }
	 else{
		 	$('#drag').stop().animate({left: posXactuel}, 1000);
	 }
	 
}

function displayTweet(){
	
	
	$.jTwitter('projet_eliom', 15, function(data){
			$('#tweets').empty();
			$.each(data, function(i, post){
				$('#tweets').append(
						'<div style="padding-bottom:25px;">'
						+ 	'<div class="tweet">'
						+ 		createTweetUrl(post.text)
						+	'</div>'
						+	'<div class="date">'
						+ 		post.created_at.replace('+0000 2010','')
						+	'</div>'
						+'</div>'
				); 
				$("#blockTwitter").jScrollPane({scrollbarWidth:10, scrollbarMargin:5});
			$('#cacheblockTwitter').fadeOut(5000);
			});
		});	
}

function createTweetUrl(input)
{
    return input
    .replace(/(ftp|http|https|file):\/\/[\S]+(\b|$)/gim,
'<a href="$&" target="_blank">$&</a>')
    .replace(/([^\/])(www[\S]+(\b|$))/gim,
'$1<a href="http://$2" target="_blank">$2</a>');
} 

/**
 * Détermine si l'utilisateur possède un navigateur assez grand
 * permettant de lui supprimer le scroll vertical
 */
function authorizeScroll(){
	var heightPage 		=  document.body.clientHeight;
	if(heightPage > 858){
		$("body").css("overflow","hidden");
	}
}

/**
 * Affiche toutes les faq uniquement quand on en a besoin
 * allege le javascript pour firefox
 */
function displayOnlyOneFAQ(){
	$('.faq').css('display','none');
	$('#faq'+faq_actuel).css('display','block');
	oneFaqActive = true;
}
/**
 * Affiche toutes les faq uniquement quand on en a besoin
 * allege le javascript pour firefox
 */
function displayAllFAQ(){
	$('.faq').css('display','block');
	$('#faq'+faq_actuel).css('display','block');
	$('#faqslide').css('left',(faq_actuel-1)*(-faq_width));
	oneFaqActive = false;
	
}
/**
 * Détermine si l'utilisateur possède un navigateur assez grand
 * permettant de lui supprimer le scroll vertical
 */
function resizeScroll(){
	var heightPage 		=  document.documentElement.clientHeight;
	var widthPage		=  document.documentElement.clientWidth;
	//if(heightPage > 622){
	$("#container").css("height",(heightPage-236)+"px");
	$c = $('#container');
	$(".jScrollPaneContainer").css("height",(heightPage-236)+"px");
	$("#container").jScrollPane({scrollbarWidth:8});
					
				
}
/**
 * Retourne la position dans un tableau
 *
 */
function getPosition(arrayName,arrayItem)
{
    for(var i=0;i<arrayName.length;i++){ 
       if(arrayName[i]==arrayItem)
            return i;
    }
}

/**
 * Permet de checker les retour arrière ou retour avant du navigateur
 *
 */
function checkChangeUrl(){
     var currhash = locationPage.get();
     if(currhash != precedentHash) {
		if(currhash == ""){
			currhash = "accueil";
			locationPage.put("accueil");
		}
     	precedentHash = currhash;
		//on va directement a la bonne page selon le hash
		var recupPos	= getPosition(namePage,locationPage.get());
		goto_page(recupPos,2000,false);
		setCurrentPage(recupPos);
     }
        
}


function changeInputTextToPassword(inputid){
   var $jQ = jQuery; //conflit avec cloneNode
   var input=$jQ("#"+inputid);
   var input2= input.clone();
   input2.type='password';
   var parent = (input.parent()).get(0);
   alert(input.type);
   parent.replaceChild(input2,input);
}

function changeInputPasswordToText(inputid){
   var $jQ = jQuery; 
   var input=$("#"+inputid);
   var input2= input.cloneNode(false);
   input2.type='text';
}

/**
 * Fonction qui permet de modifier à la voler un input classique
  * en password afin de gérer le bug ie
 */
function pwdFocus(inputid) {
  $('#'+inputid).hide();
  $('#'+inputid.replace('fake','')).show();
  $('#'+inputid.replace('fake','')).focus();
}
/**
 * Fonction qui permet de modifier à la voler un input password

  * en classique afin de gérer le bug ie
 */
function pwdBlur(inputid) {
  if ($('#'+inputid).attr('value') == '') {
	  $('#'+inputid).hide();
	  $('#'+"fake"+inputid).show();
  }
}
 
function checkIfConnected(){
	if(document.cookie.indexOf('CONNECTED') != -1){
		setToConnected();
	}
	else{
		setToDisconnected();
	}
}


function setToConnected(){
	  refreshProfil();
	  $("#btnconnect2").html('Mon profil');		
	  $("#zoneprofil").css('display','block');
	  $("#verticalconnect").css('display','none');
	  $("#btndisconnect").show();
}

function setToDisconnected(){
	  $("#btnconnect2").html('connexion/inscription');	
	  $("#zoneprofil").css('display','none');
	  $("#verticalconnect").css('display','block');
}

function desactiveMouseMenu(){
	mouseMenu		=  false;
}

function activeMouseMenu(){
	mouseMenu		=  true;
}

function activeMouseMenuandRefresh(){
	mouseMenu		=  true;
	document.location.href = document.location.href+"openprofil";
	document.location.reload();	
}

function refreshProfil(){
	
	
	$.ajax({ // fonction permettant de faire de l'ajax
		   type: "POST", 
		   url: "include-profil.php", 
		   data: "id_user="+(getCook("ID_USER")),
		   success: function(msg){ 
		   			var recup = $("#blocradiowomen").html();
		   			var recup2 = $("#blocradioman").html();
					$("#zoneprofil").html(msg);	
					img_test();
					addAjaxtoIdentificationForm();
					addAjaxtoProfilForm();
					$("#blocradiowomen").html(recup1);
					$("#blocradioman").html(recup2);
		   }
	});
}

 function getCook(nom) {
	deb = document.cookie.indexOf(nom + "=")
	if (deb >= 0) {
		deb += nom.length + 1
		fin = document.cookie.indexOf(";",deb)
		if (fin < 0) fin = document.cookie.length
		return unescape(document.cookie.substring(deb,fin))
		}
	return ""
	}







