var url = document.location.toString().split("#");
var parametres = new Object();
var url_anchor = document.location.toString().split("#!");

if ( url_anchor[1] )
{	
	if ( url_anchor[0].charAt(url_anchor[0].length-1) == "/" ) {		
		url_anchor[0] = url_anchor[0].substring(0, url_anchor[0].length-1);
	}
	if ( url_anchor[1].charAt(0) == "/" ) {		
		url_anchor[1] = url_anchor[1].substring(1, url_anchor[1].length);
	}
	
	window.location.href = url_anchor[0] + "/" + url_anchor[1];
}

if ( url[1] )
{
	var anchor = url[1].split("/");
	
	for (i=0; i<anchor.length; i++) {
		if ( i % 2 )
		{
			parametres[anchor[i-1]] = anchor[i];
		}
	}
}
else
{
	parametres["page"] = 1;
}

var img_ajax = $.create("img", {"src":"images/ajax.gif", "alt":"Chargement", "class":"ajax"}, []);

$(document).ready(function() {	
	/** Effet sur les onglets **/
	$("#haut nav > ul > li").mouseenter( function() { 
		//$(this).children("ul").show("slow");
		$(this).children("ul").show();
	});
	
	$("#haut nav > ul > li").mouseleave( function() { 
		//$(this).children("ul").hide("slow");
		$(this).children("ul").hide();
	});
		
	// Initialise les formulaires Ajax
	initFormAjax();
	
	// Initialise les évenements sur les émoticones
	initEmoticones();
	
	// Bouger accordéon
	initAccordeon();
	
	$.datepicker.setDefaults({
		regional : $.datepicker.regional['fr'],
	   	dateFormat: 'dd/mm/yy',
	   	showOn: 'both',
	   	buttonImage: 'images/icones/fugue/calendar-day.png',
	   	buttonImageOnly: false
	});
	
	$.datepicker.setDefaults($.datepicker.regional['fr']);
	
	// Facebook
	FB_RequireFeatures(["Api"], function() {
	    FB.init("b04588c41d2f3248323ff67161085094");
	});
});

/** Gestion des erreurs de formulaire en ajax **/
function verificationFormulaire(form, erreurs) 
{
    for (var i in erreurs) {
    	var nom = i;
    	var message = erreurs[i];
    	
    	var p = $.create(
    		"p", {"id":"erreur_"+nom, "class":"erreur"}, [message]
    	);
    	
    	$(p).hide();
    	$("#"+form+" [name="+nom+"]").before(p);
    	$(p).fadeIn("fast");
    }
}

// Initialise les formulaires Ajax
function initFormAjax()
{
	$(".form-ajax").unbind();
	$(".form-ajax").submit( submitFormAjax );
}

/** Gestion erreur de formulaire **/
function submitFormAjax() 
{
	var form = this;
	
	$("#"+$(form).attr("id")+" p.erreur").remove();

	$.ajax({
		url: URL+"ajax.php",
		type: "POST",
		dataType: 'json',
		data: $(this).serialize(),
		success: function(reponse){
			if ( reponse.erreurs ) {
				verificationFormulaire($(form).attr("id"), reponse.erreurs);
			}
			else {
				var callbackStr = $(form).children("input[name=callback]").val();
				var callback = eval( callbackStr );
				
				callback(reponse);
			}
		}
	});
	
	return false;
}

// Retour ok d'un formulaire
function retourMessageForm(reponse) {
	dialog("dialog", "message", reponse.message);
}

/** Afficher Page **/
function afficherPage(id, html, functionChangePage, callback) 
{
	$("#"+id).fadeOut("fast", function() {
	    $("#"+id).html(html);
	    	    
	    $("#"+id).fadeIn("fast");
	    
	    addEvenementsPage(id, functionChangePage, callback);
	});
}

/** Afficher Page **/
function addEvenementsPage(id, functionChangePage, callback) 
{
    $("#"+id+" .pagination .number").click( function() {
    	var page = $(this).attr("name");
    	
    	var callback = eval( functionChangePage );
    	
    	callback(page);
    });
    
    $("#"+id+" table th a").each( function() {	
    	if ( tri == $(this).attr("name") ) $(this).addClass(ordre);
    });
    
    $("#"+id+" table th a").click( function() {
    	var t = $(this).attr("name");
    	var o;
    	
    	if ( tri == t )
    	{
    		if ( ordre == "DESC" ) o = "ASC";
    		else o = "DESC";
    	}
    	else
    	{
    		o = "DESC";
    	}
    	
    	var fct = eval(functionChangePage);
    	fct(1, t, o);
    	
    	return false;
    });
    
    if (typeof(callback) != 'undefined') {
    	var fct = eval(callback);
    	fct();
    }
}

