function randomBloc(){
	toggleAjaxSpinner();
	$.get("homes/randomBlock", function(text){
		$("#aleatoire_content div").fadeOut("slow", function(){
			toggleAjaxSpinner();
			$("#aleatoire_content div").html(text).fadeIn("slow");
		});
	});
	setTimeout('randomBloc();', 10000);
}

function toggleAjaxSpinner(){

	if($("#ajaxSpinner").css('visibility') == 'visible'){
		$("#ajaxSpinner").css('visibility', 'hidden');
	}
	else{
		$("#ajaxSpinner").css('visibility', 'visible');
	}
}

function applyDatePicker(){
	$(".datepicker").datepicker({
	    date: $(this).val(),
	    current: $(this).val(),
	    changeYear: true,
	    changeMonth: true,
	    starts: 1,
	    position: 'r',
	    dateFormat: 'dd/mm/yy',
	    gotoCurrent: true,
	    minDate: +0,
	    monthNamesShort: ['Janvier','F&eacute;vrier','Mars','Avril','Mai','Juin','Juillet','Ao&ucirc;t','Septembre','Octobre','Novembre','D&eacute;cembre'],
	    dayNamesMin: ['Di', 'Lu', 'Ma', 'Me', 'Je', 'Ve', 'Sa'],
	    onBeforeShow: function(){
        	$(this).DatePickerSetDate($('.datepicker:first').val(), true);
    	}
	});
	
}

function allopass(id){
	
	if($("#tarif_groupe").attr("checked")){
		$("#data").val(id+"/"+$("#nombre_resa").val()+"/groupe");
	}
	else if($("#tarif_ce").attr("checked")){
		$("#data").val(id+"/"+$("#nombre_resa").val()+"/ce");
	}
	else{
		$("#data").val(id+"/"+$("#nombre_resa").val());
	}
}

function initTextInput(element, action, text) {
    var txt = text;
	if(element != null){
		switch (action) {
			case 'clear':
				if (element.value == txt) {
					element.value = "";
				}
			break;
			case 'setDefault':
				if (element.value == "") {
					element.value = txt;
				}
			break;
			default:
			break;
		}
	}
}

function validerRechercheSpectacle(){

    if( $('#nom_spec').val() == 'Nom du spectacle, troupe')
        $('#nom_spec').val('');
    if( $('#lieu_spec').val() == 'Ville, CP, d\351partement')
        $('#lieu_spec').val('');
    if( $('#date').val() == 'Jour/Mois/Ann\351e')
        $('#date').val('');
    
    return true;
    
}

function validerRechercheTroupe()
{
    if( $('#nom_tr').val() == 'Nom de la troupe')
        $('#nom_tr').val('');
    if( $('#lieu_tr').val() == 'Ville, CP, d\351partement')
        $('#lieu_tr').val('');
        
    return true;
}

function calendrier(mois)
{
	$('#calendrier_content').html('<img src="img/ajax-loader.gif" alt="Chargement..." title="Chargement..." />');
	$.post("homes/calendar/"+mois, 
		function(text) {
			$('#calendrier_content').fadeOut('normal', function(){
				$('#calendrier_content').html(text);
				$('#calendrier_content').fadeIn('normal');
			});
			
		}
	);
}

function calendrierSpectacle(mois, id)
{
	$('#calendrierSpectacle').html('<img src="img/ajax-loader.gif" alt="Chargement..." title="Chargement..." />');
	$.post("calendar/"+id+"/"+mois,
		function(text) {
			$('#calendrierSpectacle').fadeOut('normal', function(){
				$('#calendrierSpectacle').html(text);
				$('#calendrierSpectacle').fadeIn('normal');
			});
		}
	);
}

function activate(id)
{
	document.getElementById(id).disabled = false;
}

function desactivate(id)
{
	document.getElementById(id).disabled = true;
}

function showHide(id)
{
	var element = document.getElementById(id);
	
	if (element.style.display == "block") {
		element.style.display = "none";
	} 
	else {
		element.style.display = "block";
	}
}

