YAHOO.namespace("cwc.container");
YAHOO.util.Event.onAvailable('gn', initGlobalNav, this);
/**
 * Global Nav Menu Init
 ******************************************************/
function initGlobalNav() {	
	var sfEls = document.getElementById("gn-menu").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
										this.className += " sfHover";
									}
		sfEls[i].onmouseout=function() {
										this.className=this.className.substring(0, this.className.indexOf(' '));
									}
	}
	
	setGlobalNavGAEvents(); //analytics instrumentation	
}



/**
 * Adds onclick tracking to the global nav links
 ******************************************************/
function setGlobalNavGAEvents() {
	var container = $('gn');
	if (!container || !container.getElementsByTagName) 
	{ return; }
	
	var anchorList = container.getElementsByTagName("a");
	for (var i=0; i<anchorList.length; i++) {
		var a = anchorList[i];
		if (a.innerHTML) {
			addGAEventTracking(a,"Click - Internal", "Global Nav", a.innerHTML);
		}
	}
}


/**
 * Record a Google Analytics Event
 *****************************************************/
function gaRecordEvent(evtCategory, evtAction, evtLabel) {
	if(typeof(pageTracker) != "undefined") {
		pageTracker._trackEvent(evtCategory, evtAction, evtLabel);
	}
}


/**
 * Opens the Tell a Friend dialog
 ******************************************************/
function OpenTAF (c) {
	var uri = encodeURI(c);
	uri = uri.replace("\#", "%23");
	window.open(uri,'TAF','width=350,height=400,scrollbars=no,status=no,menubar=no,toolbar=no');
}

/******************************************************************************************
 * Helper Functions
 ******************************************************************************************/
function hideEl(id) {
    var el = (typeof id == "string") ? document.getElementById(id) : id;
    if (el) el.style.display = 'none';
}
function showEl(id) {
    var el = (typeof id == "string") ? document.getElementById(id) : id;
    if (el) el.style.display = 'block';
}
function ResizeOuterTo(w,h) {
 if (parseInt(navigator.appVersion)>3) {
   if (navigator.appName=="Netscape") {
    top.outerWidth=w;
    top.outerHeight=h;
   }
   else top.resizeTo(w,h);
 }
}

function getPosX(e) {
	var posx = 0;
	if (!e) var e = window.event;
	
	if (e.pageX) 	{
		posx = e.pageX;
	} else if (e.clientX) 	{
		posx = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
	}
	
	return posx;
}

function getPosY(e) {
	var posy = 0;
	if (!e) var e = window.event;
	
	if (e.pageY) 	{
		posy = e.pageY;
	} else if (e.clientY) 	{
		posy = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
	}
	
	return posy;
}

function getElement(sId) {
	return YAHOO.util.Dom.get(sId);
}

function validateEmail(addr) {
	if(addr.indexOf('@') < 1) {
		return false;
	}
	return true;
}

function getWindowHeight() {
	var windowHeight=0;
	if (typeof(window.innerHeight) == 'number') {
		windowHeight = window.innerHeight;
		
	}	else {
		if (document.documentElement && document.documentElement.clientHeight) {
			windowHeight=	document.documentElement.clientHeight;
		}	else {
			if (document.body&&document.body.clientHeight) {
				windowHeight=document.body.clientHeight;
			}
		}
	}
	
	return windowHeight;
}

function getWindowWidth() {
	var width = -1;
	
	if (self.innerWidth) {
		width = self.innerWidth;
	} else if (document.documentElement && document.documentElement.clientWidth) {
		width = document.documentElement.clientWidth;
	} else if (document.body) {
		width = document.body.clientWidth;
	}
	
	return width;
}

function getContentHeight() {
	var retVal = -1;
	var els = document.getElementsByTagName("HTML");
	if(els && els.length > 0) {
		var elHtml = els[0];
		if(elHtml && elHtml.scrollHeight) {
			retVal = elHtml.scrollHeight;
		}
	}
	return retVal;
}

function setElementHeight(sElementId, iHeightInPixels) {
	if(sElementId != null && sElementId != "") {
		var elTarget = getElement(sElementId);
		if(elTarget) {
			elTarget.style.height = iHeightInPixels +"px";
			return true;
		}
	}	
	return false;
}

function setCookie (name, value, expires, path, domain, secure) {
    if (domain && domain.match(/^\.?localhost$/))
        domain = null;
    var curCookie = name + "=" + escape(value) +
        (expires ? "; expires=" + expires.toGMTString() : "") +
        (path ? "; path=" + path : "; path=/") +
        (domain ? "; domain=" + domain : "") +
        (secure ? "; secure" : "");
    document.cookie = curCookie;
}

function getCookie (name) {
    var prefix = name + '=';
    var c = document.cookie;
    var cookieStartIndex = c.indexOf(prefix);
    if (cookieStartIndex == -1)
        return '';
    var cookieEndIndex = c.indexOf(";", cookieStartIndex + prefix.length);
    if (cookieEndIndex == -1)
        cookieEndIndex = c.length;
    return unescape(c.substring(cookieStartIndex + prefix.length, cookieEndIndex));
}

function deleteCookie(name, path, domain, secure) {
    if (getCookie(name)) {
        if (domain && domain.match(/^\.?localhost$/))
            domain = null;
        document.cookie = name + "=" +
            (path ? "; path=" + path : "") +
            (domain ? "; domain=" + domain : "") +
            (secure ? "; secure" : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}

function fixDate (date) {
    var skew = (new Date(0)).getTime();
    if (skew > 0)
        date.setTime(date.getTime() - skew);
}

function rememberMe (f) {
		var now = new Date();
		fixDate(now);
		now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000);
		setCookie('CmtrName', f.author.value, now, '', HOST, '');
		setCookie('CmtrEmail', f.email.value, now, '', HOST, '');
		setCookie('CmtrUrl', f.url.value, now, '', HOST, '');
}

function forgetMe (f) {
		deleteCookie('CmtrEmail', '', HOST);
		deleteCookie('CmtrUrl', '', HOST);
		deleteCookie('CmtrName', '', HOST);
		f.email.value = '';
		f.author.value = '';
		f.url.value = '';
}
