var homeObjet = (function () {

    var engineMultiSuggest = null;
           /* --------- swap engines Home --------- */
        $j("#choix_engine_habiter").click(function(){
            $j(this).addClass("ongl_habiter_on");
            $j("#choix_engine_investir").removeClass("ongl_investir_on");
            $j("#choix_engine_terrain").removeClass("ongl_terrain_on");
            $j("#engine_habiter").fadeIn().css({"display" : "block"});
            $j("#engine_investir, #engine_terrain").css({"display" : "none"});
        $j("#engine_onglets").removeClass().addClass("habiter_on");
        });
        $j("#choix_engine_investir").click(function(){
            $j('.engine_investir_txt #txtLoi0').css({"display" : "block"});
            getDepartmentsParLoi(1);
            $j(this).addClass("ongl_investir_on");
            $j("#choix_engine_habiter").removeClass("ongl_habiter_on");
            $j("#choix_engine_terrain").removeClass("ongl_terrain_on");
            $j("#engine_investir").fadeIn().css({"display" : "block"});
            $j("#engine_loading").hide();
            $j("#engine_habiter, #engine_terrain").css({"display" : "none"});
        $j("#engine_onglets").removeClass().addClass("investir_on");
        $j("#typeinvestissement0").trigger('click'); // chargement dpts pour 1ere loi par defaut // maj 14-03-11 : afficher un texte par défaut donc aucune loi sélectionnée par défaut
        });
        $j("#choix_engine_terrain").click(function(){
            $j(this).addClass("ongl_terrain_on");
            $j("#choix_engine_habiter").removeClass("ongl_habiter_on");
            $j("#choix_engine_investir").removeClass("ongl_investir_on");
            $j("#engine_terrain").fadeIn().css({"display" : "block"});
            $j("#engine_habiter, #engine_investir").css({"display" : "none"});
        $j("#engine_onglets").removeClass().addClass("terrain_on");
        });
        /* --------- FIN swap engines Home --------- */
        
        /* --------- affichage des textes de lois | Investir --------- */
        $j('#engine_investir_champs_left').change(function(){
            var option_id = $j(this).attr('value');
            $j('.engine_investir_txt .textLoi').hide();
            $j('#txtLoi' + option_id).fadeIn();
        });
        /* --------- FIN affichage des textes de lois | Investir --------- */


        /* --------- loader ajax investir --------- */
        $j('#engine_investir_champs_left').change(function () {
            $j("#engine_loading").show();
                getDepartmentsParLoi($j(this).val());
            $j("#engine_loading").hide();
        });
        /* --------- FIN loader ajax investir --------- */

        /* Désactiver submit sur Investir si aucun département sélectionné */
        $j('#engine_investir_submit').click(function () {
            var selectLoi = document.getElementById('engine_investir_champs_left');
            if (selectLoi.selectedIndex === 0){
                $j('#errorFormLoi').show();
                return false;
            }else if (!$j('#cp_hidden').val()) {
                $j('#errorFormDep').show();
                return false;
            }else {
                $j('#errorFormLoi').hide();
                $j('#errorFormDep').hide();
                $j(this).parents('form').submit();
            }
        });
        /* FIN Désactiver submit sur Investir si aucun département sélectionné */
        
        /* --------- lien div coups de coeur --------- */
        $j(".home_cdc_container").click(function(){
            document.location.href = $j(this).children('a:first-child').attr('href');
        });
        /* --------- FIN lien div coups de coeur --------- */
        
            /* --------- recherche par pays | Habiter --------- */    
        $j("#engine_habiter_pays").change(function(){
        document.body.className = 'wait';
        $j('#engine_habiter_submit').hide();
            if (this.value==97){
                document.location.href = "recherche.htm?idtt=9&idtypebien=15&idpays=250" + "&rnd1=" + rnd1 + "&rnd2=" + rnd2 + "&cp=" + this.value;
            }
            else if(this.value!=250){
                document.location.href = "recherche.htm?idtt=9&idtypebien=15" + "&rnd1=" + rnd1 + "&rnd2=" + rnd2 + "&idpays=" + this.value;
            }
        });
        /* --------- FIN recherche par pays | Habiter --------- */
        
        
        /* --------- envoyer l'idtypebien de la select dans la recherche --------- */
        $j('#engine_habiter_typebien').change(function () {
            $j('#idtypebien').attr('value', $j(this).val());      
        });
        /* --------- FIN envoyer l'idtypebien de la select dans la recherche --------- */
            /* --------- charger sélection terrains --------- */
        $j('#typeoffreterrain input[type="radio"]').click(function () {
            setTerrain($j(this).val());
        });
        /* --------- FIN charger sélection terrains --------- */
        
}) ();
var carou = (function (){

        /* --------- Carrousel actus --------- */
        $j(".bounceout .jCarouselLite").jCarouselLite({
            btnNext: ".bounceout .next",
            btnPrev: ".bounceout .prev",
            visible: 1,
            easing: "backout",
            speed: 1000
        });
        /* --------- FIN Carrousel actus --------- */
        /* --------- Carrousel annuaire --------- */
        $j(".bounceout .jCarouselLite_annuaire").jCarouselLite({
            btnNext: ".bounceout .next_annuaire",
            btnPrev: ".bounceout .prev_annuaire",
            visible: 1,
            auto: 1600,
            speed: 2000
        });
        /* --------- FIN Carrousel annuaire --------- */
}) ();
/* --------- charger les dpts par type de loi --------- */
function getDepartmentsParLoi(ti) {
    // Ajax
    $j.ajax({
        url: '/js,ajax,get_departments.xml',
        data: 'typeinvestissement=' + ti + '&modeDepartement=1',
        async: false,
        dataType: 'xml',
        success: function (xml) {
            if ($j(xml).find('nodepartements').length > 0) {
                $j('#liste_dyn_investir_dpts li').remove();
                $j(xml).find('nodepartements').each(function () {
                    nodep_typeinvest = $j('nodep', this).text();
                    $j('#liste_dyn_investir_dpts').append('<li class="noCursor">' + '<strong>' + nodep_typeinvest + '</strong>' + ' : aucun d&eacute;partement n\'est concern&eacute; actuellement' + '</li>')
                });
            } else {
                $j('#liste_dyn_investir_dpts li').remove();
                $j(xml).find('dept').each(function (i) {
                    departement = $j('dept_num', this).text();
                    nom = $j('dept_nom', this).text();
                    $j('#liste_dyn_investir_dpts').append('<li id="dpt_investir_' + departement + '">' + nom + ' (' + departement + ')</li>')
    
                    // M-à-j du champ hidden pour pouvoir valider le form
                    $j('#dpt_investir_' + departement).click(function () {
                        // Récupération du numéro du département pour mettre à jour le champ hidden
                        var dep = $j(this).attr('id').substring(13);
                        $j('#liste_dyn_investir_dpts li').removeClass('dep_selected');
                        $j(this).addClass('dep_selected');
                        $j('#cp_hidden').val(dep);
                    });
                });
            }
        },
        error: function () {
            alert('Le serveur est momentanément indisponible');
        }
    });
}
    /* Terrains defaut */
    function setTerrain(valeur) {
        var new_usemap;
        if (valeur == 1) {
            new_usemap = '#mapTerrainPromoteurs';
            $j("#libelle_map_terrainspro").show();
            $j("#libelle_map_terrainstous").hide();
        } else {
            new_usemap = '#mapTerrainTous';
            $j("#libelle_map_terrainstous").show();
            $j("#libelle_map_terrainspro").hide();
        }
        $j('#mapTerrains').attr('usemap', new_usemap);
    }


