﻿/// <reference path="jquery-1.2.6.js" />

var map;

function initMap() {
    if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map_canvas"));

        map.setCenter(new GLatLng(40.946714, -77.750244), 7);
        map.addControl(new GLargeMapControl3D());
        //map.setUIToDefault();

        window.onunload = GUnload;
    }
}

function findEvent(id) {

    map.clearOverlays();
    $('#eventList').html("");

    $.post("/Search/SearchEventID", { eventId: id },
        function(evt) {
    placeEventOnMap(evt, true);
    map.setCenter(new GLatLng(evt.Latitude, evt.Longitude), 12);
    

        }
     , "json");
}
function findSchoolCurrentEvents() {

    map.clearOverlays();
    $('#eventList').html("");

    $.post("/Search/SearchSchoolEvents", {},
        function(events) {
            $.each(events, function(i, evt) {
                //add the marker
                placeEventOnMap(evt, true);
                /*
                $('#eventList').append($('<li/>')
                .attr("class", "event-item-title-link")
                .append($('<a/>').attr('href', '/Events/Details/' + evt.EventID)
                .html(evt.Title))
                .append($('<span/>').attr('class', 'event-item-location')
                .html(evt.Location))
                .append($('<span/>').attr('class', 'event-item-date')
                .html(evt.StartDate)))
                */

            });


        }
     , "json");
            }

function findAllCurrentEvents() {

    map.clearOverlays();
    $('#eventList').html("");

    $.post("/Search/SearchAllEvents", {},
        function(events) {
            $.each(events, function(i, evt) {
                //add the marker
                placeEventOnMap(evt, true);

                //add event item
                /*                
                $('#eventList').append($('<li/>')
                .attr("class", "event-item-title-link")
                .append($('<a/>').attr('href', '/Events/Details/' + evt.EventID)
                .html(evt.Title))
                .append($('<span/>').attr('class', 'event-item-location')
                .html(evt.Location))
                .append($('<span/>').attr('class', 'event-item-date')
                .html(evt.StartDate)))
                */
            });
        }
     , "json");
            }
            
function placeEventOnMap(event, enableInfo) {


    var point = createPoint(event.Latitude, event.Longitude);

    var icon = new GIcon();

    if (event.Type == 'D') {

        icon.image = '/Content/Images/event-marker/image.png';
        icon.shadow = '';
        icon.iconSize = new GSize(64, 64);
        icon.shadowSize = new GSize(96, 64);
        icon.iconAnchor = new GPoint(32, 64);
        icon.infoWindowAnchor = new GPoint(32, 0);
        icon.printImage = '/Content/Images/event-marker/printImage.gif';
        icon.mozPrintImage = '/Content/Images/event-marker/mozPrintImage.gif';
        icon.printShadow = '/Content/Images/event-marker/printShadow.gif';
        icon.transparent = '/Content/Images/event-marker/transparent.png';
        icon.imageMap = [43, 0, 43, 1, 43, 2, 44, 3, 44, 4, 44, 5, 44, 6, 44, 7, 44, 8, 44, 9, 44, 10, 44, 11, 44, 12, 44, 13, 59, 14, 63, 15, 63, 16, 63, 17, 63, 18, 63, 19, 63, 20, 63, 21, 63, 22, 63, 23, 63, 24, 63, 25, 63, 26, 63, 27, 63, 28, 63, 29, 63, 30, 63, 31, 63, 32, 63, 33, 63, 34, 63, 35, 63, 36, 62, 37, 62, 38, 61, 39, 61, 40, 61, 41, 60, 42, 60, 43, 59, 44, 59, 45, 58, 46, 57, 47, 57, 48, 56, 49, 55, 50, 54, 51, 52, 52, 42, 53, 40, 54, 39, 55, 38, 56, 38, 57, 37, 58, 36, 59, 36, 60, 35, 61, 35, 62, 34, 63, 25, 63, 25, 62, 25, 61, 25, 60, 24, 59, 24, 58, 24, 57, 24, 56, 24, 55, 24, 54, 24, 53, 20, 52, 18, 51, 18, 50, 17, 49, 17, 48, 16, 47, 4, 46, 3, 45, 3, 44, 3, 43, 3, 42, 3, 41, 2, 40, 2, 39, 2, 38, 2, 37, 2, 36, 2, 35, 2, 34, 2, 33, 2, 32, 2, 31, 2, 30, 2, 29, 2, 28, 1, 27, 1, 26, 1, 25, 1, 24, 1, 23, 1, 22, 1, 21, 1, 20, 1, 19, 1, 18, 1, 17, 1, 16, 0, 15, 0, 14, 0, 13, 0, 12, 0, 11, 0, 10, 0, 9, 0, 8, 0, 7, 0, 6, 0, 5, 0, 4, 0, 3, 1, 2, 12, 1, 25, 0];


    }
    else
        icon = null;
        

    var marker = new GMarker(point, icon);       
    
    if (enableInfo)
        GEvent.addListener(marker, "click", function() {
            marker.openInfoWindowHtml(formatEventBubble(event));
        });

    map.addOverlay(marker);
    
    return;
}

