Object.extend(Event, {
  _domReady : function() {
    if (arguments.callee.done) return;
    arguments.callee.done = true;

    if (this._timer)  clearInterval(this._timer);
    
    this._readyCallbacks.each(function(f) { f() });
    this._readyCallbacks = null;
},
  onDOMReady : function(f) {
    if (!this._readyCallbacks) {
      var domReady = this._domReady.bind(this);
      
      if (document.addEventListener)
        document.addEventListener("DOMContentLoaded", domReady, false);

     if (/WebKit/i.test(navigator.userAgent)) { 
       this._timer = setInterval(function() {
         if (/loaded|complete/.test(document.readyState)) domReady(); 
       }, 10);
     }
        
        Event.observe(window, 'load', domReady);
        Event._readyCallbacks =  [];
    }
    Event._readyCallbacks.push(f);
  }
});

Event.onDOMReady(function()
{  	
	
	
	if($('flashInc'))
	{
		var so = new SWFObject("img-ecologis/maison.swf", 'flashObj" onmouseup="javascript:Lightview.show({ href: \'img-ecologis/flashEcoLogis.swf\'})"', "330", "325", "9", "#FFFFFF");
		so.addParam("quality", "high");
		so.addParam("wmode", "opaque");
		so.addParam("movie", "img-ecologis/maison.swf");
		so.write("flashInc");	
	}
	
	// implantation
	if($('adresseGoogle')){
		$('adresseGoogle').observe('focus',function(e){toogleInput(this, 'Indiquer votre adresse ici')});
		$('adresseGoogle').observe('blur',function(e) {toogleInput(this, 'Indiquer votre adresse ici')});
		click_sidebar(0);
	}
		
	
	if($('votreDossierBt')){	
		$('votreDossierBt').observe('click', function(){
			Dialogs.alert('Cette fonctionnalité est temporairement indisponible.');
		});
	}
	
	
	// passe l'onglet en statut actif
	loadActiveOnglet();
	
	// Faire clignoter le champs de recherche
	observeRecherche();
	
	// vide les champs de votre dossier
	loadVotreDossier();
	
	// Effet sur le menu
	loadMenuEffect();
	
	// Different effet sur la page contact
	listenContact();
	
	
});

function observeRecherche()
{
	if(!$('recherche_home')) return;
		
	$('recherche_home').observe('focus', function(event){
	
		toogleInput(this, 'Recherche');	
			
		var mod = 0;
		
		var p= new PeriodicalExecuter(function(perExe) {
		  	if(mod % 2) 
				$('recherche_home').setStyle({border: '1px solid #EC6A1E'});
			else
				$('recherche_home').setStyle({border: '1px solid #85B42A'});
				
			mod++;	
			
		}, 0.4);
					
		$('recherche_home').observe('blur', function(event){
			p.stop(); 
			$('recherche_home').setStyle({border: '1px solid #EC6A1E'});
			toogleInput(this, 'Recherche');			
		});			
	})		
}

function loadMenuEffect()
{
	if(!$('menu')) return;
	
	$$('#menu .canBeHover').each(function(elt){
		
		// Mouse over
		elt.observe('mouseover', function(e){
				
			// Bord de gauche
			if(elt.next().hasClassName('startMenu'))
				elt.next().down('img').src='img-ecologis/bordGaucheMenu_rol.gif';
			
			// element	
			if(!elt.previous().hasClassName('endMenu'))	
			{
				
				elt.setStyle({backgroundImage: 'url(img-ecologis/repeat-menu-hover.jpg)'});
			
				// Séparations
				if(!elt.next().hasClassName('startMenu'))
					elt.next().down('img').src='img-ecologis/separation-hover.jpg';
						
				elt.previous().down('img').src='img-ecologis/separation-hover.jpg';
			}
				
		})
			
		// Mouse out
		elt.observe('mouseout', function(e){
			
			// Bord de gauche
			if(elt.next().hasClassName('startMenu'))
				elt.next().down('img').src='img-ecologis/bordGaucheMenu.gif';
			
			// element	
			if(!elt.previous().hasClassName('endMenu'))	
			{
				elt.setStyle({backgroundImage: 'url(img-ecologis/repeat-menu.jpg)'});
				
				// Séparations
				if(!elt.next().hasClassName('startMenu') && !elt.next(1).hasClassName('mustBeActif'))
					elt.next().down('img').src='img-ecologis/separation.jpg';
					
				if(!elt.previous(1).hasClassName('mustBeActif'))	
					elt.previous().down('img').src='img-ecologis/separation.jpg';
			}					
		})	
	})
}

function toggleMenu(id, vraiId)
{
	
	$$('#contGauche .menuData').each(function(elt){
		if(elt.visible())
			new Effect.SlideUp(elt,{duration: 0.2});
	});
	
	if($$('#'+id+' li').size() >=1)
		new Effect.toggle(id,'slide', {duration: 0.2});
	
	new Ajax.Request('js/ajax.php', {
	method: 'get',
	parameters:'id_appel=10&id='+vraiId,					    	
	onComplete: function(req) {
		$('AjaxCanLoadHEre').update(req.responseText);
	}});
	
}

