var tab_lieux = new Array();
var recherche_str = "";

/**
 * Charge les lieux à afficher via un getScript sur monuments.php
 */ 
function charge_lieux(){
    loading_debut();
    var types = "";
    var deja = "";
    var sic = $("div#row_MON_ID_2 > .multiple").val();
    //alert("charge_lieux sic = " + sic);
    var in_page_monument = $("input#in_page_monument").val();
    var page_monument = $("input#page_monument").val();
    var page_hotel = $("input#page_hotel").val();
    var nb_etoiles = $("input#nb_etoiles").val();
    $("input.type_monument:checked").each(function(){
        types = types + $(this).attr('value') + ",";
    });
    for(var i=0;i<tab_lieux.length;i++){
        deja = deja + "'" + tab_lieux[i]["table"]  + '_' + tab_lieux[i]["id"] + "',";
    }
    if(recherche_str.length<=0 && recherche!=null && recherche.length>0){
        for(var i=0;i<recherche.length;i++){
            recherche_str += "&recherche["+i+"]="+recherche[i];
        }
    }
    $.getScript(
        "monuments.php?"
        +"car_id="+(car_id)
        +"&x="+(carte_pos_x+80)
        +"&y="+(carte_pos_y+200)
        +"&_map_pos_gauche="+_map_pos_gauche
        +"&_map_pos_droite="+_map_pos_droite
        +"&_map_pos_haut="+_map_pos_haut
        +"&_map_pos_bas="+_map_pos_bas
        +"&_map_width="+_map_width
        +"&_map_height="+_map_height
        +"&types="+encodeURI(types)
        +"&deja="+encodeURI(deja)
        +"&sic="+encodeURI(sic)
        +"&in_page_monument="+encodeURI(in_page_monument)
        +"&page_monument="+encodeURI(page_monument)
        +"&page_hotel="+encodeURI(page_hotel)
        +"&nb_etoiles="+encodeURI(nb_etoiles)
        +recherche_str
    ,function(){
        var cur_lieu=null;
        var cur_cat=null;
        for(var i=0;i<tab_lieux.length;i++){
            creer_lieu(tab_lieux[i]);
        }
        $("#idd_clignote_init").attr({value: ''});//on reset l'eventuel lieu de clignotement initial
        loading_fin();
    });
}

/**
 * Centre la carte sur une position
 */ 
/*function center_map(x,y){
    x = coord_to_x(x);
    y = coord_to_y(y) - 170;
    commandant= 'marker';
    marker_x=x;
    marker_y=y;
    nf_bouge();
    charge_lieux()
} */

/**
 * Retourne le lieu s'il est déjà affiché sur la carte
 */ 
function lieu_sur_map(id,table){
    return document.getElementById("lieu_"+table+"_" + id);
}

/**
 * ??
 */ 
function id_search(id){
    if(recherche!=null && recherche.length>0){
        for(var i=0;i<recherche.length;i++){
            if(recherche[i]==id){
                return i+1;
            }
        }
    }
    return 0;
}

/**
 * Détermine si l'ID d'un PR est celui que l'on cherche via SICT
 */ 
function is_monument_sic(id){
    return $("div#row_MON_ID_2 > .multiple").val() == id;
}

/**
 * Détermine si l'ID d'un hôtel est celui que l'on cherche via SICT
 */ 
function is_hotel_sic(id){
    return $("div#row_MON_ID_2 > .multiple").val() == -1 * parseInt(id);
}

/**
 * Détermine si on a cet Hotel en GET
 */ 
function is_hotel_page(id){
    return $("input#page_hotel").val() == id;
}

/**
 * Détermine si on a ce PR en GET
 */ 
function is_monument_page(id){
    return $("input#page_monument").val() == id;
}

/**
 * Détermine si on est dans l'onglet "map" d'un nombre d'étoiles
 */ 
function is_search_etoiles(){
    return $("input#nb_etoiles").val() !="";
}

/**
 * Si c'est un hôtel recherché
 */ 
function is_hotel_searched(id){
    if(recherche!=null && recherche.length>0){
        for(var i=0;i<recherche.length;i++){
            if(recherche[i]==id){
                return true;
            }
        }
    }
    return false;
}

/**
 * Affiche un lieu sur la carte
 * tab["type"] corresond au type de PR, avefc 22 = hôtel 
 */ 
