var alreadyclicked =false;
// Galerie auto entete
var intervalannonce;
var intervalEnCours=false;
var vitesseChangement=6000;
var cookiename = "immoracinespopup";

function getRand()
{
	var max = $$('.annonce_header').length;
	var index_photo = Math.ceil(Math.random()*(max-1));
	if(index_photo == 0)
		index_photo = 1;
	return index_photo;
}

function runGalerieHeader()
{
	if(!intervalEnCours)
	{
		var next = 'annonce_header_' + getRand();
		if($(next))
		{
			new Effect.Appear($(next), {queue:'front',duration: 1.0});
			$(next).className += " viewed_annonce";
			$('nav_encours').value = next;
			intervalannonce = setInterval("nextAnnonce()",vitesseChangement);
			intervalEnCours=true;
		}
	}
}
function nextAnnonce()
{
	var tabencours = $('nav_encours').value.split("_");
	var indiceencours = ((tabencours[2]*1)+1);
	//Fade annonce viewed
	$$('.viewed_annonce').each(function(item){
		item.style.display="none";
		item.className=item.className.replace("viewed_annonce","");
	})
	
	//Appear next annonce
	if($('annonce_header_'+ indiceencours))
		var nextid =  'annonce_header_'+ indiceencours;
	else if(tabencours[2]!=1)
		var nextid =  'annonce_header_1';

	new Effect.Appear($(nextid), {queue:'front',duration: 1.0});
	$('nav_encours').value = nextid;
	$(nextid).className += " viewed_annonce";
}

// Galerie manuel annonce
function loadPhoto(photo)
{
	var tab = photo.src.split("/");
	var img = tab[(tab.length-1)];
	$("photo_encours").src = $("path_photo").value+img;
	$("photo_encours").style.display="none";
	new Effect.Appear($("photo_encours"), {queue:'front',duration: 1.0});
}

// Calcul du pret
function calculPret(montant,duree,interet,datedebut)
{
	var chpost = "&montant=" + montant + "&duree="+duree + "&interet=" + interet +"&datedebut=" + datedebut;
	new Ajax.Request(urltask, {
		asynchronous: 'true',
		postBody: 'tache=calcul_pret'+chpost,
		onSuccess: function(xhr)
		{
			//Traitement du message de retour
			$("conteneur_resultat").style.display="none";
			var response = xhr.responseText.substr(13);
			$("entete_calculatrice").style.height="auto";
			$("conteneur_resultat").innerHTML = response;
			new Effect.Appear($("conteneur_resultat"), {queue:'front',duration: 1.0});
		}
	});
}


// Affichage sous-menu 
function showSousMenu()
{
	ulToShow = $('sous_menu_front');
	if(ulToShow.style.display=="none")
	{
		new Effect.BlindDown(ulToShow, { duration: 0.3 ,queue:'end'});
		ulToShow.style.display="block";
	}
	else
	{
		new Effect.BlindUp(ulToShow, { duration: 0.3 ,queue:'end'});
		ulToShow.style.display="none";
	}
}

function callback_contact(idelem, idtoupdate,other,msg_ok)
{
	$('conteneur_contact').update(msg_ok);
}

function callback_estim(idelem, idtoupdate,other,msg_ok)
{
	$('conteneur_estim').update(msg_ok);
}

function setCookiePopup()
{
	createCookie(cookiename,'true',1)
}

function PopupAlreadyDisplayed()
{
	var cook = readCookie(cookiename)
	if(cook)
	{
		return true;
	}
	return false;
}