function addMarker(point) {

    var icon = new GIcon();
    icon.image = '/Content/Images/event-marker/image.png';
    icon.shadow = '/Content/Images/event-marker/shadow.png';
    icon.iconSize = new GSize(64, 64);
    icon.shadowSize = new GSize(96, 64);
    icon.iconAnchor = new GPoint(32, 64);
    icon.infoWindowAnchor = new GPoint(32, 0);
    icon.printImage = '/Content/Images/event-marker/printImage.gif';
    icon.mozPrintImage = '/Content/Images/event-marker/mozPrintImage.gif';
    icon.printShadow = '/Content/Images/event-marker/printShadow.gif';
    icon.transparent = '/Content/Images/event-marker/transparent.png';
    
    icon.imageMap = [43, 0, 43, 1, 43, 2, 44, 3, 44, 4, 44, 5, 44, 6, 44, 7, 44, 8, 44, 9, 44, 10, 44, 11, 44, 12, 44, 13, 59, 14, 63, 15, 63, 16, 63, 17, 63, 18, 63, 19, 63, 20, 63, 21, 63, 22, 63, 23, 63, 24, 63, 25, 63, 26, 63, 27, 63, 28, 63, 29, 63, 30, 63, 31, 63, 32, 63, 33, 63, 34, 63, 35, 63, 36, 62, 37, 62, 38, 61, 39, 61, 40, 61, 41, 60, 42, 60, 43, 59, 44, 59, 45, 58, 46, 57, 47, 57, 48, 56, 49, 55, 50, 54, 51, 52, 52, 42, 53, 40, 54, 39, 55, 38, 56, 38, 57, 37, 58, 36, 59, 36, 60, 35, 61, 35, 62, 34, 63, 25, 63, 25, 62, 25, 61, 25, 60, 24, 59, 24, 58, 24, 57, 24, 56, 24, 55, 24, 54, 24, 53, 20, 52, 18, 51, 18, 50, 17, 49, 17, 48, 16, 47, 4, 46, 3, 45, 3, 44, 3, 43, 3, 42, 3, 41, 2, 40, 2, 39, 2, 38, 2, 37, 2, 36, 2, 35, 2, 34, 2, 33, 2, 32, 2, 31, 2, 30, 2, 29, 2, 28, 1, 27, 1, 26, 1, 25, 1, 24, 1, 23, 1, 22, 1, 21, 1, 20, 1, 19, 1, 18, 1, 17, 1, 16, 0, 15, 0, 14, 0, 13, 0, 12, 0, 11, 0, 10, 0, 9, 0, 8, 0, 7, 0, 6, 0, 5, 0, 4, 0, 3, 1, 2, 12, 1, 25, 0];

    var marker = new GMarker(point, icon);
    map.addOverlay(marker);
}

function formatEventBubble(event) {

    var html = "";

    html += "<span class='bubbleInfoCity'>" + event.Title + "</span>" +
            "<span class='bubbleInfoText'><label>When:</label>" + event.WhenDate + "</span>" +
            "<span class='bubbleInfoText'><label>Where:</label>" + event.Location + "<br/>" +
                                        "<label></label>" + event.Street + "<br/>" +
                                        "<label></label>" + event.City + ", " + event.State + " " + event.Zip + "</span>" +
            "<span class='bubbleInfoText'><label>Time:</label>" + event.WhenTime + "</span>"

    if (event.Type == "S" && event.URL.length > 0)
        html += "<a target='_new' href='" + event.URL + "' class='bubbleInfoBtn'>More Event Information &raquo;</a>"

    if (event.Type != "S")
        html += "<a href='/Events/Details/" + event.EventID + "' class='bubbleInfoBtn'>CLICK HERE FOR INFO &raquo;</a>";

    var p = '"' + (event.Location).replace("'", "\&#039;") + '", ' + (event.Latitude) + ',' + (event.Longitude);

    html += "<span class='map-bubble-from-text'><b>Need Directions?</b></span>" +
            "<div id='map-bubble-from'>From: <input type=text name='saddr' onkeypress='return disableEnterKey(event);'/><input type='button' onclick='showDirections(saddr.value, " + p + ");' value='Guide Me!'/></div>";

    return html;
}

function disableEnterKey(e)
{
     var key;

     if(window.event)
          key = window.event.keyCode;     //IE
     else
          key = e.which;     //firefox

     if(key == 13)
          return false;
     else
          return true;
}

function showDirections(saddr, daddrLocation, daddrLatitude, daddrLongitude) {

    var directionsURL = "http://maps.google.com/maps?daddr=" + (daddrLocation) + "+@" + (daddrLatitude) + "," + (daddrLongitude) + "&saddr=" + (saddr);

    window.open(directionsURL, "eventDirectionsWindow");
    
}


function createPoint(latitude, longitude) {
    var lat = parseFloat(latitude);
    var lng = parseFloat(longitude);

    if (lat.toString().length = 0)
        throw ("Missing Latitude Value");

    if (lng.toString().length = 0)
        throw ("Missing Longitude Value");

    return new GLatLng(lat, lng);

}
