var InfoDev = {
   /*
    *
    * Dépendances : Mootools 1.2
    */
    'auteur'      : 'Steven Titren',
    'email'   : 'stitren@ekinoxe.fr'
   /*
    *
    * Ce fichier est la propriété de la société Ekinoxe ORIGIN - www.ekinoxe.fr
    * Ce fichier n'est en aucun cas libre de droit, vous n'avez pas l'autorisation de 
    * le diffuser ou de le réutiliser dans un projet exterieur.
    *
    */
}


var __oldCrite = null;

var __ActiveBailMur = [48, 49, 51, 52, 56, 54, 57, 59, 61, 63, 88, 89, 90, 91];

var maj_critere = function(tbValue, width){
	if(typeof width == "undefined") var width = 150
    //alert(valueTB);
	//var tbValue = $$('select#type_bien')[0].options[$$('select#type_bien')[0].selectedIndex].value;
	//alert(tbValue);
	
	if(tbValue != __oldCrite){
		__oldCrite = tbValue;
		
		//console.log('return in_array() : '+in_array(tbValue.toInt(), __ActiveBailMur));
		
		if(in_array(tbValue.toInt(), __ActiveBailMur)==true)
		  change_etat_immo_pro(1);
		else
		  change_etat_immo_pro(0);
		
		new Request({
			url      : wwwroot + 'request_critere.html',
			method   : 'post',
			encoding : 'iso-8859-1',
			
			onSuccess : function(responseText, responseXML){
			    //alert(responseText);
				$('critere').set('html', responseText);
			}
		}).send('width='+width+'&type_bien='+tbValue);
	}
}

if(typeof Element.fadeTo == "undefined"){
	Element.implement({
	 fadeTo : function( amount, whenFinished, duration ) {
	   if(typeof whenFinished != 'function') {
		 whenFinished = function() {}   
	   }
	   this.setStyle('visibility','visible');
	   this.effect('opacity', {
		  transition : new Fx.Transition(Fx.Transitions.Quad.easeOut, 6).easeOut 
		, duration : ( (duration!=null && duration>0) ? duration : 500 )
		, onComplete : function() { 
			if(amount==0) this.element.setStyle('visibility','hidden');
			this.whenFinished.bind(this.element)();
		  }.bind({whenFinished:whenFinished,element:this})
	   }).start(this.getOpacity(),amount);
	   return this;
	 }
			
	});
}

if(typeof Element.moveToCenter == "undefined"){
	Element.implement({
	  moveToCenter:function(){
		var size = this.getSize();
		var docSize = document.getSize();
		var docScroll = document.getScroll();
		
		var elTop = parseInt(docSize.y / 2) - (parseInt(this.getStyle('height'))/2) + docScroll.y;
		var elLeft = parseInt(docSize.x / 2) - (parseInt(this.getStyle('width'))/2) + docScroll.x;
	
		this.setStyles({
			'position' :'absolute',
			'top' : elTop,
			'left' : elLeft
		});
	  }
	});
}

var chagezoom = function(img){
	$('zoom_visuel_annonce').set('src', img);
	
	if($$('div.menuTabs-contents').length > 0) {
	 $$("div.menuTabs-contents")[0].setStyle("height", "");
	}
}

var AntiRobotUpdate = function() {
	$('AntiRobotImage').src = 'verif_humain.jpg?v='+((new Date).getTime());	
}

var Compteur = function(Target, strlen){
	if (Target.value.length > (1000)){
		Target.value = Target.value.substr(0, 1000);
	}
	strlen.value = Target.value.length;
}

