﻿var mapVendor = null;
var geocoderVendor;
var polys = [];
var statesName = [];
var statesSortName = [];
var labels = [];
var xmlDoc = null;
var mainControl;
var J = jQuery.noConflict();

function initializeVendorMap(control, locations) {
    if (GBrowserIsCompatible()) {
        var point;
        geocoderVendor = new GClientGeocoder();

        mainControl = control;
        mapVendor = new GMap2(document.getElementById(control));
        J("#" + control).hide();

        mapVendor.disableDoubleClickZoom();
        mapVendor.disableGoogleBar();
        mapVendor.disableDragging();
        //mapVendor.addControl(new GLargeMapControl());
        //mapVendor.addControl(new GMapTypeControl());
        mapVendor.setCenter(new GLatLng(39.048199, -86.674806), 4);
        //new GLatLng(35.284191, -90.737915), 4
        var boundaries = new GLatLngBounds(new GLatLng(25.031857, -124.742432), new GLatLng(53.058796, -60.507431));
        //27.262443,-121.845707   46.715763,-60.849613
        //new GLatLng(25.042309, -111.770325), new GLatLng(49.634197, -66.875611)


        var pts = [];
        pts[0] = new GLatLng(25.031857, -124.742432);
        pts[1] = new GLatLng(51.634385, -126.137697);
        pts[2] = new GLatLng(53.058796, -60.507431);
        pts[3] = new GLatLng(21.232038, -60.747072);
        var poly = new GPolygon(pts, "#FFFFFF", 1, 1, "#FFFFFF", 1);
        mapVendor.addOverlay(poly);

        var oldmap = new GGroundOverlay("/App_Themes/FCXPerformance/images/map-final-latest-latest.png", boundaries);
        //google-map.jpg
        mapVendor.addOverlay(oldmap);

        /*
        GEvent.addListener(oldmap, "load", function () {
        //J("#" + control).show();
        //alert("Map Loaded");
        });
        */
        ///App_Themes/FCXPerformance/images/map-final.png
        //mapVendor.addOverlay(oldmap);

        //(new GLatLng(52.514884, -114.462894), 3)
        var request = GXmlHttp.create();
        request.open("GET", "/tools/Others/states.xml", true);
        request.onreadystatechange = function () {

            if (request.readyState == 4) {
                xmlDoc = GXml.parse(request.responseText);

                var states = xmlDoc.documentElement.getElementsByTagName("state");

                for (var a = 0; a < states.length; a++) {

                    var label = states[a].getAttribute("name");
                    var labelSort = states[a].getAttribute("sort");
                    var colour = states[a].getAttribute("colour");
                    var points = states[a].getElementsByTagName("point");

                    var pts = [];
                    for (var i = 0; i < points.length; i++) {
                        pts[i] = new GLatLng(parseFloat(points[i].getAttribute("lat")),
                                          parseFloat(points[i].getAttribute("lng")));
                    }

                    statesName[a] = label.toLowerCase();
                    statesSortName[a] = labelSort.toLowerCase();
                    var poly = new GPolygon(pts, "#FFFFFF", 1, 1, "#3793DC", 1, { clickable: true });
                    /*
                    GEvent.addListener(poly, 'click', function () {
                    alert("hi");
                    });
                    */
                    polys.push(poly);
                    //mapVendor.clearOverlays();
                    mapVendor.addOverlay(poly);

                }
                SetMerker(locations);
            }

        }
        request.send(null);

    }
    else {
        alert("Sorry, the Google Maps API is not compatible with this browser!");
    }
}

function SetMerker(locations) {
    var addresses = locations.split(";");

    for (var i = 0; i < addresses.length; i++) {
        AddVendorAddress(addresses[i], i);
    }
}


