/*
	Copyright (c) 2003 Macromedia Inc.
	$Revision: 1.30 $
	
*/

var submenuIO = 0;
var default_submenu = null;
var current_menu = null;
var current_submenu = null;

/* preload globalnav images */
var bg_images = new Array();
// button ON
bg_images['hover'] = new Image();
bg_images['hover'].src = '/images/globalnav/emx_globalnav_hover.gif';
bg_images['hover_first'] = new Image();
bg_images['hover_first'].src = '/images/globalnav/emx_globalnav_hover_first.gif';
bg_images['hover_left'] = new Image(33,22);
bg_images['hover_left'].src = '/images/globalnav/emx_globalnav_hover_left.gif';
bg_images['hover_right'] = new Image(7,22);
bg_images['hover_right'].src = '/images/globalnav/emx_globalnav_hover_right.gif';
bg_images['hover_last'] = new Image();
bg_images['hover_last'].src = '/images/globalnav/emx_globalnav_hover_last.gif';
bg_images['hover_search'] = new Image();
bg_images['hover_search'].src = '/images/globalnav/emx_globalnav_hover_search.gif';
// button OFF
bg_images['hoff'] = new Image();
bg_images['hoff'].src = '/images/globalnav/emx_globalnav_hoff.gif'
bg_images['pipe'] = new Image(7,22);
bg_images['pipe'].src = '/images/globalnav/emx_globalnav_pipe.gif';

function highlightOff(element) {
	
	if (element.id == 'search') {
		//element.style.background = 'url('+bg_images['hoff'].src+') repeat-x';
		element.style.background = 'url('+bg_images['pipe'].src+') repeat-x';
		document.getElementById('last').style.background = 'none';
		return;
	}
	
	var next, prev;
	
	if (element.nextSibling && element.nextSibling.className != null) {
		next = element.nextSibling;
		prev = element.previousSibling;
	} else if (element.nextSibling.nextSibling && element.nextSibling.nextSibling.className != null) {
		next = element.nextSibling.nextSibling;
		prev = element.previousSibling.previousSibling;
	} else {
		return;
	}
	
	if (next && next.className == 'pipe') {
		next.style.background = 'url('+bg_images['pipe'].src+') no-repeat';
	} else if (next && next.className == 'space') {
		//next.style.background = 'url('+bg_images['hoff'].src+') no-repeat';
		next.style.background = 'url('+bg_images['pipe'].src+') no-repeat';
	}
	if (prev && prev.className == 'pipe') {
		prev.style.background = 'url('+bg_images['pipe'].src+') no-repeat';
	} else if (prev && prev.className == 'first') {
		//prev.style.background = 'url('+bg_images['hoff'].src+') no-repeat';
		prev.style.background = 'url('+bg_images['pipe'].src+') no-repeat';
	}
	//element.style.background = 'url('+bg_images['hoff'].src+') repeat-x';
	element.style.background = 'url('+bg_images['hoff'].src+') repeat-x';
}

function highlightMenuItem(menuID) {
	var element = document.getElementById(menuID);
	if (element.id == 'search') {
		element.style.background = 'url('+bg_images['hover_search'].src+') repeat-x';
		document.getElementById('last').style.background = 'url('+bg_images['hover_last'].src+') no-repeat';
		return;
	}
	
	var next, prev;
	
	if (element.nextSibling && element.nextSibling.className != null) {
		next = element.nextSibling;
		prev = element.previousSibling;
	} else if (element.nextSibling.nextSibling && element.nextSibling.nextSibling.className != null) {
		next = element.nextSibling.nextSibling;
		prev = element.previousSibling.previousSibling;
	} else {
		return;
	}
	
	if (next && next.className == 'pipe') {
		next.style.background = 'url('+bg_images['hover_right'].src+') no-repeat';
	} else if (next && next.className == 'space') {
		next.style.background = 'url('+this.bg_images['hover_last'].src+') no-repeat';
	}
	
	if (prev && prev.className == 'pipe') {
		prev.style.background = 'url('+bg_images['hover_left'].src+') no-repeat';
	} else if (prev && prev.className == 'first') {
		prev.style.background = 'url('+bg_images['hover_first'].src+') no-repeat';
	}
	element.style.background = 'url('+bg_images['hover'].src+') repeat-x';
}

function menusOff() {
	if (current_submenu != null) current_submenu.style.display = 'none'; 
	if (current_menu != null) highlightOff(current_menu);
	submenuIO = 0;
}

function showSubmenu(menuID) {
	if (browser_test != 1) return;
	var menu = document.getElementById(menuID);
	if (submenuIO == 1) {
		menusOff();
	}
	highlightMenuItem(menuID);
	var submenuID = menuID + '-submenu';
	if (document.getElementById(submenuID) != null) {
		current_submenu = document.getElementById(submenuID);
		current_submenu.style.display = 'block';
	} else if (default_submenu != null) {
		default_submenu.style.display = 'block';
		current_submenu = default_submenu;
	}
	submenuIO = 1;
	current_menu = menu;
	return;
}

function setCartItems(n) {
	var cart_items = document.getElementById('cart-items');
	if (cart_items && n && n != '') cart_items.innerHTML = n;
}

function showCart() {
	var cart = document.getElementById('cart');
	var n = getCartItemCount();
	setCartItems(n);
	cart.style.display = 'block';
}