var convertiseur_longeur = function(){
	if($('convertiseur') == null){
		var div = new Element('div');
		div.setAttribute('id', 'convertiseur');
		div.inject($(document.body)).setOpacity(0);
		
		var div2 = new Element('div');
		div2.setAttribute('id', 'convertiseur_content');
		div2.inject($(document.body)).setOpacity(0);
		div2.setStyles({
			'z-index': 1000,
			display: 'none',
			width:400,
			height:100,
			background:'#fff'
		});
	}
	
	$('convertiseur').setStyles({
		'z-index': 999,
		position: 'absolute',
		top : 0,
		left: 0,
		cursor: 'pointer',
		display: 'block',
		width: $(document.body).offsetWidth,
		height: $(document.body).offsetHeight,
		background : '#000'
	}).fadeTo(0.6);
	
	var h = '';
	
	h += '<table cellpadding="0" cellspacing="8" style="width:335px; margin-top:7px" align="center">';
	h += '	<tr>';
	h += '		<td style="width:60px">Km:</td>';
	h += '		<td style="width:150px"><input type="text" name="convert_km" id="convert_km"  style="width:100%; background:none; border:1px solid #666" /></td>';
	h += '		<td><input type="button" value="Km -> Miles" id="km-miles" style="backgound:#fff; border:1px solid #333; color:#333; cursor:pointer" /></td>';
	h += '	</tr>';
	h += '	<tr>';
	h += '		<td style="width:60px">Miles:</td>';
	h += '		<td style="width:150px"><input type="text" name="convert_mile" id="convert_mille"  style="width:100%; background:none; border:1px solid #666" /></td>';
	h += '		<td><input type="button" value="Miles -> Km" id="miles-km" style="backgound:#fff; border:1px solid #333; color:#333; cursor:pointer" /></td>';
	h += '	</tr>';
	h += '	<tr>';
	h += '		<td colspan="3" style="padding:7px" align="center">';
	h += '			<a href="javascript:void(0);"';
	h += ' onclick="$(\'convertiseur_content\').fadeTo(0, function(){ $(\'convertiseur\').fadeTo(0, function(){ $(\'convertiseur\').setStyle(\'display\', \'none\');});});"';
	h += ' style="font: bold 14px Verdana; color:#333">Close</a>'; 
	h += '		</td>';
	h += '	</tr>';
	h += '</table>';
	
	$('convertiseur_content').setStyle('display', 'block').fadeTo(1).moveToCenter();
	$('convertiseur_content').set('html', h);
	
	$('convertiseur').addEvent('click', function(){
		$('convertiseur_content').fadeTo(0, function(){
			$('convertiseur').fadeTo(0, function(){
				$('convertiseur').setStyle('display', 'none');
			});
		});		
	});
	
	$('km-miles').addEvent('click', function(){
		var km = ($('convert_km').value).toInt();
		if(km>0){ $('convert_mille').value = (km/1.609344).round(3); }
	});
	
	$('miles-km').addEvent('click', function(){
		var miles = ($('convert_mille').value).toInt();
		if(miles>0){ $('convert_km').value = (miles*1.609344).round(3); }
	});
}

var supp_photo_ann = function (el, text){
	var photo = el.getAttribute('value');
	if(el.checked == true && confirm(text)){
		$(photo).set('html', '<input type="hidden" name="supp_photo[]" value="'+$(photo).getElement('input[name='+photo+']').value+'" /><input type="file" name="'+photo+'" />')
	}
	else{
		el.checked = false;
	}
}

var goTranslate = function(){
    if($('text_fr').value != ''){
        var text = $('text_fr').value;
        translation(text , 'fr', 'en');
        translation(text , 'fr', 'de');
        translation(text , 'fr', 'nl');
    }
    else{
        if($('text_en').value != ''){
            var text = $('text_en').value;
            translation(text , 'en', 'fr');
            translation(text , 'en', 'de');
            translation(text , 'en', 'nl');
        }
        else{
            if($('text_nl').value != ''){
                var text = $('text_nl').value;
                translation(text , 'nl', 'fr');
                translation(text , 'nl', 'en');
                translation(text , 'nl', 'de');
            }
            else{
                if($('text_de').value != ''){
                    var text = $('text_de').value;
                    translation(text , 'de', 'fr');
                    translation(text , 'de', 'en');
                    translation(text , 'de', 'nl');
                }
                else{
                    return false;
                }
            }
        }
    }
}