var myrules = {
		'#msg_bloc, #msg_display':function(popup)
		{
			var res_popup = PopupAlreadyDisplayed()

			if(res_popup == false)
			{
				new Effect.Appear($('msg_display'),{duration:0.2});
			}

			popup.onclick = function()
			{
				new Effect.Fade($('msg_display'),{duration:0.4, afterFinish:function()
					{
						setCookiePopup();
					}	
				});
			}
		},
		
		'.brecord':function(br)
		{
			br.onclick=function()
			{
				formRecord(br.id);
			}
		},
		
		//Effet du formulaire
		'#formjardin' : function(cs)
		{
			cs.onchange = function()	
			{
				var toappear = $$('.rel_jardin');
				var val_souhait = cs.options[cs.selectedIndex].value;
				if(toappear.length > 0)
				{
					if(val_souhait == "Oui")
						toappear.each(function(item){ item.style.display = "block";});
					else
						toappear.each(function(item){ item.style.display = "none";});
				}	
			}	
		},
		
		'#formsouhait' : function(cs)
		{
			cs.onchange = function()	
			{
				var no_effect_mode = true;
				var val_souhait = cs.options[cs.selectedIndex].value;
				var tovanish = $$('.no_'+val_souhait);
				var toappear = $$('.rel_'+val_souhait);
				
				if(tovanish.length > 0)
				{
					tovanish.each(function(item){
						if(no_effect_mode)
							item.style.display = "none";
						else							
							new Effect.Fade(item, {duration: 0.1,queue:'vanish'});
					});
				}

				if(toappear.length > 0)
				{
					toappear.each(function(item){
						if(no_effect_mode)
							item.style.display = "block";
						else							
							new Effect.Grow(item, {duration: 0.3,queue:'appear'});
					});
				}
			}
		},
		
		//Effet du menu
		'#menuhonorairevente' : function(mh)
		{
			Effect.Pulsate(mh, { pulses: 2, duration: 3, afterFinish: function(){new Effect.Appear(mh)}});
		},
		
		'.show_sous_menu': function(bouton)
		{
			bouton.onclick=function()
			{
				showSousMenu();
			}	
		},

		'.sous_menu_hover': function(bouton)
		{
			if(bouton.className.indexOf("current")>-1)
			{
				if(bouton.innerHTML.indexOf("&gt;")==-1)
				{
					bouton.innerHTML = "&gt; " + bouton.innerHTML +" &lt;";
				}
			}
			else
			{
				bouton.onmouseover=function()
				{
					bouton.innerHTML = "&gt; " + bouton.innerHTML +" &lt;";
				}	
	
				bouton.onmouseout=function()
				{
					bouton.innerHTML = bouton.innerHTML.substring(5,bouton.innerHTML.length-5);
				}
			}	
		},

		//Gestion de la galerie dans la fiche annonce
		'.fond_photo_encours': function(bouton)
		{
			var photo1 = $("photomini_1");
			loadPhoto(photo1);
			var photos = $$(".photomini");
			$("nb_photo").innerHTML= photos.length;
			if(photos.length >1)
				$("nb_photo").innerHTML+= " photos";
			else
				$("nb_photo").innerHTML+= " photo";
		},

		'.affiche_photo': function(bouton)
		{
			bouton.onclick=function()
			{
				var tab = bouton.id.split("_");
				var idphoto = tab[2];
				var photo = $("photomini_"+idphoto);
				loadPhoto(photo);
			}	
		},
		
		'.btn_miniphoto_prec': function(bouton)
		{
			bouton.onclick=function()
			{
				var firstindex = parseInt($("index_photo_encours").value);
				var previousindex = firstindex-1
				var lastindex = firstindex+2
				if($("photomini_"+previousindex))
				{
					new Effect.Move($("zone_move"), {
					  x: +96, y: 0
					});
					$("index_photo_encours").value = firstindex-1;
				}
			}
		},

		'.btn_miniphoto_suiv': function(bouton)
		{
			bouton.onclick=function()
			{
				var firstindex = parseInt($("index_photo_encours").value);
				var nextindex = firstindex+3
				if($("photomini_"+nextindex))
				{
					new Effect.Move($("zone_move"), {
					  x: -96, y: 0
					});
					$("index_photo_encours").value = firstindex+1;
				}
			}
		},

		//Calculatrice
		'.btn_calc': function(bouton)
		{
			bouton.onclick=function()
			{
				var montant = $("calc_saisie_montant").value;
				var duree = $("calc_saisie_duree").value;
				var interet = $("calc_saisie_interet").value;
				var datedebut = $("calc_saisie_datedebut").value;
				if(montant && duree && interet && datedebut)
				{
					calculPret(montant,duree,interet,datedebut);
				}
				else
				{
					myEffect_FlashInfo($("msg_erreur"), 5);
				}
			}
		},
		
		'#calc_saisie_interet': function(bouton)
		{
			bouton.onblur=function()
			{
				var newtext = bouton.value;
				if(newtext!="")
				{
					newtext = newtext.replace("%","");
					newtext = parseFloat(newtext);
					if(newtext<1)
					{
						newtext = newtext*100;
					}
					bouton.value = newtext + "%";
				}
			}
		},
		
		'.open_sousmenu': function(bouton)
		{
			bouton.onclick=function()
			{
				var sousmenu = $("sous_" + bouton.id);
				if(sousmenu.style.display=="none")
				{
					new Effect.SlideDown(sousmenu, {queue:'end', duration:0.1, afterFinish:function(){Behaviour.apply();}});
				}
				else
				{
					new Effect.SlideUp(sousmenu, {queue:'end', duration:0.1, afterFinish:function(){Behaviour.apply();}});
				}
			}
		},
		'.anim_de':function(hauteur)
		{
			if(hauteur.initialized!==true)
			{
				hauteur.initialized = true;
				var cursor = $(hauteur.id.replace("hauteur_","cursor_"));
				if(cursor)
				{
					var dest = hauteur.value +"px";
					new Effect.Morph(cursor, {style:"top:" + dest,queue: { scope: 'deanim'},duration: 2});		  
				}
			}
		},

		//Defilement des annonces dans le header
		'.bloc_annonce_header': function(bouton)
		{
			runGalerieHeader();
		}
};

// Enregistrement des regles definies ci dessus dans Behaviour
Behaviour.register(myrules);