function initMembership() {
	var greeting = document.getElementById('greeting');
	var screen_name = document.getElementById('screen-name');
	var account = document.getElementById('account');
	var sign_in = document.getElementById('sign-in');
	var sign_out = document.getElementById('sign-out');
	
	this.setScreenName = function() {
		var name = getScreenname();
		if (screen_name && name) { 
			screen_name.innerHTML = name;
		}
	}
	
	var level = (authCheck) ? authCheck() : 0;
	switch (level) {
		case 0:
 			greeting.style.display = 'block';
 			account.style.display = 'none';
 			sign_in.style.display = 'block';
			break;
		case 1:
			this.setScreenName();
			greeting.style.display = 'block';
			sign_out.style.display = 'block';
			showCart();
			break;
		case 2:
			this.setScreenName();
			greeting.style.display = 'block';
			sign_out.style.display = 'block';
			showCart();
			break;
		default: break;
	}
}

function browserCheck() {
	var app = navigator.appName.toLowerCase();
	var app_v = parseInt(navigator.appVersion);
	var ua = navigator.userAgent.toLowerCase();
	var plt = navigator.platform.toLowerCase();
	var check;
	if (window.opera) {
		if (ua.indexOf('opera 7') != -1 || ua.indexOf('opera/7') != -1) {
			check = 1;
		} else if (app.indexOf('opera') != -1 && app_v <= 7) {
			check = 0;
		} else {
			check = 0;
		}
	} else {
		check = 1;
	}
	return check;
}

var browser_test;
function initNav(submenuID) {
	browser_test = browserCheck();
	default_submenu = document.getElementById(submenuID);
	current_submenu = default_submenu;
	submenuIO = 1;
	//initMembership();
}

function initPage() {
	// do nothing
}

// delay globalnav initialization
setTimeout("initNav('globalmessaging-submenu');",200);

/* function to loop through all in page objects
	return a collection of all SWFs 
	allows fma swf to call omniture without killing other downloads */
function getSWFs () {
	this.objTagCol = document.getElementsByTagName('object');
	this.swfCollection = new Array();
	this.n = 0;
	this.addSWFToCol = function (swf) {
		this.swfCollection[this.n] = swf;
		this.n++;
	}
	for (var i=0; i < this.objTagCol.length; i++) {
		var current_obj = this.objTagCol[i];
		if ((current_obj.getAttribute('type') != null && current_obj.getAttribute('type').indexOf('flash') != -1) || 
				(current_obj.getAttribute('codebase') && current_obj.getAttribute('codebase').indexOf('flash') != -1)) {
			this.addSWFToCol(current_obj);
		} else {
			/* most Flash object tags will not specify the type so loop through
				the object tag params elements looking for a movie which has a .swf extension */
			var objParams = current_obj.getElementsByTagName('param');		
			for (var p=0; p < objParams.length; p++) {
				if ((objParams[p].name && objParams[p].name == 'movie') && 
						(objParams[p].value && objParams[p].value.indexOf('.swf') != -1)) {
					this.addSWFToCol(current_obj);
					break;
				}
			}
		}
	}
	return this.swfCollection;
}

window.onload = function () {
	initPage();
	if ((navigator.appName.toLowerCase().indexOf('microsoft') != -1) && 
			(navigator.userAgent.toLowerCase().indexOf('win') != -1)) {
		var swfs = getSWFs();
		for (var i=0; i<swfs.length; i++) {
			var id = swfs[i].id;
			if (id != null && id != '') window.document[id].SetVariable('getURLOK','true');
		}
	}
}

function OpenComments (c) {
    window.open(c,
                    'comments',
                    'width=480,height=480,scrollbars=yes,status=yes');
}

function OpenTrackback (c) {
    window.open(c,
                    'trackback',
                    'width=480,height=480,scrollbars=yes,status=yes');
}

function setCookie (name, value, expires, path, domain, secure) {
    var curCookie = name + "=" + escape(value) + ((expires) ? "; expires=" + expires.toGMTString() : "") + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + ((secure) ? "; secure" : "");
    document.cookie = curCookie;
}

function getCookie (name) {
    var prefix = name + '=';
    var c = document.cookie;
    var nullstring = '';
    var cookieStartIndex = c.indexOf(prefix);
    if (cookieStartIndex == -1)
        return nullstring;
    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) {
    if (getCookie(name))
        document.cookie = name + "=" + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
}

function fixDate (date) {
    var base = new Date(0);
    var skew = base.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('mtcmtauth', f.author.value, now, '', HOST, '');
    setCookie('mtcmtmail', f.email.value, now, '', HOST, '');
    setCookie('mtcmthome', f.url.value, now, '', HOST, '');
}

function forgetMe (f) {
    deleteCookie('mtcmtmail', '', HOST);
    deleteCookie('mtcmthome', '', HOST);
    deleteCookie('mtcmtauth', '', HOST);
    f.email.value = '';
    f.author.value = '';
    f.url.value = '';
}

function OpenComments (c) {
    window.open(c,
                    'comments',
                    'width=480,height=480,scrollbars=yes,status=yes');
}

function OpenTrackback (c) {
    window.open(c,
                    'trackback',
                    'width=480,height=480,scrollbars=yes,status=yes');
}


