/*
    Written by Björn Olsson
    Sublime Consulting AB
    2007
*/

var SI_CUSTOM_INFO_WINDOW_PREFIX = 
"<div class=\"googleInfoWindowWrapper\">" +
    "<div class=\"gContent\">" +
        "<div class=\"gPadding\">" + 
            "<div class=\"googleInfoWindowContent\">";

var SI_CUSTOM_INFO_WINDOW_SUFFIX =
            "</div>" +
        "</div>" +
    "</div>" +
    "<div>" +
        "<div class=\"gArrow\"><img src=\"" + mapSchemePath + "GoogleInfoWindowArrow.png\" /></div>" +
    "</div>" +
"</div>";

var SI_CUSTOM_INFO_WINDOW_SUFFIX_IE =
            "</div>" +
        "</div>" +
    "</div>" +
    "<div>" +
        "<div class=\"gArrowIE\"></div>" +
    "</div>" +
"</div>";

var SI_ICON_BROWN = null; //Default brown arrow icon
var is_ie6 = false; //Indicates whether the current browser is IE6 or below or not
var is_safari = false; //Indicates whether the current browser is Safari or not
var googleMap = null; //The google map instance
var googleMarkerManager = null; //The google marker manager instance
var markers = null;
var pendingMarker = null;

function loadGoogleMap()
{
    if (GBrowserIsCompatible())
    {
        //Check browsers
        is_ie6 = isIE6();
        is_safari = isSafari();
    
        //Setup map
        googleMap = new GMap2(document.getElementById("googleMap"));
        
        //Add custom map control (zoom in, zoom out, pan, reset)
        if(disableMapNavigationControl == false)
            googleMap.addControl(new CustomMapControl());
        //Add custom map control (switch views for map, satellite and hybrid)
        if(disableMapTypeControl == false)
            googleMap.addControl(new CustomMapTypeControl());
        //Add standard overview control (Small overview window in the bottom right corner)
        if(disableMapOverviewControl == false)
            googleMap.addControl(new GOverviewMapControl());

        //Set the default position on the map
        googleMap.setCenter(new GLatLng(default_lat, default_lng), default_zoom);

        //Create the custom brown marker icon
        SI_ICON_BROWN = createBrownArrowIcon();

        if(!is_safari)
        {   //Hide default info window for all browsers except Safari
            googleMap.getPane(G_MAP_FLOAT_SHADOW_PANE).style.display = 'none';
            googleMap.getPane(G_MAP_FLOAT_PANE).style.visibility = 'hidden';
        }
            
        if(is_ie6) //IE6 and lower can't display transparent png's correctly
            SI_CUSTOM_INFO_WINDOW_SUFFIX = SI_CUSTOM_INFO_WINDOW_SUFFIX_IE;
            
        //Create the markers
        createMarkers();
    }
}

function triggerMarkerClick(marker, zoomLevel)
{
    //The marker must be visible before we can trigger the click event, because the MarkerManager is used
    if(zoomLevel)
        googleMap.setCenter(marker.getPoint(), zoomLevel);   
    else
        googleMap.setCenter(marker.getPoint());
        
    pendingMarker = marker;
    
    googleMap.savePosition();
    
    //We can't trigger the click event directly
    window.setTimeout("triggerPendingMarker()", 0);
}

function triggerMarkerClickAtPoint(point, zoomLevel)
{
    var marker;
    var testPoint;

    for(var i = 0; i < markers.length; i++)
    {
        for(var j = 0; j < markers[i].length; j++)
        {
            marker = eval(markers[i][j]);
            testPoint = marker.getPoint();
            
            if(testPoint.lat() == point.lat() && testPoint.lng() == point.lng())
            {
                if(zoomLevel)
                    triggerMarkerClick(marker, zoomLevel);
                else
                    triggerMarkerClick(marker);
                
                return;
            }
        }
    }
}
    
function triggerMarkerClickById(id, zoom)
{
    for(var i = 0; i < markers.length; i++)
    {
        for(var j = 0; j < markers[i].length; j++)
        {
            marker = eval(markers[i][j]);
            
            if(marker.id==id)
            {
                if(zoom)
                    triggerMarkerClick(marker, marker.zoom);
                else
                    triggerMarkerClick(marker);
                return;
            }
        }
    }
}

function triggerPendingMarker()
{
    GEvent.trigger(pendingMarker, "click");
}

