var AJAX = "includes/ajax.php";
var Note_Base = "";
var alertColor = "#AE220B";
var validColor = "#2EAE0B";
var pouvoirDeVote = 0;
var nbVotes = "";

$(document).ready(function(){

	if (yScroll < 170)//déclaré dans MainJs
		AutoScroll();
		
	$('#sendThisPseudo').attr("defaut", sendThisPseudoDefaut);
	$('#sendThisEmail').attr("defaut", sendThisEmailDefaut);

	$('.mix span.mixImg a').each(function(){
		var id = $(this).attr("id");
		//alert(id);
		$(this).hover(
			function (){
				$('.mix span.mixImg a#'+id+' span.hover').fadeIn(100);
			},
			function () {
				$('.mix span.mixImg a#'+id+' span.hover').fadeOut(120)
			}
		);
		$(this).click(function (){
			var idMix = id.substring(3, 6);
			MaJ(idMix, 'total');
			MIX = idMix;
		});
	});
	//#########################INIT
	Init();
	// InfoBox
	$('#sendThisEmail, #sendThisPseudo').each(function(){
		var id = "#"+$(this).attr("id");
		$(this).val($(this).attr("defaut"));
		$(this).focus(
			function(){
				if ($(this).val() == $(this).attr("defaut"))
					$(this).val("");
				if ($(this).css("color") == alertColor || "rgb(174, 34, 11)")
					$(this).css("color", $('body').css("color"));
				$('#alert').css("visibility", "hidden");
			});
		$(this).blur(
			function(){
				if (!$(this).val())
			$(this).val($(this).attr("defaut"));	
			});
	});
	
	InitSendIt();
	$("#sending").click(function(){  
		ValidEmail();
	});
	$("#infoBoxClose, #cache").each(function(){
		$(this).click(function(){  
			$('#infoBoxConfirm').fadeOut(200);
			$('#alert').css("visibility", "hidden");
			$('#confirmText').fadeOut(200);
			$('#sendThisEmail').val($('#sendThisEmail').attr("defaut"));
			$('#sendThisEmail').css("color", $('body').css("color"));
			$('#sendThisPseudo').val($('#sendThisPseudo').attr("defaut"));
			$('#sendThisPseudo').css("color", $('body').css("color"));
		});   
	}); 
});
function InitSendIt(){
	$("a.sendThis").click(function(){
		$('#facebookShare').css("display", "inline");
		loadBox();
	});
}
function ValidEmail(){
	var VALID = true;
	if (ValidAuto("#sendThisPseudo", "#sendThisEmail") != "ok"){
		var erreur = ValidAuto("#sendThisPseudo", "#sendThisEmail");
		$('#alert span.alertContent').html(AppelInfobulle(erreur));
		$('#alert').css("visibility", "visible");
		$('#confirmText').css("display", "none");
		AlertColorOblig("#sendThisPseudo");
		AlertColorOblig("#sendThisEmail");
		$('#infoBoxConfirm').css("background", "url(design/img/infoBox/erroricon.png) no-repeat top");
		$('#infoBoxConfirm').fadeIn(200);
		VALID = false;
	}
	if (VALID){
		EnvoyerEmail($('#sendThisPseudo').val(), $('#sendThisEmail').val());
		$('#sendThisEmail').val($('#sendThisEmail').attr("defaut"));
		$('#sendThisPseudo').val($('#sendThisPseudo').attr("defaut"));
	}
}
function EnvoyerEmail(pseudo, email){
	$.post(AJAX, { req: "envoyermail_audio", emailcible: email, pseudoexped: pseudo, mix: MIX },
		function(data){
			switch (data){
				case "0":
					$('#infoBoxConfirm').css("background", "url(design/img/infoBox/validicon.png) no-repeat top");
					$('#confirmText p').html(infoBoxConfirmText0+email);
					$('#confirmText').css("color", validColor);
				break;
				case "1":
					$('#infoBoxConfirm').css("background", "url(design/img/infoBox/erroricon.png) no-repeat top");
					$('#confirmText p').html(infoBoxConfirmText1);
					$('#confirmText').css("color", alertColor);
				break;
				case "2":
					$('#infoBoxConfirm').css("background", "url(design/img/infoBox/erroricon.png) no-repeat top");
					$('#confirmText p').html(infoBoxConfirmText2);
					$('#confirmText').css("color", alertColor);
				break;
			}
		$('#infoBoxConfirm').fadeIn(200);
		$('#facebookShare').css("display", "none");
		$('#confirmText').fadeIn(200);
	});
}
//----------------RECHERCHE D'ERREURS-----------------------------------------
function CompareDefaut(input){
	if ($(input).val() == $(input).attr("defaut"))
		return false;
	else
		return true;
}
function CompareType(input){
	if (input == '#sendThisPseudo'){
		var regexVerif = new RegExp("^[a-z éèàôöêëç]{2,}$","gi");//regex contrôle nom prenom
		if (!regexVerif.test($(input).val()))
			return false;
		else
			return true	
	}
	else{
		if ($(input).val() != $(input).attr("defaut")){
				var regexVerif = new RegExp("^[a-z0-9._-]+@[a-z0-9._-]{2,}[\.]{1}[a-z]{2,4}$","g");//regex contrôle email
				if (!regexVerif.test($(input).val()))
					return false;
				else
					return true;
		}
		else
			return true
	}
}
function ValidAuto(input1, input2){
	if (input2 == null){
		if (CompareDefaut(input1)){
			if (CompareType(input1))
				return "ok";
			else
				return "2";
		}
		else{
			if (CompareType(input1))
				return "1";
			else
				return "3";
		}
	}
	else{
		if (CompareDefaut(input1) && CompareDefaut(input2)){
			if (CompareType(input1) && CompareType(input2))
				return "ok";
			else if ((!CompareType(input1) && CompareType(input2))
			|| (CompareType(input1) && !CompareType(input2))
			|| (!CompareType(input1) && !CompareType(input2)))
				return "2";
		}
		else if ((!CompareDefaut(input1) && !CompareDefaut(input2))
		|| (!CompareDefaut(input1) && CompareDefaut(input2))
		|| (CompareDefaut(input1) && !CompareDefaut(input2))){
			if (CompareType(input1) && CompareType(input2))
				return "1";
			else if ((!CompareType(input1) && CompareType(input2))
			|| (CompareType(input1) && !CompareType(input2))
			|| (!CompareType(input1) && !CompareType(input2)))
				return "3";		
		}
	}
}
function AppelInfobulle(erreur){
		switch (erreur){
			case "1":
				return infoBoxAlert1;
			case "2":
				return infoBoxAlert2;
			case "3":
				return infoBoxAlert1+'<br/>'+infoBoxAlert2;
		}
}
function AlertColorOblig(id){
	var erreur = ValidAuto(id, null)
	if (erreur != "ok")
		$(id).css("color", alertColor);
}
//-------------------PARTIE AUDIO-------------------------------------------------------------------------------
function AutoriseVote(){
	if (pouvoirDeVote == 0){
		if(!$.post(AJAX, { req: "testNav" }))
				alert(alerteAjax);
		else{
			$.post(AJAX, {req: "autoriservote", id: MIX},
			function(data){
				if (data == "error"){
					VoteNO();
				}
				else{
					nbVotes = data;
					VoteOK();
				}
			});
		}
	}
	else if (pouvoirDeVote == 1){
		VoteOK();
	}
	else{
		VoteNO();
	}
}	
function VoteOK(){
	$('#action').attr({id: 'hiddenlink'});
	$('#selecteurNote').css('visibility', 'visible');
	$('#aideNote span.content').text(aideNoteMessage1+' ('+nbVotes+')');
	$('#aideNote').css('visibility', 'visible');
	pouvoirDeVote = 1;
}
function VoteNO(){
	$('#aideNote span.content').text(aideNoteMessage2);
	$('#aideNote').css('visibility', 'visible');
	pouvoirDeVote = 2;
}
function ResetVote(){
	$('#selecteurNote').css('visibility', 'hidden');
	$('#aideNote').css('visibility', 'hidden');
	$('#hiddenlink').attr({id: 'action'});
}
function SoumettreVote(Pnote){
	if(!$.post(AJAX, { req: "testNav" }))
			alert(alerteAjax);
	else{
		$.post(AJAX, {req: "soumettrevote", id: MIX, note: Pnote},
		function(data){
			$('#grdNote').css('background', data);
			Note_Base = $('#grdNote').css('background');
			pouvoirDeVote = 2;
			$('#selecteurNote').css('visibility', 'hidden');
			$('#hiddenlink').attr({id: 'action'});
		});
	}
}
function MaJ(mix, variante){
	
	if(!$.post(AJAX, { req: "testNav" }))
			alert(alerteAjax);
	else{
		if (variante == 'total'){
			$.post(AJAX, {maj: "player", id: mix},
			function(data){
				$('#mp3Centre').html(data);
			});
			$.post(AJAX, {maj: "extra", id: mix},
			function(data){
				$('#extra').html(data);
				InitSendIt();
			});
			pouvoirDeVote = 0;
			nbVotes = "";
			$('#facebookShare a').attr("href", "http://www.facebook.com/sharer.php?u=http%3A%2F%2Fwww.fabienjora.com%2Faudio%2Fmix"+mix);
			$('title').html(pageTitle+', mix '+mix);
		}
		$.post(AJAX, {maj: "moyenne", id: mix},
		function(data){
			$('#grdNote').css('background', data);
			Note_Base = $('#grdNote').css('background');
		});	
		
	}
	
}
function Init(){
	Note_Base = $('#grdNote').css('background');
	
	$('#selecteurNote a.select').each(function(){
		var id = $(this).attr('target');
		$(this).hover(
			function(){
				switch (id){
					case '5':
						$('#grdNote').css('background', 'url(design/img/notation/V/slider.png) no-repeat bottom');
					break;
					case '4':
						$('#grdNote').css('background', 'url(design/img/notation/V/slider.png) no-repeat center -657px');
					break;
					case '3':
						$('#grdNote').css('background', 'url(design/img/notation/V/slider.png) no-repeat center -438px');
					break;
					case '2':
						$('#grdNote').css('background', 'url(design/img/notation/V/slider.png) no-repeat center -219px');
					break;
					case '1':
						$('#grdNote').css('background', 'url(design/img/notation/V/slider.png) no-repeat top');
					break;
				}
			}, function(){
				$('#grdNote').css('background', Note_Base);
			});
		$(this).click(
			function(){
				SoumettreVote(id);
			});
		});
		$('#grdNote').hover(
		function(){},
		function(){
			ResetVote();
		});
		
		$('#grdNote a#action').hover(
		function(){
			AutoriseVote();
		});
		
		$('#selecteurNote').hover(
		function(){},
		function(){
			ResetVote();
		});
		$('#extra, #sendThis, #menu, #catalog').hover(
		function(){
			ResetVote()
		});
}