﻿// JScript File
var map = null;
var sv = null; //street view
var bounds = null;
var myPano = null;

//For use with Markers reuse
var gmarkers = [];
var gmarker_selected = null; // stores the current gmarker that's selected. 
var i = 0;

 
function initMap(mapid)
{
    if (GBrowserIsCompatible()) 
	{	  
		map = new GMap2(document.getElementById(mapid));
		map.addControl(new GSmallMapControl());
		//set defaut lat/long to Saint Louis...
		map.setCenter(new GLatLng(38.632, -90.191), 13);

        //street/hybrid/satellite
		var mapTypeControl = new GMapTypeControl();
        var topRight = new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(10,10));
        map.addControl(mapTypeControl, topRight);
        
	}    
}

//Map with StreetView
function initMapwSV(mapid, svid)
{
    if (GBrowserIsCompatible()) 
	{	
		map = new GMap2(document.getElementById(mapid));
		//I add the street view reference + the error handler
		sv = document.getElementById(svid);
		//Errors occur because location isn't available (600) or service has issues (500)
		GEvent.addListener(sv, "error", function(errorCode) {sv.style.display = "none";});
        
		
		//We use the large menu map
		map.addControl(new GLargeMapControl());
		//set defaut lat/long to Saint Louis...
		map.setCenter(new GLatLng(38.632, -90.191), 13);

		//street/hybrid/satellite
		var mapTypeControl = new GMapTypeControl();
        var topRight = new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(10,10));
        map.addControl(mapTypeControl, topRight);
        
        initSVControl();
        map.addControl(new SVControl(map));
                
	}    
}
   
 
//Marker for Business Listings w/ differentiation for Advertisers/Non-Advs
function AddMarkerBusiness(latitude, longitude, bAdvertiser, name, address, city_state, phone)
{
	if (GBrowserIsCompatible()) 
	{
	    name = name.replace("'", ""); 
	    address = address.replace("'", ""); 
	    city_state = city_state.replace("'","");
	    
	    var pnt;
	    
	    //If no latitude/longitude try the Lat/Long api from Google again
	    if (latitude == 0 && longitude == 0){
	        var geocoder = new GClientGeocoder(); 
            var strAddress = address + ' ' + city_state; 	  
            geocoder.getLatLng(strAddress, 
            function(point) { 
            if (point) {                 
                latitude = point.lat();
                longitude = point.lng();               
            } else { 
              //No latitude logitude coordinates were found...   
            } 
            }); 
	    }

	    pnt = new GLatLng(latitude, longitude);
	    	
	    //Icon to use
        var icon = new GIcon(G_DEFAULT_ICON);
          
          if (bAdvertiser){
            icon.image = "images/map_advertiser_icon.gif";
            icon.shadow = "images/map_advertiser_icon_shadow.png";
            icon.iconSize = new GSize(25.0, 32.0);
            icon.shadowSize = new GSize(42.0, 32.0);
            icon.iconAnchor = new GPoint(12.0, 16.0);
            icon.infoWindowAnchor = new GPoint(12.0, 16.0);
           
          }
          else{
            icon.image = "images/map_listing_icon.gif";
            icon.shadow = "images/map_listing_icon_shadow.png";
            icon.iconSize = new GSize(22.0, 32.0);
            icon.shadowSize = new GSize(39.0, 32.0);
            icon.iconAnchor = new GPoint(11.0, 16.0);
            icon.infoWindowAnchor = new GPoint(11.0, 16.0);

          }	
	    	
	    var marker = new GMarker(pnt, icon);
	    GEvent.addListener(marker, "click", function() {
		    marker.openInfoWindowHtml("<p style='FONT-SIZE: 11px; FONT-FAMILY: Arial; text-align:left;'><b>" + name + "</b><br>" + address + "<br>" + city_state + "<br>" + phone + "</p>");
	    });
	
	    map.addOverlay(marker);
	    	 
	    //if the data is incomplete...
	    if (latitude != 0 && longitude != 0){
	        // If this is the first marker, declare bounds
	        if(bounds == null)
		        bounds = new GLatLngBounds(new GLatLng(latitude-0.0001,longitude-0.0001),new GLatLng(latitude+0.0001,longitude+0.0001));   
	        bounds.extend(marker.getPoint());				// Extend bounds to include this marker
	    }
	    
    }
} 
 
 
 //Regular marker for business
