$.extend({
	getUrlVars: function(){
		var vars = [], hash;
		var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
		for(var i = 0; i < hashes.length; i++)
		{
			hash = hashes[i].split('=');
			vars.push(hash[0]);
			vars[hash[0]] = hash[1];
		}
		return vars;
	},
	getUrlVar: function(name){
		return $.getUrlVars()[name];
	}
});

function siteSearchAlterUrl(curSiteId, newSiteId) {
	var siteIdStr = 'site=' + curSiteId
	var siteLoc = window.location.href.indexOf(siteIdStr);
	var newLoc = window.location.href.split('');
	if(siteLoc > 0) {
		newLoc.splice(siteLoc, siteIdStr.length, 'site=' + newSiteId);
	} else {
		newLoc = (window.location.href + '&site=' + newSiteId).split('');
	}
	return newLoc.join('');
}

$(document).ready(function() {
	var siteId = $.getUrlVar('site');
	var siteSearchSelectBox = $('.search select.siteDropList');
	// If the site search box exists
	if(siteSearchSelectBox) {
		// Attach a function to the "change" event to detect when the selection is altered and
		//     redirect to the new site search url
		siteSearchSelectBox.change(function() {
			window.location.href = siteSearchAlterUrl(siteId, $(this).val());
		});
		// When the page is loaded see if the current selection matches the site searched
		siteSearchSelectBox.val(siteId);
	}
});
