// Indicateur de volet
var voletOuvert	= false;
var mouseX =0;
var mouseY =0;

var dureeSlideDownVolet	= 0.3;

// Zindex infoBulle
var zindexib = 99;
var idib = 1;



/**
 * initialisation de l'observer de la souris
 */
function mouseObserver()
{
	Event.observe( document, 'dom:loaded', function(e){
		Event.observe(document, "mouseover", function(e){
			//set vars
			mouseX = Event.pointerX(e);
			mouseY = Event.pointerY(e);
		});
	});
}
mouseObserver();

/**
 * Ouvre le volet
 */
function ouvrirVolet()
{
	if (voletOuvert	== false)
	{	
		//$('dessus_volet').show();
		Effect.SlideDown('dessus_volet', { duration: dureeSlideDownVolet });
		$('fleche_replier').show();
		voletOuvert	= true;
	}
}

/**
 * Ferme le volet
 */
function fermerVolet()
{
	if (voletOuvert	== true)
	{
		Effect.SlideUp('dessus_volet', { duration: 0.3 });
		$('fleche_replier').hide();
		voletOuvert	= false;
	}
}

function redirect(url)
{
	document.location.href=url;
}

/**
 * Permet de d�finir le titre du volet
 */
function setVoletTitle(title)
{
	$('dessus_volet_titre_c').update(title);
	$('dessus_volet_titre_c').setStyle({fontSize: '20px'});
}


function test(text)
{
	alert(text);	
}

/**
 * afficher une div avec l'effet de SlideDown
*/
function openSlide(div)
{
	Effect.SlideDown(div); 
	return false;
}



/**
 * Afficher une info_bulle
 */
function infoBulle(url, option)
{
	var z_index	= null;
	var id		= null;
	
	if (option != null)
	{
		if (option["z_index"] != "")
			z_index	= option["z_index"];
		
		if (option["id"] != "")
			id	= option["id"];
	}
	else
	{
		var option = new Array();
		option['center'] = "";
	}
	
	if (z_index == null)
	{	
		z_index	= zindexib;	
		zindexib++;
	}
	
	if (z_index == null)
	{	
		id	= "ib"+idib;	
		idib++;
	}
	
	// Fermer l'infobulle 
	if(id != null)
	{
		if($(id))
		{
			$(id).remove();
			return;
		}
			
	}
	
	var screen_width  = window.screen.width;
	var screen_height = window.screen.height;
	
	
	// creation des differentes DIV de l'info_Bulle
	var info_bulle = new Element('div', { 'class': 'info_bulle', 'id': id });
	
	var info_bulle_h = new Element('div', { 'class': 'png_transp', 'id': 'info_bulle_h' });
	var info_bulle_m = new Element('div', { 'class': 'png_transp', 'id': 'info_bulle_m' });
	var info_bulle_b = new Element('div', { 'class': 'png_transp', 'id': 'info_bulle_b' });
	var info_bulle_croix = new Element('div', { 'class': 'png_transp', 'id': 'info_bulle_croix' });
	
	// tester les coordonn�es de la souris par rapport � l'ecran
	if(option['center'] == 1)
	{
		mouseX = screen_width/2-90;
		mouseY = screen_height/2-240;
	}
	else if(mouseY && mouseX)
	{
		(mouseX >= screen_width/2) ? mouseX = mouseX - 350 : mouseX = mouseX;
		(mouseY >= screen_height/2) ? mouseY = mouseY - 95 : mouseY = mouseY;
	}
	
	// fermeture de l'info_bulle
	info_bulle_croix.observe('click', function(event){
		info_bulle.remove();
	});
	
	info_bulle.appendChild(info_bulle_h);
	info_bulle.appendChild(info_bulle_croix);
	info_bulle.appendChild(info_bulle_m);
	info_bulle.appendChild(info_bulle_b);
							
	new Ajax.Request( 
		url,
		{
			onCreate: function(request)
						{ 
							// le style de l'info_bulle
							info_bulle.setStyle({
									 'position': 'absolute',
									 'zIndex':   z_index,
									 'top' :     mouseY + "px",
									 'left' :    mouseX + "px"
									});
							
							document.body.appendChild(info_bulle);
						},
			onComplete: function(request)
						{ 
							//afficher l'info_bulle
							info_bulle_m.update(request.responseText);
						}
		}
	);
}

/**
 * mettre � jouer le contenu d'une div 
 */
 function elementDisplay(hideElement,showElement)
 {
	$(hideElement).setStyle({
		'display':'none'			 
	});
	
	$(showElement).setStyle({
		'display':'block'				 
	});
 }

 