function AddMarker(latitude, longitude, name, address, city_state, phone)
{
	if (GBrowserIsCompatible()) 
	{
	    name = name.replace("'", ""); 
	    address = address.replace("'", ""); 
	    city_state = city_state.replace("'","");
	    
	    var pnt;
	    
	    //If no latitude/longitude try the Lat/Long api from Google again
	    if (latitude == 0 && longitude == 0){
	        var geocoder = new GClientGeocoder(); 
            var strAddress = address + ' ' + city_state; 	  
            geocoder.getLatLng(strAddress, 
            function(point) { 
            if (point) {                 
                latitude = point.lat();
                longitude = point.lng();               
            } else { 
              //No latitude logitude coordinates were found...   
            } 
            }); 
	    }

	    pnt = new GLatLng(latitude, longitude);
	    	
	    var marker = new GMarker(pnt);
	    GEvent.addListener(marker, "click", function() {
		    marker.openInfoWindowHtml("<p style='FONT-SIZE: 11px; FONT-FAMILY: Arial; text-align:left;'><b>" + name + "</b><br>" + address + "<br>" + city_state + "<br>" + phone + "</p>");
	    });
	
	    map.addOverlay(marker);
	    	 
	    //if the data is incomplete...
	    if (latitude != 0 && longitude != 0){
	        // If this is the first marker, declare bounds
	        if(bounds == null)
		        bounds = new GLatLngBounds(new GLatLng(latitude-0.0001,longitude-0.0001),new GLatLng(latitude+0.0001,longitude+0.0001));   
	        bounds.extend(marker.getPoint());				// Extend bounds to include this marker
	    }
	    
    }
}


//Location
function AddMarker2(latitude, longitude, city, state)
{
	if (GBrowserIsCompatible()) 
	{
	    city = city.replace("'", ""); 
	    state = state.replace("'", ""); 
	    
	    var pnt;
	    
	    //If no latitude/longitude try the Lat/Long api from Google again
	    if (latitude == 0 && longitude == 0){
	        var geocoder = new GClientGeocoder(); 
            var strLocation = city + ', ' + state; 	  
            geocoder.getLatLng(strLocation, 
            function(point) { 
            if (point) {                 
                latitude = point.lat();
                longitude = point.lng();               
            } else { 
              //No latitude logitude coordinates were found...   
            } 
            }); 
	    }

	    pnt = new GLatLng(latitude, longitude);
	    	
	    var marker = new GMarker(pnt);
	    GEvent.addListener(marker, "click", function() {
		    marker.openInfoWindowHtml("<p style='FONT-SIZE: 14px; FONT-FAMILY: Arial; text-align:left;'><b>" + city + ", " + state + "</p>");
	    });
	
	    map.addOverlay(marker);
	    	 
	    //if the data is incomplete...
	    if (latitude != 0 && longitude != 0){
	        // If this is the first marker, declare bounds
	        if(bounds == null)
		        bounds = new GLatLngBounds(new GLatLng(latitude-0.0001,longitude-0.0001),new GLatLng(latitude+0.0001,longitude+0.0001));   
	        bounds.extend(marker.getPoint());				// Extend bounds to include this marker
	    }
	    
    }
}



