﻿var MapViewer = new __MapViewer();

function __MapViewer() {}

(function()
{

    var GMAP = null;

    __MapViewer.prototype.ShowMap = function()
    {
        var panel = document.getElementById("mapPanel");

        if(!GMAP)
        {
            GMAP = new GMap2(panel);
            GMAP.disableDoubleClickZoom();
            GMAP.addControl(new GLargeMapControl());
        }

        if(GMAP)
        {
            getMap(GMAP);
        }
    }
    
    __MapViewer.prototype.ZoomTo = function(lat, lon, level)
    {
        GMAP.setCenter(new GLatLng(lat, lon), level);
    }
    
    __MapViewer.prototype.ClearMarker = function()
    {
        GMAP.clearOverlays();
    }
    
    __MapViewer.prototype.CreateMarker = function(dataIndex)
    {
        // Custom marker overlay
        var cusIcon = new GIcon();
        cusIcon.image = "http://www.startuply.com/App_Themes/" + SystemInfo.TemplateName + "/Images/Common/Map/Marker4.png";
        cusIcon.iconSize = new GSize(37, 42);
        cusIcon.iconAnchor = new GPoint(11, 30);
        cusIcon.infoWindowAnchor = new GPoint(9, 2);
        cusIcon.infoShadowAnchor = new GPoint(9, 2);

        // Create company markers
        var latlng = new GLatLng(MAP_DATA[dataIndex][2], MAP_DATA[dataIndex][3]);

        // Add marker overlay
        var marker = new GMarker(latlng, { icon:cusIcon });
        GMAP.addOverlay(marker);
        
        // Click event function
        function clickEvent()
        {
            openInfoWindow(GMAP, this);
        }

        // Add event to marker
        marker.__id = MAP_DATA[dataIndex][5];
        marker.__latitude = MAP_DATA[dataIndex][2];
        marker.__longitude = MAP_DATA[dataIndex][3];
        GEvent.addListener(marker, "click", clickEvent);
        
        return marker;
    }
    
    __MapViewer.prototype.OpenInfoWindow = function(marker)
    {
        openInfoWindow(GMAP, marker);
    }

    function getMap(map)
    {
        if(GBrowserIsCompatible())
        {
            // Set center
            map.setCenter(new GLatLng(45, -10), 2);
            
            var table = $("companyTable");
            // Create company markers
            for(var i=0; i<MAP_DATA.length; i++)
            {
                var marker = MapViewer.CreateMarker(i);
                
                for(var j=0; j<table.rows.length; j++)
                {
                    var link = $$(table.rows[j].cells[0], "a");
                    if(link.length > 0 && link[0].innerHTML == MAP_DATA[i][0])
                    {
                        link[0].__marker = MapViewer.CreateMarker(i);
                        link[0].onclick = function()
                        {
                            MapViewer.OpenInfoWindow(this.__marker);
                            return false;
                        }
                        break;
                    }
                }
            }
        }
    }

    function openInfoWindow(map, marker)
    {
        var callback =
        {   
            success: function(o)
            {
                var xmlDoc = o.responseXML;
                
                var name = $$(xmlDoc, "name")[0].firstChild.nodeValue;
                var logo = $$(xmlDoc, "logo")[0].firstChild.nodeValue;
                var info = $$(xmlDoc, "info")[0]
                if(info.firstChild) info = info.firstChild.nodeValue;
                var address = $$(xmlDoc, "address")[0].firstChild.nodeValue;
                
                var html = "<table cellspacing=\"0\" cellpadding=\"0\" style=\"width:400px;\">" +
                "<tr><td style=\"width:99%;vertical-align:top;\"><a style=\"font-size:200%;color:#e66d00;font-weight:bold;\" href=\"../Companies/" + name.replace(/[\W_1234567890]/ig,  "_") + "_" + marker.__id + ".aspx" + "\">" + name + "</a><div style=\"padding:5px 0px 10px 0px;font-size:95%;font-style:italic;\">" + address + "</div></td><td style=\"padding-left:10px;\">" + (logo != "0" ? "<img src=\"" + logo + "\" style=\"width:109px;height:40px;\" />" : "") + "</td></tr></table>" +
                "<div style=\"width:380px;padding:10px 0px;font-size:95%;\">" + info + "</div><a style=\"font-size:115%;\" href=\"../Companies/" + name.replace(/[\W_1234567890]/ig,  "_") + "_" + marker.__id + ".aspx" + "\">Full profile</a>";

                marker.openInfoWindowHtml(html);
            }
        }
        
        YAHOO.util.Connect.asyncRequest("GET", "Ajax/GetCompanyInfo.aspx?cid=" + marker.__id, callback, "");


        marker.openInfoWindowHtml("<div style=\"width:300px;\">LOADING...</div>");
        
        var zoomlevel = map.getZoom();
        if(zoomlevel >= 14)
        {
            map.setCenter(new GLatLng(marker.__latitude, marker.__longitude));
        }
        else
        {
            map.setCenter(new GLatLng(marker.__latitude, marker.__longitude), 14);
        }
    }

})();
