
    var YWSID = "MyAM8CMFb9u7yGs5j09O0A"; // common required parameter (api key)

    var map = null;
    var icon = null;
    
	
	function staticLoadScript(url)
	{
   		document.write('<script src="', url, '" type="text/JavaScript"><\/script>');
	}	
    //Warm up!
	staticLoadScript("http://maps.google.com/maps?file=api&amp;v=2&amp;key=ABQIAAAAce8n384XzKbDXldI6sYROBTspF67zkHX0Y3F4uFnDsUCAhm3lBTAngKorBJwWFrwAFKbeG9cj521nQ");
  //END WARMUP

	/*
     * Creates the map object and calls setCenterAndBounds
     * to instantiate it.
     */
    function load() {
		map = new GMap2(document.getElementById("map"));
        //GEvent.addListener(map, "load", function() {updateMap();});    
        map.setCenter(new GLatLng(37.7916,-122.4418),13);
        map.addControl(new GLargeMapControl());
        map.addControl(new GMapTypeControl());
        map.setMapType(G_NORMAL_MAP);
                
        if (window.attachEvent) window.attachEvent("onresize", function() { map.checkResize()} );
        else if (window.addEventListener) window.addEventListener("resize", function() { map.checkResize()}, false);

        // setup our marker icon
        icon = new GIcon();
        icon.image = "/images/yelp/marker_star.png";
        icon.shadow = "/images/yelp/marker_shadow.png";
        icon.iconSize = new GSize(20, 29);
        icon.shadowSize = new GSize(38, 29);
        icon.iconAnchor = new GPoint(15, 29);
        icon.infoWindowAnchor = new GPoint(15, 3);
		
		//
		hotel_icon = new GIcon();
        hotel_icon.image = "/images/yelp/hotel_marker.png";
        hotel_icon.shadow = "/images/yelp/marker_shadow.png";
        hotel_icon.iconSize = new GSize(20, 29);
        hotel_icon.shadowSize = new GSize(38, 29);
        hotel_icon.iconAnchor = new GPoint(15, 29);
        hotel_icon.infoWindowAnchor = new GPoint(15, 3);
		
		entertainment_icon = new GIcon();
        entertainment_icon.image = "/images/yelp/entertainment_marker.png";
        entertainment_icon.shadow = "/images/yelp/marker_shadow.png";
        entertainment_icon.iconSize = new GSize(20, 29);
        entertainment_icon.shadowSize = new GSize(38, 29);
        entertainment_icon.iconAnchor = new GPoint(15, 29);
        entertainment_icon.infoWindowAnchor = new GPoint(15, 3);
    }

    /*
     * Construct the URL to call for the API request
     */
    function constructYelpURL(term) {
		if (!term)
		{
		  term = "san francisco";	
		}
        var mapBounds = map.getBounds();
        var URL = "http://api.yelp.com/" +
            "business_review_search?"+
            "callback=" + "handleResults" +
            "&category=" + "hotels" + 
            "&num_biz_requested=50" +
			"&radius=5"+
            "&location=" + term +
            "&ywsid=" + YWSID;
        return encodeURI(URL);
    }