//Business of Results Map
function AddMarker3(aid, sid, intposition, bAdvertiser, url, latitude, longitude, name, address, city_state, phone)
{
	if (GBrowserIsCompatible()) 
	{
	    aid = aid.replace("'","");
	    sid = sid.replace("'","");
	    name = name.replace("'", ""); 
	    address = address.replace("'", ""); 
	    city_state = city_state.replace("'","");
	    
	    var pnt;
	    
	    //If no latitude/longitude try the Lat/Long api from Google again
	    if (latitude == 0 && longitude == 0){
	        var geocoder = new GClientGeocoder(); 
            var strAddress = address + ' ' + city_state; 	  
            geocoder.getLatLng(strAddress, 
            function(point) { 
            if (point) {                 
                latitude = point.lat();
                longitude = point.lng();               
            } else { 
              //No latitude logitude coordinates were found...   
              sv.style.display = 'none';
            } 
            }); 
	    }

	    pnt = new GLatLng(latitude, longitude);
	    	
	    //Icon to use
        var icon = new GIcon(G_DEFAULT_ICON);
          
          if (bAdvertiser){
            icon.image = "images/map_advertiser_icon.gif";
            icon.shadow = "images/map_advertiser_icon_shadow.png";
            icon.iconSize = new GSize(25.0, 32.0);
            icon.shadowSize = new GSize(42.0, 32.0);
            icon.iconAnchor = new GPoint(12.0, 16.0);
            icon.infoWindowAnchor = new GPoint(12.0, 16.0);
           
          }
          else{
            icon.image = "images/map_listing_icon.gif";
            icon.shadow = "images/map_listing_icon_shadow.png";
            icon.iconSize = new GSize(22.0, 32.0);
            icon.shadowSize = new GSize(39.0, 32.0);
            icon.iconAnchor = new GPoint(11.0, 16.0);
            icon.infoWindowAnchor = new GPoint(11.0, 16.0);

          }
	    	
	    var marker = new GMarker(pnt, icon);
	    GEvent.addListener(marker, "click", function() {
	        //I save the marker selected reference...
	        gmarker_selected = marker; 
	        
	        //Init the Street View map IF the sv container is showing. Else don't even bother. 
	        //removing the IF causes buggy conditions w/ Mozilla and Chrome
	        if(sv.style.display == "block")
	            initStreetViewLookup(marker.getLatLng()); 
	    
	        var html = "<p style='FONT-SIZE: 11px; FONT-FAMILY: Arial; text-align:left;'><b><a href='" + url +"' target='_self'><b>" + name + "</b></a></b><br>" + address + "<br>" + city_state + "<br>" + phone + "</p>"; 
	        html = html + "<div class='map_marker_window_icon'><a href=\"javascript:loadBusAdd('" + aid + "','" + sid + "');\">Add</a></div><div class='map_marker_window_icon'>|</div>" ;
	        html = html + "<div class='map_marker_window_icon'><a href=\"javascript:loadC2Call('" + aid + "','" + sid + "');\">Call</a></div><div class='map_marker_window_icon'>|</div>";
	        html = html + "<div class='map_marker_window_icon'><a href=\"javascript:loadBusMap('" + aid + "','" + sid + "');\">Map</a></div><div class='map_marker_window_icon'>|</div>";
	        html = html + "<div class='map_marker_window_icon'><a href=\"javascript:loadSendListing('" + aid + "','" + sid + "');\">Friends</a></div><div class='map_marker_window_icon'>|</div>";
	        html = html + "<div class='map_marker_window_icon'><a href=\"javascript:loadSend2Mobile('" + aid + "','" + sid + "');\">Mobile</a></div><div class='map_marker_window_icon'>|</div>";
	        html = html + "<div class='map_marker_window_icon'><a href=\"javascript:loadBusReview('" + aid + "','" + sid + "');\">Review</a></div><div class='map_marker_window_icon'>|</div>";
	        html = html + "<div class='map_marker_window_icon'><a href=\"" + url +"\">More...</a></div>";
	        
		    marker.openInfoWindowHtml(html);
		    
	    });
	
	    map.addOverlay(marker);
	    //I save the marker in memory to display later
	    gmarkers[intposition] = marker; 	
	   
	    //if the data is incomplete...
	    if (latitude != 0 && longitude != 0){
	        // If this is the first marker, declare bounds
	        if(bounds == null)
		        bounds = new GLatLngBounds(new GLatLng(latitude-0.0001,longitude-0.0001),new GLatLng(latitude+0.0001,longitude+0.0001));   
	        bounds.extend(marker.getPoint());				// Extend bounds to include this marker
	    }
	    
    }
}

// Center and zoom map to show all markers
function setMapCenter()
{
    var zoom = 13;
	var centerLat = 0;
	var centerLong = 0;
	var SW;
	var NE;
	
	if (GBrowserIsCompatible()) 
	{
	    if(bounds != null)
	    {           
	        zoom = map.getBoundsZoomLevel(bounds);	// Get zoom level that includes all markers
    	    
	        var SW = bounds.getSouthWest();
		    var NE = bounds.getNorthEast();
    		
		    // Determine the center of the bounding box holding all markers
		    var centerLat = NE.lat() - (NE.lat() - SW.lat())/2;
		    var centerLong = SW.lng() - (SW.lng() - NE.lng())/2;

		    // Set the center and zoom level
		    map.setCenter(new GLatLng(centerLat,centerLong), zoom);	    
	    }
    }	    
}




function setMapCenterLatLon(latitude, longitude, zoom)
{
    map.setCenter(new GLatLng(latitude,longitude), zoom);	   
}



function ShowRecord(latitude, longitude, name, address, city_state, phone)
{
    name = name.replace("'", ""); 
	address = address.replace("'", ""); 
	city_state = city_state.replace("'","");
	
	if (map){
	    map.setCenter(new GLatLng(latitude,  longitude), 15); 
	    map.openInfoWindowHtml(map.getCenter(), "<p style='FONT-SIZE: 11px; FONT-FAMILY: Arial; text-align:left;'><b>" + name + "</b><br>" + address + "<br>" + city_state + "<br>" + phone + "</p>");
	}
}  