function createMarker(id, zoom, point, content, jscommand)
{
    var marker = new GMarker(point, SI_ICON_BROWN);
    marker.id = id;
    marker.zoom = zoom;
    GEvent.addListener(marker, "click", function()
    {
        if(content && content.length > 0)
        {   //Open info window when the marker is clicked
            if(!is_safari)
                marker.openInfoWindowHtml(SI_CUSTOM_INFO_WINDOW_PREFIX + content + SI_CUSTOM_INFO_WINDOW_SUFFIX);
            else
                marker.openInfoWindowHtml(content); //Use standard info window in Safari
        }
        
        //Run custom js when the marker is clicked
        if(jscommand && jscommand.length > 0)
            eval(jscommand);
    });
    
    return marker;
}

function createBrownArrowIcon()
{
    var newIcon = new GIcon();

    newIcon.image = mapSchemePath + "GoogleBrownArrowMarker.png";
    newIcon.iconSize = new GSize(26,30);
    newIcon.shadow = mapSchemePath + "GoogleBrownArrowMarkerShadow.png";
    newIcon.shadowSize = new GSize(56,49);
    newIcon.transparent = mapSchemePath + "GoogleBrownArrowMarkerTransp.png";
    newIcon.iconAnchor = new GPoint(12,30);
    
    if(!is_safari)
        newIcon.infoWindowAnchor = new GPoint(114,37);
    else
        newIcon.infoWindowAnchor = new GPoint(20, 10);
    
    return newIcon;
}

function CustomMapControl() 
{
    
}

CustomMapControl.prototype = new GControl();

CustomMapControl.prototype.initialize = function(googleMap)
{
    var container = document.createElement("div");
    container.className = "googleCustomMapControl";
    
    var panUpDiv = document.createElement("div");
    panUpDiv.className = "googlePanUp";
    container.appendChild(panUpDiv);
    
    GEvent.addDomListener(panUpDiv, "click", function() {
        googleMap.panDirection(0,+1);
    });
    
    GEvent.addDomListener(panUpDiv, "mouseover", function() {
        panUpDiv.className = "googlePanUpHover";
    });
    
    GEvent.addDomListener(panUpDiv, "mouseout", function() {
        panUpDiv.className = "googlePanUp";
    });
    
    var panLeftDiv = document.createElement("div");
    panLeftDiv.className = "googlePanLeft";
    container.appendChild(panLeftDiv);
    
    GEvent.addDomListener(panLeftDiv, "click", function() {
        googleMap.panDirection(+1,0);
    });
    
    GEvent.addDomListener(panLeftDiv, "mouseover", function() {
        panLeftDiv.className = "googlePanLeftHover";
    });
    
    GEvent.addDomListener(panLeftDiv, "mouseout", function() {
        panLeftDiv.className = "googlePanLeft";
    });
    
    var panResetDiv = document.createElement("div");
    panResetDiv.className = "googlePanReset";
    container.appendChild(panResetDiv);
    
    GEvent.addDomListener(panResetDiv, "click", function() {
        googleMap.returnToSavedPosition();
    });
    
    GEvent.addDomListener(panResetDiv, "mouseover", function() {
        panResetDiv.className = "googlePanResetHover";
    });
    
    GEvent.addDomListener(panResetDiv, "mouseout", function() {
        panResetDiv.className = "googlePanReset";
    });
    
    var panRightDiv = document.createElement("div");
    panRightDiv.className = "googlePanRight";
    container.appendChild(panRightDiv);
    
    GEvent.addDomListener(panRightDiv, "click", function() {
        googleMap.panDirection(-1,0);
    });
    
    GEvent.addDomListener(panRightDiv, "mouseover", function() {
        panRightDiv.className = "googlePanRightHover";
    });
    
    GEvent.addDomListener(panRightDiv, "mouseout", function() {
        panRightDiv.className = "googlePanRight";
    });
    
    var clearDiv = document.createElement("div");
    clearDiv.className = "googleClear";
    container.appendChild(clearDiv);
    
    var panDownDiv = document.createElement("div");
    panDownDiv.className = "googlePanDown";
    container.appendChild(panDownDiv);
    
    GEvent.addDomListener(panDownDiv, "click", function() {
        googleMap.panDirection(0,-1);
    });
    
    GEvent.addDomListener(panDownDiv, "mouseover", function() {
        panDownDiv.className = "googlePanDownHover";
    });
    
    GEvent.addDomListener(panDownDiv, "mouseout", function() {
        panDownDiv.className = "googlePanDown";
    });

    var zoomInDiv = document.createElement("div");
    zoomInDiv.className = "googleZoomIn";
    container.appendChild(zoomInDiv);
    
    GEvent.addDomListener(zoomInDiv, "click", function() {
        googleMap.zoomIn();
    });
    
    GEvent.addDomListener(zoomInDiv, "mouseover", function() {
        zoomInDiv.className = "googleZoomInHover";
    });
    
    GEvent.addDomListener(zoomInDiv, "mouseout", function() {
        zoomInDiv.className = "googleZoomIn";
    });

    var zoomOutDiv = document.createElement("div");
    zoomOutDiv.className = "googleZoomOut";
    container.appendChild(zoomOutDiv);
    
    GEvent.addDomListener(zoomOutDiv, "click", function() {
        googleMap.zoomOut();
    });
    
    GEvent.addDomListener(zoomOutDiv, "mouseover", function() {
        zoomOutDiv.className = "googleZoomOutHover";
    });
    
    GEvent.addDomListener(zoomOutDiv, "mouseout", function() {
        zoomOutDiv.className = "googleZoomOut";
    });

    googleMap.getContainer().appendChild(container);
    return container;
}

