
// preload any images used in this JavaScript so that they show instantly.
if( document.images ) {
	img1 = new Image();
	img1.src = "images/progressbar.gif";

	img2 = new Image();
	img2.src = "images/circularArrowDown.gif";
}

// This function will either enable or disable the display of an element
// based upon the class name that is passed to it and the value of bDisplay
// Since more than one type of element can have the same class, you can use
// the attr and attrValue variables to limit what elements are affected.
// Example: add a new tag to an HTML element - <div class="skim" type="locwrap"></div>
// <input type="checkbox" class="skim"> if you call the function like the following, then
// only the div element will be affected: displayElements("skim", true, "type", "locwrap");
// className must be a string and bDisplay must be a boolean
// The scriptaculous and prototype libraries must be included for this to work correctly
function displayElements(className, bDisplay, attr, attrValue)
{
	// Ensure that we have a class name. A zero length name will result in an error.
	if( className.length > 0 )
	{
		// The getElementsByClassName is part of the scriptaculous javascipt
		var elements = document.getElementsByClassName(className);

		// loop through all of the elements and set their display value
		for( i = 0; i < elements.length; i++ )
		{
			if( elements[i].readAttribute( attr ) == attrValue ) {
				// Use the display style element so that the list will appear to shrink/grow
				// and not just have missing elements.
				if( bDisplay )
				{
					Effect.BlindDown(elements[i]);
				}
				else
				{
					Effect.BlindUp(elements[i]);
				}
			}
		}
	}
}

// This function will click on an element that has the checked property and is clickable
// It searches for elements that are contained within the wrapper element and have
// the class name that is passed to it.  It then sets the checked value of the element
// based on the bChecked value.  Wrapper and className must be strings and bChecked must be a boolean
// The scriptaculous and prototype libraries must be included for this to work correctly
function clickCheckBoxWithClassName( wrapper, className, bChecked ) {
	// The getElementsByClassName is part of the scriptaculous javascipt
	var elements = $(wrapper).getElementsByClassName(className);

	// loop through all of the elements and click them
	for( i = 0; i < elements.length; i++ )
	{
		try {
			if( elements[i].readAttribute( "type" ) == "checkbox" ) {
				// Check to see if the value of the element is the same as the passed boolean value
				if( elements[i].checked != bChecked ) {
					elements[i].click();
					elements[i].checked = bChecked;
				}
			}
		} catch (e) {;}
	}
}

// This function is called in the onclick event of a span element
// The span element should look similar to this:
// <span id="searchText" onclick="JavaScript: displaySearch(this);"></span>
// This function requires a div tag with the id="searchCriteria"
function displaySearch( span ) {
	if( span.innerHTML.indexOf("Show") == -1 ) {
		// Hide the Search Criteria
		span.innerHTML = "Show Search <img src=\"images/circularArrowUp.png\">";
		Effect.SlideUp("searchCriteria");
	} else {
		// Show the Search Criteria
		span.innerHTML = "Hide Search <img src=\"images/circularArrowDown.png\">";
		Effect.SlideDown("searchCriteria");
	}
}

// The ajax javascript must be included on the page for this to work
// There must be a div with the ID of "weatherDetailWrap" on the page
// and an input with the name tag of zipcode.
function callWeatherFunction() {
	// AJAX call.
    document.getElementById("weatherDetailWrap").innerHTML="Fetching Data: <img src='images/progressbar.gif' alt='Progress Bar'>";
    var zipcode = document.getElementsByName("zipcode");
	updateText("getWeather.php5?zipcode="+zipcode[0].value,"weatherDetailWrap");

}

/**************************************************************************/
/************    BEGIN: Map Related Functions and Variables    ************/
/**************************************************************************/
var map;
var geocoder;
var markers = new Array();
var locationID = "";
var addressText = "";

// addAddressToMap() is called when the geocoder returns an
// answer.  It adds a marker to the map with an open info window
// showing the nicely formatted version of the address and the country code.
function addAddressToMap(response) {
  if (!response || response.Status.code != 200) {
    alert("Sorry, we were unable to geocode the address:\n" + addressText);
    var element = document.getElementById(locationID);
    element.checked = false;
  } else {
    place = response.Placemark[0];
    point = new GLatLng(place.Point.coordinates[1],
                        place.Point.coordinates[0]);

    // Store these points for later comparison.
    var lat = place.Point.coordinates[1];
    var lng = place.Point.coordinates[0]

	// Loop through the existing markers to make sure that
	// we don't already have a marker at these coordinates
	// This allows markers to be toggled on and off
    var found = false;
    var marker = new GMarker(point);
    for( var i = 0; i < markers.length; i++ ) {
      if( (marker.getPoint().lat() == markers[i][0].getPoint().lat()) &&
          (marker.getPoint().lng() == markers[i][0].getPoint().lng()) ) {
        found = true;
        map.removeOverlay(markers[i][0]); // Remove the marker from the map
        markers.splice(i,1); // Remove the marker from the array
      }
    }

	// There is not a marker already at these coordinates, add a new one
    if( !found ) {
      map.addOverlay(marker);
      // This adds a function to the onclick event of a marker.
      GEvent.addListener( marker, 'click', function() {
        var foundPos = -1;
        // Loop through the existing markers to find the one that was clicked on
        for( var i = 0; i < markers.length; i++ ) {
          if( (marker.getPoint().lat() == markers[i][0].getPoint().lat()) &&
              (marker.getPoint().lng() == markers[i][0].getPoint().lng()) ) {
            foundPos = i;
          }
        }
        // Display the Info Window with the Markers Information
        marker.openInfoWindowHtml( markers[foundPos][1] );
      });
      // Add the new marker to the existing markers array.
      // The second position in the array is used as the display for the markers info window.
      markers[markers.length] = new Array(marker, place.address);
    }
  }
}

