var MapController = function() {
    var options = { mapTypes: [G_PHYSICAL_MAP] };
    this.map = new google.maps.Map2(document.getElementById("map"), options);
    this.map.setCenter(new GLatLng(35.9669, -113.76), 9);
    this.map.disableDoubleClickZoom();
    this.markers = {};
    this.infoWindow = null;
    this.iwWidth = 200;
    this.iwHeight = 125;
    this.gap = -10;
    this.infoWindowBorder = RUZEE.ShadedBorder.create({ corner:10, shadow:16,  border:2 });
};

MapController.prototype = {
    
    createInfoWindow: function(title, description, uri) {
        var div = jQuery([
            "<div class=\"map-info-window\">",
            "<a href=\"#\"><img src=\"/lib/css/core/jquery/images/888888_11x11_icon_close.gif\" class=\"close-window\" /></a>",
            "<h3><a href=\""+uri+"\">"+title+"</a></h3>",
            "<span>"+description+"</span>",
            "<a href=\""+uri+"\">read more…</a>",
            "</div>"].join("\n"));
        jQuery("a", div).filter(":first").click(function() {
            jQuery(".map-info-window").remove();
        });
        return div;
    },
    
    markerClicked: function(marker) {
        jQuery(".map-info-window").remove();
        
        var iw = this.createInfoWindow(
            this.markers[marker.hashKey].title,
            this.markers[marker.hashKey].description,
            this.markers[marker.hashKey].uri
        );
        
        var position = this.getInfoWindowPosition(marker.getPoint(), marker.getIcon());
        this.infoWindowBorder.render(iw);
        iw.css("position", "absolute");
        iw.css("top", position.y + "px");
        if(position.rightSide)
            iw.css("left", position.x + "px");
        else
            iw.css("right", -position.x + "px");
        iw.css("z-index", 600001);
        iw.css("opacity", 0.9);
        
        this.infoWindow = iw.get(0);
        this.map.getPane(G_MAP_FLOAT_PANE).appendChild(this.infoWindow);
    },
    
    registerMapData: function(data) {
        var bounds = new google.maps.LatLngBounds();
        var icon = new google.maps.Icon(G_DEFAULT_ICON);
        icon.image = "/lib/images/map/star-animated.gif";
        icon.printImage = "/lib/images/map/star-animated.gif";
        icon.mozPrintImage = "/lib/images/map/star-animated.gif";
        icon.shadow = "/lib/images/map/star-transparent.png";
        icon.transparent = "/lib/images/map/star-transparent.png";
        icon.printShadow = null;
        icon.iconSize = new google.maps.Size(35,35);
        icon.shadowSize = new google.maps.Size(35, 35);
        icon.iconAnchor = new google.maps.Point(0,0);
        icon.maxHeight = 35;
        icon.imageMap = null;
        
        if(data["placemarks"])
            for(var i=0; i < data.placemarks.length; i++) {
                var p = data.placemarks[i];
                var c = new google.maps.LatLng(p.lat, p.lon);
                var m = new google.maps.Marker(c, {
                    clickable: true,
                    icon: icon
                });
                m.hashKey = i;
                this.markers[m.hashKey] = {
                    title:          p.title,
                    description:    p.description,
                    uri:            p.uri
                };
                
                google.maps.Event.addListener(m, "click", function() {
                    mapController.markerClicked(this);
                });
                
                bounds.extend(c);
                this.map.addOverlay(m);
            }
        
        this.map.setCenter(bounds.getCenter(), 9);
    },
    
    getInfoWindowPosition: function(point, icon) {
        var gap = this.gap;
        var map = this.map;
        var pt  = point;
        var ttPos = this.map.fromLatLngToDivPixel(pt);
        var theIcon = icon;
        
        ttPos.y -= Math.floor(theIcon.iconAnchor.y/2);
        
        var rightSide = true;
        var bounds = map.getBounds();
        var boundsSpan  = bounds.toSpan();
        var longSpan = boundsSpan.lng();
        var mapWidth = map.getSize().width;
        
        var tooltipWidthInDeg = (this.iwWidth + theIcon.iconSize.width + 6) / mapWidth * longSpan;
        if (pt.lng() + tooltipWidthInDeg > bounds.getNorthEast().lng())
            rightSide = false;
        ttPos.y -= Math.floor(this.iwHeight/2);
        delta = (theIcon.iconSize.width - theIcon.iconAnchor.x) + gap;
        if (rightSide)
            ttPos.x += delta;
        else
            ttPos.x -= delta
        
        var ret = {
            rightSide: rightSide,
            x: ttPos.x,
            y: ttPos.y
        };
        
        return ret;
    }
}

var mapController = null;

google.load("maps", "2");
google.setOnLoadCallback(function() {
    jQuery(document).ready(function() {
        mapController = new MapController();
        if(window["mapData"])
            mapController.registerMapData(mapData);
    });
    
    //jQuery("[src=/lib/images/map/star-animated.gif]").each(
    //    function() { jQuery(this).effect("bounce", { times: Math.random()*20}, Math.floor(Math.random()*3001)) 
    //});
});