function isIE6 (  ) {
    return (jQuery.browser.msie && jQuery.browser.version == '6.0');
}

// Shop Marker
function ShopMarker ( s ) {
    this.shop = s;
    this.loc = new GLatLng(s.lat, s.lng);
}

(function (  ) {
    ShopMarker.bgImage = photoServer + '/images/v2/map/map_point_02.png',
    ShopMarker.shadowImage = 'http://www.google.com/mapfiles/shadow50.png';
    ShopMarker.iconOffsets = { 0: 1, 6: 2, 7: 3, 8: 4, 9: 5, 10: 6, 14: 1, 21: 8, 20: 9 };
    ShopMarker.prototype = new GOverlay();
    if( isIE6() ) {
        ShopMarker.prototype.__fillDiv = function ( d, x, y ) {
        var i = document.createElement('div');
            i.style.filter      = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + ShopMarker.bgImage + "', sizingMethod='crop')";
            i.style.position    = 'relative';
            i.style.top         = y + 'px';
            i.style.left        = x + 'px';
            i.style.width       = '46px';
            i.style.height      = '408px';
            d.style.overflow    = 'hidden';
            d.appendChild(i);
        };
        ShopMarker.prototype.__createShadowDom = function (  ) {
        var s = document.createElement('div');
            s.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + ShopMarker.shadowImage + "', sizingMethod='crop')";
            return s;
        };
        ShopMarker.prototype.__setSpriteOffset = function ( offset ) {
            this.div.getElementsByTagName('div')[0].style.left = offset + 'px';
        };
    } else {
        ShopMarker.prototype.__fillDiv = function ( d, x, y ) {
            d.style.background  = "url('" + ShopMarker.bgImage + "') no-repeat " + x + 'px ' + y + 'px';
        };
        ShopMarker.prototype.__createShadowDom = function (  ) {
        var s = document.createElement('img');
            s.src = ShopMarker.shadowImage;
            return s;
        };
        ShopMarker.prototype.__setSpriteOffset = function ( offset ) {
            this.div.style.background = "url('" + ShopMarker.bgImage + "') no-repeat " + offset + 'px ' + this.spriteY + 'px';
        };
    }

    ShopMarker.prototype.__createDiv = function (  ) {
    var s = this.shop, d = document.createElement('div');
    var x = 0, y = ShopMarker.iconOffsets[s.clsid] * -34, zIndex = 200;
        if( s.vip == 1 ) {
            x = -23;
            zIndex = 300;
        }
        d.style.width       = '23px';
        d.style.height      = '34px';
        d.style.cursor      = 'pointer';
        d.style.position    = 'absolute';
        d.style.zIndex      = zIndex;
        this.__fillDiv(d, x, y);
        d.marker = this;
    var title = s.name;
        if( s.dvs )
            title += ' (' + s.dvs + ')';
        d.setAttribute('title', title);
        GEvent.addDomListener(
                d, 'click',
                function (  ) { GEvent.trigger(this.marker, 'click'); }
            );
        this.map.getPane(G_MAP_MARKER_PANE).appendChild(d);
        this.spriteY = y;
        return d;
    }

    ShopMarker.prototype.__createShadow = function (  ) {
    var s = this.__createShadowDom();
        s.style.width       = '37px';
        s.style.height      = '34px';
        s.style.position    = 'absolute';
        s.style.border      = '0px none';
        this.map.getPane(G_MAP_MARKER_SHADOW_PANE).appendChild(s);
        return s;
    }

    ShopMarker.prototype.initialize = function ( m ) {
        this.map    = m;
        this.div    = this.__createDiv();
        this.shadow = this.__createShadow();
    }

    ShopMarker.prototype.remove = function (  ) {
        this.div.parentNode.removeChild(this.div);
        this.shadow.parentNode.removeChild(this.shadow);
    } 
    ShopMarker.prototype.copy = function (  ) {
        return new ShopMarker(this.shop);
    }

    ShopMarker.prototype.redraw = function ( force ) {
        if( !force )
            return ;
    var p = this.map.fromLatLngToDivPixel(this.loc);
    var y = p.y + 'px';
    var d = this.div, s = this.shadow;
        d.style.left = p.x + 'px';
        s.style.left = (p.x + 2) + 'px';
        d.style.top  = s.style.top  = y;
    }

    ShopMarker.prototype.select = function ( selected ) {
    var offset;
        if( selected == undefined || selected == true ) {
            offset = -46;
            this.oldZIndex = this.div.style.zIndex;
            this.div.style.zIndex = 550;
        } else {
            offset = (this.shop.vip ? 1 : 0) * -23;
            this.div.style.zIndex = this.oldZIndex;
        }
        this.__setSpriteOffset(offset);
    }

    ShopMarker.prototype.getZIndex = function (  ) {
        return this.div.style.zIndex;
    }

    ShopMarker.prototype.getPoint = function (  ) {
        return this.loc;
    }
})();

var customMap = {
    map:    null,
    onMove: null,
    loading:false,
    shops:  null,
    current:0,

    __onMove_handler: function (  ) {
    var h = customMap.onMove, m = customMap.map;
        if( h && h.call )
            h(m.getCenter(), m.getZoom());
    },

    init:   function ( dom, defLat, defLng, defZoom ) {
    var defLoc = new GLatLng(defLat, defLng),
        dom = jQuery(dom), map = new GMap2(dom[0]);
        map.setCenter(defLoc, defZoom);
        map.setUIToDefault();
        map.disableScrollWheelZoom();
        GEvent.addListener(map, 'moveend', this.__onMove_handler);
        GEvent.addListener(map, 'zoomend', this.__onMove_handler);
        this.map = map;
    },
    gc:     function (  ) {
        GUnload();
    },
    __marker_onClick_handler: function (  ) {
    var shop = this.shop;
        customMap.map.closeInfoWindow();
        customMap.map.openInfoWindowHtml(
                this.loc,
                '店名：' + shop.name + '<br />' +
                '地址：' + shop.addr
            );
    },
    __asyncLoadShops: function (  ) {
    var shops = customMap.shops,
        stop = Math.min(customMap.current + 20, shops.length);
        for( i = customMap.current; i < stop; i++ ) {
        var shop = shops[i], marker = new ShopMarker(shop);
            customMap.map.addOverlay(marker);
            GEvent.addListener(marker, 'click', this.__marker_onClick_handler);
        }
        customMap.current = stop;
        if( stop < shops.length )
            setTimeout('customMap.__asyncLoadShops();', 400);
        else
            customMap.loading = false;
    },
    loadArray: function ( shops ) {
        if( this.loading )
            return;
        customMap.map.clearOverlays();
        customMap.shops = shops;
        customMap.loading = true;
        customMap.current = 0;
        customMap.__asyncLoadShops();
    },
    load:   function ( loc, data ) {
        jQuery.getJSON(loc, data, this.loadArray);
    }
};

(function ( $ ) {
function onReady (  ) {
    customMap.init(map_config.container, map_config.defLat, map_config.defLng, map_config.defZoom);
    $(window).unload(customMap.gc);
    if( map_config.request )
        customMap.load(map_config.request, {});
}
$(document).ready(onReady);
})(jQuery);