//Used to show the record and point the SV viewer to the correct location
function ShowRecordWSV(latitude, longitude, name, address, city_state, phone)
{
    name = name.replace("'", ""); 
	address = address.replace("'", ""); 
	city_state = city_state.replace("'","");
	
	if (map){
	    map.setCenter(new GLatLng(latitude,  longitude), 15); 
	    map.openInfoWindowHtml(map.getCenter(), "<p style='FONT-SIZE: 11px; FONT-FAMILY: Arial; text-align:left;'><b>" + name + "</b><br>" + address + "<br>" + city_state + "<br>" + phone + "</p>");
	    
	    if (sv){
            if(sv.style.display == "block")
	            initStreetViewLookup(new GLatLng(latitude,  longitude)); 
	    }
	}
}  


function ShowMarker(latitude, longitude, markerposition){
    map.setCenter(new GLatLng(latitude,  longitude), 15);
    
    if (gmarkers[markerposition] != null){
        //I select the marker that's clicked on...
        gmarker_selected = gmarkers[markerposition]; 
        
        //I simulate a click on the marker    
        GEvent.trigger(gmarker_selected, "click");
    }
}






//Receives a gLatLng object. Inits the Street View Object
function initStreetViewLookup(glatlng){
    
    var panoramaOptions = { latlng:glatlng };
    
    
    if (myPano == null)
        myPano = new GStreetviewPanorama(sv, panoramaOptions);
    else
         myPano.setLocationAndPOV(glatlng);
    
}

//Used when the user clicks the Street View Button on the map
function toggleStreetView(){
    if (sv.style.display == 'block'){
	    sv.style.display = 'none';
	    map.getContainer().style.height = '550px';
	    
	}
	else{
	    //Show the SV map and resize the canvas_map to smaller size
	    sv.style.display = 'block';
	    map.getContainer().style.height = '348px';
	    
	    var marker = gmarker_selected;
	    
	    //I need to redraw/re-init the SView object if I want it to display correct
	    //after it's been hidden
	    if (marker != null){
	        //start the sview object
	        initStreetViewLookup(marker.getLatLng());
	        
	    }
	}
}


// A SVControl is a GControl that toggles the display and hide of the Street View Map
/*****************/
/*  SV CONTROL   */
/*****************/    


// We define the function first
function SVControl() 
{}

function initSVControl(){
    // To "subclass" the GControl, we set the prototype object to
	// an instance of the GControl object
	SVControl.prototype = new GControl();

	// Creates a one DIV for each of the buttons and places them in a container
	// DIV which is returned as our control element. We add the control to
	// to the map container and return the element for the map class to
	// position properly.
	SVControl.prototype.initialize = function(map) {
	  var container = document.createElement("divSV");
	  container.style.width = "350px";

	  var zoomInDivOuter = document.createElement("div");
	  var zoomInDivInner = document.createElement("div");
	  var isActive = false;
	  
	  

	  this.setOuterButtonStyle_ (zoomInDivOuter);
	  this.setInnerButtonStyleInactive_ (zoomInDivInner, "Street View");

       
	  container.appendChild(zoomInDivOuter);
	  zoomInDivOuter.appendChild(zoomInDivInner);

        
	  GEvent.addDomListener(zoomInDivOuter, "click", toggleStreetView);

        
	  map.getContainer().appendChild(container);
	  return container;
	}

	// By default, the control will appear in the top left corner of the
	// map with 7 pixels of padding.
	SVControl.prototype.getDefaultPosition = function() {
	  return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(100, 12));
	}

	SVControl.prototype.setInnerButtonStyleInactive_ = function(button, name) {
		button.style.borderStyle = "solid";
	   	button.style.borderColor = "white"; 
		button.style.borderWidth = "1px"; 
		button.style.fontSize = "12px";
		button.style.fontWeight = "bold";
		button.title = name;
		button.innerHTML = name;
		button.style.display = "inline-block";
	}

	SVControl.prototype.setOuterButtonStyle_ = function(button) {

		button.style.border = "1px solid black"; 
		button.style.backgroundColor = "white"; 
		button.style.textAlign = "center"; 
		button.style.width = "6em"; 
		button.style.cursor = "pointer"; 
		button.style.display = "inline-block";
	}

	SVControl.prototype.setInnerButtonStyleActive_ = function(button, name) {

		button.style.borderStyle = "solid"; 
		button.style.borderColor = "white"; 
		button.style.borderWidth = "1px"; 
		button.style.fontSize = "12px"; 
		button.style.fontWeight = "bold";
		button.title = name;
		button.innerHTML = name;
	}
}