/*############## Global Variables ##############*/
var map;
var directions;
var tsp_locations = new Array (); //Filled by parse_location()

/*############## tsp_location Constructor ##############*/
function tsp_location (branch_name, latitude, longitude, address)
{
	this.branch_name = branch_name;
	this.point = new GLatLng (latitude, longitude);
	this.marker = createMarker (this.point, address, this.branch_name);
}

/*############## Create TSP Main Map ##############*/
function load_map ()
{
	/*##### Check to make sure components are in place #####*/
	if (!document.getElementById("Addresses")) 
	{
		return false;
	}
	else
	{
		var addresses = document.getElementById("Addresses");
		var message = document.getElementById("LocationMessage");
	}
	if (!GBrowserIsCompatible()) return false;
	
	/*##### Create Map Container #####*/
	var map_container = document.createElement("div");
	map_container.setAttribute("id", "map_container");
	if(message)
	{
		addresses.parentNode.insertBefore(map_container, message);
	}
	else
	{
		addresses.parentNode.appendChild(map_container);
	}
	
	/*##### Load GMap into Map Container #####*/
	map = new GMap2(map_container);
	map.setCenter(new GLatLng ('47.547481', '-122.318691'), 9); //Centered on Georgetown, zoomed to see WA branches
	map.addControl(new GSmallMapControl());
	
	var entries = getElementsByClass('vcard', addresses, 'li');
	parse_locations (entries); //Load Location info from #Addresses

	/*##### Load Markers onto GMap #####*/
	for (var i=0; i<tsp_locations.length; i++)
	{
		map.addOverlay (tsp_locations[i].marker);
	}
	
	/*##### Create Directions Form and Container #####
	<form id='source_form'>
		<h2>Get Directions</h2>
		<p>Simply fill out your address in the field below, select a Branch Location either from the map or list above, and hit the "Get Directions" button.</p>
		<label>Your Address: <input type='text' id='source_address' /></label>
	</form>
	<div id='directions_container'></div>*/
	var source_form = document.createElement('form');
	source_form.setAttribute('id', 'source_form');
	var form_title = document.createElement('h2');
	form_title.appendChild(document.createTextNode('Get Directions'));
	var form_text = document.createElement('p');
	form_text.appendChild(document.createTextNode('Simply fill out your address in the field below, select a Branch Location either from the map or list above, and hit the "Get Directions" button.'));
	source_form.appendChild(form_title);
	source_form.appendChild(form_text);
	
	var source_address_label = document.createElement('label');
	source_address_label.appendChild(document.createTextNode('Your Address'));
	var source_address = document.createElement('input');
	source_address.setAttribute('type', 'text');
	source_address.setAttribute('id', 'source_address');
	source_address_label.appendChild(source_address);
	source_form.appendChild(source_address_label);
	
	addresses.parentNode.appendChild(source_form);
	
	var directions_container = document.createElement('div');
	directions_container.setAttribute('id', 'directions_container');
	addresses.parentNode.appendChild(directions_container);
	directions = new GDirections(map, directions_container);
}

/*############## Create tsp_locations from #Addresses ##############*/
function parse_locations (entries)
{
	for (var i=0; i < entries.length; i++)
	{
		var branch_name = entries[i].getAttribute("id");
		var address = entries[i].cloneNode('true');
		
		//Set up for the "Get Directions" button
		var directions_button = document.createElement('input');
		directions_button.setAttribute('type','button');
		directions_button.setAttribute('value', 'Get Directions');
		directions_button.onclick = get_directions;
		address.appendChild(directions_button);
		
		/*##### Get Lat+Lon #####*/
		var longitude_node = getElementsByClass('longitude', entries[i], 'span')[0];
		if (longitude_node)
		{
			var latitude_node = getElementsByClass('latitude', entries[i], 'span')[0];
			if (latitude_node)
			{
				var longitude = longitude_node.firstChild.nodeValue;
				var latitude = latitude_node.firstChild.nodeValue;
				tsp_locations.push(new tsp_location (branch_name, latitude, longitude, address));
			}
		}
	}
}