function loadVotreDossier()
{	
	
	if(!$('vd_addMail') && !$('vd_mdp')) return;
	
	$('vd_addMail').observe('focus',function(e){toogleInput(this, 'Votre adresse e-mail')});
	$('vd_addMail').observe('blur',function(e) {toogleInput(this, 'Votre adresse e-mail')});
	
	$('vd_mdp').observe('focus',function(e){toogleInput(this, 'Mot de passe')});
	$('vd_mdp').observe('blur',function(e) {toogleInput(this, 'Mot de passe')});
}

// effet sur les champ
function toogleInput(id, text)
{			
	if($F(id) == text)
		$(id).clear();
	else if($F(id).empty())
		$(id).value= text;	
}

function loadActiveOnglet()
{	
	if($('menu') && $('menu').select('.mustBeActif').size() >=1)
	{
		var elt = $('menu').select('.mustBeActif').first();
			
		// Bord de gauche
		if(elt.next().hasClassName('startMenu'))
			elt.next().down('img').src='img-ecologis/bordGaucheMenu_rol.gif';
		
		// element	
		if(!elt.previous().hasClassName('endMenu'))	
		{		
			elt.setStyle({backgroundImage: 'url(img-ecologis/repeat-menu-hover.jpg)'});
		
			// Séparations
			if(!elt.next().hasClassName('startMenu'))
				elt.next().down('img').src='img-ecologis/separation-hover.jpg';
				
			elt.previous().down('img').src='img-ecologis/separation-hover.jpg';
		}		
			
		elt.removeClassName('canBeHover');		
	}
}

function listenContact()
{
	if(!$('contactez-nous')) return;
	
	$$('[name="typeReq"]').each(function(elt){
		elt.observe('click',function(event){
			
			if($F(elt) == 3) { // Projet
				
				$('textForm').update('J\'ai un projet');
				
				$('projetFiche').show();
				
				$('Cobjet').hide();
				$('Cobjet').down(2).value ='';
				
				$('DegradBasDroit').hide();
				$('DegradBasGauche').hide();
								
			}
			else{ // autre
				
				$('textForm').update('Besoin d\'information');
				
				$('projetFiche').hide();
						
				if(!$('Cobjet').visible())
					$('Cobjet').show();	
					
			}
			
		})
	})
	
	
	//
	
	
	$$('[name="etat"]').each(function(elt){
		elt.observe('click',function(event){

			if($(elt).id == 'etatN') { //Neuf

			$('anCon').hide()

			}
			else{ // Rénov

				$('anCon').show()

			}

		})
	})
	
	//
	
	
	$$('[name="jardin"]').each(function(elt){
		elt.observe('click',function(event){

			if($F(elt) == 1) { 

				$('jardine').down('input').value=''; 
				$('jardine').show();

			}
			else
				$('jardine').hide();
		})
	})
	
}

function addAddress()
{
	
	$$('.retirer').invoke('remove');
	
	var nbrLn = ($$('.reference').size()+1);
	
	var ln = '<tr class="reference" id="ln_'+nbrLn+'"><td class="gris">Adresse Email #'+nbrLn+'</td><td><input type="text" name="email_'+nbrLn+'" value=""  class="invInput" />&nbsp;<a href="javascript:removeAddress('+nbrLn+');" class="retirer orange">Retirer</a></td></tr>';
	
	$('addeur').insert({before: ln});
	
}

function removeAddress(num)
{
	$('ln_'+num).remove();
	
	var nbrLn = ($$('.reference').size());
	
	if(!$('ln_'+nbrLn)) return;
	
	$('ln_'+nbrLn).down('input').insert({after:'&nbsp;<a href="javascript:removeAddress('+nbrLn+');" class="retirer orange">Retirer</a>'});
	
}

function ecoPrint(parent, id){
	
	var top=(screen.height-600)/2;

	var left=(screen.width-800)/2;
	
	window.open("page/popup.php?id="+id+"&p="+parent,"","top="+top+",left="+left+",width=800,height=600,menubar=no, scrollbars=yes, statusbar=no");
}

function ecoPrintActu(parent, id){
	
	var top=(screen.height-600)/2;

	var left=(screen.width-800)/2;
	
	window.open("page/popupActu.php?id="+id+"&p="+parent,"","top="+top+",left="+left+",width=800,height=600,menubar=no, scrollbars=yes, statusbar=no");
}

function ecoPrintContact(rech){
	
	r = rech || '';
	
	var top=(screen.height-600)/2;

	var left=(screen.width-830)/2;
	
	window.open("page/popupContact.php?r="+r,"","top="+top+",left="+left+",width=830,height=600,menubar=no,scrollbars=yes,statusbar=no");
}