﻿var map = null;

function LoadHomepageMap() {
    map = new VEMap('Map');
    map.LoadMap(new VELatLong(53.4763, -2.2411), 5, 'h', false);
    //PopulateMap();
}

function LoadMap(lat, lon, zoomLevel) {
    map = new VEMap('Map');
    var pinLocation = new VELatLong(lat, lon);
    map.LoadMap(new VELatLong(lat, lon), zoomLevel, 'b', false);
    AddPushpin(lat, lon, '', '');
    map.SetMapStyle(VEMapStyle.Birdseye);
    //if (map.IsBirdseyeAvailable()) {
        //map.SetBirdsEyeScene(pinLocation, null, 20, EndZoomHandler);  
    //}

}

function EndZoomHandler(e) {
    //PopulateMap();
}

function PopulateMap() {
    //map = new VEMap('Map');
    //map.LoadMap();
    map.DeleteAllShapes();
    var mapWidth = document.getElementById('Map').style.width;
    var mapHeight = document.getElementById('Map').style.height;

    topRight = map.PixelToLatLong(new VEPixel(mapWidth, 0)).toString();
    topLeft = map.PixelToLatLong(new VEPixel(0, 0)).toString();
    bottomLeft = map.PixelToLatLong(new VEPixel(0, mapHeight)).toString();
    bottomRight = map.PixelToLatLong(new VEPixel(mapWidth, mapHeight)).toString();

    var strOut = "";
    strOut = strOut + "Top left: " + topLeft + "\n";
    strOut = strOut + "Top right: " + topRight + "\n";
    strOut = strOut + "Bottom left: " + bottomLeft + "\n";
    strOut = strOut + "Bottom right: " + bottomRight + "\n";

    //alert(strOut);

    if (objXmlHttp) {
        //  Ajax object created :-)
        objXmlHttp.onreadystatechange = PopulateMapWithData;
        objXmlHttp.open("GET", "/Ajax/LoadHomepageMaps/" + topLeft + "/" + topRight + "/" + bottomLeft + "/" + bottomRight, true);
        objXmlHttp.send(null);
    }

}

function PopulateMapWithData() {
    var ret = "";
    if (objXmlHttp.readyState == 4) {
        ret = objXmlHttp.responseText;
        var allItems = ret.split("@@@");
        for (k = 0; k < allItems.length; k++) {
            var thisItem = allItems[k].split("|||");
            var thisID = thisItem[0];
            var thisLat = thisItem[1];
            var thisLon = thisItem[2];
            var thisStar = thisItem[3];
            var thisTitle = thisItem[4];
            var thisCity = thisItem[5];
            var thisCountry = thisItem[6];
            var thisTelephone = thisItem[7];
            var thisPhoto = thisItem[8];

            if (thisStar == null || thisStar == "") {
                thisStar = 0;
            }

            if (thisPhoto == null || thisPhoto == "") {
                thisPhoto = "/content/images/venues/no-photo_Thumb.jpg";
            }

            var strTitle = "<a href='/Venue/Details/" + thisID + "'>" + thisTitle + "</a>";

            var strDescription = "<div class='mapbubble'>";
            strDescription = strDescription + "<img src='" + thisPhoto + "' style='float: left;margin: 0 5px 5px 0;' />";
            strDescription = strDescription + "<ul style='padding: 20px 0 0 0;'>";
            strDescription = strDescription + "<li style='text-align: left;padding: 0 5px 5px 5px;'>" + thisCity + "</li>";
            strDescription = strDescription + "<li style='text-align: left;padding: 0 5px 5px 5px;'>" + thisCountry + "</li>";
            strDescription = strDescription + "<li style='text-align: left;padding: 0 5px 5px 5px;'><strong>tel: </strong>" + thisTelephone + "</li>";
            strDescription = strDescription + "<li style='text-align: left;padding: 0 5px 5px 5px;'><img src='/content/images/stars/" + thisStar + ".png' alt='' /></li>";
            strDescription = strDescription + "<li style='text-align: left;padding: 0 5px 5px 5px;'><a href='/Venue/Details/" + thisID + "'>View Details &raquo;</a></li>";
            strDescription = strDescription + "</ul>";
            strDescription = strDescription + "</div>";

            try {
                AddPushpin(thisLat, thisLon, strTitle, strDescription);
            } catch (err) {

            }
        }
    }
}

function CreateXmlHttp() {
    //Creating object of XMLHTTP in Internet Explorer
    try {
        objXmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e) {
        try {
            objXmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch (oc) {
            objXmlHttp = null;
        }
    }
    //Creating object of XMLHTTP in Mozilla and Safari
    if (!objXmlHttp && typeof XMLHttpRequest != "undefined") {
        objXmlHttp = new XMLHttpRequest();
    }
}

function AddPushpin(lat, lon, title, description) {
    var pinLocation = new VELatLong(lat, lon);
    var shape = new VEShape(VEShapeType.Pushpin, pinLocation);
    var icon = "<img src='/content/images/icons/venue.png' />";
    shape.SetCustomIcon(icon);
    shape.SetTitle(title);
    shape.SetDescription(description);
    map.AddShape(shape);
}


CreateXmlHttp();
