﻿var timeout = 5000;
var timer = 0;
// Create a base icon for all of our markers that specifies the
// shadow, icon dimensions, etc.
var baseIcon = new GIcon(G_DEFAULT_ICON);
baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
baseIcon.iconSize = new GSize(20, 34);
baseIcon.shadowSize = new GSize(37, 34);
baseIcon.iconAnchor = new GPoint(9, 34);
baseIcon.infoWindowAnchor = new GPoint(9, 2);

jQuery(document).ready(function() {
    initialize_Events();
    loadMarquee_Events();
    LoadRequestHandler_Events();
    ExpandFunction_Events();
});

function initialize_Events() {
    if (GBrowserIsCompatible()) {
        if (timer) {
            window.clearTimeout(timer);
        }

        var timer = window.setTimeout('HideGMapStuff()', timeout);
        var map = new GMap2(document.getElementById("map_canvas_Events"));
        var object = jQuery('.MDTCalMod_InfoContainer_Events');
        var point;
        var count = 0;
        var marker;
        var latlngbounds = new GLatLngBounds();
        
        while (object.find('#Latitude_Events').val() != null) {
            point = new GLatLng(object.find('#Latitude_Events').val(), object.find('#Longitude_Events').val());
            marker = createMarker(point, count, object);
            count = count + 1;
            object = getNextObject(object);

            map.addOverlay(marker);

            latlngbounds.extend(point);
        }

        //map.setCenter(new GLatLng(38.462222, -92.301944), 5);
        map.setCenter(latlngbounds.getCenter(), map.getBoundsZoomLevel(latlngbounds) - 1);

        map.setUIToDefault();
    }
    else {
        jQuery('#map_canvas_Events').html("Sorry, your browser is not compatible with google maps.");
    }

    jQuery('.eventsResultsCount').val(jQuery('.MDTCalMod_TotalRecords_Events').html());
}

function loadMarquee_Events() {
    jQuery('.MarqueeTitle_Events').html(jQuery('div').filter('.MDTCalMod_InfoTitle_Events').html());
    jQuery('.MarqueeCity_Events').html(jQuery('div').filter('.MDTCalMod_InfoCity_Events').html());
    jQuery('.MarqueeType_Events').html(jQuery('div').filter('.MDTCalMod_InfoType_Events').html());
    jQuery('.MarqueeDate_Events').html(jQuery('div').filter('.MDTCalMod_InfoDate_Events').html());
    jQuery('.MarqueeDescription_Events').html(jQuery('div').filter('.MDTCalMod_InfoDescription').html());
}

function getNextObject(object) {
    return object.next();
}

// Creates a marker whose info window displays the letter corresponding
// to the given index.
function createMarker(point, index, object) {
    // Create a lettered icon for this point using our icon class
    var letter = String.fromCharCode("A".charCodeAt(0) + index);
    var letteredIcon = new GIcon(baseIcon);
    letteredIcon.image = "http://www.google.com/mapfiles/marker" + letter + ".png";

    // Set up our GMarkerOptions object
    markerOptions = { icon: letteredIcon };
    var marker = new GMarker(point, markerOptions);

    object.find('.googleMapMarker').attr('src', letteredIcon.image);

    return marker;
}

function LoadRequestHandler_Events() {
    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler_Events);
}

function EndRequestHandler_Events(sender, args) {
    if (args.get_error() == undefined) {
        initialize_Events();
        loadMarquee_Events();
        ExpandFunction_Events();
    }
}

function ExpandFunction_Events() {
    jQuery('.MDTCalMod_ShowOne_Events').hover(function() { jQuery(this).addClass("hilite_Events"); }, function() { jQuery(this).removeClass("hilite_Events"); });
    jQuery('.MDTCalMod_HideOne_Events').hover(function() { jQuery(this).addClass("hilite_Events"); }, function() { jQuery(this).removeClass("hilite_Events"); });
    jQuery('.MDTCalMod_ShowOne_Events').click(function() {
        jQuery(this).hide();
        jQuery(this).parent().children('.MDTCalMod_HideOne_Events').show();
        jQuery(this).parent().parent().parent().children('.MDTCalMod_InfoExpandable_Events').show('slow');
    });

    jQuery('.MDTCalMod_HideOne_Events').click(function() {
        jQuery(this).hide();
        jQuery(this).parent().children('.MDTCalMod_ShowOne_Events').show();
        jQuery(this).parent().parent().parent().children('.MDTCalMod_InfoExpandable_Events').hide('slow');
    });

    jQuery('.MDTCalMod_HideOne_Events').hide();
    jQuery('.MDTCalMod_InfoExpandable_Events').hide();
}

function HideGMapStuff() {
    jQuery('.MDTContentListGoogleMap div:nth-child(3) span:nth-child(2)').hide();
}