$(document).ready(function(){
	$('.select').addClass("hide");
});

/********************************************************/
// Variables globales
/********************************************************/

ongletHover = 0;

/********************************************************/
// Fonctions
/********************************************************/

//Impression de la fenêtre
function toPrint(){	
		window.print();
		return false;	
}

/*
 * Rubrique "Espace" 
 *
 **/
 
var eventsTools = function (){
	
	// Affichage / Masquage des événements
	function toggleView(){		
	
		 var eContent = $(this).parent().parent().find('.content');
		 eContent.css('display');
				 
		 if (eContent.css('display') == 'block')
			eContent.hide();
		 else
			eContent.show(); 
		 
		$(this).parent().parent().toggleClass('close');
		 
		 return false;
	
	};

	//Gestion de la zone de commentaire
	function toggleTextArea(){
		
		$(this).parent().prev().toggleClass('on');	
		$(this).parent().prev().attr('disabled','');	
			
		return false;
			
	};
	
	function init(){
		
		//On masque le bouton submit
		$('p.lienModifier input').hide();
		//pour afficher le lien html
		$('p.lienModifier a').show();
		
		$('textarea.toggleTextArea').addClass('off');

		//Gestionnaire d'évenement pour ouvrir / fermer les événements
		$('a.arr').bind('click', toggleView );
		//On masque les inputs text
		$('ul.tools li.inputDate').hide();
		//On affiche le picto Agenda
		$('ul.tools li.agendaButton').css('display','inline');
		//On affiche le picto Map
		$('ul.tools li.mapButton').css('display','inline');
		
		$('p.lienModifier a').bind('click', toggleTextArea);

	};		

	return {		
		init: init		
	};
		
}();

function showPopin(type){
    
	var dim = getPageSize();
	var scroll = getPageScroll();
	var overlay = $('#popinOverlay');
	var popin = $('#'+type);
	var popinBG = $('.popinBG');

	overlay.css({
		width: dim[0],
		height: dim[1],
		opacity: 0.7,
		display: 'block'
	}).fadeIn();
	
	if($('.popinBG').length){
		
		$(popinBG).css({
			top: scroll[1] + ($(popinBG).height() / 10),
			left: (dim[2]-$(popinBG).width())/2,
			opacity: 1,
			display: 'block'
		});
	}
	popin.css({
		top: scroll[1] + (dim[3] / 10),
		left: (dim[2]-$(popin).width())/2,
		opacity: 1,
		display: 'block'
	}).fadeIn();
	
	popin.find('.btnFermer').bind('click', function(){
		overlay.fadeOut();
		popin.fadeOut(function(){
			if($.browser.msie){ $("select").css("visibility","visible");	}	// enlever les selects pour IE
		});
		if(popinBG) popinBG.fadeOut();
		
	});			
}

/**
 * Crée un évènement de clic sur le lien "Ajouter à mon espace"
 * 
 */
function addToMySpace(){
	
	//ajout à mon espace
	$('a.addToEspace').bind('click', function(){	
		var visu = $('.visu img',$(this).parent().parent()).attr('src');
		var typeElem        			= $("#typeElem").val();
		var coe_i_id_get        		= $("#coe_i_id_get").val();
		var typeReel					= $(this).parent().attr("rel");
		
		if(typeReel == "fiches") {
			 
			var id_user           		= $("#id_user").val(); 
			var id_multilang        	= $(this).parent().attr("id");
			var coe_i_fiche 			= $(this).parent().attr("class");
			var id_rubrique				= $(this).parent().attr("name"); 	 
		}
		else {	
			
			typeReel					= "guides";
			var id_user           		= $("#id_user").val(); 
			var iddelapage        		= $("#iddelapage").val();
			var id_multilang        	= iddelapage;
			var coe_i_fiche 			= iddelapage;
			var id_rubrique          	= $("#id_rubrik").val();
		}
		
		var dataString      = 'iddelapage='+coe_i_fiche+'&id_rubrique='+id_rubrique+'&id_multilang='+id_multilang+'&id_user='+id_user+'&add=selection&submit=valider'+'&typeElem='+typeReel;
		
		$.ajax({
			type: "POST",        
			url: "/FRONT/CRT_PIDF/include/myspace/ajax/action_selection.php",
			data: dataString,
			async : false,
			success: function(data) {
				
				if(data == 'false') {
					
					showPopin('popinAuth');
				}
				else if(data == '0') { 
					
					//cas de lelement qui existe deja dans mon espace. 
					showPopin("popinAlreadyAdded");
					$("#popinTextAlreadyExist").show();
					$("#popinTextConfirmInsert").hide();
				}
				else {
					
					var newVarElements =  $("span[@id=nbCharsSelectionHead]").attr("title");
					newVarElements = parseInt(newVarElements) + 1;
					$("#nbCharsSelectionHead").empty();
					document.getElementById('nbCharsSelectionHead').innerHTML = ' ';
					$("#nbCharsSelectionHead").append(newVarElements+' ');
					
					//effacer lancien contenu le lattribut id
					$("span[@id=nbCharsSelectionHead]").removeAttr("title");
					
					//mettre en place le nouvel attribut id
					$("span[@id=nbCharsSelectionHead]").attr("title",newVarElements);  
					
					$("#spanMessageSel").show();
					
					if(parseInt(newVarElements)==0) {
						
						$("#spanMessageSel").hide();
					}
					
					showPopin("popinAlreadyAdded");
					$("#popinTextConfirmInsert").show();
					$("#popinTextAlreadyExist").hide();
				}
			}
		});
		
		return false;		
	});
}	

