$(document).ready(function(){
	load();
});
$(window).unload(function(){
	GUnload();
});

var marker;
var pointerMarker;
var overlayInstance = null;
var map;
var client;
var lastMarkerLocation;
var panorama;
var mapT;
var svControl;
var guyIcon;
var geocoder=null;
var bubbleOpen = false;
var contentNode;


function load() {
  client = new GStreetviewClient();
  var latlng = new GLatLng(-33.55, 150.69603);
  //var latlng = new GLatLng(-33.713613,150.311637);
  map = new GMap2(document.getElementById("map"));
  map.addControl(new GLargeMapControl());
  map.addControl(new GMapTypeControl());
  geocoder = new GClientGeocoder();
  
  svControl = new StreetViewControl();
  map.addControl(svControl);
  
  map.setCenter(latlng, 11);

  guyIcon = new GIcon(G_DEFAULT_ICON);
  guyIcon.image = "http://maps.google.com/intl/en_us/mapfiles/cb/man_arrow-0.png";
  guyIcon.transparent = "http://maps.google.com/intl/en_us/mapfiles/cb/man-pick.png";
  guyIcon.imageMap = [
        26,13, 30,14, 32,28, 27,28, 28,36, 18,35, 18,27, 16,26,
        16,20, 16,14, 19,13, 22,8
     ];
  guyIcon.iconSize = new GSize(49, 52);
  guyIcon.iconAnchor = new GPoint(25, 35);  // near base of guy's feet
  guyIcon.infoWindowAnchor = new GPoint(25, 5);  // top of guy's head

  lastMarkerLocation = latlng;
  
  marker = new GMarker(lastMarkerLocation, {icon: guyIcon, draggable: true});
  GEvent.addListener(marker, "dragend", onDragEnd);
  GEvent.addListener(marker, "click", openPanoramaBubble);
  marker.setLatLng(latlng);
  
  
  
	if(document.getElementById("searchLoc").value != ""){
		showAddress(document.getElementById("searchLoc").value,document.getElementById("searchLoc").value,'','')
	}
	if(document.getElementById("searchBus").value != ""){
		ajaxFunction();
		document.getElementById('mapQuickSearchResults').style.visibility='visible';
	}
  
}
function openPanoramaBubble() {
	bubbleOpen = true;
	contentNode = document.createElement('div');
	contentNode.style.textAlign = 'center';
	/*contentNode.style.width = '400px';
	contentNode.style.height = '200px';*/
	contentNode.style.width = '660px';
	contentNode.style.height = '360px'
	contentNode.innerHTML = 'Loading panorama';
	marker.openInfoWindow("<div id='pano' style='width:400px;height:200px;'></div>", {maxContent: contentNode, maxTitle: "Full screen"});
  
	setTimeout("doPanorama()", 500);
}

function doPanorama() {
  panorama = new GStreetviewPanorama(document.getElementById("pano"));
  panorama.setLocationAndPOV(marker.getLatLng());

  GEvent.addListener(panorama, "newpano", onNewLocation);
  GEvent.addListener(panorama, "yawchanged", onYawChange);

  var iw = map.getInfoWindow();
  GEvent.addListener(iw, "maximizeend", function() {
    panorama.setContainer(contentNode);  
    window.setTimeout("panorama.checkResize()", 5);
  });
}

function closePanoramaBubble() {
	bubbleOpen = false;
	marker.closeInfoWindow();
}

function toggleOverlay() {
  if (!overlayInstance) {
	toggleGuy(true);
    overlayInstance = new GStreetviewOverlay();
    map.addOverlay(overlayInstance);
	if (pointerMarker) {map.removeOverlay(pointerMarker);}
	svControl.turnOn_();
	openPanoramaBubble();
  } else {
	toggleGuy(false);
    map.removeOverlay(overlayInstance);
    overlayInstance = null;
	svControl.turnOff_();
	closePanoramaBubble();
  }
}

function toggleGuy(state) {
	if (state) {
		map.addOverlay(marker);
	} else {
		map.removeOverlay(marker);
	}
}

