// common.js
//

var popUpWin = false; 	// track popup windows so they can be closed on unload
var hdrSelTab = 1; 			// selected tab in header nav

/* =========================================
/*		Close existing popup windows (e.g. on page unload)
/* ========================================= */
function closePopups() { if (popUpWin) popUpWin.close(); }


/* =========================================
/*		Open a page in a popup window
/* ========================================= */
function popUpPage(url) {
	if (popUpWin.open) popUpWin.close();
	popUpWin = window.open(url, "popupwin", "width=650,height=500,scrollbars=1,toolbar=1")
}


/* =========================================
/*		Cross browser function to find an element by id
/* ========================================= */
function objectById(id) {
	if (document.getElementById) return document.getElementById(id);
	else if (document.all) return document.all[id];
	else if (document.layers) return document.layers[id];
}


/* =========================================
/*		Textbox focus and blur events (change active css)
/* ========================================= */
function focusTxt(obj) { obj.className = obj.className.replace(" Highlight", "") + " Selected"; }
function blurTxt(obj) { obj.className = obj.className.replace(" Selected", "").replace(" Highlight", ""); }


/* =========================================
/*		Change the number of results per page on searches
/* ========================================= */
function changeRPP(sender, defaultRPP) {
	// determine url without the RPP selection
	eval("var url = baseUrl.replace(/(rpp|pg)\\/.*/, \"\")");
	if (sender.options[sender.selectedIndex].value != defaultRPP) url += "/rpp/" + sender.selectedIndex;
	location.href = url.replace("//", "/");
}


/* =========================================
/*		Change the sort order on searches
/* ========================================= */
function changeSort(sender, defaultSort) {
	// determine url without the sort by selection
	eval("var url = baseUrl.replace(/(sort|pg)\\/[0-9]+\\/*/, \"\")");
	if (sender.options[sender.selectedIndex].value != defaultSort) {
		if (url.indexOf("/rpp/") > -1) url = url.substr(0, url.indexOf("/rpp/")) + "/sort/" + sender.selectedIndex + url.substr(url.indexOf("/rpp/"));
		else url += "/sort/" + sender.selectedIndex;
	}
	location.href = url.replace("//", "/");
}

/* =========================================
/*		Header navigation jQuery functions
/* ========================================= */
$(document).ready(function()
{
	// change nav bar bg position to show correct highlight
	$("#hdrNav li").hover(function()
	{
		$("#hdrNav ul.nav").css("background-position", "left -" + ($(this).attr('class').substr(1) - 1) * 42 + "px");
	}, function()
	{
		$("#hdrNav ul.nav").css("background-position", "0px -" + ((hdrSelTab - 1) * 42) + "px");
	});
});

