window.onload = function()
{
	load();
}
window.unonload = function()
{
	GUnload();
}

function getGeoFromPage()
{
	var geo1 = document.getElementById("geoOne");
	var geo2 = document.getElementById("geoTwo");
	return new Array(geo1.innerHTML, geo2.innerHTML);
}

var icon = new GIcon();
function load()
{
	if (GBrowserIsCompatible())
	{
		var geo = getGeoFromPage();
    var latLong = new GLatLng(geo[0], geo[1]);
		var map = new GMap2(document.getElementById("map"));
		if( ( geo[0] != 0 ) && ( geo[1] != 0 ) )
		{
      var mapControlPosition = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(0, 32));
      map.addControl(new GSmallMapControl(), mapControlPosition);
			map.addControl(new GMapTypeControl());
			map.addMapType(G_PHYSICAL_MAP);
			icon.image = iconName;
			icon.iconSize = new GSize(iconSize[0],iconSize[1]);
			icon.iconAnchor = new GPoint(25,40);
			map.setCenter(latLong, 15);
			var point = latLong;
			var marker = createMarker(point);
			map.addOverlay(marker);
      
      // Determine whether or not to load street view
      var streetviewClient = new GStreetviewClient();
      streetviewClient.getNearestPanoramaLatLng(latLong, function (newLatLong) {
        if (null != newLatLong) {
          map.addControl(new StreetViewSuperboxControl(map, newLatLong.lat(), newLatLong.lng()));
          $.superbox.settings = {
            hideCallback: streetview_cleanup,
            boxHeight: 600,
            boxWidth:800
          };
          $.superbox();
        }
      });
      
      
      map.addControl(new NeighbourhoodSuperboxControl(map, latLong.lat(), latLong.lng()));
      $.superbox.settings = {
        hideCallback: streetview_cleanup,
        boxHeight: 600,
        boxWidth:800
      };
      $.superbox();
      
		}else
		{
			map.innerHTML = 'Map for this property cannot be displayed for some reason';
		}
	}
}

function createMarker(point)
{
	var marker = new GMarker(point,icon);
	return marker;
}

var streetPano = null;  // Streetview panorama object


/**
 * The Neighbourhood Superbox Control loads the street view in a jQuery superbox when clicked
 */
function NeighbourhoodSuperboxControl() {
}

// Subclass the GControl 
NeighbourhoodSuperboxControl.prototype = new GControl();

// By default, the control will appear in the top left corner of the
// map with 7 pixels of padding.
NeighbourhoodSuperboxControl.prototype.getDefaultPosition = function() {
  return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(2,275));
}


/**
 * Constructor for the Neighbourhood Superbox Control
 * @param {Object} map
 */
NeighbourhoodSuperboxControl.prototype.initialize = function(map) {
  var latitude = $("#geoOne").html();
  var longitude = $("#geoTwo").html(); 
  	
  var NeighbourhoodDiv = document.createElement("a");
  
  //var url = 'http://' + document.location.hostname + "/property_neighbourhood/" + latitude + '/' + longitude;
  
  $(NeighbourhoodDiv).attr('href', '#propertyMap2');
  $(NeighbourhoodDiv).html('Neighbourhoods');
  $(NeighbourhoodDiv).css('display',"block");
  $(NeighbourhoodDiv).css('height',"20px");
  $(NeighbourhoodDiv).css('width',"90px");
  $(NeighbourhoodDiv).css('color',"white");
  $(NeighbourhoodDiv).css('font',"12px Arial");
  $(NeighbourhoodDiv).css('padding',"5px 5px 0px");
  $(NeighbourhoodDiv).css('textAlign',"center");  
  $(NeighbourhoodDiv).css('background-image', "url(/modules/property/images/button.png)");
  $(NeighbourhoodDiv).click(function(){
    $('#oldmap').hide('slow');
    $('#newPropertyMap').show('slow',function(){
      if(map2 == null){
        InitMap2();
      }
    });
  });

  map.getContainer().appendChild(NeighbourhoodDiv);
  return NeighbourhoodDiv;  
}

/**
 * The StreetView Superbox Control loads the street view in a jQuery superbox when clicked
 */
function StreetViewSuperboxControl() {
}

// Subclass the GControl 
StreetViewSuperboxControl.prototype = new GControl();

/**
 * Constructor for the StreetView Superbox Control
 * @param {Object} map
 */
StreetViewSuperboxControl.prototype.initialize = function(map) {
  var coords = getGeoFromPage();
  var url = 'http://' + document.location.hostname + "/property_streetview/" + coords[0] + '/' + coords[1];
  
  var link = document.createElement("a");
  $(link).attr('href', url);
  $(link).css('border-bottom', "1px solid rgb(176, 176, 176)");
  $(link).css('border-right', "1px solid rgb(176, 176, 176)");
  $(link).css('color',"#000000");
  $(link).css('display',"block");
  $(link).css('background-color',"white");
  $(link).css('font',"12px Arial");
  $(link).css('padding',"2px 4px 0");
  $(link).css('textAlign',"center");
  $(link).css('cursor',"pointer");
  $(link).css('height',"13px");
  $(link).html('StreetView');
  $(link).attr('rel', 'superbox[ajax][' + url + '][800x600]');
  
  var streetViewDiv=document.createElement("div");
  $(streetViewDiv).css('border', "1px solid black");
  $(streetViewDiv).css('color',"#000000");
  $(streetViewDiv).css('display',"block");
  $(streetViewDiv).css('height',"16px");
  $(streetViewDiv).css('background-color',"white");
  
  streetViewDiv.appendChild(link);
  
  map.getContainer().appendChild(streetViewDiv);
  return streetViewDiv;  
}

// By default, the control will appear in the top left corner of the
// map with 7 pixels of padding.
StreetViewSuperboxControl.prototype.getDefaultPosition = function() {

  return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(272, 7));
}

/**
 * Loads a streetview object into the specified ID
 * @param {Object} elementID
 * @param {Object} latitude
 * @param {Object} longitude
 */
function streetview_load(elementID, latitude, longitude) { 
  // Set the position & orientation of the viewer
  var position = new GLatLng(latitude, longitude);
  var orientation = {
    yaw: 0,
    pitch: 0,
    roll: 0
  };
  
  var panoramaOptions = {
    latlng: position,
    pov: orientation
  };
  
  // Load the panorama
  streetPano = new GStreetviewPanorama(document.getElementById(elementID), panoramaOptions);
}

/**
 * Cleans up streetview
 */
function streetview_cleanup() {
  if(null != streetPano){
    streetPano.remove();
  }
}
