
// ---------------------------------------------------------------------------
// Cross-browser event functions. Created by Scott Andrew, tweaked by WSOD.
// ---------------------------------------------------------------------------

var EVENT = {
	ONLOAD: "load",
	ONUNLOAD: "unload",
	ONMOUSEOVER: "mouseover",
	ONMOUSEOUT: "mouseout",
	ONMOUSEMOVE: "mousemove",
	ONCLICK: "click",
	ONCHANGE: "change",
	ONSUBMIT: "submit",
	ONBLUR: "blur",
	ONFOCUS: "focus",
	ONCHANGE: "change"
};

function getSrcElement() {
	//allows elements to be passed directly into event handlers
	if(arguments[0].tagName) {
		return arguments[0];
	} else {
		var event = arguments[0];
		return event.srcElement || event.currentTarget || window;
	};
};

/*
function getSrcElement () {
	if (false && arguments[0].getAttribute) {
	// extension to allow for passing elements directly into event handlers 
		return arguments[0];
	} else {
		var event = arguments[0];
		return (event.srcElement) ? event.srcElement : event.currentTarget;
	}
}*/

function addEvent(obj, eventType, afunction, isCapture) {
	// W3C DOM
	if (obj.addEventListener) {
	   obj.addEventListener(eventType, afunction, isCapture);
	   return true;
	}
	// Internet Explorer
	else if (obj.attachEvent) {
	   return obj.attachEvent("on" + eventType, afunction);
	}
	else return false;
}

function removeEvent(obj, eventType, afunction, isCapture) {
	if (obj.removeEventListener) {
	   obj.removeEventListener(eventType, afunction, isCapture);
	   return true;
	}
	else if (obj.detachEvent) {
	   return obj.detachEvent("on" + eventType, afunction);
	}
	else return false;
}

function removeTextSelect() {
	if (!document.onselectstart) {
		document.onselectstart = function() {
			cancelEvent();
			return false;
		}
	}
}
function enableTextSelect() {
	if (document.onselectstart) {
		document.onselectstart = null;
	}
}

function cancelEvent(event){
	if (event) {
		if (event.preventDefault){event.preventDefault()};
		if (event.stopPropagation){event.stopPropagation()};
		event.cancelBubble = true;
		event.returnValue = false;
	}
	
	return false;
}

