var last_id=0;

/**
 * Affiche les boutons au survol de la zone d'un programme
 * 
 * @param id
 */
function afficherListeBoutonsCallToAction(id)
{
	/*cacherListeBoutonsCallToAction(last_id);*/
	last_id=parseInt(id);

	$(".listeResultats hr").remove();
	if( $("#listeBoutonsCallToAction_"+id+':hidden').length>0 )
	{
		$("#listeBoutonsCallToAction_"+id).css("display","block");
		//$("#listeBoutonsCallToAction_"+id).slideToggle();
		$("#listeBoutonsCallToAction_"+id).before("<hr style=color:#B0AEC3;padding-left:inherit/>");
		$("#"+id+'.listeResultats').css("background-color","#CBC9D6");
		$(".span_descProg_"+id).css("color","#636870");
	}
	
	infoLots(id);
}

/**
 * Masque les boutons lorsque l'on quitte la zone d'un programme 
 * 
 * @param id
 */
function cacherListeBoutonsCallToAction(id)
{
	$(".listeResultats hr").remove();	
	if( $("#listeBoutonsCallToAction_"+id+":visible").length>0 )
	{
		$("#listeBoutonsCallToAction_"+id).css("display","none");
		//$("#listeBoutonsCallToAction_"+id).slideToggle();
		$("#"+id+'.listeResultats').css("background-color","");
		$(".span_descProg_"+id).css("color","");
	}
	
	infoLots(id);
	
	$(".hauteurResult hr").remove();
}

/**
 * Affiche ou masque les informations sur les lots d'un programme
 * 
 * @param id
 * 
 */
function infoLots(id)
{
	/*alert(rpath);*/
	if( $("#infosLots_"+id).css("display") == "none")
	{
		$("#infosLots_"+id).css("display","block");
		/*$("#gt_"+id).html("&or;");*/
		$("#igt_"+id).css("display","none");
		$("#ior_"+id).css("display","inline");
		/*$("#gt_"+id).html('<img id="igt_'+id+'" src="'+rpath+'image/v2/programmes/arrow_down.png" alt=">" />');*/
	}
	else
	{
		$("#infosLots_"+id).css("display","none");
		/*$("#gt_"+id).html("&gt;");*/
		//$("#igt_"+id).attr("{rpath}image/v2/programmes/arrow.png","src")
		//$("#igt_"+id).attr("v","alt")
		$("#ior_"+id).css("display","none");
		$("#igt_"+id).css("display","inline");
	}
}

/*******************************************************
 * FONCTIONS POUR RECHERCHE RAPIDE
 * 
*******************************************************/

/**
 * permet de remettre la ville selectionnée
 * dans la recherche precedente
 */
function specialOnLoad()
{
	if($('#init_ville1').length>0){
		var init_ville1 		= document.getElementById('init_ville1').value;
		var init_ville2			= document.getElementById('init_ville2').value;
		var init_ville_liste 	= document.getElementById('sel_region').value;
		
		if (init_ville1.length != 0 )
		{
			updateVilleReduit();
			var i=0;
			
			//Si ville selectionnée on positionne la selectbox sur la bonne ville
			if (init_ville1.length != 0)
			{
				var options_length1 = document.getElementById('sel_ville1').options.length;
							
				for(i=0; i< options_length1; i++)
				{
					if (document.getElementById('sel_ville1').options[i].value == init_ville1)
					{
						document.getElementById('sel_ville1').selectedIndex = i;
					} 
				}
			}
		}
		
		if (init_ville2.length != 0 )
		{
			if (init_ville1.length == 0 )
			{
				updateVilleReduit();
			}
			var i=0;
			
			//Si ville selectionnée on positionne la selectbox sur la bonne ville
			if (init_ville2.length != 0)
			{
				var options_length2 = document.getElementById('sel_ville2').options.length;
							
				for(i=0; i< options_length2; i++)
				{
					if (document.getElementById('sel_ville2').options[i].value == init_ville2)
					{
						document.getElementById('sel_ville2').selectedIndex = i;
					} 
				}
			}
		}
		
		//Si region selectionné et ville non selectionné
		if(init_ville_liste.length != 0 && init_ville1.length == 0)
		{
			updateVilleReduit();
		}
	}
}

/**
 *  Met a jour la liste des villes en fonction
 *  de la region ou du departement
 */
function updateVilleReduit()
{
	var id_region = $('#sel_region option:selected');
	var region = id_region.val();
	var tabCity = [];
	
	if (region.substring(0,1) === 'r')
	{
		//Choix d'une région
		tabCity = tabCityByRegion[region];	
    }
    else
    	if(region==='')
    	{
   			//Choix d'un département
    		tabCity = tabAllCity;
    	}
    	else
    	{
    		tabCity = tabCityByDepartment[region];
    	}
		
		$("#sel_ville1").find("option").remove();
		$("#sel_ville2").find("option").remove();
		
		cityArray = [];
		cityArray[0]= "<option></option>";
				
		$.each(tabCity,function(i, valeurs){
			y = i + 1;
			cityArray[y] = "<option value='" + valeurs[0] + "," + valeurs[1] + "'>" + valeurs[1] + " (" + valeurs[0] + ")</option>";
		});
	
		$("#sel_ville1").append(cityArray.join(''));
		$("#sel_ville2").append(cityArray.join(''));
}


/**
 * Fonction permettant de gérer la pagination des liens d'un programme
 */
function paginationLiensProgrammes(idProg,page,nbPages,nbMaxLiensParPage,param_langCode,nbPage)
{
	$.ajax(
	{
		type: "GET",
		url: '../pagination/pagination_liens_' + param_langCode + '.html?idProg=' + idProg + 
							'&nbPages=' + nbPages +
							'&nbMaxLiensParPage=' + nbMaxLiensParPage +
							'&page=' + page +
							'&langue=' + param_langCode,

		processData: false,
		success: function(response) 
		{ 
			if(nbPage == 2){
				$('#page_two').css('background-color', '#FFFFFF');
				$('#page_two').css('color', '#525276');
				$('#page_one').css('background-color', '');
				$('#page_one').css('color', '');
			} else {
				$('#page_one').css('background-color', '#FFFFFF');
				$('#page_one').css('color', '#525276');
				$('#page_two').css('background-color', '');
				$('#page_two').css('color', '');
			}
			var recup_liens = $('#contenuPageLiens #numerotationPagesLiens');
			$('#contenuPageLiens').remove();
			$('.encart').append(response);
			$('#contenuPageLiens .lien:last').after(recup_liens);			
			
			return false;
		},
		async: false
	});
	 return false;
}

$(document).ready(function() 
{
	//On controle si le formulaire reduit existe
	if($("#recherche").length>0){
		specialOnLoad();
		$("#sel_region").bind("change",function(){
			updateVilleReduit();
		});
	}
	
	//Fix le pb sur IE des selectbox
	$(".select2QuickFind").ieSelectStyle({
        applyWidth : true,
		applyStyle : false
    });
	
	// Pour gérer le carousel de la page programme
	$('#slider1').bxSlider({
	    auto: true,
	    autoControls: false
	 });
	$('.bx-prev').html('');
	$('.bx-next').html('');
});

