/*######################################################################################
# PHP STORE LOCATOR SCRIPT (phpscriptindex.com, phpstorelocatorscript.com)
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#
# NOTICE: (C) DB DESIGN 2007, DO NOT COPY OR DISTRIBUTE CODE WITH OUT PERMISSION
# Code is NOT open source and subject to a software license agreement. You are
# allowed to modify the software to meet the needs of your domain in accordance with
# the software license agreement.
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#
# SUPPORT: phpstorelocatorscript.com, phpscriptindex.com/support/
# EMAIL SUPPORT: phpsales@gmail.com Monday - Friday 10:00am to 5:00pm EST
######################################################################################*/

var map = '';
var liveMap = '';
var mapDiv = '';
var html = Array();
var markers = Array();
var overlayInstance = '';
var svHandle = '';
var myPano = '';


function addIconMarker(lat,lng,text){
	var point = new GLatLng(lat, lng);

	var BigIcon = new GIcon();
	BigIcon.image = "http://hdvirtualcitytours.com/hdvirtualcitytours-icon.png";
	BigIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
	BigIcon.iconSize = new GSize(50, 50);
	BigIcon.shadowSize = new GSize(50, 50);
	BigIcon.iconAnchor = new GPoint(6, 20);
	BigIcon.infoWindowAnchor = new GPoint(5, 1);
	markerOptions = { icon:BigIcon };	
	var marker = new GMarker(point,markerOptions);
	map.addOverlay(marker);
	
	GEvent.addListener(marker, "click", function(){
		marker.openInfoWindowHtml(text);	
	});
				   	
}

function addMarkers(lat,lng,text,color){
	var point = new GLatLng(lat, lng);

	var tinyIcon = new GIcon();
	tinyIcon.image = "http://labs.google.com/ridefinder/images/mm_20_"+color+".png";
	tinyIcon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
	tinyIcon.iconSize = new GSize(12, 20);
	tinyIcon.shadowSize = new GSize(22, 20);
	tinyIcon.iconAnchor = new GPoint(6, 20);
	tinyIcon.infoWindowAnchor = new GPoint(5, 1);
	markerOptions = { icon:tinyIcon };	
	var marker = new GMarker(point,markerOptions);
	map.addOverlay(marker);
	
	GEvent.addListener(marker, "click", function(){
		marker.openInfoWindowHtml(text);	
	});
				   	
}

function load(lat, lng, level) {	
	try{
		var mapDiv = document.getElementById('mapDiv');
		mapDiv.innerHTML = '<div id="map" style="width: 100%; height: 100%"></div>';
	} catch(e){
		try{
			var mapDiv = document.getElementById('smallMapDiv');	
			mapDiv.innerHTML = '<div id="map" style="width: 100%; height: 100%"></div>';
		}catch(e){
			alert('Unable to load map');
			return;
		}
	}
	
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map"));	
		map.setMapType(G_HYBRID_MAP);

		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());	
		map.addControl(new GOverviewMapControl());


	}else{
		alert('Cannot load map. Web browser does not support Google Maps');
		return;
	}

	try{
		map.setCenter(new GLatLng(lat, lng), level);
		/*
		overlayInstance = new GStreetviewOverlay();
		map.addOverlay(overlayInstance);		
		var myPano = new GStreetviewPanorama(document.getElementById("streetviewpano"));		
		GEvent.addListener(map,"click", function(overlay,latlng) {
		  myPano.setLocationAndPOV(latlng);
		})
		*/
	}catch(e){
		alert('Cannot center map');
		return;
	}
}

function toggleStreetView(button) {
  if (!overlayInstance) {
	button.value = "Hide Street View";
    overlayInstance = new GStreetviewOverlay();
    map.addOverlay(overlayInstance);
	myPano = new GStreetviewPanorama(document.getElementById("streetviewpano"));		
	svHandle = GEvent.addListener(map,"click", function(overlay,latlng) {
		  var div = document.getElementById("streetviewcontainer");
		  div.style.display='';
		  myPano.setLocationAndPOV(latlng);
		  myPano.checkResize();		  
	})		
  } else {
	button.value = "Show Street View";
    map.removeOverlay(overlayInstance);
	GEvent.removeListener(svHandle);
    overlayInstance = null;
  }
}