var translation = function(text, langDepart, langArrive){
    google.language.translate(text, langDepart, langArrive, function(result) {
        if (!result.error){
            $('text_'+langArrive).value = result.translation;
            $('nb_char_'+langArrive).value = result.translation.length;
            return true;
        }
        else{
            $('text_'+langArrive).value = 'Error Translation';
            $('nb_char_'+langArrive).value = 0;
            return false;
        }
    });
}

var critete_demande = {
    
    fichier: wwwroot + 'demande_imo.html',
    
    loadTypeBien : function(recherche){
        new Request({
			url      : critete_demande.fichier,
			method   : 'post',
			encoding : 'utf-8',
			
			onSuccess : function(responseText, responseXML){
			 if(Browser.Engine.trident!=true){
				$('type_1').set('html', '<option value="0">' + $('type_1').getElements('option')[0].innerHTML + '</option>' + responseText);
				$('type_2').set('html', '<option value="0">' + $('type_2').getElements('option')[0].innerHTML + '</option>' + responseText);
		   }
		   else{
		    $('type_1').innerHTML('<option value="0">' + $('type_1').getElements('option')[0].innerHTML + '</option>' + responseText);
				$('type_2').innerHTML('<option value="0">' + $('type_2').getElements('option')[0].innerHTML + '</option>' + responseText);
		   }
			}
		}).send('load=type&recherche='+recherche.value);
    },
    
    loadPays : function(pays, selectedIndex){
        /*
            17 = france
            4 = Belgique
            2 = Allemagne
            28 = Royaume-uni
        */
        paysVal = pays.value;
        
        paysVal = (paysVal=='Belgique' || paysVal=='Belgium' || paysVal=='Belgien'  || paysVal=='België') ? 4 : paysVal;
        paysVal = (paysVal=='France' || paysVal=='Frankreich' || paysVal=='Frankrijk') ? 17 : paysVal;
        paysVal = (paysVal=='Allemagne' || paysVal=='Germany' || paysVal=='Duitsland' || paysVal=='Deutschland') ? 2 : paysVal;
        paysVal = (paysVal=='Royaume-Uni' || paysVal=='The United Kingdom' || paysVal=='Das Vereinigte Königreich' || paysVal=='Verenigd Koninkrijk') ? 28 : paysVal;
        
        //console.log(paysVal);
        
        if(paysVal == 2 || paysVal == 4 || paysVal == 17 || paysVal == 28){
            if(selectedIndex==null) var selectedIndex = 0;
            new Request({
                url      : critete_demande.fichier,
                method   : 'post',
                encoding : 'utf-8',
                
                onSuccess : function(responseText, responseXML){
                  json = Json.decode(responseText);
                  
                  if(Browser.Engine.trident){
                    if($('dep1')!=null) { $('dep1').setHTML(''); }
                    if($('dep2')!=null) { $('dep2').setHTML(''); }
                    if($('dep3')!=null) { $('dep3').setHTML(''); }
                  }
                  else{
                    if($('dep1')!=null) { while(typeof $('dep1').options[0] != 'undefined' ) { $('dep1').options[0].remove(); }; }
                    if($('dep2')!=null) { while(typeof $('dep2').options[0] != 'undefined' ) { $('dep2').options[0].remove(); }; }
                    if($('dep3')!=null) { while(typeof $('dep3').options[0] != 'undefined' ) { $('dep3').options[0].remove(); }; }
                  }
                  
                  if((pays.value).toInt()==4){
                    if($('label_BEL')!=null){ $('label_BEL').setStyle('display', ''); }
                    if($('label_DPT')!=null){ $('label_DPT').setStyle('display', 'none'); }
                  }
                  else{
                    if($('label_BEL')!=null){ $('label_BEL').setStyle('display', 'none'); }
                    if($('label_DPT')!=null){ $('label_DPT').setStyle('display', ''); }
                  }
                  
                  var newElement = new Element('option').setProperties({'value':''}).setText(' -- -- ');
                  if($('dep1')!=null) { $('dep1').adopt( newElement ); }
                  if($('dep2')!=null) { $('dep2').adopt( newElement ); }
                  if($('dep3')!=null) { $('dep3').adopt( newElement ); }
                  
                  for(j=0;j<json.total;j++) {
                    cat = json.dpt[j];
                    
                    var newElement = new Element('option').setProperties({'value':cat.id}).setText(cat.nom);
                    
                    if(selectedIndex.toInt()==cat.id.toInt()){
                        newElement.setProperties({'selected':'selected'});
                    }
                    
                    if($('dep1')!=null) { $('dep1').adopt( newElement ); }
                    if($('dep2')!=null) { $('dep2').adopt( newElement ); }
                    if($('dep3')!=null) { $('dep3').adopt( newElement ); }
                  }
                }
            }).send('load=dpt&pays='+paysVal+'&selected='+selectedIndex);
        }
        else{
            if($('label_BEL')!=null){ $('label_BEL').setStyle('display', ''); }
            if($('label_DPT')!=null){ $('label_DPT').setStyle('display', 'none'); }
            
            if(Browser.Engine.trident){
                if($('dep1')!=null) { $('dep1').setHTML(''); }
                if($('dep2')!=null) { $('dep2').setHTML(''); }
                if($('dep3')!=null) { $('dep3').setHTML(''); }
            }
            else{
                if($('dep1')!=null) { while(typeof $('dep1').options[0] != 'undefined' ) { $('dep1').options[0].remove(); }; }
                if($('dep2')!=null) { while(typeof $('dep2').options[0] != 'undefined' ) { $('dep2').options[0].remove(); }; }
                if($('dep3')!=null) { while(typeof $('dep3').options[0] != 'undefined' ) { $('dep3').options[0].remove(); }; }
            }
        }
    }
}