function lockUnlock(id){
	document.getElementById(id).disabled = !document.getElementById(id).disabled;
}

function submitCategorie(){
	var categorie = $("#type").val();
	
	//$('#categoriesListe').html('<img src="images/ajax-loader.gif" alt="Chargement..." title="Chargement..." />');
	$("#ajaxSpinner").show();
	$.post("categories/?action=getCategories", {type:categorie}, 
		function(text){
			$('#categoriesListe').fadeOut('normal', function(){
				$('#categoriesListe').html(text);
				$('#categoriesListe').fadeIn('normal', function(){$("#ajaxSpinner").hide();});
			});
		}
	);
	
	
    /*
    var type = $('#type').val();
    switch( type )
    {
        case '1':
            window.location = server+'categories/a-la-maniere-de/' ;
        break;
        
        case '2':
            window.location = server+'categories/contraintes/' ;
        break;
        
        case '3':
            window.location = server+'categories/registres/' ;
        break;
        
        case '0':
            window.location = server+'categories/' ;
        break;
    }
    */
    
}

function getActualites(){
    var height = $("#actualites").height();
    var numberToDisplay = parseInt((height - 60) / 13);
    var length = $('#actualitesListe > *').length;
    for(i=numberToDisplay; i<=length; i++){
        $("#actualitesListe li:nth-child("+i+")").hide();
    }
}

function resizeIndex(){
    var fixe = $("#identification").height()
    + $("#calendrier").height()
    + $("#partenaires").height()
    + 20;

	var colonne_gauche = $("#colonne_gauche").height(); 
	var aleatoire = $("#aleatoire_content").height();
	var partenaires = $("#partenaires_content").height();
	var actualites = $("#actualites_content").height();
	var colonne_droite = $("#colonne_droite").height();
	
//	alert("fixe : " + fixe + " gauche : " + colonne_gauche);
	
	if( colonne_gauche < fixe ){
		$("#aleatoire_content").height(aleatoire + fixe - colonne_gauche);
	}
	else{
		$("#partenaires_content").height(partenaires + colonne_gauche - fixe - 50);
	}
	
	$("#actualites_content").height(actualites + $("#colonne_gauche").height() - colonne_droite);	
}

function resizeTroupe(){
    var info_tr = $("#info_troupe").height();
    var presentation = $("#presentation").height();
    var presentation_content = $("#presentation_content").height();
    
    if(info_tr > presentation){
        $("#presentation_content").height(presentation_content + info_tr - presentation - 20);
    }
    else{
        var diff = presentation - info_tr;
        var info_gene = $("#info_gene_content").height();
        var info_comp = $("#info_comp_content").height();
        var pro_dates = $("#pro_dates_content").height();
        var concepts_content = $("#concepts_content").height();
        var toadd = Math.floor(diff / 4);
        var reste = diff % 4;

        $("#info_gene_content").height(info_gene + toadd + reste + 6);
        $("#info_comp_content").height(info_comp + toadd);
        $("#pro_dates_content").height(pro_dates + toadd);
        $("#concepts_content").height(concepts_content + toadd);
    }
}

function resizeSpectacle(){
    var height_infos = $("#infos_spectacle").height();
    var height_lieu = $("#lieu_dates").height();
    if( height_infos > height_lieu )
        $("#lieu_dates_content").height(height_infos-8);
    else
        $("#infos_content").height(height_lieu);	

}

function resizeUserSpace(){
	
	var infos_persos = $("#infos_persos").height();
	var infos_persos_content = $("#infos_persos_content").height();
	var commentaires = $("#commentaires").height();
	var commentaires_content = $("#commentaires_content").height();
	
	if(infos_persos > commentaires){
	   $("#commentaires_content").height(commentaires_content + infos_persos - commentaires);
	}
	else{
	   $("#infos_persos_content").height(infos_persos_content + commentaires - infos_persos);
	}
}
/*
function useAvoir(id){

	$('#useAvoir').html('<img src="images/ajax-loader.gif" alt="Chargement..." title="Chargement..." />');
	$.post("utiliser-avoir/", { date:id },
		function(text) {
			$('#useAvoir').html(text);
		}
	);
}
*/
function signalerAbusif(ele){
	var id = $(ele).attr('id');
	id = id.split('-');
	id = id[1];

	$('#commentaire-'+id).html('<img src="images/ajax-loader.gif" alt="Chargement..." title="Chargement..." />');
	$.get("spectacles/signaler-commentaire-abusif/"+id, { ajax:'1' },
		function(text) {
			$('#commentaire-'+id).fadeOut('normal', function(){
				$('#commentaire-'+id).replaceWith(text);
				$('#commentaire-'+id).fadeIn('normal');
			});
		}
	);
	return false;
}