/*##### GMarker Constructor w/ attached Functions #####*/
function createMarker(point, address, branch_name)
{
	/*##### Create TSP Marker Icon #####*/
	var icon = new GIcon();
	icon.image = "/images/map_marker.gif";
	icon.iconAnchor = new GPoint(9, 19);
	icon.infoWindowAnchor = new GPoint(10,1);
	icon.iconSize = new GSize(18, 19);
	
	var marker = new GMarker(point, icon);
	GEvent.addListener(marker, "click", function() //attach infoWindow to marker
	{
		map.panTo(point);
		marker.openInfoWindowHtml(address);
	});
	GEvent.addDomListener(document.getElementById(branch_name), "click", function ()
	{
		map.panTo(point);
		marker.openInfoWindowHtml(address);
	});
	
	return marker;
}

function get_directions ()
{
	//Check that we have a source
	if ((document.getElementById('source_address').value != ''))
	{
		//Source Info
		var source_address = document.getElementById('source_address').value;
		
		//Destination Info
		var destination_street = getElementsByClass('street-address', this.parentNode, 'div')[0].firstChild.nodeValue;
		var destination_city = getElementsByClass('locality', this.parentNode, 'span')[0].firstChild.nodeValue;
		var destination_state = getElementsByClass('region', this.parentNode, 'span')[0].firstChild.nodeValue;
		var destination_zip = getElementsByClass('postal-code', this.parentNode, 'span')[0].firstChild.nodeValue;
		var destination_address = destination_street + ', ' + destination_city + ' ' + destination_state + ' ' + destination_zip;
		
		/*
		Can we specify custom icons for the directions?
		Check current custom icons--they seem to be off the mark.
		*/
		map.getInfoWindow().hide();
		directions.clear();
		var trafficInfo = new GTrafficOverlay();
		map.addOverlay(trafficInfo);
		directions.load(source_address + " to " + destination_address);
	}
	else
	{
		alert('Please completely fill out a source address.');
	}
}

/*##### Manage Employment #####*/
function manage_employment ()
{
	var branches = getElementsByClass('Branch', null, 'li');
	if (!branches) return false;
	
	for (var i = 0; i < branches.length; i++)
	{
		branches[i].getElementsByTagName('h2')[0].getElementsByTagName('a')[0].onclick = expand_this;
	}
}
function expand_this ()
{
	this.onclick = collapse_this;
		
	var old_class = this.parentNode.parentNode.className;
	this.parentNode.parentNode.className = old_class + ' expanded';
	
	return false;
}
function collapse_this ()
{
	this.onclick = expand_this;
		
	var old_class = this.parentNode.parentNode.className;
	this.parentNode.parentNode.className = old_class.replace(' expanded', '');
	
	return false;
}



function validate_application()
{
	var validate = [
//					"available_for",
					"job_application_first_name",
					"job_application_last_name",
					"job_application_phone",
					"job_application_email",
					"job_application_current_street",
					"job_application_current_city",
					"job_application_current_state",
					"job_application_current_zip",
					"job_application_current_from",
//					"essentially_capable",
					"job_application_previous_employment1_employer",
//					"previous_employment1_contact",
					"job_application_previous_employment1_from",
					"job_application_previous_employment1_title"
//					"applicant_signature"
					];
	for(i = 0; i < validate.length; i++)
	{
		var field = document.getElementById(validate[i]);
		if(field.value == "" || field.value == null)
		{
			alert("We need some information you didn't provide. You will be taken to the appropriate field.");
			$(field).focus();
			return false
		}
	}
}





function addLoadEvent (func)
{
	var oldonload = window.onload;
	if (typeof window.onload != 'function')
	{
		window.onload = func;
	}
	else
	{
		window.onload = function ()
		{
			oldonload ();
			func ();
		}
	}
}

function addUnloadEvent (func)
{
	var oldonunload = window.onunload;
	if (typeof window.onunload != 'function')
	{
		window.onunload = func;
	}
	else
	{
		window.onunload = function ()
		{
			oldonunload ();
			func ();
		}
	}
}

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;
}

addLoadEvent (load_map);
addLoadEvent (manage_employment);
addUnloadEvent (GUnload);
addLoadEvent (setupZoom);