if ( isW3CDOM && isPrototype() ) {
  	/* happens from top down */
	Event.observe(window, 'load', addFilterTriggers, false);
}


/*  *******************************************************************************************************************
	Assigns event handlers to the 'filtering' links
		- mcconnelll @ 20080915
*/
function addFilterTriggers ( e ) {
	// the 'category' filters
/*	$$('#blue-holidays .links li a').each(function(obj) {
		Event.observe(obj , 'click', function(e){applyFilter(e, '', '', '')});
	}); */

	// onchange event for the origin select box.
	if($('setOrigin')){
		Event.observe($('setOrigin') , 'change', function(e){elem = Event.element(e); applyFilter(e, '', '', elem.value); setCookie("lastFromCity", parseCityValue(elem.value));});
	}
}


/*  *******************************************************************************************************************
	Appends filtering information to the symfony route / link
		- mcconnelll @ 20080915
*/
function applyFilter (e, targetUrl, field, fieldVal) {
	var elem = Event.element(e);
	if (targetUrl) {
		targetUrl = targetUrl + field + "/" + fieldVal + "/";
	}
	else {
		targetUrl = fieldVal;
	}

	executeFilter(targetUrl);
}


/* change this to ajax!!! */
function executeFilter (filteredUrl) {
	if (filteredUrl) {
		window.location = filteredUrl;
	}
}

// strip any symfony query content, if it exists
function parseCityValue (sValue)
{
	route_match = sValue.match(/from\/(\w+)/);
	if (route_match) {
		sValue = route_match[1];
	}
	return(sValue);
}

function setCookie(sName, sValue)
{
		var days = 90; // time to keep cookie
		var expiry = new Date();
		expiry.setTime(expiry.getTime() + (days * 86400000)); // days * [milliseconds in a day]

		document.cookie = sName+'='+sValue+'; expires='+expiry.toGMTString()+'; path=/';
}

// Retrieve the value of the cookie with the specified name.
function getCookie(sName)
{
	// cookies are separated by semicolons
	var aCookie = document.cookie.split("; ");
	for (var i=0; i < aCookie.length; i++)
	{
		// a name/value pair (a crumb) is separated by an equal sign
		var aCrumb = aCookie[i].split("=");
		if (sName == aCrumb[0]) 
			return unescape(aCrumb[1]);
	}

	// a cookie with the requested name does not exist
	return null;
}