function initHome() {
        
        launchMultiSuggest();
    /* --------- Blur des champs de recherche --------- */
    $j('#ville_multi').each(function(){
        $j(this).val('villes, codes postaux ou département');
    });
    $j('#nb_piecesmin').each(function(){
        $j(this).val('Min.');
    });
    $j(' #pxmax').each(function(){
        $j(this).val($j(this).attr("title"));
    });
    var blurValue;
    $j('#ville_multi, #nb_piecesmin, #pxmax').focus(function () {
        blurValue = $j(this).val();
        $j(this).val('');
    });
    $j('#nb_piecesmin, #pxmax').blur(function () {
        if ($j(this).val() == '') {
            $j(this).val(blurValue);
        }
    });
    /* --------- FIN Blur des champs de recherche --------- */
 

    setTerrain($j('#typeoffreterrain input[type="radio"]:checked').val());

    /* --------- Autosuggest activation recherche --------- */
    
    // check for suggest existence
     if (typeof(engineMultiSuggest) !== 'undefined')
    {
        //capture submit event
        $j("#form_engine_habiter").submit(function(){
          if($j("#ville_multi").val() === "villes, codes postaux ou département"){
          $j('#errorFormHabiter').show();
          return false;
      }
        });
    }
    /* --------- FIN Autosuggest activation recherche --------- */
    
    genParamTri();
}
/* --------- FIN initHome --------- */

function genParamTri()
{
    // generation des 2 parametres de tri envoyes au moteur (random a la 1ere recherche du user)

    rnd1 = 10000*Math.random()|0;
    rnd2 = 10000*Math.random()|0;
    
    while(rnd1%rnd2==0 || rnd2%rnd1==0) {
        rnd1++;
    }

    $j("#engine form input[name='rnd1']").each(function() {
        $j(this).val(rnd1);
    });
    $j("#engine form input[name='rnd2']").each(function() {
        $j(this).val(rnd2);
    });
    $j("#MapHabiter area").each(function() {
        this.href = this.href + "&rnd1=" + rnd1 + "&rnd2=" + rnd2;
    });
    $j("#mapTerrainPromoteurs area").each(function() {
        this.href = this.href + "&rnd1=" + rnd1 + "&rnd2=" + rnd2;
    });
    $j("#mapTerrainTous area").each(function() {
        this.href = this.href + "&rnd1=" + rnd1 + "&rnd2=" + rnd2;
    });
}
/*-----------------------FIN paramTri -------------------------*/