function creer_lieu(tab){
    var page_monument = $("input#page_monument").val();

    var affiche_type = $("#cb_"+tab["type"]).is(":checked"); //si on a coché ce type de lieu dans "show on the map"
    var cli = false; //indique si le PR doit clignoter
    
    //Si c'est le PR cherché via SICT
    if(tab["type"]!=22 && tab["table"]=="M" && is_monument_sic(tab["id"])){
        affiche_type=true; //on l'affiche
        if(sic){ //On vient de cliquer dessus, on centre et on le fait clignoter
            gps(tab["x"],tab["y"]);
            cli=true;
            sic=false;
        }
    }
    
    //Si c'est l'hôtel cherché via SICT
    if(tab["type"]==22 && tab["table"]=="H" && is_hotel_sic(tab["id"])){
        affiche_type=true; //on l'affiche
        if(sic){ //On vient de cliquer dessus, on centre et on le fait clignoter
            gps(tab["x"],tab["y"]);
            cli=true;
            sic=false;
        }
    }
    
    //Si on est dans l'onglet "map" de ce monument ou on recherche ce monument
    if(tab["type"]!=22 && tab["table"]=="M" && is_monument_page(tab["id"])){
        affiche_type=true; //on l'affiche
        if(page_monument_locate){
            //gps(tab["x"],tab["y"]);
            cli=true; // il clignote
            page_monument_locate=false;
        }
    }
    
    //Si on est dans l'onglet "map" de cet hôtel
    if(tab["type"]==22 && tab["table"]=="H" && is_hotel_page(tab["id"])){
        affiche_type=true; //on l'affiche
        if(page_hotel_locate){
            //gps(tab["x"],tab["y"]);
            cli=true;
            page_hotel_locate=false;
        }
    }
    
    //Si on est dans l'onglet "map" d'un nombre d'étoiles, on affiche tous les hôtels concernés
    if(tab["type"]==22 && tab["table"]=="H" && is_search_etoiles()){
        affiche_type=true;
    }
      
    //Si c'est un hôtel recherché   
    if(tab["type"]==22 && tab["table"]=="H" && is_hotel_searched(tab["id"])){
        affiche_type=true;
    }
    
    // Si on est pas sencé l'afficher, mais que c'est un hôtel, on l'affiche (??)
    if(!affiche_type && tab["type"]==22 && tab["table"]=="H"){
        affiche_type=true;
    }
    
    
    //---------------- PARTIE AFFICHAGE ----------------
    
    //Si le lieu est déjà sur la carte
    if(lieu_sur_map(tab['id'],tab['table'])){
        $("#lieu_" + tab['table']+"_" + tab['id']).css("display",(affiche_type?"block":"none"));
        
    //Sinon on l'ajoute sur la carte    
    }else{
        var num_pr=tab["tmo"]; //type de monument sur deux chiffres
        if(num_pr.length==1){
            num_pr = '0' + num_pr;
        }
        var a = document.createElement("label");
        
        //Si c'est un hôtel recherché
        if(tab["table"] == "H" && is_hotel_searched(tab["id"])){
            a.className="map_point search_point pr_" + tab["type"];
            $(a).html('<span>'+id_search(tab["id"])+'</span>');
            //si c'est le premier hotel recherché et qu'on a pas définit de PR
            if(id_search(tab["id"])==1 && page_monument.length==0){
                cli = true;
            }
            $(a).css('background','transparent url(http://www.duo-rez.com/images_icone_pr/000000-full.gif) scroll no-repeat center top');
            
        //Sinon, c'est un PR ou hôtel affiché hors recherche 
        }else{
            $(a).css('background','transparent url(http://www.duo-rez.com/images_icone_pr/0000'+num_pr+'-full.gif) scroll no-repeat center top');
            a.className="map_point pr_" + tab["type"];
        }
        
        //$(a).css('z-index','1');
        var x = coord_to_x(tab["x"]) - (icone_width/2);
        var y = coord_to_y(tab["y"]) - (icone_height/2);
        $(a).attr('id','lieu_'+tab['table']+'_' + tab['id']);
        $(a).css("display",(affiche_type?"block":"none"));
        $(a).mouseover(function(){
            clignote ('lieu_'+tab['table']+'_' + tab['id']);
        });
        //$(a).attr('href','detail_lieu.php?id=' + tab["id"]);
        $(a).attr('target','detail_lieu');
        //$(a).attr('title',tab['libelle'] + "\n" + tab['x'] + " " + tab['y']);

        //alert(tab['table']);

        //Si on est sur l'onglet "map" de cet hôtel, aucune info à son survol
        if(tab["table"] == "H" && tab["type"]==22 && is_hotel_page(tab['id'])){

        //Sinon, info à son survol
        }else{
            var div_parent = document.createElement("div");
            div_parent.className="map_parent";
            //$(div_parent).css('z-index',10);
            $(div_parent).attr('id','vignette_lieu_' + tab['id']);
            $(div_parent).load(debut_url+"detail_lieu.php?mini=ok&table="+tab['table']+"&id="+tab['id']);
            $(a).mouseover(function(){
                //on affiche le div de manière à ce qu'il tienne dans la carte
                var dir_x = 'droite';
                var dir_y = 'bas';
                if(tab['x'] < x_to_coord(carte_pos_x + map_width/2)){
                    dir_x = 'gauche';
                }
                if(tab['y'] > y_to_coord(carte_pos_y + map_height/2)){
                    dir_y = 'haut';
                }

                $(this).removeClass('haut_droite');
                $(this).removeClass('bas_droite');
                $(this).removeClass('haut_gauche');
                $(this).removeClass('bas_gauche');
                $(this).addClass(dir_y+'_'+dir_x);
                
                //Si c'est un hôtel recherché, on scroll et surbrille cet hôtel dans la liste des résultats de gauche
                if(tab["type"]==22 && tab["table"]=="H" && is_hotel_searched(tab["id"])){
                    $("#liste_result_hotels").scrollTo(".one_result #div_up_" + tab["id"],1);
                    $("#liste_result_hotels .one_result #div_up_" + tab["id"] + " #resume_hotel_"+tab["id"]).addClass('active');
                }
            });
            
            //Si c'est un hôtel recherché, on enlève la surbrillance dans la liste des résultats de gauche
            if(tab["type"]==22 && tab["table"]=="H" && is_hotel_searched(tab["id"])){
                $(a).mouseout(function(){
                    $("#liste_result_hotels .one_result #div_up_" + tab["id"] + " #resume_hotel_"+tab["id"]).removeClass('active');
                });
            }
            
            //au clic, on affiche le div d'infos de gauche en ajax
            $(a).click(function(){
                //setTimeout(function(){
                  $("#iframe_lieu")
                    .find("#contenu_lieu")
                        .html("<img src='jquery/indicator.gif' alt='' />")
                    .end()
                    .slideDown("slow", function(){
                        if(tab["table"] == "H" && is_hotel_searched(tab["id"])){ //hotel recherché
                              $("#iframe_lieu #contenu_lieu").load(debut_url+"detail_lieu.php?rang="+id_search(tab["id"])+"&table="+tab['table']+"&id="+tab['id']);
                        }else{ //hotel ou PR normal
                              $("#iframe_lieu #contenu_lieu").load(debut_url+"detail_lieu.php?table="+tab['table']+"&id="+tab['id']);
                        }
                    });
                //},100);
            });
        }

        var div_top = document.createElement("div");
        div_top.className="map_top lieu_"+tab['table'].toLowerCase()+'_' + tab['id'];
        $(div_top).attr('id','top_lieu_'+tab['table']+'_' + tab['id']);
        $(div_top).css('left',x + "px");
        $(div_top).css('top',y + "px");
        $(a).append(div_parent);
        $(div_top).append(a);
        $("#grande_carte").append(div_top);
    }
    
    //l'id de ce lien
    var idd = 'lieu_'+tab['table']+'_' + tab['id'];
    
    //si on avait précisé un lien initial à faire clignoter
    var idd_clignote_init = $("#idd_clignote_init").val();
    if (idd_clignote_init.length > 0) {
        cli = (idd_clignote_init == idd); //on fait clignoter uniquement ce monument
    }
    
    if(cli){ //si on a demandé son clignotement
        //alert('clignote : lieu_'+tab['table']+'_' + tab['id']);
        clignote(idd);
    }
}

/**
 * Indique si un lieu a déjà été chargé
 */ 
function lieu_deja_charged(id,table){
    for(var i=0;i<tab_lieux.length;i++){
        if(tab_lieux[i]["id"] == id){
            if(tab_lieux[i]["table"] == table){
                return true;
            }
        }
    }
    return false;
}

/**
 * ??
 */ 
function open_thickbox(){
    $("#lien_iframe_thickbox").click();
}
