var otherPointArray = new Array(0);
var starPointArray = new Array(0);
var mapViewed = false;
var opArray = new Array(0);
var spArray = new Array(0);
var map; 

function setSPArray(spArray)
{
	starPointArray = spArray;
}
function setOPArray(opArray)
{
	otherPointArray = opArray;
}

var tabViewMap = {'mapView':document.getElementById('mapView'),'listView':document.getElementById('listView')}; 
var tabMap = {'mapTab':document.getElementById('mapTab'),'listTab':document.getElementById('listTab')}; 

function toggleTabs(tabId)
{
    //cycle over views
    for(var tabKey in tabViewMap)
    {
        if(tabKey != tabId+"View")
        {
            tabViewMap[tabKey].style.display = "none";
        }
        else
        {
            tabViewMap[tabKey].style.display = "block";
        }
    }
    
    //cycle over tabs       
    for(var tabKey in tabMap)
    {
        if(tabKey != tabId+"Tab")
        {
            tabMap[tabKey].className = "tab";
        }
        else
        {
            tabMap[tabKey].className = "tab selectedTab";
        }
    }
    
}


function gmapLoadUp()
{
  if (GBrowserIsCompatible()) 
  { 
    map = new GMap2(document.getElementById("google_map"));
    map.addControl(new GLargeMapControl());
    //map.addControl(new GScaleControl()) ;
    map.enableScrollWheelZoom();
    map.addControl(new GMapTypeControl());
    map.removeMapType(G_SATELLITE_MAP);
    
    map.setMapType(G_NORMAL_MAP);
  }
}

function updatePoints()
{
	var bounds = new GLatLngBounds();
	//console.log("Adding " +otherPointArray.length+ " other points.");
    for(var i = 0; i < otherPointArray.length; i++)
    {
        plotPoint(otherPointArray[i], false);
        bounds.extend(new GLatLng(otherPointArray[i].latitude, otherPointArray[i].longitude));
    }

    //console.log("Adding " +starPointArray.length+ " star points.");
    for(var i = 0; i < starPointArray.length; i++)
    {
        plotPoint(starPointArray[i], true);
        bounds.extend(new GLatLng(starPointArray[i].latitude, starPointArray[i].longitude));
    }

    map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));	
}


function Point(id, latitude, longitude, name, description)
{
  this.id=id;
  this.latitude=parseFloat(latitude);
  this.longitude=parseFloat(longitude);
  this.name=name;
  this.description=description;
  this.clicked = false;
}



function getDescription(point, marker)
{
	dojo.xhrGet({
	    url:"http://www.wininganddining.co.za/ShowAreaMap!descriptionBlock?restaurantId="+point.id,
	    handleAs:"text",
	    load: function(data){
		marker.openInfoWindowHtml(data, {});
		point.description = data;
		point.clicked = true;
		}
	});
}


function plotPoint(point, star)
{
   var markerOptions;
    
    //console.log("Plotting point (" + point.latitude+ ", " + point.longitude + ")");
   var marker = new GMarker(new GLatLng(point.latitude, point.longitude), {title: point.name});
  
    GEvent.addListener(marker, "click", function() {
    marker.openInfoWindowHtml(point.description, {});
    if(!point.clicked)
    {
    	getDescription(point, marker);
    }
 });
   map.addOverlay(marker);

   if(!(dojo.isIE && dojo.isIE < 7) &&  star)
       marker.setImage("images/green_marker.png");
   //else
       //marker.setImage("images/turq_marker.png");    
}

function viewMap(gmapURL)
{
	if(!mapViewed)
	{
		gmapLoadUp();
		//dojo json stuff
		// get some data, convert to JSON
dojo.xhrGet({
    url:gmapURL,
    handleAs:"json",
    load: function(data){
        //for(var i in data){
        //   console.log( i, " : ", data[i]);
        //}
        placePoints(data);
    }
});
	    mapViewed = true;
	}
}


function placePoints(data)
{
    
    for(var i in data["restaurantPoints"])
    {
         //console.log("name: ", data["restaurantPoints"][i][0], "lat: ", data["restaurantPoints"][i][1], "long: ", data["restaurantPoints"][i][2]);
         spArray[spArray.length] = new Point(data["restaurantPoints"][i][3], data["restaurantPoints"][i][1], data["restaurantPoints"][i][2], data["restaurantPoints"][i][0],"<table><tr><td width='400' height='120' align='center' valign='middle'><img src='images/loading.gif'/></td></tr></table>");
    }
    setSPArray(spArray);
    setOPArray(opArray);
    updatePoints();
}   

function popUp(URL, id) {
	day = new Date();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=520,height=495,left = 470,top = 200');");
	}

_factories = [
              function(){return new XMLHttpRequest();},
              function(){return new ActiveXObject("Msxml2.XMLHTTP");},
              function(){return new ActiveXObject("Microsoft.XMLHTTP");}
          ];
          
          _factory = null;
          
          newRequest = function(){
              if(_factory != null) return _factory();
                  
              for(var i = 0; i < _factories.length; i++)
              {
                  try
                  {
                      var factory = _factories[i];
                      var request = factory();
                      if(request != null)
                      {
                          _factory = factory;
                          return request;
                      }
                  }
                  catch(e)
                  {
                      continue;       
                  }       
              
              }
              
              _factory = function()
              {
                  throw new Error("XMLHttpRequest not supported");
              }
              _factory();
          }
          
          
          function showPhoneDetails()
          {
              var phoneDetails = document.getElementById("phone");
              var requestPhoneDetails = document.getElementById("requestPhone");
              requestPhoneDetails.style.display = "none";
              phoneDetails.style.display = "inline";
          }
          
          function doPost(restaurantId)
          {
        	  //request.open("POST",'/restaurant/RequestPhoneNumber.action?id=1101', true)
        		dojo.xhrPost({
        		    url:"/restaurant/RequestPhoneNumber.action?id="+restaurantId
        		});
          }

