/* js Events */


function addEvent( obj, type, fn ) {
  if ( obj.attachEvent ) {
    obj['e'+type+fn] = fn;
    obj[type+fn] = function(){obj['e'+type+fn]( window.event );}
    obj.attachEvent( 'on'+type, obj[type+fn] );
  } else
    obj.addEventListener( type, fn, false );
}

function getElementsByClassName(className, tag, elm) {
	var testClass = new RegExp("(^|\\s)" + className + "(\\s|$)");
	var tag = tag || "*";
	var elm = elm || document;
	var elements = (tag == "*" && elm.all)? elm.all : elm.getElementsByTagName(tag);
	var returnElements = [];
	var current;
	var length = elements.length;
	for(var i=0; i<length; i++){
		current = elements[i];
		if(testClass.test(current.className)){
			returnElements.push(current);
		}
	}
	return returnElements;
}

addEvent(window,'load',function() {


		
	if (document.getElementById("map") && GBrowserIsCompatible && GBrowserIsCompatible()) {		
		var directionsPanel = document.getElementById("directions-panel");
		
		var directionsForm = document.getElementById("get-directions");
		directionsForm.innerHTML = 
'<form action="#" id="getDirectionsForm">' +
'		<input type="hidden" id="toDirections" name="toDirections" value="2-572 Weber Street North, Waterloo, Ontario, Canada, N2L 5C6" />' +
'		<div>' +
'			<label for="from">To get directions enter your address</label>' +
'			<input type="text" size="25" id="fromDirections" name="fromDirections" value=""/>' +
'		</div>' +
'		<input name="submit" type="submit" value="Get Directions!" />' +
'</form>';

		var getDirectionsForm = document.getElementById("getDirectionsForm");


		var map = new GMap2(document.getElementById("map"));
		map.addControl(new GSmallMapControl());
		
		var gdir = new GDirections(map, directionsPanel);
		
		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. Try to be more specific.");
			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_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. Try entering a different address.");
		}
		
		function onGDirectionsLoad(){ 
			// Use this function to access information about the latest load()
			// results.
			//alert(gdir.getStatus().code);
			// e.g.
			// document.getElementById("getStatus").innerHTML = gdir.getStatus().code;
			// and yada yada yada...
		}
		GEvent.addListener(gdir, "load", onGDirectionsLoad);
		GEvent.addListener(gdir, "error", handleErrors);
	
		getDirectionsForm.onsubmit = function() {
			var address = "from: " + this.fromDirections.value + " to:" + this.toDirections.value;
			//alert(address);
			gdir.load(address);
			return false;
		};
		
		var location = new GLatLng(43.495513,-80.548582);
		map.setCenter(location, 3);
		
		var marker = new GMarker(location);
		map.addOverlay(marker);
		
		var info = document.getElementById('address').cloneNode(true);
		GEvent.addListener(marker, 'click', function() {
			marker.openInfoWindow(info);
		});
		//marker.openInfoWindow(info);
		
	
		addEvent(window,'unload',GUnload);
	
	}
	
	if(document.getElementById('submit')) {
	
		var submit = document.getElementById('submit');
	
		//look for the form
		var form = submit.parentNode;
		while(form) {
			if(form.nodeName == 'FORM') {
				break;
			}
			form = form.parentNode;
		}
		
		if(form) {
		
			addEvent(form,'submit',function(event) {
		
				var required = getElementsByClassName('required','li',form);
				for(var i=0; i<required.length ; i++) {
					for(c in required[i].childNodes) {
						var child = required[i].childNodes[c];
						switch(child.nodeName) {
							default: break;
							case 'INPUT': 
								if(child.value.length == 0) {
									alert('Please fill in all the required information in the form.');
									
									if(event && event.preventDefault) {
										event.preventDefault();
									} else if(window.event) {
										window.event.returnValue = false;
									}
									return false;
								}
							break;
						}
					}
				}		
			
			
			});
		
		}
	
		
		
		
		
	}
		
		
});