(function(){
  
  

FRANCE = 1;
MAGHREB = 2;

 Carte = $.inherit({

    __constructor : function(){
        this.zones = $('#image_carteMap area');
        this.current_elt_over = null;
        this.maroc_btn = $("#to_maroc");
        this.zone = FRANCE;
        this.current_region = null; // region selectionnée
        this.current_mag = null;//magasin selectionné

        this.first_click = true;
        this.tool_tips = null;
        
        this.init(this);
    },

    init:function(obj){
        this.zones.each(function(index, elt){
            $(this).mouseenter(function(evt){
                if(obj.zone == MAGHREB && !$(this).hasClass('zone_maghreb')){ // si on est en zone maghreb et qu'on survole la france, on ne fait rien (IE)
                    return;
                }
                var elt_name = $(this).attr("id").replace("map", "over");
                obj.displayOver(elt_name, obj);
            });

            $(this).mouseleave(function(evt){
                evt.preventDefault();
                obj.removeOver(obj);
            });

            $(this).click(function(evt){
                evt.preventDefault();
                evt.stopPropagation();
            });
        });

        $(".img_over").each(function(index, elt){
            $(this).mouseenter(function(evt){
                $(this).css("display", "block");
                obj.tool_tips = new RegionToolTips("rhône-alpes", $(this), new Object({
                    x:evt.pageX, 
                    y:evt.pageY
                    }) );
            });

            $(this).mouseleave(function(){
                $(this).css("display", "none");
                obj.removeToolTips(obj);
            });

            $(this).click(function(evt){
                evt.preventDefault();
                evt.stopPropagation();

                if(obj.first_click){
                    if(obj.zone == MAGHREB){
                        $("#to_france_zone").css({
                            "width": "200px"
                        });
                    }
                    $("#block_carte").animate({
                        "width" : "475px"
                    });
                }
                obj.first_click = false;
         
                var region_id = $(this).attr("alt");
                obj.getRegion(region_id, obj);
                obj.addSelectToRegion($(this), obj);
            });
        });

        $('#btn_maroc').click(function(evt){
            obj.moveToMaroc(obj);
        });

        $("#to_france_zone").click(function(evt){
            evt.stopPropagation();
            evt.preventDefault();
            obj.moveToFrance(obj);
        });

        $("#btn_france").click(function(evt){
            evt.stopPropagation();
            evt.preventDefault();
            obj.moveToFrance(obj);
        });


        if($("#map_magasin_listing").children().length){
          obj.initListingMagasinListeners(obj);
          if($(".map_magasin_listing_content li.select").length){
            var elt = $(".map_magasin_listing_content li.select").first();
            obj.addSelectToMagasin(elt, obj);
            
            this.visionneuse = new MagasinVisionneuse(".map_magasin_image", 3000);
            antispam.init(".map_magasin_desc");
          }
          
          var selectedRegionId = $("#map_magasin_listing").data("selected-region");
          var selectedRegionElt = $('#' + selectedRegionId +"_over");
          if($("#is_maroc").length){
            obj.moveToMaroc(obj, false, {callback:obj.addSelectToRegion, args:selectedRegionElt});
          }else{
            this.addSelectToRegion(selectedRegionElt, obj);
          }
          
         $('.scroll-pane').jScrollPane();
        }
    },


    /**************************************
 *                                    *
 *    switch entre france et magreb   *
 *                                    *
 *************************************/
    moveToMaroc:function(obj){
      var open_map = (arguments.length > 1)?false:false;
      
      if(arguments.length == 3){ // on arrive directement sur une mapge maroc/algerie
        var callback = arguments[2].callback;
        var args = arguments[2].args;
        var extented_area = false;
      }else{
        callback = null;
        extented_area = true;
      }
      
        $('#btn_maroc').css("display", "none");
        obj.removeSelectToRegion(obj);
        obj.zone = MAGHREB;

        $("#block_carte .map_titre h3").text("Algérie et Maroc");
        $("#image_carte").animate({
            "left" :"-54px", 
            "top" : "-276px"
        }, function(){
            obj.first_click = true;
            if(open_map){
              $("#block_carte").animate({
                  "width" : "575px"
              });
            }
            obj.initToFranceArea(obj, extented_area);
            if(callback){
              callback(args, obj);
            }
        });
    },



    initToFranceArea:function(obj){
      if(arguments.length == 2 && arguments[1] == false){
        var france_zone_w = "200px";
      }else{
        france_zone_w = "300px";
      }

      $("#to_france_zone").css({
          "display": "block", 
          "width":france_zone_w
      });
      
      $("#btn_france").css({
          "display": "block"
      });
      
      $("#to_france_zone").mouseover(function(evt){
          $(this).css("cursor","pointer");
      });
   },

    moveToFrance:function(obj){
        obj.removeSelectToRegion(obj);
        /// $("#to_france_zone").unbind("click");
        $("#block_carte .map_titre h3").text("France ");
        $("#btn_france").css({
            "display": "none"
        });

        $("#image_carte").animate({
            "left" :"-232px", 
            "top" : "-1px"
        }, function(){
            $('#btn_maroc').css("display", "block");
            $("#to_france_zone").css({
                "display": "none"
            });
            $("#block_carte").animate({
                "width" : "575px"
            });
            obj.first_click = true;
            obj.zone =FRANCE;
        });
    },


    /**************************************
*                                     *
*   récupération de la regions        *
*                                     *
**************************************/

    addSelectToRegion:function(elt, obj){
        if(obj.current_region){
            obj.removeSelectToRegion(obj);
        }

        elt.css("display","none");
        var id = elt.attr("id").replace("over", "select");
        obj.current_region = $("#"+id);
        obj.current_region.css("display", "block");
    },

    removeSelectToRegion:function(obj){
        if(obj.current_region){
            obj.current_region.css("display", "none");
            $("#map_magasin").children().remove();
            $("#map_magasin_listing").children().remove();
        }
    },


    displayOver:function(elt_name, obj){
        if(obj.current_elt_over){
            obj.current_elt_over.css("display", "none");
        }
        obj.current_elt_over = $("#"+elt_name);
        obj.current_elt_over.css("display", "block");
    },

    removeOver:function(obj){
        if(obj.current_elt_over){
            obj.current_elt_over.css("display", "none");
        }
        obj.current_elt_over = null;
    },


    /*
   * ajax
   * récupération de la region
   */
    getRegion:function(region_id, obj,callback){
        var url = ROOT_URL+"contenu.php";
        var datas = new Array();
        datas.push("region_id="+region_id.toString());
        datas.push("action=get_magasin_by_region");

        if($("#map_magasin").children().length){
          $("#map_magasin").children().remove();
        }

        $("#map_magasin_listing").children().remove();
        $("#map_magasin_listing").append($('<div class="ajax-loader">'));

        $.ajax({
            data:datas.join('&'),
            type:"POST",
            url:url,
            dataType:"html",
            success:function(responseHTML){
              
                $("#map_magasin_listing").children().remove();
                $("#map_magasin_listing").append(responseHTML);
                $('.scroll-pane').jScrollPane();
                
                obj.initListingMagasinListeners(obj);
                if(typeof(callback)=='function'){
                    callback.call();
                }
            },
            error:function(){
            // alert("error");
            }
        });
    },



    /********************************
 *                              *
 *    récupération du magasin   *
 *                              *
 *******************************/

    /*
 * initialisation des liens dans le lsiting magasin
 */
    initListingMagasinListeners:function(obj){
        $(".map_magasin_listing_content ul li").each(function(index, elt){
            if($(this).hasClass('no_store')){
                return;
            }
            //     $(this).css({"background-color" : "#ffffff", "cursor":"default"});
            obj.addListingMagasinListener($(this), obj);
        });
    },

    addListingMagasinListener:function(elt, obj){
        elt.click(function(evt){
            evt.preventDefault();
            obj.addSelectToMagasin(elt, obj);

            var mag_id = $(this).attr("id").match('[0-9]+');
            obj.getMagasin(mag_id);
        });

        elt.mouseenter(function(evt){
            elt.css({
                "background-color" : "#E5E5E5", 
                "cursor":"pointer"
            });
        })

        elt.mouseleave(function(evt){
            elt.css({
                "background-color" : "transparent", 
                "cursor":"default"
            });
        })
    },

    /*
 *  ajout du select sur le magasin selectionné
 */
    addSelectToMagasin:function(elt, obj){
        if(obj.current_mag){
            obj.removeSelectMagasin(obj);
        }
        elt.unbind("mouseleave");
        elt.unbind("mouseenter");
        elt.css({
            "background-color" : "#333333", 
            "cursor":"default"
        });
        elt.addClass("select");
        obj.current_mag = elt;
    },


    removeSelectMagasin:function(obj){
        obj.current_mag.removeClass("select");
        obj.current_mag.css({
            "background-color" : "#ffffff"
        });
        obj.addListingMagasinListener(obj.current_mag, obj);
    },



    /*
 * récupération du magasin
 */
    getMagasin:function(mag_id){
        var url = ROOT_URL+"contenu.php";
        var datas = new Array();
        datas.push("magasin_id="+mag_id);
        datas.push("action=get_magasin");

        $("#map_magasin").children().remove();
        $("#map_magasin").append($('<div class="ajax-loader">'));

        $.ajax({
            type:"POST",
            url:url,
            data:datas.join('&'),
            dataType:"html",
            success:function(responseHTML){
                $("#map_magasin").children().remove();
                $("#map_magasin").append(responseHTML);
                this.visionneuse = new MagasinVisionneuse(".map_magasin_image", 3000);
                antispam.init(".map_magasin_desc");
            },
            error:function(){
            //  alert("error");
            }
        });
    },



   /***************
   *              *
   *  tools tips  *
   ***************/
    removeToolTips:function(obj){
        obj.tool_tips.close(obj.tool_tips);
    }
  
});






/************************************
 *                                  *
 *  tool tip de survol de région    *
 *                                  *
 ***********************************/

RegionToolTips =  $.inherit({

    __constructor : function(nom, zone, coords){
        this.initRegions();
    
        this.nom = nom;
        this.zone = $(zone);
        this.createToolTip(this.zone);

        this.offset = new Object({
            x: parseInt(this.container.offset().left, 10), 
            y: parseInt(this.container.offset().top,10 )
            });
        this.elt.css({
            top:coords.y-this.offset.y-34, 
            left:coords.x-this.offset.x-20
            });

        this.open(this);
    },

    createToolTip:function(zone){
        var cle = zone.attr("id").replace('_over', '');
        var i = 0;
        var found = false;
    
        for( i=0; i < this.tab_regions.length; i++){
            if(this.tab_regions[i].id == cle){
                found = true;
                break;
            }
        }

        if(found){
            this.elt = $('<div id="region_tooltip"><span class="region_tooltip_content">'+this.tab_regions[i].libelle+'</span></div>');
            this.container = $("#map_container_carte");
            this.container.append(this.elt);
        }
    },

    open:function(obj){
        obj.elt.css("display", "block");
        var w = parseInt(this.elt.children().first().text().length, 10)*7.6;
        obj.elt.animate({
            width : w
        });
        obj.initListener(obj);
    },

    close:function(obj){
        obj.zone.unbind("mousemove");
        obj.elt.remove();
    },

    initListener:function(obj){
        obj.zone.mousemove(function(evt){
            obj.updateCoord(new Object({
                x:parseInt(evt.pageX,10), 
                y:parseInt(evt.pageY,10)
                }), obj);
        });
    },

    updateCoord:function(coords, obj){
        var x = coords.x - obj.offset.x - 20;

        if((x + parseInt(obj.elt.css("width"),10)+20) > parseInt(obj.container.css("width"), 10)){
            x = parseInt(obj.container.css("width"), 10) - parseInt(obj.elt.css("width"),10)-20;
        }
        var y = coords.y - obj.offset.y - 30;

        obj.elt.css({
            top:y, 
            left:x
        });
    },

    initRegions:function(){
        this.tab_regions = TAB_REGIONS;
    }


});


})();