function onYawChange(newYaw) {
  var GUY_NUM_ICONS = 16;
  var GUY_ANGULAR_RES = 360/GUY_NUM_ICONS;
  if (newYaw < 0) {
    newYaw += 360;
  }
  guyImageNum = Math.round(newYaw/GUY_ANGULAR_RES) % GUY_NUM_ICONS;
  guyImageUrl = "http://maps.google.com/intl/en_us/mapfiles/cb/man_arrow-" + guyImageNum + ".png";
  marker.setImage(guyImageUrl);
}

function onNewLocation(lat, lng) {
  var latlng = new GLatLng(lat, lng);
  marker.setLatLng(latlng);
}

function onDragEnd() {
  var latlng = marker.getLatLng();
  if (panorama) {
    client.getNearestPanorama(latlng, onResponse);
  }
}

function onResponse(response) {
  if (response.code != 200) {
    marker.setLatLng(lastMarkerLocation);
  } else {
    var latlng = new GLatLng(response.Location.lat, response.Location.lng);
    marker.setLatLng(latlng);
    lastMarkerLocation = latlng;
    openPanoramaBubble();
  }
}

function StreetViewControl() {
}

StreetViewControl.prototype = new GControl();

StreetViewControl.prototype.initialize = function(map) {
  var container = document.createElement("div");

  this.buttonDiv = document.createElement("div");
  this.setButtonStyle_();
  container.appendChild(this.buttonDiv);
  GEvent.addDomListener(this.buttonDiv, "click", function() {
    toggleOverlay();
  });

  map.getContainer().appendChild(container);
  return container;
}

// By default, the control will appear in the top left corner of the
// map with 7 pixels of padding.
StreetViewControl.prototype.getDefaultPosition = function() {
  return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(262, 4));
}

// Sets the proper CSS for the given button element.
StreetViewControl.prototype.setButtonStyle_ = function() {
  this.buttonDiv.style.margin = "3px";
  this.buttonDiv.style.cursor = "pointer";
  this.buttonDiv.style.width = "93px"
  this.buttonDiv.style.height = "19px"
  this.buttonDiv.style.backgroundImage = "url(/maps/images/streetview_off.gif)"
}
// Sets the proper CSS for the given button element.
StreetViewControl.prototype.turnOn_ = function() {
  this.buttonDiv.style.backgroundImage = "url(/maps/images/streetview_on.gif)";
}
StreetViewControl.prototype.turnOff_ = function() {
  this.buttonDiv.style.backgroundImage = "url(/maps/images/streetview_off.gif)";
}

