﻿var map;      
var geocoder;
var mapLocations;
    
    function showReviews(doScroll) {        
      $("reviewsContainer").style.display = isIE ? "inline-block" : "block";         
      if(doScroll)scrollToElement($("reviewsContainer"));
    }
    
    function hideReviews(doScroll){
        $("reviewsContainer").style.display = "none";
        if(doScroll)scrollToElement($("globalContainer"));
    }

    function showMap(doScroll) {
        if (doScroll) scrollToElement($("venueMapContainer"));
    }
    
    function showAll(){
        showReviews();
    }
    
    function hideAll(){
        hideReviews();
    }
    
    function showSendToPhone(doScroll){
        $("sendToPhoneContainer").style.display = isIE ? "inline-block" : "block";
        if(doScroll)scrollToElement($("sendToPhoneContainer"));
    }
    
    function hideSendToPhone(doScroll){
        $("sendToPhoneContainer").style.display = "none";
        $("sendToPhonePhoneFeedback").innerHTML = "";
        if(doScroll)scrollToElement($("sendToPhoneContainer"));      
    }
           
    function initPage(){
        Event.observe($("venueMapAddressField"), 'keypress', onMapKeyPress);     
        //initMap();
    }
    
    function onMapKeyPress(e){
        if(e.keyCode == 13){
            mapUserLocation();
            Event.stop(e);
        }
    };
    
    function checkHash(){
        if(location.hash.length > 0){
            try{
                
                var hash = location.hash.substring(1);                
                if(hash == "phone"){
                    showSendToPhone();
                }else{              
                    showAll();
                }
            }catch(e){}
        }else{
            showAll();
        }
    }
    
    // Add event for page load
    $(document).observe("dom:loaded", initPage);
    
 function sendToPhone(venueId){
 
    var url = '/sendtophone/';
    var provider = $("mobileProvider").value;
    var number = $("mobileNumber").value.replace(/[^\d]/g, "");
    var params = "p="+provider+"&n="+number+"&v="+venueId;
    
    var request = new Ajax.Request(
		url, 
		{
			method: 'get', 
			parameters: params, 
			onComplete: onSendToPhoneResponse
		});
		
	$("sendToPhonePhoneFeedback").innerHTML = "<img src='/images/sendtophone/sending_ani.gif' />";
		
 }
 
 function onSendToPhoneResponse(response){
    if(response.responseText == "true"){
        $("sendToPhonePhoneFeedback").innerHTML = "Your message has been sent!";
    }else{
        $("sendToPhonePhoneFeedback").innerHTML = "There was an error sending your message. Please try again.";
    }
 }
 
 function createMap(){
    mapLocations = new Array();
    map = new GMap2($("venueMap"));
    map.setCenter(new GLatLng(37.4419, -122.1419), 13);
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());
    geocoder = new GClientGeocoder();
 }
 
 function mapVenue(venueData){
    getLocationLatLong(venueData,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 html = "<div class='googleMapPopup'><b>"+location.name+"</b><br />"+location.address+"<br /><a href='#'>Directions</a>";
                var marker = createMapMarker(point,"/images/map/marker_location.png",html);
            }else{
                var html = "<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",html);
            }
            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 = $("venueMapAddressField").value;
    userLocation = {name:"Your Location",address:userAddress,commonName:"user"};
    getLocationLatLong(userLocation,0);
}

function createMapMarker(point,imagePath,html){
    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, "rollover", function() {
        marker.openInfoWindowHtml(html);     
        
    });
  return marker;
}