function AddVendorAddress(address, id) {

    var data = address.split("~");
    if (data.length >= 4) {
        var locationlatlng = data[3];
        var templatlng = locationlatlng.split(",");

        var templatlng1 = new GLatLng(parseFloat(templatlng[0]),
                                          parseFloat(templatlng[1]));

        var customIcon = new GIcon();
        var image = "App_Themes/FCXPerformance/images/btn_map_indicator.png";
        customIcon.image = image;

        customIcon.iconSize = new GSize(24, 24);
        customIcon.iconAnchor = new GPoint(0, 0);
        customIcon.infoWindowAnchor = new GPoint(0, 0);

        var marker = new GMarker(templatlng1, customIcon);
        mapVendor.addOverlay(marker);

        GEvent.addListener(marker, 'click', function () {
            ClickAddress(data[0], data[1]);
        });

        GEvent.addListener(marker, "mouseover", function () {
            //marker.openInfoWindowHtml(html, { suppressMapPan: true, noCloseOnClick: true });
            J("#mapInfo").html(data[2]);

            var position = J("#mtgt_unnamed_" + id).position();
            J("#mapInfo").show();
            var height = J("#mapInfo").height();
            var width = J("#mapInfo").width();
            J("#mapInfo").css('left', position.left - width / 2);
            J("#mapInfo").css('top', position.top - height - 5);
            //J("#mapInfo").offset({ top: position.top, left: position.left });

            //alert(position.left + "," + position.top);
        });

        GEvent.addListener(marker, "mouseout", function () {
            //marker.closeInfoWindowHtml(html);
            J("#mapInfo").hide();
        });
    }
}

String.prototype.trim = function () {
    return this.replace(/^\s+|\s+$/g, "");
}


jQuery(function () {
    J(J("#ctl00_cphContent_ctl00_map").children().get(1)).hide();
    J(J("#ctl00_cphContent_ctl00_map").children().get(2)).hide();
    J("#" + mainControl).show();
});

function ClickAddress(address, id) {
    var isVisible = document.getElementById("vendorInfo").offsetWidth > 0 || document.getElementById("vendorInfo").offsetHeight > 0;

    if (isVisible == true)
        J("#vendorInfo").slideUp("slow");

    var isVisible = document.getElementById("vendorList").offsetWidth > 0 || document.getElementById("vendorList").offsetHeight > 0;

    if (isVisible == true)
        J("#vendorList").hide();

    var temp = address.split(", ");
    var stateTemp = temp[temp.length - 1].trim();
    var temp2 = stateTemp.split(" ");
    var currentSate = temp2[0].toLowerCase();

    for (var a = 0; a < statesName.length; a++) {
        if (statesName[a].toString() == currentSate || statesSortName[a].toString() == currentSate) {

            for (var b = 0; b < polys.length; b++) {
                if (a == b) {
                    polys[b].setFillStyle({ color: "#03568C", opacity: 1 });

                }
                else {
                    polys[b].setFillStyle({ color: "#3793DC", opacity: 1 });
                }
            }

            break;
        }
    }

    J("#vendorListContainer").html("");
    J.ajax({
        url: "VendorsLogoByLocationHandler.ashx",
        type: "GET",
        dataType: 'html',
        data: { 'ID': id },
        success: function (data) {
            J("#vendorListContainer").html(data);
        }
    });

    J("#vendorList").show();
}


function HideVendorList() {
    var isVisible = document.getElementById("vendorInfo").offsetWidth > 0 || document.getElementById("vendorInfo").offsetHeight > 0;

    if (isVisible == true)
        J("#vendorInfo").slideUp("slow");

    J("#vendorList").hide();
}

function ShowVendorInfo(id) {
    var isVisible = document.getElementById("vendorInfo").offsetWidth > 0 || document.getElementById("vendorInfo").offsetHeight > 0;

    if (isVisible == true) {
        J("#vendorInfo").slideUp("slow", function () {
            J("#vendorInfoContainer").html("<div class='vendor_scrollable'></div>");
            J.ajax({
                url: "VendorProductDescriptionHandler.ashx",
                type: "GET",
                dataType: 'html',
                data: { 'ID': id },
                success: function (data) {
                    J("#vendorInfoContainer").html(data);
                    J("#vendorInfo").css("display", "");
                    var top = -parseInt(J("#vendorInfo").height()) + 25;
                    J("#vendorInfo").css("margin-top", top + "px");
                    J("#vendorInfo").css("display", "none");
                    J("#vendorInfo").slideDown("slow");
                }
            });
        });
    }
    else {
        J("#vendorInfoContainer").html("<div class='vendor_scrollable'></div>");
        J.ajax({
            url: "VendorProductDescriptionHandler.ashx",
            type: "GET",
            dataType: 'html',
            data: { 'ID': id },
            success: function (data) {
                J("#vendorInfoContainer").html(data);
                J("#vendorInfo").css("display", "");
                var top = -parseInt(J("#vendorInfo").height()) + 25;
                J("#vendorInfo").css("margin-top", top + "px");
                J("#vendorInfo").css("display", "none");
                J("#vendorInfo").slideDown("slow");
            }
        });
    }
}

function HideVendorInfo() {
    J("#vendorInfo").slideUp("slow");
}