// Removes all markers from the map and clears the markers array
function clearAllOverlays() {
	for( var i = 0; i < markers.length; i++ ) {
		map.removeOverlay(markers[i][0]);
		markers.splice(i,1);
	}
}

// showLocation() is called when you click on the Search button
// in the form.  It geocodes the address entered into the form
// and adds a marker to the map at that location.
function showLocation( address, id ) {
	addressText = address;
	locationID = id;
	geocoder.getLocations(address, addAddressToMap);
}

// Creates a new Google Map object centered on the
// specified latitude and longitude with the zoom level applied
function loadMap( latitude, longitude, zoom ) {
	map = new GMap2( document.getElementById("map") );
	map.addControl( new GLargeMapControl() ); // Adds the Zoom and Pan Controls
	map.addControl( new GMapTypeControl() ); // Adds the Maps, Satellite, Hybrid Controls
	map.setCenter( new GLatLng(longitude, latitude), zoom );
	map.enableDoubleClickZoom();
	map.enableContinuousZoom();
	geocoder = new GClientGeocoder(); // Sets up the Geocoder for getting positions of coordinates and addresses
}

/**************************************************************************/
/************     END: Map Related Functions and Variables     ************/
/**************************************************************************/


/**************************************************************************/
/**********    BEGIN: Tooltip Related Functions and Variables    **********/
/**************************************************************************/
// Function called in the mouse over event
// sets the top and left of where the tooltip should appear.
function ShowTip(msg)
{
	var left = X + document.body.scrollLeft + document.documentElement.scrollLeft;
	var top = Y + document.body.scrollTop + document.documentElement.scrollTop;
	UnHideTip( top, left, msg );
}

// Function to Show the ToolTip DIV and set it's contents
function UnHideTip( top, left, tip )
{
	document.getElementById("tooltip").innerHTML = tip;
	document.getElementById("tooltip").style.top = (top+5)+"px";
	document.getElementById("tooltip").style.left = (left+10)+"px";
	// Would like to use a scriptaculous effect here, but couldn't get it to work.  Research Later.
	document.getElementById("tooltip").style.display = "block";
}

// Function to hide the ToolTip DIV and reset it's contents
function HideTip()
{
	// Prototype.js call
	Element.hide("tooltip");
	document.getElementById("tooltip").innerHTML = "";
}

// global variables set on the onmousemove event.
var X = 0;
var Y = 0;

// capture the coordinates of the mouse whenever it moves
document.onmousemove=getCoordinates;

// Function to capture the mouse movement.
function getCoordinates( evt ) {
	if( !evt ) evt = window.event;

	if( evt.pageX || evt.pageY ) {
		X = evt.pageX;
		Y = evt.pageY;
	}
	else if( evt.clientX || evt.clientY ) {
		X = evt.clientX;
		Y = evt.clientY;
	}
}
/**************************************************************************/
/**********     END: Tooltip Related Functions and Variables     **********/
/**************************************************************************/

function initTinyMCE() {
	try {
		tinyMCE.init({
		  	mode : "none",
			theme : "advanced",
			plugins : "save",
			theme_advanced_buttons1_add_before : "save,separator",
			theme_advanced_buttons1 : "bold,italic,underline,separator,strikethrough",
			theme_advanced_buttons2 : "justifyleft,justifycenter,justifyright, justifyfull",
			theme_advanced_buttons3 : "bullist,numlist,undo,redo,link,unlink",
			theme_advanced_toolbar_location : "top",
			theme_advanced_toolbar_align : "left",
			theme_advanced_path_location : "bottom",
			extended_valid_elements : "a[name|href|target|title|onclick],img[class|src|border=0|alt|title|hspace|vspace|width|height|align|onmouseover|onmouseout|name],hr[class|width|size|noshade],font[face|size|color|style],span[class|align|style]"

		});
	} catch(e) { alert(e); }
}

function toggleEditor( element, callingElement ) {
	var elm = document.getElementById( element );

	if( tinyMCE.getInstanceById( element ) == null ) {
		callingElement.innerHTML = "Cancel";
		tinyMCE.execCommand('mceAddControl', false, element);
	} else {
		var data = document.getElementById(element+'_FORCANCEL');
		callingElement.innerHTML = "Edit";
		tinyMCE.execCommand('mceSetContent', false, data.value);
		tinyMCE.execCommand('mceRemoveControl', false, element);
	}
}




