﻿var venueList = [];
var map;      
var geocoder;
var mapLocations;

function initMap(){
    mapLocations = new Array();
    $("multiVenueMapContainer").style.display = isIE ? "inline-block" : "block";  
    createMap();
    Event.observe($("multiVenueMapAddressField"), 'keypress', onMapKeyPress);          
}

function onMapKeyPress(e){      
    if(e.keyCode == 13){
        mapUserLocation();
        Event.stop(e);
    }
};


function createMap(){
    map = new GMap2(document.getElementById("multiVenueMap"));
    map.setCenter(new GLatLng(37.4419, -122.1419), 13);
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());
    geocoder = new GClientGeocoder();
    for(var i=0; i<venueList.length; i++){
        getLocationLatLong(venueList[i],i+1);
    }
}

function getLocationLatLong(location,iconNumber){
    var callback = function(response){
        if (!response || response.Status.code != 200) {
            //alert("Sorry, we were unable to geocode the address for "+location.name);
        } else {
            place = response.Placemark[0];
            point = new GLatLng(place.Point.coordinates[1],place.Point.coordinates[0]);
            
            if(location.commonName != "user"){
                var info = "<div class='googleMapPopup'><b>"+location.name+"</b><br />"+location.address+"<br /><a href='#'>Directions</a>";
                var marker = createMapMarker(point,"/images/map/marker_"+iconNumber+".png",info);
            }else{
                var info = "<div class='googleMapPopup'><b>"+location.name+"</b><br />"+location.address+"<br /><a href='#'>Directions</a>";
                var marker = createMapMarker(point,"/images/map/marker_"+location.commonName+".png",info);
            }
            mapLocations.push({name:location.name,
                            address:location.address,
                            commonName:location.commonName,
                            point:point,
                            marker:marker
            });
            refreshMap();
        }                                
    }
    geocoder.getLocations(location.address, callback);
}

function refreshMap(){
    map.clearOverlays();
    var bounds = new GLatLngBounds();
    for(var i=0; i<mapLocations.length; i++){
        map.addOverlay(mapLocations[i].marker);
        bounds.extend(mapLocations[i].point);
    }
    map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));        
}

function mapUserLocation(){        
    var userAddress = $("multiVenueMapAddressField").value;
    userLocation = {name:"Your Location",address:userAddress,commonName:"user"};
    getLocationLatLong(userLocation,0);
}

function createMapMarker(point,imagePath,infoHtml){
    var baseIcon = new GIcon();
    baseIcon.iconSize = new GSize(41, 41);
    baseIcon.iconAnchor = new GPoint(9, 34);
    baseIcon.infoWindowAnchor = new GPoint(9, 2);
    baseIcon.infoShadowAnchor = new GPoint(18, 25);        
    var sneakyIcon = new GIcon(baseIcon);
    sneakyIcon.image = imagePath;
    markerOptions = {icon:sneakyIcon};
    var marker = new GMarker(point, markerOptions);
    GEvent.addListener(marker, "click", function() {
        marker.openInfoWindowHtml(infoHtml);
    });
    return marker;
}

function showVideo(vidId){
    Lightview.show({
      href: 'http://www.sneakysunday.tv/videoplayer/?bctid='+vidId,
      rel: 'iframe',          
      options: {
        width: 800,
        height: 540,
        menubar: false
      }
    });
    window.location = "#"+vidId;
}