var ajaxUri = '%64%61%74%61%2F%61%6A%61%78%2E%70%68%70';
var miniloader = '&nbsp;<img src="images/miniload.gif" alt="" />';

function checkInsert(_this){
	if(_this.selectedIndex > 2)
	{
		document.getElementById("is_mot_exp").style.display = 'none';
		document.getElementById("insert").disabled = true;
	}
	else
	{
		document.getElementById("is_mot_exp").style.display = '';
		document.getElementById("is_mot_exp").disabled = false;
	}
}

function __getPage(elem, msg, url) {
	$(elem).slideUp("slow", function()
	{
		$(elem).html(msg).fadeIn("slow", function()
		{
			$(elem).load(url, null, function()
			{
				var tampon = $(elem).html();
					$(elem).html(tampon);
					$(elem).slideDown("slow");
					});
		});
	});
}

$(document).ready(function()
{
	//Toggle input vals
	$("input[id='shout-txt']").toggleVal({populateFrom: "custom",text: "Ecrire sur le mur..."});
	$("textarea[id='area-article-com']").toggleVal({populateFrom: "custom",text: "Commenter..."});
	$("input[id='new-article-title']").toggleVal({populateFrom: "custom",text: "Titre de l'article..."});
	
	//Signup and signin
	$("a.signin-link, a.signup-link, a.tzags-link").click(function() {
		__getPage("#forms-data", "<p class='center'>"+miniloader+"</p>", this.href);
		return false;
	});
	
	//Ecrire un article
	$("#btn-article").click(function() {
		var title = document.getElementById('new-article-title');
		var article = document.getElementById('area-new-article');
		if(title.value == 'Titre de l\'article...')
			return alert('Le titre est obligatoire!');
		if(article.value.length < 20)
			return alert('C\'est tout???');
			
		$.ajax({
			type: "POST",
			url: "data/ajax.php",
			data: "ajaxAction=write_article&title="+title.value+"&article="+article.value,
			error:function(msg)
			{
				alert( "Error : " + msg );
			},
			success:function(data)
			{
				if(data == 'ok')
					{
						alert('Article envoye!');
						location.reload();
					}
				else
					alert(data);
			}
		});
	});
	
	//Ecrire un commentaire article
	$("#btn-article-com").click(function() {
		var art_id = document.getElementById('txt-article-id');
		var com = document.getElementById('area-article-com');
		if(com.value.length < 20)
			return alert('C\'est tout???');
			
		$.ajax({
			type: "POST",
			url: "data/ajax.php",
			data: "ajaxAction=com_article&com="+com.value+"&art_id="+art_id.value,
			error:function(msg){
				alert( "Error : " + msg );
			},
			success:function(data){
				if(data == 'ok'){
						alert('Article commente!');
						location.reload();
					}
				else
					alert(data);
			}
		});
	});
	
	//Shoutbox mur
	$("#shout-btn").click(function(){
		var shout = document.getElementById('shout-txt');
		if(shout.value == 'Ecrire sur le mur...' || shout.value.length < 5)
			return alert('Ecrire minimum un mot!');
		
		$("#shoutbox").html(miniloader);
		$.get(unescape(ajaxUri), {
			ajaxAction: "shoutbox", shout: shout.value
		},
		function(data){
			if(data) {
				$("#shoutbox").html(data);
				shout.value = '';
			}
			else {
				alert('Error!');
			}
		});
	});
	
	//Login
	$("#signin-btn").live('click', function() {
		var pseudo = document.getElementById('username');
		var pass = document.getElementById('password');
			
		if(pseudo.value=='' || pseudo.value=='Pseudo')
			return alert('Mets ton pseudo STP!');
		if(pass.value=='' || pass.value=='******')
			return alert('Mets ton mot de passe STP!');
				
		$.get(unescape(ajaxUri), {
			ajaxAction: "signin", pseudo: pseudo.value, pass: pass.value
		},
		function(data)
		{
			if(data == 'ok')
				location.href = location;
			else
				alert(data);
		});
	});
	
	//Logout
	$("#signout-link").click(function(){	
		$.get(unescape(ajaxUri), { ajaxAction: "signout"},
		function(data){
			if(data == 'ok')
				location.href = location;
			else
				alert(data);
		});
	});
	
	//Insert Tzags into DB
	$("#tzags-btn").live('click', function() {
		var insert_type = document.getElementById('insert_type').options[document.getElementById('insert_type').selectedIndex];
		var insert = document.getElementById('insert');
		var insert_def = document.getElementById('insert_def');
		
		if(insert_type.value == '') {
			return alert('Selectionne le type d\'insertion STP!');
		}
		
		if(insert_type.value == 'mot') {
			if(insert.value.length < 3)
				return alert('Ce mot est trop petit pour \352tre un mot!');
			if(insert.value.indexOf(" ") > -1)
				return alert('Un mot ne doit comporter aucun espace!\n\nSi c\'est un mot compos\351, utilise le trait d\'union.\n\nSinon utilise la rubrique EXPRESSION ou la d\351finition pour les d\351tails!');
		}
		if(insert_type.value == 'expression'){
			if(insert.value.length < 3)
				return alert('Cette expression est trop petite pour \352tre une expression!');
		}
		if(insert_def.value.length < 10)
			return alert('D\351finition ou description trop courte!');
		
		document.getElementById('loading').innerHTML = miniloader;
		
		$.ajax({
			type: "POST",
			url: "data/ajax.php",
			data: "ajaxAction=tzagInsert&insert_type="+insert_type.value+"&insert="+insert.value+"&insert_def="+insert_def.value,
			error:function(msg)
			{
				alert( "Error : " + msg );
			},
			success:function(data)
			{
				if(data == 'ok')
				{
					document.getElementById('loading').innerHTML = "Merci pour ta contribution! ";
					document.getElementById('tzags-btn').disabled = true;
				}
				else
				{
					document.getElementById('loading').innerHTML = "";
					alert(data);
				}
			}
		});
	});
	
	
	
	//ADD comment to Tzags into DB
	$("#btn-tzag-com").live('click', function() {
		var insert_id = document.getElementById('insert_id');
		var com = document.getElementById('txt-tzag-com');
		var coms_from = document.getElementById('coms_from');
		if(com.value.length < 10)
			return alert('Commentaire trop court, faites un effort!');

		$("#loading").html(miniloader);
		
		$.ajax({
			type: "POST",
			url: "data/ajax.php",
			data: "ajaxAction=tzagComment&insert_id="+insert_id.value+"&com="+com.value+"&coms_from="+coms_from.value,
			error:function(msg)
			{
				alert( "Error : " + msg );
			},
			success:function(data)
			{
				if(data)
				{
					$("#loading").html("");
					var newData = $("#coms-box").html()+data;
					$("#coms-box").html(newData);
				}
				else
					alert(data);
			}
		});
	});
	
	
	//Notes
	$("#notation a").click(function() {
		$("#notes").html(miniloader);
		
		$.ajax({
			type: "POST",
			url: "data/ajax.php",
			data: "ajaxAction=notation&note="+this.id,
			error:function(msg)
			{
				alert( "Error : " + msg );
			},
			success:function(data){
				if(data.length < 10)
				{
					$("#notes").html(data);
					$("#notation span").html("a note");
				}
				else
					alert(data);
			}
		});
	});
	
	
	
	
	
});

