var Aq_Map = Class.create({
    stores : null,
    targetId : null,
    map : null,
    center : null,
    contentCanvas : null,
    currentPageURL : null,
    panelURL : null,
    
    init:function(_center, _stores, _targetId, _contentCanvas, store, _currentPageURL, _panelURL){
        this.panelURL = _panelURL;
        this.currentPageURL = _currentPageURL;
        this.targetId = _targetId;
        this.stores = _stores;
        this.center = new GLatLng(_center.latitude, _center.longitude);
        this.contentCanvas = _contentCanvas;
        var _this = this;
        
        var map = new GMap2(document.getElementById("map_canvas"));
        map.addControl(new GLargeMapControl());
        map.setCenter(this.center,5);
        this.map = map;
                
        var icon = new GIcon(G_DEFAULT_ICON);
        icon.image = Const.SITE_URL+'public/gfx/pictos/marker.png';
        icon.iconSize = new GSize(39,39);
        icon.iconAnchor = new GPoint(19, 19);    
        icon.infoWindowAnchor = new GPoint(19,19); 
        icon.imageMap = [0,0, 39,0, 39,39, 0,39];    
        
        $(stores).each(function(id, val){
            var  marker = new GMarker( new GLatLng(val['latitude'], val['longitude']), {icon:icon});

            GEvent.addListener(marker, 'mouseover', function(latlng){
                
            	var html = _this.getPopupHtml(val);
            	
            	if (html)
            	{
            	    marker.openExtInfoWindow(
	                    map,
	                    "aq_map",
	                    html,
	                    {beakOffset: 3}
	                ); 
	                Cufon.refresh();
            	}
            });
            marker.url2go=Const.SITE_URL + _currentPageURL+'?id=' + val.id;
            GEvent.addListener(marker, 'click', function(){
                /*
            	var center = new GLatLng(
            	    marker.getPoint().lat(),
            	    marker.getPoint().lng() + 0.05);
                map.setCenter(center, 12);
            	var html = _this.getPopupHtml(val);
            	if (html)
            	{
            	    marker.openExtInfoWindow(
	                    map,
	                    "aq_map",
	                    html,
	                    {beakOffset: 3}
	                ); 
            	}
            	_this.loadPanel(val.id);
            	*/
            	window.location.href=marker.url2go;
            });
            
            if (val.id == store) {
            	var center = new GLatLng(
            			marker.getPoint().lat(),
            			marker.getPoint().lng() + 0.05);
                map.setCenter(center, 12);
            	var html = _this.getPopupHtml(val);
            	if (html)
            	{
	                marker.openExtInfoWindow(
	                    map,
	                    "aq_map",
	                    html,
	                    {beakOffset: 3}
	                ); 
            	}
            	_this.loadPanel(val.id);
            }
            
            map.addOverlay(marker);
        });
    },
    loadPanel:function(id)
    {
		$.ajax({
			  url: this.panelURL,
			  data:"id=" + id,
			  success: function(html){
				$("#reseau").html(html);
				$("#reseau").show();
				$("#contactreseau").show();
                Cufon.refresh();
			  }
		});
    },
    getPopupHtml:function(val)
    {
        $('div#'+this.contentCanvas+' > * > p.mapName').html(val.name);
        $('div#'+this.contentCanvas+' > * > p.mapCity').html(val.city?val.city:".");
        
        $('a.mapLinkA').html('Voir le contact');
        $('a.mapLinkA').attr('href', Const.SITE_URL + this.currentPageURL+'?id=' + val.id);
        
        var result  = $('#'+this.contentCanvas).html();
        
        return result;
    }    	
});
