﻿/// <reference path="jquery.intellisense.js"/>

/* for the default text hints work */
$(function() {
    $(".defaultText").focus(function(srcc) {
        if ($(this).val() == $(this)[0].title) {
            $(this).removeClass("defaultTextActive");
            $(this).val("");
        }
    });

    $(".defaultText").blur(function() {
        if ($(this).val() == "") {
            $(this).addClass("defaultTextActive");
            $(this).val($(this)[0].title);
        }
    });


    var itm = $('#findMusic');
    if ($(itm).val() == '' || $(itm).val() == itm.attr('title')) {
        $(".defaultText").focus();
        $(itm).val(cityByIP + ', ' + stateByIP);        
    }

    $(document).ready(function() {
        $('table.searchResults tr:odd').addClass('odd');
        $('table.searchResults tr:even').addClass('even');
    });

    $("#cmdAdvancedSearch").toggle(
        function() {
            $("#searchAgain").animate({ height: 50 }, 750, function() { $("#searchAgain").show(); });
            $(this).attr("class", "panelOpen");
        }, function() {
            //$("#searchAgain").fadeOut(); 
            $("#searchAgain").animate({ height: 0 }, 750, function() { $("#searchAgain").hide(); });
            $(this).attr("class", "panelClosed");
        });

    // make all the TR's clickable
    $(".searchTR").hover(function() {
        $(this).addClass("searchItemHilite");
    }, function() {
        $(this).removeClass("searchItemHilite");
    });

    $(".defaultText").blur();

});



function doArtistSearch() {
    var query = $("#artist").val();
    var title = $("#artist").attr('title');
    if (query != null) {
        if (query == title) query = "";
        query = jQuery.trim(query);
        if (query.length > 0) {
            var s = "/results.aspx?q=" + query;            
            document.location = s;
        }
    }
    return false;
}

function doSearch() {
    var query = $("#findMusic").val();
    var title = $("#findMusic").attr('title');
    if (query != null) {
        if (query == title) query = "";
        query = jQuery.trim(query);
        if (query.length > 0) {
            var s = "/search.aspx?q=" + query;
            s += "&miles=" + jQuery.trim($("#miles").val());
            s += "&days=" + jQuery.trim($("#daysOut").val());
            document.location = s;
        }
    }
    return false;
}

/* map functions */
function displayPoint(markerData, index) {
    map.closeInfoWindow();
  
    // pan the map.
    map.panTo(markerData.Marker.getLatLng());
    var evt = getEventByEvent(markerData.EventId);
    var s = getBalloonText(evt);
    markerData.Marker.openInfoWindowHtml(s, { maxWidth: 200 });

    // set hilite and scroll.
    $("#searchRow" + markerData.EventId).parent().parent().addClass("searchItemHilite");
    $(".resultsDiv").scrollTo($("#searchRow" + markerData.EventId), 1000, { offset: { top: -50, left: 0} })
};

function initMap() {
    initVenues();

    if (searchLat != "") myLatitude = searchLat;
    if (searchLng != "") myLongitude = searchLng;

    map = new GMap2($("#map").get(0));
    map.addControl(new GLargeMapControl());
    var loc = new GLatLng(myLatitude, myLongitude);
    map.setCenter(loc, 11);

    // Create and add the circle
    //circle = new CircleOverlay(map.getCenter(), circleRadius, "#336699", 1, 1, '#336699', 0.25);
    //map.addOverlay(circle);

    // set each marker on the map.  store extra data.
    var i = 0;
    if (results != null) {
        for (var v in venues) {
            var evt = getEventByVenue(v);
            var point = new GLatLng(evt.lat, evt.lng);
            var name = evt.name + ' @ ' + evt.VName;
            marker = new GMarker(point, { title: name });
            marker.bouncy = true;
            map.addOverlay(marker);

            var mdata = new MarkerData(marker, evt.ID, evt.VID);
            var s = getBalloonText(evt);
            GEvent.addListener(marker, "click", function() {
                marker.openInfoWindowHtml(s, { maxWidth: 200 });
            });
            map.addOverlay(marker);
    

            markers[i] = mdata;
            i++;
        }
    }

    // make all the TR's clickable
    $(".searchTR").click(function() {
        // get the child info.
        var itm = $(this).find(".eventDate");
        var id = $(this).find(".eventDate").attr("id");
        var eid = id.substring(9, id.length);
        var mData = getMarkerData(eid);
        if (mData)
            panToMarker(mData);
    });

  
    $(markers).each(function(i, mData) {
        GEvent.addListener(mData.Marker, "click", function() {
            $(".searchTR").removeClass("searchItemHilite");
            displayPoint(mData, i);
        });
    });
}
// Creates a marker at the given point with the given number label
//function createBalloon_del(marker, evt) {
//    var s = getBalloonText(evt);

//    GEvent.addListener(marker, "click", function() {
//        marker.openInfoWindowHtml(s, { maxWidth: 200 });
//    });
//    return marker;
//}

function MarkerData(m, eid, vid) {
    this.Marker = m;
    this.EventId = eid;
    this.VenueId = vid;
}

function VenueData(vid, vname, lat, lng ) {
    this.VenueId = vid;
    this.VName = vid;
    this.lat = lat;
    this.lng = lng;
}