//Tabs script
$(function () {
	var tabContainers = $('div.tabs > div');
	tabContainers.hide().filter(':first').show();
                        
	$('div.tabs ul.tabs-nav a').click(function () 
	{
		tabContainers.hide();
		tabContainers.filter(this.hash).show();
		$('div.tabs ul.tabs-nav a').removeClass('selected');
		$(this).addClass('selected');
		return false;
	}).filter(':first').click();
});

//toggle vals
(function($){$.fn.toggleVal=function(theOptions){if(!theOptions||typeof(theOptions)=="object"){theOptions=$.extend({focusClass:"tv-focused",changedClass:"tv-changed",populateFrom:"default",text:null,removeLabels:false},theOptions)}else if(typeof(theOptions)=="string"&&theOptions.toLowerCase()=="destroy"){var destroy=true}return this.each(function(){if(destroy){$(this).unbind("focus.toggleval").unbind("blur.toggleval").removeData("defText");return false}var defText="";switch(theOptions.populateFrom){case"alt":defText=$(this).attr("alt");$(this).val(defText);break;case"label":defText=$("label[for='"+$(this).attr("id")+"']").text();$(this).val(defText);break;case"custom":defText=theOptions.text;$(this).val(defText);break;default:defText=$(this).val()}$(this).addClass("toggleval").data("defText",defText);if(theOptions.removeLabels==true){$("label[for='"+$(this).attr("id")+"']").remove()}$(this).bind("focus.toggleval",function(){if($(this).val()==$(this).data("defText")){$(this).val("")}$(this).addClass(theOptions.focusClass).removeClass(theOptions.changedClass)}).bind("blur.toggleval",function(){if($(this).val()==""){$(this).val($(this).data("defText"))}$(this).removeClass(theOptions.focusClass);if($(this).val()!=$(this).data("defText")){$(this).addClass(theOptions.changedClass)}else{$(this).removeClass(theOptions.changedClass)}})})}})(jQuery);