/**
 * Selectionner ou deselectionner les checkbox d'un formulaire ayant un nom name != "msg_none"
 */
 
function selectAllCheckBox(formId, checked)
{
	oCheckList = $(formId);
	for ( var i=0; i < oCheckList.length; i++ )
	{
		if( oCheckList.elements[i].type == "checkbox" && oCheckList.elements[i].name != "msg_none" )
		{
			oCheckList.elements[i].checked=checked;
		}
	}
}

/**
 * S�lectionne les �lements d'un "formId" du type "type" et affect l'attribut disabled � la valeur de "state"
 */
function disabledElements(formId, type, state)
{
 	$(formId).select('input[type=' + type + ']').each( 
 		function(input)
 		{ 
 			input.disabled = state 
 		} 
 	);
}


/**
 * Messagerie : R�cuperer la liste des informations destin�es � la suppression
 */
function setSelectedInformationConfirm() 
{
	var a_ids= [];
	
	$('f_messagerie').select('input[type=checkbox]').each( 
		function( input ) 
		{
			if( input.checked )
			{
				a_ids.push( input.value );
			}
		} 
	);
	
	$('concatInfoSelected').value = a_ids;
	
}


/**
 * Messagerie : Classe de navigation � travers les fiches Stars
 * @param array ids_stars
 * @param int currentStar
 */
function NavigationFicheStars(ids_stars, currentStar, num_oeuvre, urlPasserCasting)
{
	this.ids_stars 			= ids_stars;
	this.currentStar 		= currentStar;
	
	//Affiche la star pr�cedente si existante, cache le bouton si premiere star
	this.displayPrevStar = function()
	{
		position 	= this.getPosition(this.currentStar);
		idPrevStar 	= this.getIdStar(position - 1);
		
		if( this.getPosition(idPrevStar) != -1 )
		{
			$("ficheStar_" + this.currentStar).hide();
			$("ficheStar_" + idPrevStar).show();
			$("btn_castingStar_" + this.currentStar).hide();
			$("btn_castingStar_" + idPrevStar).show();
			
			this.setCurrentStar(idPrevStar);
			
			if( idPrevStar != this.ids_stars.max() )
			{
				$("btn_next_star").show();
			}
			if( idPrevStar == this.ids_stars.min() )
			{
				$("btn_prev_star").hide();
			}
			return true;
			
		}
		return false;
	}
	
	//Affiche la star suivante si existante, cache le bouton si derni�re star
	this.displayNextStar = function()
	{
		position 	= this.getPosition(this.currentStar);
		idNextStar 	= this.getIdStar(position + 1);
		
		if( this.getPosition(idNextStar) != -1 )
		{
			$("ficheStar_" + this.currentStar).hide();
			$("ficheStar_" + idNextStar).show();
			$("btn_castingStar_" + this.currentStar).hide();
			$("btn_castingStar_" + idNextStar).show();
			
			this.setCurrentStar(idNextStar);
			
			if( idNextStar != this.ids_stars.min() )
			{
				$("btn_prev_star").show();
			}
			if( idNextStar == this.ids_stars.max() )
			{
				$("btn_next_star").hide();
			}
			return true;
			
		}
		return false;
	}
	
	this.getPosition = function(value)
	{
		return this.ids_stars.indexOf(value);
	}
	
	this.setCurrentStar = function(idStar)
	{
		this.currentStar = idStar;
	}
	
	this.getIdStar = function(position)
	{
		return this.ids_stars[position];
	}
}


function getCookie(c_name)
{
	if (document.cookie.length>0)
	  {
	  c_start=document.cookie.indexOf(c_name + "=");
	  if (c_start!=-1)
	    { 
	    c_start=c_start + c_name.length+1; 
	    c_end=document.cookie.indexOf(";",c_start);
	    if (c_end==-1) c_end=document.cookie.length;
	    return unescape(document.cookie.substring(c_start,c_end));
	    } 
	  }
	return "";
}

function setCookie(c_name,value,expiredays)
{
	var path = '/';
	var exdate	 = new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString())+
	";path="+path;
}


/**
 * Retourne une date au format YYYY-mm-dd
 *
 * @param Timestamp date
 * @return String 
 */
function format_date(date)
{
	var	day		= date.getDate();
	var	month	= date.getMonth()+1;
		
	if (day < 10) 	day 	= "0" + day;
	if (month < 10) month 	= "0" + month;
		
	return date.getFullYear() + "-" + month + "-" + day;
}


function includeJS( url )
{
	try{
   script = document.createElement("script");
   script.type = "text/javascript";
   script.src  = url;
   head = document.getElementsByTagName("head");
   head[0].appendChild(script);
  }catch(e){
   alert(url);
  }
}
