function addBookmarkForBrowser() {
	if (window.external) {
		window.external.AddFavorite(document.location, document.title);
	} else if(window.sidebar) {
		window.sidebar.addPanel(document.title, document.location, "");
	}
}

function checkScroll() {
	var value = varName = "";
	var contentVars = ["doc", "news", "event", "cat", "property"];
	var qs = getQs();
	var thisUrl = location.href.replace(/&(admin|doc|news|event|cat|property)=[^&]*/gi, "");
	var tempUrl;
	var pos, i , j;
	var qs2;
	
	for (i = 0; i < contentVars.length; i++) {
		varName = contentVars[i];
		value = qs[varName];
		if (value) {
			break;
		}
	}
	if (varName && value) {
		pos = getAnchorPosition(varName + value);
		if (pos && (pos.y > windowScroll().y + windowSize().height || pos.y < windowScroll().y)) {
			scrollTo(0, pos.y);
		}
	}
	
	// search for any links to items on this page
	for (i = 0; i < document.links.length; i++) {
		tempUrl = document.links[i].href;
		if (tempUrl.replace(/&(admin|doc|news|event|cat|property)=[^&]*/gi, "") == thisUrl) {
			value = null;
			qs2 = getQs(tempUrl.substr(tempUrl.indexOf("?")));
			for (j = 0; j < contentVars.length; j++) {
				varName = contentVars[j];
				value = qs2[varName];
				if (value) {
					pos = getAnchorPosition(varName + value);
					if (pos && (pos.y > windowScroll().y + windowSize().height || pos.y < windowScroll().y)) {
						document.links[i].href = "javascript:scrollTo(0, " + pos.y + ")";
					}
					break;
				}
			}
		}
	}
	
}

function getAnchorPosition(anchorName) {
	var coords = null, anchor;
	
	if (document.anchors) {
		for(var i = 0; i < document.anchors.length; i++) {
			if (anchorName == document.anchors[i].name) {
				anchor = document.anchors[i];
			}
		}
	}
	else if (document.getElementById) {
		anchor = document.anchors[anchorName];
	}
	if (anchor) {
		coords = findPos(anchor);
	}
	return coords;
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft;
		curtop = obj.offsetTop;
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		}
	}
	return { "x": curleft, "y": curtop };
}

function getQs(searchString) {
	var qsParams = new Object();
	searchString = searchString || window.location.search;
	var parms = searchString.substring(1).split('&');
	for (var i=0; i<parms.length; i++) {
		var pos = parms[i].indexOf('=');
		if (pos > 0) {
			var key = parms[i].substring(0,pos);
			var val = parms[i].substring(pos+1);
			qsParams[key] = val;
		}
	}
	return qsParams;
}

function onDomReady(funcToCall) {
		if (document.addEventListener) {
            document.addEventListener("DOMContentLoaded", funcToCall, false);
        } else {
            document.write("<scr" + "ipt id='__ieinit' defer='true' " + "src='//:'><\/script>");
            var script = document.getElementById("__ieinit");
            script.onreadystatechange = function() {
                if (this.readyState != "complete") return;
                this.parentNode.removeChild(this);
                funcToCall();
            }
            script = null;
        }
};

function windowSize() {
	var myWidth = 0, myHeight = 0;
	if(typeof(window.innerWidth ) == 'number') {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	return { "width": myWidth, "height": myHeight };
}

function windowScroll() {
	var scrOfX = 0, scrOfY = 0;
	if( typeof( window.pageYOffset ) == 'number' ) {
		//Netscape compliant
		scrOfY = window.pageYOffset;
		scrOfX = window.pageXOffset;
	} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		//DOM compliant
		scrOfY = document.body.scrollTop;
		scrOfX = document.body.scrollLeft;
	} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		//IE6 standards compliant mode
		scrOfY = document.documentElement.scrollTop;
		scrOfX = document.documentElement.scrollLeft;
	}
	return { "x": scrOfX, "y": scrOfY };
}

function emailLink() {
	window.open("mailto:?subject=" + document.title + "&body=" + escape("I thought you might be interested in this:\n\n" + location.href));
}

function bookmarkLink() {
	if (document.all)
		window.external.AddFavorite(location.href, document.title);
	else if (window.sidebar)
		window.sidebar.addPanel(location.href, document.title, "");
	else
		alert("Please use the controls in your browser to bookmark this page");
}

function printThis() {
	window.open("press_release_print_1.aspx?" + window.location.search, "print", "");
}

onDomReady(checkScroll);

/*** keep alive ***/
document.write("<script type=\"text/javascript\" src=\"/assets/scripts/EdeptiveAjax.js\"></script>");
var goAjax = null;
function keepAlive() {
	var strUrl = "";
	var sTiming = document.lastChild.lastChild.innerHTML;
	var sParams = "";

	sTiming = sTiming.substring(sTiming.lastIndexOf("<!--"));
	sTiming = sTiming.substring(5, sTiming.indexOf("-->"));
	strUrl = window.location.protocol + "//" + window.location.host + "/webservices/keep_alive.asmx/PingWithTiming";
	sParams = "Timing=" + sTiming;
	try {
		goAjax = new EdeptiveAjax();
		goAjax.postRequest(strUrl, sParams, onKeepAliveResponse);
	} catch(e) {
		setTimeout("keepAlive()", 1000*1);
	}
}
function onKeepAliveResponse() {
	if(goAjax.checkReadyState("keepAlive", "ping.", "ping..", "ping...") == "OK") {
		if(document.getElementById("keepAlive")) {
			var response = goAjax.request.responseXML.documentElement;
			document.getElementById("keepAlive").innerText = "sid=" + getText(response.firstChild);
		}
		setTimeout("keepAlive()", 1000*60);
	}
}
setTimeout("keepAlive()", 1000*2);
