
/**********************************************
           Map Code for Dynamic map search
**********************************************/
var map;
var gdir;
var geocoder = null;
var addressMarker;

    function initialize() 
	{
		// Map code
		  if (GBrowserIsCompatible()) 
		  {      
			map = new GMap2(document.getElementById("map_canvas"));
			gdir = new GDirections(map, document.getElementById("directions"));
			GEvent.addListener(gdir, "load", onGDirectionsLoad);
			GEvent.addListener(gdir, "error", handleErrors);
			
			setDirections("Des Moines, IA", "2736 Hubbell Ave. Des Moines, IA 50317", "en_US");
		  }
		
		// Street View code
		var gvsv = new GStreetviewPanorama(document.getElementById("photo"));
		GVBuilding = new GLatLng(41.6177, -93.5646);
		myPOV = {yaw:370.64659986187695,pitch:-20};
		gvsv.setLocationAndPOV(GVBuilding, gvsv);
		GEvent.addListener(gvsv, "error", handleNoFlash);
		
		// Show the tab
		showTab( "window1", "tab1" );
      
    }
    
    function setDirections(address, toAddress, locale ) 
    {
      var fromAddress = address;
    
      gdir.load("from: " + fromAddress + " to: " + toAddress,
                { "locale": locale });
	  
	  showTab( "window2", "tab2" );
    }

    function handleErrors(){
	   if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
	     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)
	     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)
	     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_UNAVAILABLE_ADDRESS)  <--- Doc bug... this is either not defined, or Doc is wrong
	//     alert("The geocode for the given address or the route for the given directions query cannot be returned due to legal or contractual reasons.\n Error code: " + gdir.getStatus().code);
	     
	   else if (gdir.getStatus().code == G_GEO_BAD_KEY)
	     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)
	     alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
	    
	   else alert("An unknown error occurred.");
	   
	}

	function onGDirectionsLoad(){ 
      // Use this function to access information about the latest load()
      // results.

      // e.g.
      // document.getElementById("getStatus").innerHTML = gdir.getStatus().code;
	  // and yada yada yada...
	}
	
	/***********************( END map code)********************/
	
	/**********************************************************
	                   Street View code
	**********************************************************/
	// See initialize code at the top for additional settings.
    
    function handleNoFlash(errorCode) {
      if (errorCode == 603) {
        alert("Error: Flash doesn't appear to be supported by your browser");
        return;
      }
    }
	/*******************(End Street Code)******************/
	
	// TAB Navigation code
	
	/****************************************************
	 Function: login
	 description:  This function is called when the page
				 is loaded.
	****************************************************/
	 window.onload = function ()
	 {
		
		// Start tab when system loads
		showTab( "window1", "tab1" );
	 }
 
	
	/**********************************************************************
	function: showTab
	description: This function will hide all the other tabs open and show 
				 the selected tab given by the parameter
	**********************************************************************/
	function showTab( frame, tab ) 
	{
		if( frame.length < 1 ) 
		{ 
			return; 
		}
	
		// hide all windows on screen
		var page = getElementsByClass( "window" );
		if( page != null )
		{
			for( var i = 0; i < page.length; i++ )
			{
			   page[i].style.display = "none";
			}
		}
		else
		{
			alert( "Error! -- selected window was not found in the 'ShowTab()'." );
		}
		
		// Show the selected ifram.
		document.getElementById( frame ).style.display = "block";
		
		
		// Chang Tab Color to show the tab is active
		var hideTabs = getElementsByClass( "tab" );
		if( hideTabs != null )
		{
			for( var i = 0; i < hideTabs.length; i++ )
			{
			   hideTabs[i].style.background = "#f8d45d";
			   
			   hideTabs[i].style.borderBottomColor = "#000000";
			   hideTabs[i].style.borderBottomStyle = "groove";
			   hideTabs[i].style.borderBottomWidth = '2';
			   
			   hideTabs[i].style.borderStyle = "solid";
			   hideTabs[i].style.borderWidth = '1';
			}
		}
		
		// Set active tab to wight
		document.getElementById( tab ).style.background = "#ffffff";
		document.getElementById( tab ).style.borderBottomColor = "#ffffff";
		document.getElementById( tab ).style.borderStyle = "groove";
		document.getElementById( tab ).style.borderWidth = '3';
	}
	
	/***************************************************
	from: http://www.dustindiaz.com/top-ten-javascript/
	***************************************************/
	function getElementsByClass(searchClass,node,tag) {
		var classElements = new Array();
		if ( node == null )
			node = document;
		if ( tag == null )
			tag = '*';
		var els = node.getElementsByTagName(tag);
		var elsLen = els.length;
		var pattern = new RegExp('(^|\\s)'+searchClass+'(\\s|$)');
		for (i = 0, j = 0; i < elsLen; i++) {
			if ( pattern.test(els[i].className) ) {
				classElements[j] = els[i];
				j++;
			}
		}
		return classElements;
	}
	/*****************( End of TAB and Window Code)****************/