function showAddress(town,address,bname,image,w,h,id,gltlng) {
	if (geocoder) {
		if (gltlng == ""){
			if (address == ""){
				load();
				}else{
				geocoder.getLatLng(
				  address,
				  function(point) {
					if (!point) {
						if(town != ""){
						showAddress('',town,bname,image,w,h,id,gltlng)
						  //alert(point + "point not found");
						}else{
						  alert(address + " not found");
						}
					} else {
						  //alert(point );
					  map.setCenter(point, 14);
						marker.setLatLng(point);
					  //var marker = new GMarker(point);
						if (overlayInstance) {
							if (panorama) {
								client.getNearestPanorama(point, onResponse);
							}
						} else {
							pointerMarker = new GMarker(point);
							map.addOverlay(pointerMarker);
							  if(town != ""){
								  if(image != ""){
									  pointerMarker.openInfoWindowHtml("<div style='text-align:center;'><a href='../directory_details.asp?ID="+id+"'><img src='"+image+"' border='0' width='"+w+"' height='"+h+"' /><br><b>"+bname+"</b></a><br>" + address +"</div>");
								  }else{
									  pointerMarker.openInfoWindowHtml("<div style='text-align:center;'><b><a href='../directory_details.asp?ID="+id+"'>"+bname+"</a></b><br>" + address +"</div>");
								  }
							  }else{
								  
								  if(image != ""){
									  pointerMarker.openInfoWindowHtml("<div style='text-align:center;'><a href='../directory_details.asp?ID="+id+"'><img src='"+image+"' border='0' width='"+w+"' height='"+h+"' /><br><b>"+bname+"</b></a><br>Address unavailable - " + address +"</div>");
								  }else{
									  pointerMarker.openInfoWindowHtml("<div style='text-align:center;'><b><a href='../directory_details.asp?ID="+id+"'>"+bname+"</a></b><br>Address unavailable - " + address +"</div>");
								  }
								  }
							}
					  }
				  }
				);
			  }
		}else{
			 var glat1;
			 var glng1;
			 if(gltlng != undefined){
				 for(var i=1; i<=gltlng.length; i++){
					 if(gltlng.substring(i-1,i) == ",")
						{
							glat1 = gltlng.substring(0,i-1);
							glng1 = gltlng.substring(i,gltlng.length);
						}
					 }
				 var point1 = new GLatLng(parseFloat(glat1),
								parseFloat(glng1));
				  map.setCenter(point1, 14);
					marker.setLatLng(point1);
					if (overlayInstance) {
						if (panorama) {
							client.getNearestPanorama(point1, onResponse);
						}
					} else {
						pointerMarker = new GMarker(point1);
					  map.addOverlay(pointerMarker);
					  if(town != ""){
						  if(image != ""){
							  pointerMarker.openInfoWindowHtml("<div style='text-align:center;'><a href='../directory_details.asp?ID="+id+"'><img src='"+image+"' border='0' width='"+w+"' height='"+h+"' /><br><b>"+bname+"</b></a><br>" + address +"</div>");
						  }else{
							  pointerMarker.openInfoWindowHtml("<div style='text-align:center;'><b><a href='../directory_details.asp?ID="+id+"'>"+bname+"</a></b><br>" + address +"</div>");
						  }
					  }else{
						  if(image != ""){
							  pointerMarker.openInfoWindowHtml("<div style='text-align:center;'><a href='../directory_details.asp?ID="+id+"'><img src='"+image+"' border='0' width='"+w+"' height='"+h+"' /><br><b>"+bname+"</b></a><br>Address unavailable - " + address +"</div>");
						  }else{
							  pointerMarker.openInfoWindowHtml("<div style='text-align:center;'><b><a href='../directory_details.asp?ID="+id+"'>"+bname+"</a></b><br>Address unavailable - " + address +"</div>");
						  }
					  }
				  }
			  }
		}
	}
}

function gotoregion(gltlng) {
if (gltlng == ""){
	load();
	}else{
	if (geocoder) {
			 var glat1;
			 var glng1;
			 if(gltlng != undefined){
				 for(var i=1; i<=gltlng.length; i++){
					 if(gltlng.substring(i-1,i) == ",")
						{
							glat1 = gltlng.substring(0,i-1);
							glng1 = gltlng.substring(i,gltlng.length);
						}
					 }
				 var point1 = new GLatLng(parseFloat(glat1),
								parseFloat(glng1));
				  map.setCenter(point1, 11);
					marker.setLatLng(point1);
					if (overlayInstance) {
						if (panorama) {
							client.getNearestPanorama(point1, onResponse);
						}
					} else {
						pointerMarker = new GMarker(point1);
				  }
			  }
		}
	}
}

	
function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

function closeSearchWindow() {
	document.getElementById('mapQuickSearchResults').style.visibility='hidden'
}

function ajaxFunction()
  {  var xmlHttp;
  try
    {    // Firefox, Opera 8.0+, Safari    
	xmlHttp=new XMLHttpRequest();
	}
  catch (e)
    {    // Internet Explorer    
	try
      {      
	  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); 
	  }
		catch (e)
		  {      
		  try
			{        
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); 
			}
		  catch (e)
			{        
			alert("Your browser does not support AJAX!");
			return false;
			}
		}
	}
    xmlHttp.onreadystatechange=function()
      {
      if(xmlHttp.readyState==4)
        {
        document.getElementById("searchresults").innerHTML=xmlHttp.responseText;
        }
      }
   	xmlHttp.open("GET","searchresults.asp?name="+document.getElementById("searchBus").value+"",true);
    xmlHttp.send(null);
	}