/*##################################################################################################*/

//Fake checkboxes

/*##################################################################################################*/



$(document).ready(function(){

	// check for what is/isn't already checked and match it on the fake ones

	$("input:checkbox").each( function() {

		(this.checked) ? $("#fake"+this.id).addClass('fakechecked') : $("#fake"+this.id).removeClass('fakechecked');

	});

	// function to 'check' the fake ones and their matching checkboxes

	$(".fakecheck").click(function(){

		($(this).hasClass('fakechecked')) ? $(this).removeClass('fakechecked') : $(this).addClass('fakechecked');

		$(this.hash).trigger("click");

		return false;

	});

});


/*##################################################################################################*/

//Close any div with id idlayer

/*##################################################################################################*/
function fermeChamp(idlayer){

	if(document.getElementById(idlayer).style.display==""){
	document.getElementById(idlayer).style.display = 'none';
	}

}

/*##################################################################################################*/

//onclick change/ajoute la class de lelement parent en la classe choisie (pour des ul li)

/*##################################################################################################*/
function changeClass(idtochange,nomClasse){
	if (document.getElementById(idtochange).className == 'selectReplacement selectOpen') {
				document.getElementById(idtochange).className = 'selectReplacement';
			} else {
				document.getElementById(idtochange).className += ' ' +nomClasse;
			}

}

$(document).ready(function(){
	$('ul#navig_lang_ul > li').click(function() {
	changeClass('navig_lang_ul','selectOpen');
	});
});

/*##################################################################################################*/

//effet de glissement dans le cas de lien de type anchor dans la meme page:

/*##################################################################################################*/

	$(document).ready(function(){
  $('a[href*=#]').click(function() {
 if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
 && location.hostname == this.hostname) {
   var $target = $(this.hash);
   $target = $target.length && $target
   || $('[name=' + this.hash.slice(1) +']');
   if ($target.length) {
  var targetOffset = $target.offset().top;
  $('html,body')
  .animate({scrollTop: targetOffset}, 700);
    return false;
   }
 }
  });
});


function remonte(inWhat,whatToLoad) {
			var $target = $('#global');
			$target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
		   	if ($target.length) {
		  		var targetOffset = $target.offset().top;
		  		$('html,body').animate({scrollTop: targetOffset}, 300);
		   }
		}
/*##################################################################################################*/

//Change les SELECT du formulaire (id) defini ds la fct SetForm en LI aisement manipulables en CSS

/*##################################################################################################*/
     $(document).ready(function(){
      setForm('mycur');
    });



	function selectReplacement(obj) {
      // append a class to the select
      obj.className += ' replaced';
      // create list for styling
      var ul = document.createElement('ul');
      ul.className = 'selectReplacement';
      var opts = obj.options;
      for (var i=0; i<opts.length; i++) {
        var selectedOpt;
        if (opts[i].selected) {
          selectedOpt = i;
          break;
        } else {
          selectedOpt = 0;
        }
      }
      for (var i=0; i<opts.length; i++) {
        var li = document.createElement('li');
        var txt = document.createTextNode(opts[i].text);
        li.appendChild(txt);
        li.selIndex = opts[i].index;
        li.selectID = obj.id;
        li.onclick = function() {
          selectMe(this);
        }
		if (i == 0) {//pour que la 1ere option ait la fleche
			li.className = "firstLi";
		}
        if (i == selectedOpt) {// pour loption selectionnee soit marquee
          li.className = 'selected';
          li.onclick = function() {
            this.parentNode.className += ' selectOpen';
            this.onclick = function() {
              selectMe(this);
            }
          }
        }
        if (window.attachEvent) {// rend IE6 aware des hover sur li
          li.onmouseover = function() {
            this.className += ' hover';
          }
          li.onmouseout = function() {
            this.className =
              this.className.replace(new RegExp(" hover\\b"), '');
          }
        }
        ul.appendChild(li);
      }
      // add the input and the ul
      obj.parentNode.appendChild(ul);
    }

    function selectMe(obj) {
      var lis = obj.parentNode.getElementsByTagName('li');

	 for (var i=0; i<lis.length; i++) {
        if (lis[i] != obj) { // not the selected list item
          lis[i].className='';
          lis[i].onclick = function() {
            selectMe(this);
          }
       } else {
          setVal(obj.selectID, obj.selIndex);
		  document.getElementById('mycur').submit();//on soumet immediatement le formulaire
          obj.className='selected';
          obj.parentNode.className =
            obj.parentNode.className.replace(new RegExp(" selectOpen\\b"), '');
          obj.onclick = function() {
            obj.parentNode.className += ' selectOpen';

			/*kaput : faire en sorte que la liste se referme en rollout obj.parentNode.mouseout(
				function(){
					$(this).slideUp("fast");
				}
			);*/

            this.onclick = function() {
              selectMe(this);
            }
          }
        }
      }

    }

    function setVal(objID,val) {
      var obj = document.getElementById(objID);
      obj.selectedIndex = val;
	  //document.mycur.submit();
    }

    function setForm(theFormYouWant) {
      var s = document.getElementById(theFormYouWant).getElementsByTagName('select');
      for (var i=0; i<s.length; i++) {
        selectReplacement(s[i]);
      }
    }


