var infoPage;
var timerShout;

/*--------------------------------------*\
|| GÉNÉRALE
\*--------------------------------------*/
function ajaxLoadPage(message){
	var ui_message = "Please wait...";
	
	if(!isVide(message)){
		ui_message = message;
	}
	
	$.blockUI({
		message: '<h1>' + ui_message + '</h1>',
		css: {
		border: 'none', 
        padding: '15px', 
        opacity: .8,
        color: '#0099FF'
       	} 
	});
}

function checkAll(element, name){
	if(element.attr('checked')){
		$("input[name="+ name +"]").attr('checked', true);
	}else{
		$("input[name="+ name +"]").attr('checked', false);
	}
}

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();
}

function toolTipInit(){
	$("a").easyTooltip();
}

function buttonInit(){
	$( "button, input:submit, a", ".jqueryBtn" ).button();
	$( "a", ".jqueryBtn" ).click(function() { return false; });
}

function superAllInInit(){
	$("a").easyTooltip();
	/*$( "button, input:submit, a", ".jqueryBtn" ).button();
	$( "a", ".jqueryBtn" ).click(function() { return false; });*/
}

function loadSection(section, pageload){
	section.empty();
		
	section.load(pageload, function() {
		shoutboxInit();
	});
}

function showAndHideBox(element){
	if(element.is(":visible")){
		element.hide();
	}else{
		element.show();
	}
}




/*--------------------------------------*\
|| INIT
\*--------------------------------------*/
function init(){
	$(document).ready(function(){		
		// Charger le tooltip	
		$("a").easyTooltip();
		
		// Refresh the shoutbox
		if($("#shoutBoxContent").length){
			refreshShout(1);
		}
	});
	
	// Hide all awnser
	$('.question').hide();
	// When you click a question
	$("a.qLink").click(function(){
		$('.question').hide();
		$($(this).attr("href")).fadeIn("slow");
		$("#qBox").height(260 + $($(this).attr("href")).height());
	});
}


/*--------------------------------------*\
|| INBOX
\*--------------------------------------*/
function postMessage(to, subject, msg, option){
	var check = true;
	
	messageShow("");
	
	// Check if all field are ok
	if(isVide(to.val())){
		to.css("background-color","#B79797");
		check = false;
	}else{
		to.css("background-color","#FFFFFF");
	}
	if(isVide(subject.val())){
		subject.css("background-color","#B79797");
		check = false;
	}else{
		subject.css("background-color","#FFFFFF");
	}
	if(isVide(msg.val())){
		msg.css("background-color","#B79797");
		check = false;
	}else{
		msg.css("background-color","#FFFFFF");
	}

	if(check){
		// Post message
		$.ajax({
			beforeSend: function(html){
				ajaxLoadPage("");
       		},
			type: "POST",
   			url: "/projet/action/sendMessage.php",
   			data: "to=" + to.val() + "&subject=" + subject.val() + "&message=" + msg.val(),
  			dataType: "html",
			success: function(html){
				$.unblockUI();
		
				switch(html){
					case "OK;": 
						//Animation
						if(option == 1){
							$('#inboxSection').slideUp("slow", function(){ 
								$('#inboxSection').empty().append("<strong><font size='3' color='green'> Your message have been sent successfully! </font></strong> <br /><br />");
								$('#inboxSection').slideDown("slow");
							});
						}else{
							$('.boxSectionContent').slideUp("slow", function(){ 
								$('.boxSectionContent').empty().append("<strong><font size='3' color='green'> Your message have been sent successfully! </font></strong> <br /><br /> <a href='/inbox/'>Return to your inbox</a> <br /><br />");
								$('.boxSectionContent').slideDown("slow");
							});
						}
						break;
					case "ERR;1": 
						messageShow("You don't seem to be loged in anymore.");
						break;
					default:
						messageShow("Something bad happened!");
						break;
				}
			}
		});
	}else{
		alert("All fields must have text.");
	}
}

function deleteMessage(idMsg){
	var answer = confirm("Are you sure?");
	var arrayAllMsg = new Array;
	var myVar;

	if(answer){
		if(idMsg == null){
			$("input:checked").each(function(id) {
				myVar = $("input:checked").get(id);
				if(myVar.value != null){
					arrayAllMsg.push(myVar.value); 
				}
			});
		}else{
			arrayAllMsg.push(idMsg);
		}

		if(arrayAllMsg.length>0){
			$.ajax({
				type: "POST",
	  			url: "/projet/action/deleteMessage.php",
	  			dataType: "HTML",
	  			data: "allMsg=" + arrayAllMsg,
				success: function(html){
					switch(html){			
						case "ERR;1":
							alert("You don't seem to be loged in anymore.");
							break;
						case "ERR;2":
							alert("Something bad happened!");
							break;
					}
				
					window.location = "/inbox/";
				}
			});
		}
	}
}








/*--------------------------------------*\
|| SHOUTBOX
\*--------------------------------------*/
function destroyAutoRefresh(){
	clearTimeout(timerShout);
}

function refreshShout(startAuto){
	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);
	}
	
	if(startAuto == 1){
		timerShout = setTimeout("refreshShout(1)", 15000);
	}
}


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;
		}
	});
}



