var infoPage;
var timerShout;

/*--------------------------------------*\
|| GÉNÉRALE
\*--------------------------------------*/
function isVide(strValue){
	if(trim(strValue) == ""){
		return true;	
	}else{
		return false;	
	}
}

function trim(strValue){
	return strValue.replace(/^\s+/g,'').replace(/\s+$/g,'');
} 

function valideChamp(strSection, strSyntaxe){
	var champOk = true;
	
	reg = new RegExp(strSyntaxe, "g");
	
    if(!reg.test($('#'+strSection).val())){
		champOk = false;
	}
	
	return champOk;
}

function verifyText(champ, bouton){
	var notFit = false;
	
	if(($("#"+champ).val().length<3)||($("#"+champ).val().length>1000)){
		notFit = true;
	}

	if((notFit) || ($("#"+champ).val() == "")){ 
	 	$('#'+bouton).attr("disabled", "true"); 
	}else{ 
		$('#'+bouton).removeAttr('disabled');
	}
}

function messageShow(msg){
	if(isVide(msg)){
		$('#message').empty();
		$('#message').removeClass("message");	
	}else{
		$("#message").fadeIn("slow");
		$('#message').addClass("message");
		$('#message').append(msg);	
	}
}

function shoutboxInit(){
	$("a").easyTooltip();
}


/*--------------------------------------*\
|| INIT
\*--------------------------------------*/
function init(){
	$(document).ready(function(){
		// Charger le tooltip	
		$("a").easyTooltip();
		
		// Refresh the shoutbox
		if($("#shoutBoxContent").length){
			refreshShout();
		}
		
		
		// Button user menu
		$("#btnInboxEmpty").hover(function() {
			$(this).attr("src", "/projet/img/btnInboxEmptyOver.jpg");
		}, function() {
			$(this).attr("src", "/projet/img/btnInboxEmpty.jpg");
		});
		$("#btnInbox").hover(function() {
			$(this).attr("src", "/projet/img/btnInboxOver.jpg");
		}, function() {
			$(this).attr("src", "/projet/img/btnInbox.jpg");
		});
		$("#btnMyaccount").hover(function() {
			$(this).attr("src", "/projet/img/btnMyaccountOver.jpg");
		}, function() {
			$(this).attr("src", "/projet/img/btnMyaccount.jpg");
		});
		$("#btnLogOut").hover(function() {
			$(this).attr("src", "/projet/img/btnLogOutOver.jpg");
		}, function() {
			$(this).attr("src", "/projet/img/btnLogOut.jpg");
		});
	});
	
	// Hide all awnser
	$('.question').hide();
	// When you click a question
	$("a.qLink").click(function(){
		$('.question').hide();
		$($(this).attr("href")).fadeIn("slow");
      	$("#qBox").height(162 + $($(this).attr("href")).height());
	});
}


/*--------------------------------------*\
|| CONTACT
\*--------------------------------------*/
function contactForm(){
	var dataString = 'message='+ $("#userQuestion").val();
	
	messageShow("");
	
	if($("#userQuestion").val() != ''){
		$.get("/projet/action/sendContact.php?" + dataString, function(data){
			switch(data){
				case "OK":
					$('#boxSectionContentSub').empty().append(" <font size='3'><i> Your question/comment have been sent. <br /> Allow 24-48 hours for an awnser. </i></font> <br /><br />");
					break;
				case "ERR;1":
					messageShow("The mail have failed to be sent, make sure you are still loged in.");
					break;
				case "ERR;2":
					messageShow("The mail have failed to be sent, make sure you input something in the white box.");
					break;
				default :
					messageShow("The mail have failed to be sent, try again later.");
					break;	
			}
		});
	}else{
		messageShow("You must enter your question/comment in the white box.");
	}
}


/*--------------------------------------*\
|| SHOUTBOX
\*--------------------------------------*/
function refreshShout(){
	if($("#shoutBoxContent").length){
		// Reload (no blink)
		$.ajax({
  			url: "/projet/page/shoutBoxContent.php",
  			dataType: "html",
			success: function(html){
				$("#shoutBoxContent").empty();	
				$("#shoutBoxContent").append(html)
				shoutboxInit();
			}
		});
	}else{
		clearTimeout(timerShout);
	}
	
	timerShout = setTimeout("refreshShout()", 20000);
}


function postShout(){
	var text = $("#sbText").val();
	$("#sbText").val("");
	
	messageShow("");
	
	if((!isVide(text))&&(text != "Message")){
		// Post message
		$.get("/projet/action/postShout.php?message=" + text, function(data){
			switch(data){
				case "OK;": 
					// Reload shoutbox
					$("#shoutBoxContent").empty();	
					$("#shoutBoxContent").load("/projet/page/shoutBoxContent.php", function() {
						shoutboxInit();
					});						
					break;
				case "ERR;1": 
					messageShow("You don't seem to be loged in anymore.");
					break;
				case "ERR;2": 
					messageShow("No message was set! Please try again.");
					break;
				case "ERR;3": 
					messageShow("Something bad happened!");
					break;	
				default:
					messageShow("Something bad happened!");
					break;
			}
		});
	}else{
		alert("You must enter a message in the textbox");
	}
}

function deleteShout(idShout, idUser, warn){
	var dataString;

	messageShow("");
	dataString = 'idShout=' + idShout + "&idUser=" + idUser + "&warn=" + warn;

	$.get("/projet/action/admin/deleteShout.php?" + dataString, function(data){
		switch(data){
			case "OK;": 
				// Reload shoutbox
				$("#shoutBoxContent").empty();	
				$("#shoutBoxContent").load("/projet/page/shoutBoxContent.php", function() {
					shoutboxInit();
				});				
				break;
			case "ERR;1": 
				messageShow("You are not a moderator!");
				break;
			case "ERR;2": 
				messageShow("Something bad happened!");
				break;
			case "ERR;3": 
				messageShow("Something bad happened!");
				break;	
			default:
				messageShow("Something bad happened!");
				break;
		}
	});
}