/*##################################################################################################*/

//Load le contenu de detail objet via jquery en ajax et affiche le fond noir

/*##################################################################################################*/


		//!\\
		//pour alleger et ne pas avoir tout qui se produit en meme temps
		//et aussi pour pouvoir mesurer la hauteur variable du fond noir selon le detail de l objet affiche:

		//1. remonter la page si on est en bas
		//2. afficher un fond noir provioire ayant la hauteur du viewport
		//3. afficher l'animation de loading
		//4. charger le contenu, invisible mais #ajaxLoad a la nouvelle hauteur
		//5. afficher le contenu (par-dessus le loading)
		//6. faire disparaitre le loading
		//7. calculer la hauteur a prendre en compte pour overlay : si #ajaxLoad > #global, on affiche #ajaxLoad +225px (hauteur du header et une marge de 25), et inversement
		//8. adapter le fond noir a la hauteur finale


		//trigger
		function loadContent(inWhat,whatToLoad) {
			monte(inWhat,whatToLoad);
		}

		function monte(inWhat,whatToLoad) {
		//1. remonter la page si on est en bas
			var $target = $('#global');
			$target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
		   	if ($target.length) {
		  		var targetOffset = $target.offset().top;
		  		$('html,body').animate({scrollTop: targetOffset}, 300, function(){
  					showMinimalOverlay(inWhat,whatToLoad);
				});

		   }
		}

		function showMinimalOverlay(inWhat,whatToLoad){
		//2. afficher un fond noir provioire ayant la hauteur du viewport
		var hauteurDuViewport = $(window).height();
		$('#overlay').animate({height: hauteurDuViewport},"slow",function(){
			showLoading(inWhat,whatToLoad);
		});
		}

		function showLoading(inWhat,whatToLoad){
		//3. afficher l'animation de loading
		$('#loading').css({
				display: 'block',
				opacity:1
			})
		//4. charger le contenu, invisible mais #ajaxLoad a la nouvelle hauteur
		$(inWhat).load(whatToLoad, continueShow);
		//5. afficher le contenu (sous le loading)
		$('#closeAjax').css("display","block");
		$('#ajaxLoad').css("display","block");
		}

		function continueShow(){
		//6. faire disparaitre le loading
		$('#loading').animate({opacity: 0},"slow",function () {
        $('#loading').css("display","none");
      	});
		//7. calculer la hauteur a prendre en compte pour overlay
			var hauteurdeAjaxLoad = $('#ajaxLoad').height() + 240;//padding haut et bas de 20px + distance du bord de la fenetre a la bande rouge 200px
			var hauteurdeGlobal = $('#global').height();

			if (hauteurdeAjaxLoad > hauteurdeGlobal) {
				var hauteurAprendreEnCompte = hauteurdeAjaxLoad + 50;////marge du bas = 50
			} else {
				var hauteurAprendreEnCompte = hauteurdeGlobal + 50;//marge du bas = 50
			}
		//8. adapter le fond noir a la hauteur finale	 et le loading en cas de rechargement par suivant/precedent
			$('#loading').css("height",hauteurAprendreEnCompte-50);
			$('#overlay').css("height",hauteurAprendreEnCompte);
		}


		function closeAll(){
			$('#overlay').animate({height: 0},"slow",function () {
        		$('#overlay').css("display","none");
      		});
			$('#closeAjax').css("display","none");
			$('#ajaxLoad').css("display","none");
			$("#info2_container").remove();//virer tout le contenu comme ca quand ca recharge un autre contenu on ne voit pas l ancien
			$('#loading').css("height",250);

		};