/**
 * Fonction qui est appelée par le Flash (le caroussel des pages Home de niveau 2
 * 
 * @param {Object} id
 * @param {Object} sImg
 */
function addItemToSelection(id, sImg) {
    
	var visu			= sImg;
	var coe_i_id		= $("div[@id=coe_i_id_name]").attr("name");
	var id				= id;
	var dataString		= 'id='+id+'&visu='+visu+'coe_i_id='+coe_i_id;
	
	$.ajax({
		type: "POST",        
		url: "/FRONT/CRT_PIDF/php/addToSelectionCarrousel.php",
		data: dataString,
		async : false,
		success: function(data) {
			
			if(data == 'userNotConnected') {
				
				showPopin('popinAuth');
			}
			else if (data == '0') {
		
				//cas de lelement qui existe deja dans mon espace. 
				showPopin("popinAlreadyAdded");
				$("#popinTextConfirmInsert").hide();
				$("#popinTextAlreadyExist").show();
			}
			else {
				
				var newVarElements	=  $("span[@id=nbCharsSelectionHead]").attr("title");
				newVarElements		= parseInt(newVarElements) + 1;
				$("#nbCharsSelectionHead").empty();
				document.getElementById('nbCharsSelectionHead').innerHTML = ' ';
				$("#nbCharsSelectionHead").append(newVarElements+' ');
				
				//effacer lancien contenu le lattribut id
				$("span[@id=nbCharsSelectionHead]").removeAttr("title");
				
				//mettre en place le nouvel attribut id
				$("span[@id=nbCharsSelectionHead]").attr("title",newVarElements);
				$("#mySelectionEmpty").hide();  
				$("#spanMessageSel").show();
				
				showPopin("popinAlreadyAdded");
				$("#popinTextAlreadyExist").hide();
				$("#popinTextConfirmInsert").show();
			}
		}
	});
	
	return false;		
}	

function focusBlur(){
    
    // Search fields
    $("input[@id=search]").bind('focus', function (event){
            $("input[@id=search]").val('');
        }
    );
}


/********************************************************/
// Chargement de la page
/********************************************************/

$(document).ready(function(){
	
	if($('#notAuth').length == 0){
		
		$('.addFav').each(function(i, elem){
			
			$(this).attr('rel', 'messageExistSelection');
			$(this).attr('class', 'addFav popinEspaceTrigger popin btnAjoutselection addToEspace');
		});
		
		addToMySpace();
	}
	
	focusBlur();

	$('li.print a').bind('click', toPrint );
	
	//Commentaires
	eventsTools.init();	

	/* Fix pb titre Home 3 */
	$('#inner .homeNiveau3 .intro h2').css('display', 'block');
	
	navigationDisplay();
	
	/* affichage/masquage du choix de date sur la home, en fonction de la sélection dans le bloc "rechercher un événement" */
	radioCheckAndSwap(".radioContainer input[name='rWhen']",".dateContainer",3);
});

/* affichage/masquage du choix de date sur la home, en fonction de la sélection dans le bloc "rechercher un événement" */
var radioCheckAndSwap = function(radioList,itemToSwap,numForSwapingItem){	
	jQuery(radioList+":eq("+numForSwapingItem+")").bind("click",function(){
		jQuery(itemToSwap+" input").each(function(index){jQuery(this).removeAttr("disabled")});
		jQuery(itemToSwap+" img").css({"visibility":"visible"});	
	});
	jQuery(radioList).each(function(index){
		if(index!=numForSwapingItem){
			jQuery(this).bind("click",function(){
				jQuery(itemToSwap+" input").each(function(index){jQuery(this).attr("disabled","disabled")});
				jQuery(itemToSwap+" img").css({"visibility":"hidden"});		
			});
		}
	});		
	//on masque la première fois
	jQuery(itemToSwap+" input").each(function(index){jQuery(this).attr("disabled","disabled")});
	jQuery(itemToSwap+" img").css({"visibility":"hidden"});	
}
	

/**
 * On stoppe la recherche MNO Go search s'il y a moins de 4 caractères
 */
function stopRecherche() {
	var champsRecherche = document.getElementById('search');
	if (champsRecherche.value.length >= 3) {
		return true;
	}
	return false;
}

/* Fonction show/hide nav */
function navigationDisplay(){
	jQuery("ul#mainNav > li").hover(function(){
				 jQuery(this).addClass("activeMenuItem");
	  },function(){
			 jQuery(this).removeClass("activeMenuItem");
	  });
}