var __ancienTypeBien = null
var affiche_type_bien = function(el){
    if(el.value != __ancienTypeBien){
        if($('type_bien_' + __ancienTypeBien)!=null){
            $('type_bien_' + __ancienTypeBien).setStyle('display', 'none');
        }
        if($('type_bien_'+el.value) != null){
            $('type_bien_'+el.value).setStyle('display', '');
            __ancienTypeBien = el.value;
        }
    }
}

var affiche_formulaire_contact = function(id_an, id_user){
    $$('form.formulaire_contact').each(function(el){
        el.setStyle('display', 'none');
    });
    
    
    $('formulaire_contact_'+id_an+'_'+id_user).setStyle('display', '');
}

var setCodeDPT = function(selectArray){
    $$('input#DptMap')[0].setProperty('value', selectArray);
}

var __carteAffiche = 0;
var affiche_carte = function(){
    var num_map = $('pays').options[$('pays').selectedIndex].value;
    
    $$('#page div').each(function(el){
       var id = el.getProperty('id');
       if(id!=null){
         reg = id.test(/carte\_[\w]/i);
         if(reg)
          el.setStyle('display','none');
       }
    });
    
    if($('carte_'+__carteAffiche)!=null) $('carte_'+__carteAffiche).setStyle('display', 'none');
    if($('carte_'+num_map)!=null){ 
        $('carte_'+num_map).setStyle('display', 'block');
        __carteAffiche = num_map;
    }
}

var rechercheRapide = function(){
  var formulaire = $('recherche_rapide');
  var radios = formulaire.type_vente_loc;
  var choix;
  var cid;
  var json;
  var cat;
  var j;
  var i;
  
  for(var i=0;i<radios.length;i++){
    radios[i].addEvent('change',function() {
      cid = this.value;
      new Request({
         url : 'limmo/get_categorie.php?cid='+cid
        ,onSuccess : function(r) {
        
          json = Json.decode(r);
          
          if(Browser.Engine.trident){
            $('critere_recherche_rapide').setHTML('');
          }
          else{
            while(typeof $('critere_recherche_rapide').options[0] != 'undefined' ) {
              $('critere_recherche_rapide').options[0].remove();
            }
          }
          
          for(j=0;j<json.count;j++) {
            cat = json.list[j];
            $('critere_recherche_rapide').adopt( new Element('option').setProperties({'value':cat.id}).setText(cat.nom) );
          }
          
          formulaire.action = wwwroot+''+json.url;
          
        }
      }).post();
    });
  }
 
  var current = radios[ ( self.location.toString().test(/location\.html/) ? 1 : 0 ) ];
  current.fireEvent('change');
  current.checked = "checked";
}