// -------------------------------------------------------------------
// parseSelector() : parse CSS selectors
// Writen by Shaun Inman + Mark Wubben; tweaked by WSOD
// -------------------------------------------------------------------
var parseSelector = function(){
	var reParseSelector = /^([^#.>`]*)(#|\.|\>|\`)(.+)$/;
	function parseSelector(sSelector, oParentNode){
		var listSelectors = sSelector.split(/\s*\,\s*/);
		var listReturn = [];
		for(var i = 0; i < listSelectors.length; i++){
			listReturn = listReturn.concat(doParse(listSelectors[i], oParentNode));
		};
		
		return listReturn;
	};
	
	function doParse(sSelector, oParentNode, sMode){
		sSelector = sSelector.replace(" ", "`");
		var selector = sSelector.match(reParseSelector);
		var node, listNodes, listSubNodes, subselector, i, limit;
		var listReturn = [];
		
		if(selector == null){ selector = [sSelector, sSelector] };
		if(selector[1] == ""){ selector[1] = "*" };
		if(sMode == null){ sMode = "`" };
		if(oParentNode == null){
			oParentNode = document;
		};

		switch(selector[2]){
			case "#":
				subselector = selector[3].match(reParseSelector);
				if(subselector == null){ subselector = [null, selector[3]] };
				node = 	document.getElementById(subselector[1]);
				if(node == null || (selector[1] != "*" && !matchNodeNames(node, selector[1]))){
					return listReturn;
				};
				if(subselector.length == 2){
					listReturn.push(node);
					return listReturn;	
				};
				return doParse(subselector[3], node, subselector[2]);
			case ".":
				if(sMode != ">"){
					listNodes = getElementsByTagName(oParentNode, selector[1]);
				} else {
					listNodes = oParentNode.childNodes;
				};
				
				for(i = 0, limit = listNodes.length; i < limit; i++){
					node = listNodes[i];
					if(node.nodeType != 1){
						continue;	
					};
					subselector = selector[3].match(reParseSelector);
					if(subselector != null){
						if(node.className == null || node.className.match("\\b" + subselector[1] + "\\b") == null){
							continue;
						};
						listSubNodes = doParse(subselector[3], node, subselector[2]);
						listReturn = listReturn.concat(listSubNodes);	
					} else if(node.className != null && node.className.match("\\b" + selector[3] + "\\b") != null){
						listReturn.push(node);
					};
				};
				return listReturn;
			case ">":
				if(sMode != ">"){
					listNodes = getElementsByTagName(oParentNode, selector[1]);
				} else {
					listNodes = oParentNode.childNodes;
				};
								
				for(i = 0, limit = listNodes.length; i < limit; i++){
					node = listNodes[i];
					
					if(node.nodeType != 1){
						continue;	
					};
					
					if(!matchNodeNames(node, selector[1])){
						continue;
					};
					listSubNodes = doParse(selector[3], node, ">");
					listReturn = listReturn.concat(listSubNodes);	
				};
				return listReturn;
			case "`":
				listNodes = getElementsByTagName(oParentNode, selector[1]);
				for(i = 0, limit = listNodes.length; i < limit; i++){
					node = listNodes[i];
					listSubNodes = doParse(selector[3], node, "`");
					listReturn = listReturn.concat(listSubNodes);	
				};
				return listReturn;
			default:
				if(sMode != ">"){
					listNodes = getElementsByTagName(oParentNode, selector[1]);
				} else {
					listNodes = oParentNode.childNodes;
				};

				for(i = 0, limit = listNodes.length; i < limit; i++){
					node = listNodes[i];
					if(node.nodeType != 1){
						continue;	
					};
					if(!matchNodeNames(node, selector[1])){
						continue;
					};
					listReturn.push(node);
				};
				return listReturn;
		};
	};
	
	function getElementsByTagName(oParentNode, sTagName){
		/*	IE5.x does not support document.getElementsByTagName("*")
			therefore we're falling back to element.all */
		if(sTagName == "*" && oParentNode.all != null){
			return oParentNode.all;
		};
		return oParentNode.getElementsByTagName(sTagName);
	};
	
	function matchNodeNames(node, sMatch){
		if(sMatch == "*"){
			return true;
		};
		return node.nodeName.toLowerCase().replace("html:", "") == sMatch.toLowerCase();
	};
	
	return parseSelector;
}();

// alternate name
document.getElementsBySelector = parseSelector;

/* -------------------------------------------------------------------
	Class Functions
   ------------------------------------------------------------------- */
   
function removeClass (_obj, _class) {
	if (_obj) {
		if (_obj.className) {
			var re = new RegExp('\\s*\\b'+_class+'\\b\\s*');
			_obj.className = _obj.className.replace(re, ' ');
			_obj.className = _obj.className.replace(/^\s*/, '');
			_obj.className = _obj.className.replace(/\s*$/, '');
		}
	}
}

function addClass (_obj, _class) {
	if (_obj) {
		var re = new RegExp('\\b'+_class+'\\b');
		if (_obj.className.search(re) == -1) {
			_obj.className = _obj.className ? _obj.className + ' ' + _class : _class;
		}
	}
}

function hasClass (_obj, _class) {
	if (_obj) {
		var re = new RegExp('\\b'+_class+'\\b');
		return (_obj.className.search(re) != -1);
	} else {
		return false;
	}
}


var gContentFrameId = false;
	
function loadContent (contentURL, contentFrameId, loadingBuffer, type) {
		
		if (loadingBuffer) {
			
			if (type == "textarea") {
			
				document.getElementById(contentFrameId).value = document.getElementById(loadingBuffer).innerHTML;
			} else {
			
				document.getElementById(contentFrameId).innerHTML = document.getElementById(loadingBuffer).innerHTML;
			}
		}
		
	document.getElementById("buffer").src = contentURL;
	gContentFrameId = contentFrameId;
}
	
function handleContentLoadComplete (contentId, type) {
	
	if (gContentFrameId) {
		
		if (type == "textarea") {
			
			var data = frames["buffer"].document.getElementById(contentId).innerHTML;
			
			data = data.replace(/\&gt\;/g, ">");
			data = data.replace(/\&lt\;/g, "<");
		
			document.getElementById(gContentFrameId).value = data;
			
		} else {
		
			document.getElementById(gContentFrameId).innerHTML = frames["buffer"].document.getElementById(contentId).innerHTML;
		}
		document.getElementById(gContentFrameId).scrollTop = 0;
	}
		
	gContentFrameId = false;
		
	onLoadContent();
}
	
function onLoadContent () {
	
}

function prodExpand(elem) {
	if (!document.createElement) return;

	if (hasClass(elem.parentNode, "expand")){
		removeClass(elem.parentNode, "expand");
	}else{
		addClass(elem.parentNode, "expand");
	}
	
	if (hasClass(elem.nextSibling, "expand")){
		removeClass(elem.nextSibling, "expand");
	}else{
		addClass(elem.nextSibling, "expand");
	}
	return false;
}addEvent(window, "load", startList);

function go_quicklink()
{
	box = document.forms[0].quicklink;
	destination = box.options[box.selectedIndex].value;
	if (destination) location.href = destination;
}

function startList() {
	if (document.all&&document.getElementById) {
		navRoot = document.getElementById("nav-main");
		for (i=0; i<navRoot.childNodes.length; i++) {
			node = navRoot.childNodes[i];
			if (node.nodeName=="LI" && node.className!= "quicklinks") {
				for (j=0; j< node.childNodes.length; j++) {
					switch(node.childNodes[j].nodeName) {
					case "IFRAME":
						node.hider = node.childNodes[j]; break;
					case "UL":
						node.list = node.childNodes[j]; break;
					}
				}
				node.onmouseover=function() {
					this.className+=" over";
					if (this.hider && this.list)
						this.hider.style.height = this.list.clientHeight + 'px';
				}
				node.onmouseout=function() {
					this.className=this.className.replace(" over", "");
				}
			}
		}
	}
}

addEvent(window, "load", processButtons);

/*	generate list of buttons and apply drop shadows */
function processButtons () {
	
	var buttonList = parseSelector("a.button", document);
		
	for (var i = 0; i < buttonList.length; i++) {
		/*
		
		convert:
		
		<a href="#" class="button buttonOrange">something</a>
		
		into:
		
		<span class="button buttonOrange">
			something
			<span class="shad">something</span>
			<a href="#" class="high">something</a>
		</span>
		
		*/
		
		try {
		
			var b = buttonList[i];
			var p = b.parentNode;
			var btext = b.cloneNode(true);
			
			for (var j = 0; j < btext.childNodes.length; j++) {

				if (btext.childNodes[j].className && btext.childNodes[j].className.indexOf("noshad") != -1) {
					btext.removeChild(btext.childNodes[j]);
				}
			}
			
			var paddingLeft = parseInt(getStyle(b, "padding-left"));
			var paddingRight = parseInt(getStyle(b, "padding-right"));
			var width = parseInt(getStyle(b, "width"));
						
			var wrap = document.createElement("span");
			wrap.className = b.className;
			wrap.innerHTML = "<nobr>" + btext.innerHTML + "</nobr>";
			
			if (b.id) {
				wrap.id = b.id;
				b.id = "";
			}
					
			p.replaceChild(wrap, b);
	
			var shad = document.createElement("span");
			shad.className = "shad";
			shad.innerHTML = "<nobr>" + btext.innerHTML + "</nobr>";
	
			b.className = "high";
			
			wrap.appendChild(shad);
			wrap.appendChild(b);
			
			if (wrap.className.indexOf("buttonDirTab") == -1) {
	
				if (paddingLeft) {
					wrap.style.paddingLeft = paddingLeft + "px";
					shad.style.paddingLeft = (paddingLeft - parseInt(getStyle(shad, "margin-left"))) + "px";
					b.style.marginLeft = paddingLeft + "px";
				}
				
				if (paddingRight) {
					wrap.style.paddingRight = paddingRight + "px";
					shad.style.paddingRight = paddingRight + "px";
				}
				
				if (width) {
					wrap.style.width = width + "px";
					shad.style.width = width + "px";
					b.style.width = width + "px";
				}

			} else {
				// alert(wrap.outerHTML);
			}
						
		} catch (e) {
			dbg(e);
		}
	}
}

function updateButtonText (button, text) {
	
	if (button.nodeType == 3) {
		button.nodeValue = text;
	} else if (button.childNodes.length) {
		
		for (var i = 0, c = button.childNodes.length; i < c; i++) {
			updateButtonText(button.childNodes[i], text);
		}
	}
}

function getStyle (el, styleProp) {

	if (window.getComputedStyle) {
	
		return window.getComputedStyle(el, null).getPropertyValue(styleProp);
	
	} else if (el.currentStyle) {
		
		styleProp = styleProp.replace(/\-(.)/g, function () {
			return arguments[1].toUpperCase();
		});
		return el.currentStyle[styleProp];
	
	} else if (styleProp == "width") {
		
		return el.offsetWidth;
	}
	
	return null;
}		


/*	Parse CSS selectors.
	Courtesy Shaun Inman. */
var parseSelector = function(){
	var reParseSelector = /^([^#\.>\`]*)(#|\.|\>|\`)(.+)$/;
	function parseSelector(sSelector, oParentNode, sMode){
		sSelector = sSelector.replace(" ", "`");
		var selector = sSelector.match(reParseSelector);
		var node, listNodes, listSubNodes, subselector;
		var listReturn = [];

		if(selector == null){ selector = [sSelector, sSelector]	};
		if(selector[1] == ""){ selector[1] = "*" };
		if(sMode == null){ sMode = "`" };
		
		switch(selector[2]){
			case "#":
				subselector = selector[3].match(reParseSelector);
				if(subselector == null){ subselector = [null, selector[3]] };
				node = 	document.getElementById(subselector[1]);
				if(node == null || (selector[1] != "*" && node.nodeName.toLowerCase() != selector[1].toLowerCase())){
					return listReturn;
				};
				if(subselector.length == 2){
					listReturn.push(node);
					return listReturn;	
				};
				return parseSelector(subselector[3], node, "#");
			case ".":
				if(sMode == "`"){
					listNodes = getElementsByTagName(oParentNode, selector[1]);
				} else {
					listNodes = oParentNode.childNodes;
				};
				
				for(var i = 0; i < listNodes.length; i++){
					node = listNodes[i];
					if(node.nodeType != 1){
						continue;	
					};

					subselector = selector[3].match(reParseSelector);
					if(subselector != null){
						if(node.className.match("\\b" + subselector[1] + "\\b") == null){
							continue;
						};
						listSubNodes = parseSelector(subselector[3], node, subselector[2]);
						listReturn = listReturn.concat(listSubNodes);	
					} else if(node.className.match("\\b" + selector[3] + "\\b") != null){
						listReturn.push(node);
					};
				};
				return listReturn;
			case ">":
				if(sMode == "`"){
					listNodes = getElementsByTagName(oParentNode, selector[1]);
				} else {
					listNodes = oParentNode.childNodes;
				};
				for(var i = 0; i < listNodes.length; i++){
					node = listNodes[i];
					if(node.nodeType != 1){
						continue;	
					};
					if(node.nodeName.toLowerCase() != selector[1].toLowerCase()){
						continue;
					};
					listSubNodes = parseSelector(selector[3], node, ">");
					listReturn = listReturn.concat(listSubNodes);	
				};
				return listReturn;
			case "`":
				listNodes = getElementsByTagName(oParentNode, selector[1]);
				for(var i = 0; i < listNodes.length; i++){
					node = listNodes[i];
					listSubNodes = parseSelector(selector[3], node, "`");
					listReturn = listReturn.concat(listSubNodes);	
				};
				return listReturn;
			default:
				listNodes = getElementsByTagName(oParentNode, selector[0]);
				for(var i = 0; i < listNodes.length; i++){
					listReturn.push(listNodes[i]);
				};
				return listReturn;
		};
	};
	
	function getElementsByTagName(oParentNode, sTagName){
		/*	IE5.x does not support document.getElementsByTagName("*")
			therefore we're resorting to element.all */
		if(sTagName == "*" && oParentNode.all != null){
			return oParentNode.all;
		};
		return oParentNode.getElementsByTagName(sTagName);
	};
	
	return parseSelector;
}();

function popPDF (url, filename, w, h) {

	// add .pdf for webtrends download report if it isn't already part of the filename
	filename += (filename.indexOf(".pdf") > -1 ? "" : ".pdf");

	dcsMultiTrack('DCS.dcsuri', unescape(filename));
	popWindow(url, w, h);
	
}function popWindow(url, w, h)
{
	var width = w || 450;
	var height = h || 600;

	//have to do the open/close since IE seems to have a problem focusing to a window with a pdf in it
	var winObj = window.open("", "LipperPopWindow");
		winObj.close();
		winObj = window.open(url,'LipperPopWindow','width=' + width + ',height=' + height + ',top=100,left=100,resizable=yes,scrollbars=yes');
		winObj.focus();

	return false;
}addEvent(window, "load", addSiteSettingLinks);

function addSiteSettingLinks () {
	/* add popup onClick to disclaimer link in footer */
	if(document.getElementById("showLogin")){
		var link = document.getElementById("showLogin");
		link.onclick = function () {
				return showLogin()
			}
	}

	if(document.getElementById("cancelSettings")){
	var link2 = document.getElementById("cancelSettings");
	link2.onclick = function () {
			return cancelLogin()
		}
	}

	if(document.getElementById("showCountries")){
	var link3 = document.getElementById("showCountries");
	link3.onclick = function () {
			return showCountries()
		}
	}

	if(document.getElementById("cancelCountries")){
	var link4 = document.getElementById("cancelCountries");
	link4.onclick = function () {
			return cancelCountries()
		}
	}

	if(document.getElementById("countrySelected")){
	var link5 = document.getElementById("countrySelected");
	link5.onchange = function () {
			return countrySelected()
		}
	}
}

function showLogin () {
	box = document.getElementById("site-login");
	box.style.display = 'block';
	return false;
}

function cancelLogin () {
	box = document.getElementById("site-login");
	box.style.display = 'none';
	return false;
}

function showCountries () {
	box = document.getElementById("site-countries");
	box.style.display = 'block';
	return false;
}

function cancelCountries () {
	box = document.getElementById("site-countries");
	box.style.display = 'none';
	return false;
}

function countrySelected () {
	var ind = document.countryForm.countrySelected.selectedIndex;
    var val = document.countryForm.countrySelected.options[ind].value;

	var url = String(self.location);
	var urlParam = "co=" + val;

	if(/\?/.test(url))
	{
		var regexmatch = /co\=\w\w/i.exec(url)
		if(regexmatch)
		{
			url = url.replace(regexmatch[0], urlParam);
		}
		else{ url += "&" + urlParam; }
	}
	else{ url += "?" + urlParam; }

	window.location.href = url;

	box = document.getElementById("site-countries");
	box.style.display = 'none';

	return false;
}

addEvent(window, "load", addPopDisclaimer);

function addPopDisclaimer () {
	/* add popup onClick to disclaimer link in footer */
	var link = document.getElementById("popDisclaimer");
	
	if (link) {
		link.onclick = function () {
			return popWindow(link.href)
		}
	}
}



addEvent(window, "load", addShadows);

function addShadows () {

	/* add page footer shadow */ 
	var container = document.getElementById("container");
	var shadow = document.createElement("DIV");
	
	shadow.setAttribute("id", "footerShadow");
	document.body.appendChild(shadow);

	/* add breadcrumb shadows 
	if (document.getElementById("breadcrumb")) {
		var items = parseSelector("li", document.getElementById("breadcrumb"));
		addTextShadows(items);
	};*/

	/* add main-nav text shadows 
	var items = parseSelector("li", document.getElementById("nav-main"));
	items.pop(); // don't shadow the last element in this list
	addTextShadows(items);*/
	
	/* add side nav shadows */
	var items = parseSelector("dl.nav dt", document);
	addTextShadows(items);

	/* add sidebar shadows 
	var items = parseSelector("dl.sidebar dt", document);
	addTextShadows(items);*/
}

function addTextShadows (nodes) {

	if (!nodes) return;

	for (var i = 0; i < nodes.length; i++) {

		var shadow = document.createElement("SPAN");
		
		shadow.className = "shadow";
		shadow.appendChild(document.createTextNode(getNodeText(nodes[i])));
		nodes[i].insertBefore(shadow, nodes[i].firstChild);
	}
}

function getNodeText (n) {

	if (n.nodeType == 3) {
		return n.nodeValue;
	} else {
		
		var nodeText = "";
		
		for (var i = 0; i < n.childNodes.length; i++) {
			nodeText += getNodeText(n.childNodes[i]);
		}
		
		return nodeText;
	}
}