function getEventByVenue(vid) {
    var thisIndex = -1;
    for (var i = 0; i < results.eventItems.length; i++) {
        if (results.eventItems[i].VID == vid) {
            thisIndex = i;
            break;
        }
    }
    if (thisIndex > -1)
        return results.eventItems[thisIndex];
    else
        return null;
}

function getEventByEvent(eid) {
    var thisIndex = -1;
    for (var i = 0; i < results.eventItems.length; i++) {
        if (results.eventItems[i].ID == eid) {
            thisIndex = i;
            break;
        }
    }
    if (thisIndex > -1)
        return results.eventItems[thisIndex];
    else
        return null;
}
function getVenueByEvent(eid) {
    var thisIndex = -1;
    for (var i = 0; i < results.eventItems.length; i++) {
        if (results.eventItems[i].ID == eid) {
            thisIndex = i;
            break;
        }
    }
    if (thisIndex > -1)
        return results.eventItems[thisIndex].VID;
    else
        return null;
}

function getMarkerData(eid) {
    var thisIndex = -1;
    var thisVid = getVenueByEvent(eid);

    for (var i = 0; i < markers.length; i++) {
        if (markers[i].VenueId == thisVid) {
            thisIndex = i;
            break;
        }
    }
    if (thisIndex > -1)
        return markers[thisIndex];
    else
        return null;
}


function getBalloonText(evt) {
    var s = "<div class='balloon'><span class='highlight'>" + evt.Name + "</span>";
    s += " live at <span class='highlight'>" + evt.VName + "</span>";
    s += " on <span class='highlight'>" + evt.Date + ' ' + evt.Time + "</span>";

    s += "<p>view: <a class='viewitem' href='/events/" + evt.ID + "/" + evt.ID + ".aspx'>event</a>"; 
    s += " | <a class='viewitem' href='/venues/" + evt.VID + "/" + evt.sName + ".aspx'>venue</a>";
   

    return s;
}

function panToMarker(mdata) {
    map.closeInfoWindow();
    map.panTo(mdata.Marker.getLatLng());
    setTimeout(function() {
        var evt = getEventByEvent(mdata.EventId);
        var s = getBalloonText(evt);
        mdata.Marker.openInfoWindowHtml(s, { maxWidth: 200 });
    });
    
    
}

function initVenues() {
    if (results.eventItems != undefined) {
        $.each(results.eventItems, function() {
            venues[this.VID] = this.VID;
        });
    }
}



















// This file adds a new circle overlay to GMaps2
// it is really a many-pointed polygon, but look smooth enough to be a circle.
var CircleOverlay = function(latLng, radius, strokeColor, strokeWidth, strokeOpacity, fillColor, fillOpacity) {
    this.latLng = latLng;
    this.radius = radius;
    this.strokeColor = strokeColor;
    this.strokeWidth = strokeWidth;
    this.strokeOpacity = strokeOpacity;
    this.fillColor = fillColor;
    this.fillOpacity = fillOpacity;
}

// Implements GOverlay interface
CircleOverlay.prototype = GOverlay;

CircleOverlay.prototype.initialize = function(map) {
    this.map = map;
}

CircleOverlay.prototype.clear = function() {
    if (this.polygon != null && this.map != null) {
        this.map.removeOverlay(this.polygon);
    }
}

// Calculate all the points and draw them
CircleOverlay.prototype.redraw = function(force) {
    var d2r = Math.PI / 180;
    circleLatLngs = new Array();
    var circleLat = this.radius * 0.014483;  // Convert statute miles into degrees latitude
    var circleLng = circleLat / Math.cos(this.latLng.lat() * d2r);
    var numPoints = 40;

    // 2PI = 360 degrees, +1 so that the end points meet
    for (var i = 0; i < numPoints + 1; i++) {
        var theta = Math.PI * (i / (numPoints / 2));
        var vertexLat = this.latLng.lat() + (circleLat * Math.sin(theta));
        var vertexLng = this.latLng.lng() + (circleLng * Math.cos(theta));
        var vertextLatLng = new GLatLng(vertexLat, vertexLng);
        circleLatLngs.push(vertextLatLng);
    }

    this.clear();
    this.polygon = new GPolygon(circleLatLngs, this.strokeColor, this.strokeWidth, this.strokeOpacity, this.fillColor, this.fillOpacity);
    this.map.addOverlay(this.polygon);
}

CircleOverlay.prototype.remove = function() {
    this.clear();
}

CircleOverlay.prototype.containsLatLng = function(latLng) {
    // Polygon Point in poly
    if (this.polygon.containsLatLng) {
        return this.polygon.containsLatLng(latLng);
    }
}

CircleOverlay.prototype.setRadius = function(radius) {
    this.radius = radius;
}

CircleOverlay.prototype.setLatLng = function(latLng) {
    this.latLng = latLng;
}



function setFormPos() {
    var frm = $("#searchAgain");
    frm.hide();
    var itm = $("#cmdAdvancedSearch");
   
    var formLeft = (m.width() + m.offset().left - frm.width());
    var formTop = (m.offset().top );
    $("#searchAgain").offset({ top: formTop, left: formLeft });
    
 }
