﻿var googlemap = Class.create({
    initialize: function() {
        if (GBrowserIsCompatible()) {
            this.map = new GMap2(document.getElementById("mapDiv"));
            this.map.setMapType(G_SATELLITE_MAP);
            this.map.setCenter(new GLatLng(20, -10), 1);
            this.map.addControl( new GLargeMapControl() );
            if(markers.status != "fail")
                this.loadMarkers(markers.rows);
        }
    },
    loadMarkers: function(json){
        var bounds = new GLatLngBounds();
        var markerArray = [];
        json.each( function(marker){
            var point = new GLatLng(parseFloat(marker.Lat), parseFloat(marker.Long));
            var mLabel = ( marker.City == "" ? marker.Country : marker.City);         
            var newIcon = MapIconMaker.createMarkerIcon({width: 25, height: 25, primaryColor: "#B00"});
            var pushpin = new GMarker(point, {icon: newIcon});
                
            var tooltip = new Tooltip(pushpin,mLabel,4);
            pushpin.tooltip = tooltip;
            this.map.addOverlay(tooltip);
            GEvent.addListener(pushpin,'mouseover',function(){
	            this.tooltip.show();
            });
            GEvent.addListener(pushpin,'mouseout',function(){
	            this.tooltip.hide();
            });  
            GEvent.addListener(pushpin,'click',function(){
	            this.handleCountryClick(marker);
            }.bind(this));         
                
            bounds.extend(point);
            this.addToList(marker);
            this.map.addOverlay(pushpin);
            //markerArray.push(pushpin);
        }.bind(this));
        var zoom = this.map.getBoundsZoomLevel(bounds);
        if(zoom > 4)
            zoom = 4;
        else
            zoom = 1;
        this.map.setZoom(zoom);
        this.map.setCenter(bounds.getCenter());
        this.map.checkResize();
    //var mcOptions = {gridSize: 10};
    //var markerCluster = new MarkerClusterer(this.map, markerArray, mcOptions);
    },  
    handleCountryClick: function(data){
        var Country = data.Country;
        var City = data.City;
        var AId = data.AId;
        var page = "articles.aspx?AId=" + AId  + "&Country=" + Country + "&City=" + City;
        document.location.href = page;
    },
    addToList: function(marker){
        var Country = marker.Country;
        var City = marker.City;
        var AId = marker.AId;
        var list = $('alphaArticles');
        if(City != "")
            aLabel = Country + ' <span class="cityLabel">' + City + '</span>';
        else
            aLabel = Country;
        list.insert("<li><a href='articles.aspx?AId=" + AId + "&Country=" + Country + "&City=" + City + "'>" + aLabel + "</a></li>");
    }
});
