
$(document).ready(function() {
	// set new window functionality for all external links
	$("a[rel=external]").click(function(event) {
		event.preventDefault();
		
		var newWindow = window.open($(this).attr("href"));
		newWindow.focus();
	});
});

function _get(id) {
	return document.getElementById(id);
}

function _show(elementID)
{
	var element = document.getElementById(elementID);
	element.style.visibility="visible"; element.style.display="block";
}
function _hide(elementID)
{
	var element = document.getElementById(elementID);
	element.style.visibility="hidden"; element.style.display="none";
}

function _showHide(elementID)
{
	var element = document.getElementById(elementID);
	if (element.style.display=="block")
	{
		element.style.visibility="hidden";
		element.style.display="none";
	}
	else
	{
		element.style.visibility="visible";
		element.style.display="block";
	}
}
function _expandContract(imgID, divID)
{
	var theImg = document.getElementById(imgID);
	var theDiv = document.getElementById(divID);
	if (theDiv.style.display=="block")
	{
		theDiv.style.display="none";
		theImg.src = '/images/icon-expand.gif';
	}
	else
	{
		theDiv.style.display="block";
		theImg.src = '/images/icon-contract.gif';
	}
}

function _open(event, source)
{
	var newWindow = window.open(source);
	newWindow.focus();
	event.returnValue = false;
	return false;
}
// MAKE SURE THIS IS CALLED in onclick and not href!!! (otherwise, any special characters will be decoded when passed as a parameter)
function _openPopUp(source, name, width, height)
{
	var selectSource = window.open(source, name,'width=' + width + ',height=' + height + ',top=' + height/2 + ',left=' + width/2 + ',scrollbars=1,location=0,status=1,resizable=1,menubar=0');
	selectSource.focus();
}
	
function _openVideo(source) {
	var w = 645, h = 315; //465

	//if (document.all || document.layers) {
		popW = screen.availWidth;
		popH = screen.availHeight;
	//}

	var leftPos = (popW - w)/2; 
	var topPos = (popH - h)/2;

	selectSource = window.open(''+source+'','MVEVideo','width=' + w + ',height=' + h + ',top=' + topPos + ',left=' + leftPos + ',scrollbars=1,location=0,status=1,resizable=0,menubar=0');
	selectSource.focus();
}

function _urlRemoveParam(url, paramName) {
	var newURL = url;
	
	// remove all instances of query parameter
	var param = '&' + paramName + '=';
	var endIndex;
	var paramIndex = newURL.indexOf(param);
	while (paramIndex > -1)
	{
		// get the index of the location after the offending parameter
		endIndex = newURL.indexOf('&', paramIndex + param.length); // start after the offending parameter
		if (endIndex == -1)
			endIndex = newURL.length;
		
		// get url before the offending parameter
		urlBefore = newURL.substring(0, paramIndex);
		
		// get url after the offending parameter
		urlAfter = newURL.substring(endIndex, newURL.length);
		
		// recreate the location
		newURL = urlBefore + urlAfter;
					
		// get the index of the next offending parameter
		paramIndex = newURL.indexOf(param);
	}
	return newURL;
}

function _setCookie(name, value, expires, path, domain, secure)
{
    document.cookie= name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

function _getCookie(name)
{
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1)
    {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    }
    else
    {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1)
    {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

function _getWindowHeight()
{
	var height;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		height = window.innerHeight;
	} 
	else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		height = document.documentElement.clientHeight;
	}
	return height;
}
function _getScrollY()
{
   var scrollY;

   if (document.all) /* Internet Explorer */
   {
        if (!document.documentElement.scrollTop)
           scrollY = document.body.scrollTop;
        else
           scrollY = document.documentElement.scrollTop;
   }   
   else
       scrollY = window.pageYOffset;
        
   return scrollY;
}

function _urlParseQS (str) {
	str = str ? str : location.search;
	var query = str.charAt(0) == '?' ? str.substring(1) : str;
	var args = new Object();
	if (query) {
		var fields = query.split('&');
		for (var f = 0; f < fields.length; f++) {
			var field = fields[f].split('=');
			args[unescape(field[0].replace(/\+/g, ' '))] = unescape(field[1].replace(/\+/g, ' '));
		}
	}
	return args;
}
/*
function _openNewsletter(event, yearQuarter)
{
	_openPopUp('/newsletters/' + yearQuarter + '.html','newsletter',800,600);
	event.returnValue=false;
	return false;
}
*/