function Yangsam() {
    this.initialize();
}

Yangsam.prototype.initialize = function() {
    this.itor = 0;
    this.yshop = {};
    this.marker;
    
    if(GBrowserIsCompatible()) {
        this.map = new GMap2(document.getElementById('map'));
        this.map.setMapType(G_NORMAL_MAP);
        this.map.removeMapType(G_SATELLITE_MAP);
        this.map.removeMapType(G_HYBRID_MAP);
        this.map.setCenter(new GLatLng(23.137242407348136, 113.28623056411743), 7);
        
        var control = new GMapTypeControl();
        this.map.addControl(control);
        this.map.addControl(new GSmallZoomControl3D());
        
        this.fetchData();
        
        var that = this;
        $('#help').click(function() {
        	var top = (that.map.getSize().height * 0.5) - ($('#help-box').height() * 0.6);
        	var left = (that.map.getSize().width/2) - 175;
        	$('#help-box').css({
        		'position': 'absolute',
        		'top': top,
        		'left': left
        	}).fadeIn("slow");
        });
        
        $('#help-box a').click(function() {
        	$('#help-box').fadeOut("slow");
        });
    }
};

Yangsam.prototype.fetchData = function() {
    var that = this;
    $.ajax({ url: '/manage/ajax/maps?' + (new Date).getTime(), type: 'GET', timeout: 2500, dataType: "json",
        success : function(yshop) {
    		
            if(yshop && !yshop['exception'] && yshop.siteUrl != that.yshop.siteUrl && yshop.location != that.yshop.location) {
                that.yshop = $.extend(true, {}, yshop);
                
                if (yshop.location[0] == 0) {
                	yshop.location[0] = 23.129075;
                }
                if (yshop.location[1] == 0) {
                	yshop.location[1] = 113.2644227;
                }
                var point = new GLatLng(yshop.location[0], yshop.location[1]);
                that.map.panToInclude(point, 85, 85, 145, 85);
                var bubble = new YBubble(point, (yshop.newSite ? '<span class="bubbleTips">最新加盟:</span>':'<span class="bubbleTips">加盟店:</span>')+yshop.title, yshop.siteUrl);
                that.map.addOverlay(bubble);

                var remove = (function(marker) { return function() { marker.remove(); }; }(bubble)); 
                setTimeout(remove, 35000);
            }
                        
            var check = (function(that) { return function() { that.fetchData(); }; }(that)); 
            setTimeout(check, 4250);
        },
        
        error : function() {
            that.fetchData();
        }
    });
};


GMap2.prototype.panToInclude = function(point, top, right, bottom, left) {
    point = this.fromLatLngToContainerPixel(point);
    var mapDimensions = this.getSize();
    var horizontal = 0;
    var vertical = 0;
    
    if(point.x - left < 0) 
        horizontal = Math.abs(point.x - left);
    
    if(point.x + right > mapDimensions.width)
        horizontal =  mapDimensions.width - (point.x + right);
    
    if(point.y - top < 0)
        vertical = Math.abs(point.y - top);
        
    if(point.y + bottom > mapDimensions.height)
        vertical = mapDimensions.height - (point.y + bottom);
        
    if(horizontal || vertical) {
        this.panBy(new GSize(horizontal, vertical));
    }
};

function YBubble (latLng, title, siteUrl) {
    this.itemLatLng = latLng;
    this.title = title;
    this.siteUrl = siteUrl;
    this.prototype = new GOverlay();
    
    this.initialize = function(map) {
        var item = $('<a href="' + this.siteUrl + '" target="_new">'+this.title+'</a>');
		
		if($('#queue a').length == 10) {
			var last = $('#queue a:first');
			last.remove();
			last = null;
		}
        
        var notch = $('<img src="/images/maps/notch.png" alt="" class="notch" />');
        var div = $('<div />');
        div.css({
            background: '#fff',
            border: '2px solid #669',
            padding: 10
        }).append(item);
        
        var out = $('<div class="bubble" />').css({
            position : 'absolute',
            display: 'none'
        });
        out.append(div).append(notch).appendTo(map.getPane(G_MAP_FLOAT_PANE));
        
        this.div = out;
        this.map = map;
        
        this.redraw(true);
    };
    
    this.redraw = function(force) {
        if(!force) return;
        var point = this.map.fromLatLngToDivPixel(this.itemLatLng);
        this.div.css({
            left : point.x - 50,
            top : point.y - 25,
            display: 'block',
            opacity: 0
        }).animate({
            top: point.y - 5,
            opacity: 1
        }, 500);
    };
    
    this.remove = function() {
        var that = this;
        var position = this.div.position();
        
        $(this.div).animate({
            top: position.top + 20,
            opacity: 0
        }, 500, function() {
            that.div.remove();
            that.div = null;
        });
    };
};


$(document).ready(function(){
    new Yangsam();
});

$(window).unload(function() { GUnload(); });