// Initialise les évenements sur les émoticones
function initEmoticones() {
	$(".emoticones-precedent").unbind();
	$(".emoticones-precedent").click( bougerEmoticones );
	
	$(".emoticones-suivant").unbind();
	$(".emoticones-suivant").click( bougerEmoticones );
	
	$(".ajoutEmoticone").unbind();
	$(".ajoutEmoticone").click( function(){
		if ( $(this).attr("complet") == "1" )
			var code = $(this).attr("name");
		else
			var code = ':'+$(this).attr("name")+':';
		
		code = ' '+code+' ';
		
		$(this).parents("form").find("textarea[name=texte]").insertAtCaret(code);

		return false;
	});
}

// Initialise les évenements sur les accordéon
function initAccordeon() {
	$(".accordeon-precedent").unbind();
	$(".accordeon-precedent").click( bougerAccordeon );
	
	$(".accordeon-suivant").unbind();
	$(".accordeon-suivant").click( bougerAccordeon );
}

/** Bouge accordéon **/
function bougerAccordeon() {
	var strClass = $(this).attr("class");
	var id = $(this).attr("name");
	
	var largeurAffichage = 580;
	var transit = 10;
	var largeurAccordeon = $("#accordeon-"+id+" li:last").position().left + $("#accordeon-"+id+" li:last").outerWidth();
	
	if ( strClass == "accordeon-precedent" )
	{
		var transitLeft = $("#accordeon-"+id).position().left - transit;
		var newLeft = $("#accordeon-"+id).position().left + largeurAffichage;

		if ( newLeft > 0 ) {
			newLeft = 0;
		}
	}
	else if ( strClass == "accordeon-suivant" )
	{			
		var transitLeft = $("#accordeon-"+id).position().left + transit;
		var newLeft = $("#accordeon-"+id).position().left - largeurAffichage;
		
		var difference = largeurAccordeon + newLeft;
		
		if ( difference < largeurAffichage ) {
			newLeft = 0 - ( largeurAccordeon - largeurAffichage );
		}
	}
	
	$("#accordeon-"+id).animate({ left: transitLeft }, 200).animate({ left: newLeft }, 200);
	
	return false;
}

/** Bouge emoticones **/
function bougerEmoticones() {
	var parent = $(this).parent();
	
	var strClass = $(this).attr("class");
	
	var largeurAffichage = 460;
	var transit = 10;
	var largeurEmoticones = parent.find(".emoticones-bouge li:last").position().left + parent.find(".emoticones-bouge li:last").outerWidth();
	
	if ( strClass == "emoticones-precedent" )
	{
		var transitLeft = parent.find(".emoticones-bouge").position().left - transit;
		var newLeft = parent.find(".emoticones-bouge").position().left + largeurAffichage;

		if ( newLeft > 0 ) {
			newLeft = 0;
		}
	}
	else if ( strClass == "emoticones-suivant" )
	{		
		var transitLeft = parent.find(".emoticones-bouge").position().left + transit;
		var newLeft = parent.find(".emoticones-bouge").position().left - largeurAffichage;
		
		var difference = largeurEmoticones + newLeft;
		
		if ( difference < largeurAffichage ) {			
			newLeft = 0 - ( largeurEmoticones - largeurAffichage );
		}
	}
	
	parent.find(".emoticones-bouge").animate({ left: transitLeft }, 200).animate({ left: newLeft }, 200);
	
	return false;
}

/** Insert le code d'un émoticone à l'endroit séléctionné **/
$.fn.insertAtCaret = function (myValue) {
        return this.each(function(){
                //IE support
                if (document.selection) {
                        this.focus();
                        sel = document.selection.createRange();
                        sel.text = myValue;
                        this.focus();
                }
                //MOZILLA/NETSCAPE support
                else if (this.selectionStart || this.selectionStart == '0') {
                        var startPos = this.selectionStart;
                        var endPos = this.selectionEnd;
                        var scrollTop = this.scrollTop;
                        this.value = this.value.substring(0, startPos) + myValue + this.value.substring(endPos, this.value.length);
                        this.focus();
                        this.selectionStart = startPos + myValue.length;
                        this.selectionEnd = startPos + myValue.length;
                        this.scrollTop = scrollTop;
                } else {
                        this.value += myValue;
                        this.focus();
                }
        });

};

/**
// to Json
function toJSON(obj){
	var json = '({';
	
	$.each(obj, function(k,v){
		var q = typeof v == 'string' ? ~v.indexOf("'") ? '"' : "'" : '';
		
		if (typeof v == 'object')
 			v = toJSON(v).slice(0,-1).substr(1);
 			
		json+= k + ':'+ q + v + q + ',';
	});
	return json.slice(0,-1)+'})';
}; 
**/