function constructYelpSearchURL(city,term) {
		if (!city)
		{
		  city = "san francisco";	
		}
        var mapBounds = map.getBounds();
        var URL = "http://api.yelp.com/" +
            "business_review_search?"+
            "callback=" + "handleResults" +
            "&category=" + term + 
            "&num_biz_requested=100" +
			"&radius=5"+
            "&location=" + city +
            "&ywsid=" + YWSID;
        return encodeURI(URL);
    }

    /*
     * Called on the form submission: updates the map by
     * placing markers on it at the appropriate places
     */
    function updateMap() {
        // turn on spinner animation
        //document.getElementById("spinner").style.visibility = 'visible';

        var yelpRequestURL = constructYelpURL();

        /* clear existing markers */
        map.clearOverlays();
        
        /* do the api request */
        var script = document.createElement('script');
        script.src = yelpRequestURL;
        script.type = 'text/javascript';
        var head = document.getElementsByTagName('head').item(0);
        head.appendChild(script);
        return false;
    }
	
	
	
	 function updateMapLocation(term) {
        // turn on spinner animation
        //document.getElementById("spinner").style.visibility = 'visible';

        var yelpRequestURL = constructYelpURL(term);

        /* clear existing markers */
        map.clearOverlays();
        
        /* do the api request */
        var script = document.createElement('script');
        script.src = yelpRequestURL;
        script.type = 'text/javascript';
        var head = document.getElementsByTagName('head').item(0);
        head.appendChild(script);
        return false;
    }

    function addToMap(city,term)
	{
	  var yelpRequestURL = constructYelpSearchURL(city,term);
	  script = document.createElement('script');
      script.src = yelpRequestURL;
      script.type = 'text/javascript';
      var head = document.getElementsByTagName('head').item(0);
       head.appendChild(script);	
			
	}
    function filterRequests(address,text)
	{
		/* clear existing markers */
        map.clearOverlays();
		
	  var terms = "";
	  if (document.getElementById("hotels").checked)
		addToMap(address,"hotels");
	  if (document.getElementById("food").checked)
		addToMap(address,"food");
	  if (document.getElementById("entertainment").checked)
		addToMap(address,"arts");
		
		if (text)
		  showAddress(address,text);
		return false;
	}

    /*
     * If a sucessful API response is received, place
     * markers on the map.  If not, display an error.
     */
    function handleResults(data) {
        // turn off spinner animation
        //document.getElementById("spinner").style.visibility = 'hidden';
        if(data.message.text == "OK") {
            if (data.businesses.length == 0) {
                alert("Error: No businesses were found near that location");
                return;
            }
            for(var i=0; i<data.businesses.length; i++) {
                biz = data.businesses[i];
				//alert(biz.categories[0].category_filter );
                createMarker(biz, new GLatLng(biz.latitude, biz.longitude), i,biz.categories[0].category_filter);
            }
        }
        else {
           //alert("Error: " + data.message.text);
        }
    }

    /*
     * Formats and returns the Info Window HTML 
     * (displayed in a balloon when a marker is clicked)
     */
    function generateInfoWindowHtml(biz) {
        var text = '<div class="marker">';

        // image and rating
        text += '<img class="businessimage" src="'+biz.photo_url+'"/>';

        // div start
        text += '<div class="businessinfo">';
        // name/url
        text += '<a href="'+biz.url+'" target="_blank">'+biz.name+'</a><br/>';
        // stars
        text += '<img class="ratingsimage" src="'+biz.rating_img_url_small+'"/>&nbsp;based&nbsp;on&nbsp;';
        // reviews
        text += biz.review_count + '&nbsp;reviews<br/><br />';
        // categories
        text += formatCategories(biz.categories);
        // neighborhoods
        if(biz.neighborhoods.length)
            text += formatNeighborhoods(biz.neighborhoods);
        // address
        text += biz.address1 + '<br/>';
        // address2
        if(biz.address2.length) 
            text += biz.address2+ '<br/>';
        // city, state and zip
        text += biz.city + ',&nbsp;' + biz.state + '&nbsp;' + biz.zip + '<br/>';
        // phone number
        if(biz.phone.length)
            text += formatPhoneNumber(biz.phone);
        // Read the reviews
        text += '<br/><a href="'+biz.url+'" target="_blank">Read the reviews »</a><br/>';
        // div end
        text += '</div></div>'
        return text;
    }

    /*
     * Formats the categories HTML
     */
    function formatCategories(cats) {
        var s = 'Categories: ';
        for(var i=0; i<cats.length; i++) {
            s+= cats[i].name;
            if(i != cats.length-1) s += ', ';
        }
        s += '<br/>';
        return s;
    }

    /*
     * Formats the neighborhoods HTML
     */
    function formatNeighborhoods(neighborhoods) {
        s = 'Neighborhoods: ';
        for(var i=0; i<neighborhoods.length; i++) {
            s += '<a href="' + neighborhoods[i].url + '" target="_blank">' + neighborhoods[i].name + '</a>';
            if (i != neighborhoods.length-1) s += ', ';
        }
        s += '<br/>';
        return s;
    }

    /*
     * Formats the phone number HTML
     */
    function formatPhoneNumber(num) {
        if(num.length != 10) return '';
        return '(' + num.slice(0,3) + ') ' + num.slice(3,6) + '-' + num.slice(6,10) + '<br/>';
    }
    
    /*
     * Creates a marker for the given business and point
     */
    function createMarker(biz, point, markerNum,specialmarker) {
        var infoWindowHtml = generateInfoWindowHtml(biz)
        var marker;
		
		if (specialmarker == "arts" || specialmarker == "arcades" ||  specialmarker =="galleries" || specialmarker =="gardens" ||specialmarker =="casinos" ||specialmarker =="movietheaters" ||specialmarker =="jazzsandblues" ||specialmarker =="museums" ||specialmarker == "musicvenues" || specialmarker =="theater" || specialmarker =="sportsteams" ||specialmarker =="psychic_astrology" ||specialmarker == "social_clubs" ||specialmarker =="stadiumsarenas" || specialmarker == "wineries" )
		 marker = new GMarker(point, entertainment_icon);
		else if (specialmarker == "hotels")
		  marker = new GMarker(point, hotel_icon);
		  else
		   marker = new GMarker(point, icon);
        map.addOverlay(marker);
        GEvent.addListener(marker, "click", function() {
            marker.openInfoWindowHtml(infoWindowHtml, {maxWidth:800});
        });
        // automatically open first marker
        if (markerNum == 0)
            marker.openInfoWindowHtml(infoWindowHtml, {maxWidth:800});
    }
function showAddress(address,thetext) {
	var geocoder = new GClientGeocoder();
  geocoder.getLatLng(
    address,
    function(point) {
      if (!point) {
        alert(address + " not found");
      } else {
        map.setCenter(point, 12);
        var marker = new GMarker(point);
        map.addOverlay(marker);
        marker.openInfoWindowHtml(thetext);
      }
    }
  );
}