function changeCommentsPage(ele, currentPage, spectacle){
	
	$("#throttleComment").toggle();
	
	var page = ele.html();
	if(page == '&lt;')
		page = parseInt(currentPage) - 1;
	else if(page == '&gt;')
		page = parseInt(currentPage) + 1;
	
	
	$.get("spectacle.php", { page:page, spectacle:spectacle, action:'getCommentsPage' },
		function(text) {
		$('#allComments').html(text);
		$(".pagination a").click(function(){
	        return changeCommentsPage($(this), page, spectacle);
	    });
		$("#throttleComment").toggle();
		//$("#throttleComment").css("display", "none");
		
		//$("#throttleComment").css("visibilty", "hidden");
			/*
			$('#allComments').fadeOut('fast', function(){
				$('#allComments').replaceWith(text);
				$('#allComments').fadeIn('normal');
				$(".pagination a").click(function(){
			        return changeCommentsPage($(this), page, spectacle);
			    });
			});
			*/
		}
	);
	return false;
}
/*
this.tooltip = function(){	
	// CONFIG 		
		xOffset = 10;
		yOffset = 20;		
		// these 2 variable determine popup's distance from the cursor
		// you might want to adjust to get the right result		
	// END CONFIG 		
	$("area.tooltip").hover(function(e){											  
		this.t = this.title;
		this.title = "";									  
		$("body").append("<p id='tooltip'>"+ this.t +"</p>");
		$("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("fast");		
    },
	function(){
		this.title = this.t;		
		$("#tooltip").remove();
    });	
	$("area.tooltip").mousemove(function(e){
		$("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});			
};



this.tooltip = function(){     
	        
        xOffset = 10;
        yOffset = 20;          
        // these 2 variable determine popup's distance from the cursor
        // you might want to adjust to get the right result            
               
    $("area.tooltip").hover(function(e){                                                                                        
    		
    		//$("body").append('<p id="tooltip"><img src="images/ajax-loader.gif" alt="Chargement..." title="Chargement..." /></p>');
    		
	    	var link = this.href;
	        link = link.split("/");
	        var len = link.length - 2;
    		var zone = link[len];
    		//alert(dept[zone]);
    		if(dept[zone] == null || dept[zone] == ''){
    		//if(this.alt == null || this.alt == ''){

	            $.post("troupes/carte/" + link[len] + "/", {getNumber:'1'}, function(text){
	            	//$("#tooltip").html(text);
	            	dept[zone] = text;
	            	//$(this).attr('alt', text);
	            	//alert(text);
	            	//$("body").append("<p id='tooltip'>"+ text +"</p>");
	            	$("body").append("<p id='tooltip'>"+ text +"</p>");
	            });
            }
    		else{
    			$("body").append("<p id='tooltip'>"+ dept[zone] +"</p>");
    		}
            
    		$("#tooltip")
        	.css("top",(e.pageY - xOffset) + "px")
        	.css("left",(e.pageX + yOffset) + "px")
        	.fadeIn("fast");
            
                           
		},
	    function(){
            //this.title = this.t;
            $("#tooltip").remove();
            //$(this).attr('alt', temp);
            //alert(temp);
		}
	); 
    $("area.tooltip").mousemove(function(e){
            $("#tooltip")
                .css("top",(e.pageY - xOffset) + "px")
                .css("left",(e.pageX + yOffset) + "px");
    });                    
};
*/