CustomMapControl.prototype.getDefaultPosition = function()
{
    return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(4, 4));
}

function CustomMapTypeControl() 
{
    
}

CustomMapTypeControl.prototype = new GControl();

CustomMapTypeControl.prototype.initialize = function(googleMap)
{
    var container = document.createElement("div");
    
    var classSuffix = "";
    if(is_ie6)
        classSuffix = "IE";
    
    var mapDiv = document.createElement("div");
    mapDiv.className = "googleMapTypeMap" + classSuffix;
    container.appendChild(mapDiv);
    
    GEvent.addDomListener(mapDiv, "click", function() {
        googleMap.setMapType(G_NORMAL_MAP);
    });
    
    GEvent.addDomListener(mapDiv, "mouseover", function() {
        mapDiv.className = "googleMapTypeMapHover" + classSuffix;
    });
    
    GEvent.addDomListener(mapDiv, "mouseout", function() {
        mapDiv.className = "googleMapTypeMap" + classSuffix;
    });
    
    var satelliteDiv = document.createElement("div");
    satelliteDiv.className = "googleMapTypeSatellite" + classSuffix;
    container.appendChild(satelliteDiv);
    
    GEvent.addDomListener(satelliteDiv, "click", function() {
        googleMap.setMapType(G_SATELLITE_MAP);
    });
    
    GEvent.addDomListener(satelliteDiv, "mouseover", function() {
        satelliteDiv.className = "googleMapTypeSatelliteHover" + classSuffix;
    });
    
    GEvent.addDomListener(satelliteDiv, "mouseout", function() {
        satelliteDiv.className = "googleMapTypeSatellite" + classSuffix;
    });
    
    var hybridDiv = document.createElement("div");
    hybridDiv.className = "googleMapTypeHybrid" + classSuffix;
    container.appendChild(hybridDiv);
    
    GEvent.addDomListener(hybridDiv, "click", function() {
        googleMap.setMapType(G_HYBRID_MAP);
    });
    
    GEvent.addDomListener(hybridDiv, "mouseover", function() {
        hybridDiv.className = "googleMapTypeHybridHover" + classSuffix;
    });
    
    GEvent.addDomListener(hybridDiv, "mouseout", function() {
        hybridDiv.className = "googleMapTypeHybrid" + classSuffix;
    });
    
    var clearDiv = document.createElement("div");
    clearDiv.className = "googleClear";
    container.appendChild(clearDiv);

    googleMap.getContainer().appendChild(container);
    return container;
}

CustomMapTypeControl.prototype.getDefaultPosition = function()
{
    return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(4, 4));
}

function isIE6()
{
    version = 0;
    
    if (navigator.appVersion.indexOf("MSIE") != -1 && navigator.userAgent.toLowerCase().indexOf("Opera") == -1)
        version=parseFloat(navigator.appVersion.split("MSIE")[1])

    return (version != 0 && version < 7)
}

function isSafari()
{
    return navigator.userAgent.toLowerCase().indexOf("safari") != -1;
}