var toogleFormulaireContact = function(){
  if($('contact_proprio').getStyle('display')=='none'){
    $('contact_proprio').setStyle('display', '');
  }else{
    $('contact_proprio').setStyle('display', 'none');
  }
}

var PlusOptions = function(){
    $('type_bien').addEvent('change', function(){
        maj_critere($('type_bien').options[$('type_bien').selectedIndex].value);
    });
    
    maj_critere($('type_bien').options[$('type_bien').selectedIndex].value);
}

var MoinsOptions = function(){
    if(Browser.Engine.trident)
        $('critere').setHTML('');
    else
        $('critere').set('html', '');
    
    $('type_bien').removeEvents('change');
}

var change_affiche = function(id){
   if(id==1){
       $('liste_2').setStyle('display', 'none');
       $('liste_1').setStyle('display', 'block');
   }
   else{
       $('liste_1').setStyle('display', 'none');
       $('liste_2').setStyle('display', 'block');
   }
}



var changePage = function(Page, addValue){
  if(typeof addValue == 'undefined') var addValue = false;
  if(addValue==true) {
    $('FPPage').set('value', ($('FPPage').get('value').toInt()+Page));
  } else {
    $('FPPage').set('value', (Page));
  }
  setTimeout(function(){ $('Fpagination').submit() }, 10);
}

var change_etat_immo_pro = function(etat) {
  
  if(etat.toInt() == 1) {
      $$('.ImmoProChamp').setStyle('display', '');
      $$('.price').setStyle('display', 'none');
  } else {
      $$('.price').setStyle('display', '');
      $$('.ImmoProChamp').setStyle('display', 'none');
  }
  
}

var pubScrollers = function() {
  $$('div.scrollerpub').each(function(el) {
    new PubScroll(el);
  });
}

var PubScroll = new Class({
  
  options : {
    duration:500,
    transition : Fx.Transitions.Cubic.easeOut,
    width:529,
    nbCase : 3
  },
  
  Implements: [Options, Events],
  
  initialize : function(el, options) {
    this.fgauche = el.getElement('img.fgauche');
    this.fdroite = el.getElement('img.fdroite');
    this.div = el.getElement('div');
    this.current = 0;
    
    if(this.div!=null && this.fdroite!=null && this.fgauche!=null) {
      //console.log(this.div);
      this.nb = (this.div.getElements('div.case').length / this.options.nbCase);
      
      if(this.nb > this.nb.toInt()) {
        this.nb = this.nb.toInt()+1;
      } else {
        this.nb = this.nb.toInt();
      }
    
      //this.fx = new Fx.Morph(this.div, {duration: this.options.duration, transition: this.options.transition});
      this.fx = new Fx.Scroll(el, {duration: this.options.duration, transition: this.options.transition});
      
      this.fgauche.addEvent('mouseup', this.scrollGauche.bind(this));
      this.fdroite.addEvent('mouseup', this.scrollDroite.bind(this));
      
      //console.log('depart',this.current, this.nb, this.div.getElements('div.case').length);
    } else {
      //console.log('erreur');
    }
  },
  
  scrollGauche : function() {
    
    this.current--;
    
    if(this.current<=0) {
      this.current = 0
    }
    
    //console.log('gauche',this.current, this.nb, (this.current*this.options.width));
    
    this.fx.start((this.current*this.options.width), 0);
  },
  
  scrollDroite : function() {
    
    this.current++;
    
    if(this.current>=this.nb) {
      this.current = this.nb-1;
      return;
    }
    
    //console.log('droite',this.current, this.nb, (this.current*this.options.width*(-1)));
    
    this.fx.start((this.current*this.options.width), 0);
  }
  
});