function loadDirectionsForm(location){
	closeWindows();
	var form = document.getElementById('dirForm');
	form.fromAddress.value = location;
	document.getElementById('directionscontainer').style.display='';
}


function loadBirdEyeView(lat,lng){
	closeWindows();
	document.getElementById('mapDiv').style.display='none';	
	document.getElementById('birdseyecontainer').style.display='';
	
	var latlng = new VELatLong(lat,lng);			  
	liveMap = new VEMap('liveMapDiv');
	liveMap.LoadMap(latlng, 14, VEMapStyle.Birdseye, false, VEMapMode.Mode2D, true, 1);	
	

	// Add a new pushpin to the center of the map.
	pinPoint = liveMap.GetCenter();
	pinPixel = liveMap.LatLongToPixel(pinPoint);
	liveMap.AddPushpin(pinPoint);	
}

function closeBirdEye(){
	document.getElementById('birdseyecontainer').style.display='none';
	document.getElementById('mapDiv').style.display='';
}


function loadStreetView(lat,lng){
	closeWindows();
	var panolatlng = new GLatLng(lat,lng);
	panoramaOptions = { latlng:panolatlng };
	myPano = new GStreetviewPanorama(document.getElementById("streetviewpano"), panoramaOptions);
	var handler = function(errorCode){
		if(errorCode){
			document.getElementById('streetviewcontainer').style.display='none';
			alert('Sorry, street view not avaiable for this location');
		}
	}
	GEvent.addListener(myPano, "error",handler);
	var loadhandler = function(i){
	    var div = document.getElementById("streetviewcontainer");
		div.style.display='';				
	}
	GEvent.addListener(myPano, "load",loadhandler);
}

function createMarker(lat, lng, html,icon, index) {
  var point = new GLatLng(lat, lng);
  var marker = new GMarker(point, icon);
  GEvent.addListener(marker, "click", function() {
    marker.openInfoWindowHtml(html);
	showInfo(index, 'hiddenDiv'+index)
  });
  return marker;
}

function showMiniMap(index){
	markers[index].showMapBlowup();
}
function showInfo(index, hiddenDivName){
	var hiddenDiv = document.getElementById(hiddenDivName);
	for(var i=0; i<html.length; i++){
		document.getElementById('itemDiv'+i).style.display = ""; //show item display
		document.getElementById('hiddenDiv'+i).style.display="none"; //hide details display
	}
	document.getElementById('itemDiv'+index).style.display = "none";
	hiddenDiv.style.display = "";
	markers[index].openInfoWindowHtml(html[index]);
	
}

//********************************************
    var drMap;
    var gdir;
    var geocoder = null;
    var addressMarker;    

    function setDirections(fromAddress, toAddress, locale) {
		document.getElementById("drOutDiv").style.display = '';		
		document.getElementById("directions").innerHTML = '';
		drMap = new GMap2(document.getElementById("drMap"));
		gdir = new GDirections(drMap, document.getElementById("directions"));
		GEvent.addListener(gdir, "error", handleErrors);		
		gdir.load("from: " + fromAddress + " to: " + toAddress, { "locale": locale });
    }

    function handleErrors(){
		if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS){
			document.getElementById("drOutDiv").style.display = 'none';		   
			alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + gdir.getStatus().code);
		}else if (gdir.getStatus().code == G_GEO_SERVER_ERROR){
			document.getElementById("drOutDiv").style.display = 'none';		   		   
			alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);
		}else if (gdir.getStatus().code == G_GEO_MISSING_QUERY){
			document.getElementById("drOutDiv").style.display = 'none';		   		   			
			alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);
		}else if (gdir.getStatus().code == G_GEO_BAD_KEY){
			document.getElementById("drOutDiv").style.display = 'none';		   		   		
			alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);
		}else if (gdir.getStatus().code == G_GEO_BAD_REQUEST){
			document.getElementById("drOutDiv").style.display = 'none';		   		   			
			alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
		}else{ 
			document.getElementById("drOutDiv").style.display = 'none';		   		   					
			alert("Sorry. I'm unable to get directions for this address.");
		}
	}