/*##################################################################################################*/

//megafocus sur login INACTIF (sur demande P.Maillard - 20.02.2009)

/*##################################################################################################*/

	/*$(document).ready(function(){
		$('#login_name').click(function() {

			var hauteurdeLaPage = $('body').height();

			var winHeight  = $(window).height();

			if (hauteurdeLaPage > winHeight) {
				var terminatorHeight = hauteurdeLaPage +25;
			} else {
				var terminatorHeight = winHeight;
			}

			$('#overlay').animate({height: terminatorHeight},"slow");

			$('#identification')
			.css({
				position: 'absolute',
				top:0,
				left: 11,
				zIndex: 1000,
			})

		});
});
*/

/*##################################################################################################*/

//LOCALISER dans un div plutot que popup

/*##################################################################################################*/


/*##################################################################################################*/

//VENTE/LOCATION SWITCHER

/*##################################################################################################*/

function metEnPremierLoc() {
	$('#montreVentes').animate({
		left: "100px"
		}, 500, 'swing' );

	$('#montreLocations').animate({
		left: "0px"
	  }, 500, 'swing' );
}

function metEnPremierVen() {
	$('#montreLocations').animate({
		left: "100px"
		}, 500, 'swing' );

	$('#montreVentes').animate({
		left: "0px"
	  }, 500, 'swing' );
}

$(document).ready(function(){

	var posVente="haut";
	var posLocation="haut";

	$('#menu_sup').css({display:"none"});

	if($('#ventes').height()==100&&$('#ventes').css("display")=="block"){
		$('#menu_sup').css({display:"block"});
	}else if($('#locations').height()==100&&$('#locations').css("display")=="block"){
		$('#menu_sup').css({display:"block"});
	}
	$('#menu_sup').click(function(){
		if($('#ventes').height()==100&&posVente=="haut"&&$('#ventes').css("display")=="block"){
			$("#menu_object ul#ventes").animate({marginTop:"-50px"}, 200, function(){
				posVente="bas";
			});
			$("#menu_sup").css({backgroundPosition:"bottom left"});
		}else if($('#ventes').height()==100&&posVente=="bas"&&$('#ventes').css("display")=="block"){
			$("#menu_object ul#ventes").animate({marginTop:"0px"}, 200, function(){
				posVente="haut";
			});
			$("#menu_sup").css({backgroundPosition:"top left"});
		}else if($('#locations').height()==100&&posLocation=="haut"&&$('#locations').css("display")=="block"){
			$("#menu_object ul#locations").animate({marginTop:"-50px"}, 200, function(){
				posLocation="bas";
			});
			$("#menu_sup").css({backgroundPosition:"bottom left"});
		}else if($('#locations').height()==100&&posLocation=="bas"&&$('#locations').css("display")=="block"){
			$("#menu_object ul#locations").animate({marginTop:"0px"}, 200, function(){
				posLocation="haut";
			});
			$("#menu_sup").css({backgroundPosition:"top left"});
		};
	});

	$("#montreLocations").click(function () {
	  if(posLocation=="bas"){
		$("#menu_sup").css({backgroundPosition:"bottom left"});
	  }else if(posLocation=="haut"){
		$("#menu_sup").css({backgroundPosition:"top left"});
	  }else{
	  };
	  $('#menu_sup').css({display:"none"});
      $("#menu_object ul#locations").show('slow', function(){
		if($('#locations').height()==100){
			$('#menu_sup').css({display:"block"});
		}
	  });
	  $("#menu_object ul#ventes").hide('slow');
	  $(this).addClass('selected');
	  $("#montreVentes").removeClass('selected');
	  setTimeout('metEnPremierLoc()',250);

	  return false;
    });

	$("#montreVentes").click(function () {
	  if(posVente=="bas"){
		$("#menu_sup").css({backgroundPosition:"bottom left"});
	  }else if(posVente=="haut"){
		$("#menu_sup").css({backgroundPosition:"top left"});
	  }else{
	  };
	  $('#menu_sup').css({display:"none"});
	  $("#menu_object ul#ventes").show('slow', function(){
		if($('#ventes').height()==100){
			$('#menu_sup').css({display:"block"});
		}
	  });
	  $("#menu_object ul#locations").hide('slow');
	  $(this).addClass('selected');
	  $("#montreLocations").removeClass('selected');
	  setTimeout('metEnPremierVen()',250);

	  return false;
    });

});
