
/***
 * function to load the google map for a single business
 * elementID: The element id
 * latitude: the latitude of the current business location
 * longitude: the longitude of the current busieness location
 * data: The business dada 
 */
var map;
var data; 
var address; 
var gmarkers = [];
var htmls = [];
var gindex = 0;

function loadMap(elementID,  info){

    data = info;
	if (GBrowserIsCompatible()) {
    // Initialize the map.
    map = new GMap2(document.getElementById(elementID));

    map.addControl(new GLargeMapControl());
    	
    // Create new geocoding object
    var geocoder = new GClientGeocoder();

    var content = "";
	content += data['company_name'];
	content += '<h3 class="biz_info_h3">location</h3>';
	content += data['address'] + ' ' + data['city'] + ' ' + data['state'] + ' ' + data['postal_code'];
	content += '<br /> phone number: ' + data['phone_number'] + '<br />' 
	data['fax_number'] ? content+= 'fax number: ' : + data['fax_number'];
    	
	address = data['address'] + ' ' + data['city'] + ' ' + data['state'];

    // Retrieve location information, pass it to addToMap()
    geocoder.getLocations(address, addToMap);

   }
}
   
   function addToMap(response){
   	  //Retrieve the object
   	  if (!response || response.Status.code != 200) {
        //can't find the place, find by postal code
   	  	var geocoder = new GClientGeocoder();
   	  	geocoder.getLocations(data['postal_code'], addToMap);
   	  	
      } else {
        var place = response.Placemark[0];
        var point = new GLatLng(place.Point.coordinates[1],
                            place.Point.coordinates[0]);
              // Center the map on this point
        var html = '<div style="width: 180px; padding: 5px;">' + address + " " + data['postal_code'] + '</div>';
        marker = createMarker(point, html);
        map.setCenter(point, 13);
        map.addOverlay(marker);
      }
   }
   
   function mapHome(data){
   	   if (GBrowserIsCompatible() && data != null && data.length != 0) {
   	   	
   	   	 $('#map').width(250);
   	   	 $('#map').height(250);
   	   	 var map = new GMap2(document.getElementById('map'));
   	   	 map.addControl(new GSmallZoomControl() );
         map.setCenter(new GLatLng(0,0),0);
         var bounds = new GLatLngBounds();

         for(var i = 0; i < data.length; i++){
           var company = data[i];

           var latitude = company['latitude'];
           var longitude = company['longitude'];
 
           var point = new GLatLng(latitude, longitude);
           var width = outputStar(company['rate']);
          
           var companyName = company['company_name'].replace(/\s/g, '-');
           var content = "";
	     	    //according to the rate, calculate the width for star
	       content += '<div style=" text-align: left;">' + '<span style="color: #ff0000; font-weight: bold;"><a href="/'+ currentCity + '/view/'+ companyName + '-' + company['ID'] 
	     	             +'/ " class="biz_tit_link">' + company['company_name'] +  '</a></span>' + 
	      		'<div class="rate"><div class="rate_star"  id = "rate" ><span style="width:' + width + 'px "></span></div>' + company['rate'] + '/5 (' + company['num_of_rate'] + ' reviews)';
	       content += '<br /><a href="/'+ currentCity + '/view/'+ companyName + '-' + company['ID'] 
	     	             +'/ " class="biz_tit_link">view</a>';	
	       content += '</div>';

           var marker = createNoPopupMarker(point, content);
           map.addOverlay(marker);
           bounds.extend(point);
         }

        map.setZoom(map.getBoundsZoomLevel(bounds));
        map.setCenter(bounds.getCenter());
        
        hoverPopup();
   	   }
   }
   
   function mapHomeStatic(data, key) {
   		var url = "http://maps.google.com/staticmap?key="+key+"&sensor=false&zoom=12&size=256x256&maptype=mobile";
		if ( data.length > 0 ) {
			url += "&markers=";
			for (var i = 0; i < data.length; i++) {
				var company = data[i];
				var latitude = company['latitude'];
           		var longitude = company['longitude'];
				if (i>0) {
					url += "%7C";
				}
				url += latitude+","+longitude+",redc";
			}	
		}
		$("#map").html('<img width="250px" height="250px" src="'+url+'" alt="Business map" />');
   }
   
  function createNoPopupMarker(point, html){
  	   var marker = new GMarker(point);

   	   gmarkers[gindex] = marker;
       htmls[gindex] = html;
       gindex++;
       
   	   return marker;
  }
  
  function createMarker(point, html){
   	   var marker = new GMarker(point);
   	   GEvent.addListener(marker, "click", function(){
   	   	 marker.openInfoWindowHtml(html);
   	   })
   	   
   	   gmarkers[gindex] = marker;
       htmls[gindex] = html;
       gindex++;
       
       
   	   return marker;
   }
   
   function hoverPopup()
   {
	$('a.featured_business').hover(
	  function(){
	   //var i = $(this).attr('rel');
	   //gmarkers[i].openInfoWindowHtml(htmls[i]);
	   
	}, function(){

	})
}
