var map = new GMap2(document.getElementById("map"));
var geocoder = new GClientGeocoder();
var icons = [];
var currentYear = null;

var icon1 = new GIcon();
icon1.image = "http://www.lpassociation.com/templates/lpa4/images/tour/markers/lp_upcoming.png";
icon1.iconSize = new GSize(29, 29);
icon1.shadowSize = new GSize(0, 0);
icon1.iconAnchor = new GPoint(14, 14);
icon1.infoWindowAnchor = new GPoint(0, 0);
icons["lp_upcoming"] = icon1;

var icon2 = new GIcon();
icon2.image = "http://www.lpassociation.com/templates/lpa4/images/tour/markers/lp_past.png";
icon2.iconSize = new GSize(29, 29);
icon2.shadowSize = new GSize(0, 0);
icon2.iconAnchor = new GPoint(14, 14);
icon2.infoWindowAnchor = new GPoint(0, 0);
icons["lp_past"] = icon2;

var icon3 = new GIcon();
icon3.image = "http://www.lpassociation.com/templates/lpa4/images/tour/markers/fm_upcoming.png";
icon3.iconSize = new GSize(29, 30);
icon3.shadowSize = new GSize(0, 0);
icon3.iconAnchor = new GPoint(14, 14);
icon3.infoWindowAnchor = new GPoint(0, 0);
icons["fm_upcoming"] = icon3;

var icon4 = new GIcon();
icon4.image = "http://www.lpassociation.com/templates/lpa4/images/tour/markers/fm_past.png";
icon4.iconSize = new GSize(29, 30);
icon4.shadowSize = new GSize(0, 0);
icon4.iconAnchor = new GPoint(14, 14);
icon4.infoWindowAnchor = new GPoint(0, 0);
icons["fm_past"] = icon4;

var icon5 = new GIcon();
icon5.image = "http://www.lpassociation.com/templates/lpa4/images/tour/markers/lp_special.png";
icon5.iconSize = new GSize(29, 29);
icon5.shadowSize = new GSize(0, 0);
icon5.iconAnchor = new GPoint(14, 14);
icon5.infoWindowAnchor = new GPoint(0, 0);
icons["lp_special"] = icon5;

var icon6 = new GIcon();
icon6.image = "http://www.lpassociation.com/templates/lpa4/images/tour/markers/fm_special.png";
icon6.iconSize = new GSize(29, 30);
icon6.shadowSize = new GSize(0, 0);
icon6.iconAnchor = new GPoint(14, 14);
icon6.infoWindowAnchor = new GPoint(0, 0);
icons["fm_special"] = icon6;

var icon7 = new GIcon();
icon7.image = "http://www.lpassociation.com/templates/lpa4/images/tour/markers/dbs_past.png";
icon7.iconSize = new GSize(29, 29);
icon7.shadowSize = new GSize(0, 0);
icon7.iconAnchor = new GPoint(14, 14);
icon7.infoWindowAnchor = new GPoint(0, 0);
icons["dbs_past"] = icon7;

var icon8 = new GIcon();
icon8.image = "http://www.lpassociation.com/templates/lpa4/images/tour/markers/dbs_upcoming.png";
icon8.iconSize = new GSize(29, 29);
icon8.shadowSize = new GSize(0, 0);
icon8.iconAnchor = new GPoint(14, 14);
icon8.infoWindowAnchor = new GPoint(0, 0);
icons["dbs_upcoming"] = icon8;

var icon9 = new GIcon();
icon9.image = "http://www.lpassociation.com/templates/lpa4/images/tour/markers/dbs_special.png";
icon9.iconSize = new GSize(29, 29);
icon9.shadowSize = new GSize(0, 0);
icon9.iconAnchor = new GPoint(14, 14);
icon9.infoWindowAnchor = new GPoint(0, 0);
icons["dbs_special"] = icon9;

map.setCenter(new GLatLng(39.807699, -93.556016), 3);
map.addControl(new GSmallMapControl());

function loadMarkers(year) {
    currentYear = year;
    map.clearOverlays();
    clearUpcomingTable();
    document.getElementById('displayedYear').innerHTML = year;
    
    url = "http://www.lpassociation.com/mapAJAXhelper.php?action=getXMLByYear&year=" + year;
    GDownloadUrl(url, function(data) {
        var xml = GXml.parse(data);
        var markers = xml.documentElement.getElementsByTagName("show");
        for (var i=0; i < markers.length; i++) {
            var id = markers[i].getAttribute("id");
            var band = markers[i].getAttribute("fullBandName");
            var lat = markers[i].getAttribute("lat");
            var long = markers[i].getAttribute("long");
            var location = markers[i].getAttribute("location") + ", " + markers[i].getAttribute("country");
            var date = markers[i].getAttribute("date");
            var venue = markers[i].getAttribute("venue");
            var notes = markers[i].getAttribute("notes");
            var icon = markers[i].getAttribute("icon");
            var point = new GLatLng(parseFloat(lat), parseFloat(long));
            var marker = createMarker(point, location, date, venue, notes, icon);
            map.addOverlay(marker);
            addTableRow(id, date, band, location, venue, notes, year, lat, long);
        }
    });
}

function addTableRow(id, date, band, location, venue, notes, year, lat, long) {
    var tourTable = document.getElementById("pastShows");
    var tourRow = tourTable.insertRow(-1);
    
    var dateCell = tourRow.insertCell(0);
    dateCell.innerHTML = date;
    dateCell.style.width = "15%";
    
    var bandCell = tourRow.insertCell(1);
    bandCell.innerHTML = band;
    bandCell.style.width = "15%";
    
    var locationCell = tourRow.insertCell(2);
    locationCell.innerHTML = '<a href="javascript:displayShow('+year+', '+lat+', '+long+');">'+location+'</a>';
    locationCell.style.width = "25%";
    
    var venueCell = tourRow.insertCell(3);
    venueCell.innerHTML = venue;
    venueCell.style.width = "25%";
    
    var notesCell = tourRow.insertCell(4);
    if (notes == "") {
        notesCell.innerHTML = "&nbsp;";
    } else {
        notesCell.innerHTML = notes;
    }
    notesCell.style.width = "20%";
}

function clearUpcomingTable() {

    var tourTable = document.getElementById("pastShows");
    for (var i= tourTable.rows.length - 1; i > 0; i--) {
        tourTable.deleteRow(i);
    }
}

function jumpToLocation(location) {
    geocoder.getLatLng(location, function(point) {
        if (!point) {
            alert('The city "'+location+'" was not found. Please try again.');
        } else {
            map.setCenter(point, 10);
        }
    });
}

function displayShow(showYear, showLat, showLong) {
    if (showYear != currentYear) {
        loadMarkers(showYear);
        document.getElementById("y"+currentYear).selected=true;
    }
    map.setCenter(new GLatLng(showLat, showLong), 10);
    window.location.hash = "topMap";
}

function createMarker(point, location, date, venue, notes, icon) {
    var marker = new GMarker(point, icons[icon]);
    var html = "<b>Location:</b> " + location + "<br /><b>Date:</b> " + date + "<br /><b>Venue:</b> " + venue;
    if (notes != "") {
        html = html + "<br /><br />" + notes;
    }
    GEvent.addListener(marker, 'click', function() {
        marker.openInfoWindow(html);
    });
    return marker;
}
