/* $Id$ */

YAHOO.namespace("example.container");
var vendorBoxVisible=1;
var map = null;
var geocoder = null;
var gmarkers = [];
var htmls = [];
var to_htmls = [];
var from_htmls = [];
var i=0;
      
function load()
{
	//doReloadSuggestions(); // run it once to fill it in from the start
	doReloadSuggestions_IndexPage();
	//setInterval ("doReloadSuggestions()", 5000);
	//equalize();
}

function loadMap()
{
	load(); // run it once to fill it in from the start
  if (GBrowserIsCompatible())
  {
    map = new GMap2(document.getElementById("map"));
		//map.setCenter(new GLatLng(), 13);
    geocoder = new GClientGeocoder();
  }
}
    
function showAddress(address, title, user_address)
{
  if (geocoder)
  {
    geocoder.getLatLng(address,
			function(point) {
        if (!point) {
         // alert(address + " not found");
        } else {
          map.setCenter(point, 13);
          var providerMap = 'map';
          var marker = createMarker(point, providerMap, address, user_address);

          map.addOverlay(marker);
          marker.openInfoWindowHtml(htmls[0]);
			}
    });
  }
 }
   
    
    
  // functions that open the directions forms
  function tohere(i)
  {
 	 gmarkers[i].openInfoWindowHtml(to_htmls[i]);
  }
  
function fromhere(i)
{
  gmarkers[i].openInfoWindowHtml(from_htmls[i]);
}
       
// A function to create the marker and set up the event window
function createMarker(point,name,html,user_address)
{
  var marker = new GMarker(point);

  // The info window version with the "to here" form open
  to_htmls[i] = html + '<br>Directions: <b>To here</b> - <a href="javascript:fromhere(' + i + ')">From here</a>' +
     '<br>Start address:<form action="http://maps.google.com/maps" method="get" target="_blank">' +
     '<input type="text" SIZE=40 MAXLENGTH=200 name="saddr" id="saddr" value="'+ user_address+ '" /><br>' +
     '<INPUT value="Get Directions" TYPE="SUBMIT">' +
     '<input type="hidden" name="daddr" value="' + point.lat() + ',' + point.lng() + 
            // "(" + name + ")" + 
     '"/>';
  // The info window version with the "to here" form open
  from_htmls[i] = html + '<br>Directions: <a href="javascript:tohere(' + i + ')">To here</a> - <b>From here</b>' +
     '<br>End address:<form action="http://maps.google.com/maps" method="get"" target="_blank">' +
     '<input type="text" SIZE=40 MAXLENGTH=200 name="daddr" id="daddr" value="'+ user_address+ '"/><br>' +
     '<INPUT value="Get Directions" TYPE="SUBMIT">' +
     '<input type="hidden" name="saddr" value="' + point.lat() + ',' + point.lng() +
            // "(" + name + ")" + 
     '"/>';
  // The inactive version of the direction info
  html = html + '<br>Directions: <a href="javascript:tohere('+i+')">To here</a> - <a href="javascript:fromhere('+i+')">From here</a>';
	GEvent.addListener(marker, "click", function() {marker.openInfoWindowHtml(html);});
  gmarkers[i] = marker;
  htmls[i] = html;
  i++;
  return marker;
}

function changeListingImage(imgName)
{
  document.listingImage.src = imgName;
} 

function doReloadSuggestions() 
{
  
  var handleSuccess = function(o)
  {
  	var div = document.getElementById("suggestedVendorBox_1");
   if (o.responseText !== undefined)
   {
  		div.innerHTML=o.responseText;
  	}
  	else
  	{
  		//div.innerHTML="failed";
  	}
  };
  
  var handleFailure = function(o)
  {
  };
  
  var callback =
  {	
    success:handleSuccess,
  		failure:handleFailure
  };
  
  var sUrl = "/suggestedVendorPage.php";
  var request = YAHOO.util.Connect.asyncRequest("POST", sUrl, callback);
}

function doReloadSuggestions_IndexPage() 
{
  
  var handleSuccess = function(o)
  {
  	var div= document.getElementById("suggestedVendorBox_indexPage");
   if (o.responseText !== undefined)
   {
  		div.innerHTML=o.responseText;
  	}
  	else
  	{
  		//div.innerHTML="failed";
  	}
  };
  
  var handleFailure = function(o)
  {
  };
  
  var callback =
  {	
    success:handleSuccess,
  		failure:handleFailure
  };
  
  var sUrl = "/suggestedVendorPage.php?page=index";
  var request = YAHOO.util.Connect.asyncRequest("POST", sUrl, callback);
}
		
