function initNav(divs){
	var effects_a = new Array();
	var lis = divs.getElementsByTagName('li');
	for(var i =0;i<lis.length;i++){
		lis[i].onmouseover = mlover;
		lis[i].onmouseout = mloutSetTimeout;
		var uls = lis[i].getElementsByTagName('ul');
		for(var k=0;k<uls.length;k++){
			var found = 'no';
			for(var z=0;z<effects_a.length;z++){
				if(effects_a[z] == uls[k]){
					found = 'yes';
				}
			}
			if(found == 'no'){
				effects_a[effects_a.length] = uls[k];
				uls[k].style.zIndex = '1000';
				mlEffectLoad(uls[k]);
			}
		}
	}
}
function mloutSetTimeout(e){
	if(!e){
		var the_e = window.event;
	}
	else{
		var the_e = e;
	}
	var reltg = (the_e.relatedTarget) ? the_e.relatedTarget : the_e.toElement;
	if(reltg){
		var under = ancestor(reltg,this);
		if(under === false && reltg != this){
			window.mlLast = this;
			var parent = this.parentNode;
			while(parent.parentNode && parent.className.indexOf('mlmenu') == -1){
				parent = parent.parentNode;
			}
			window.mlTimeout = setTimeout(function(){mlout()},1000);
		}
	}
}
function mlout(){
if(window.mlLast==null)return false;
	var uls = window.mlLast.getElementsByTagName('ul');
	for(var i=0;i<uls.length;i++){
		mlEffectOut(uls[i]);
		window.mlLast.className = 'haschild hide';
	}
	window.lastover = null;
}
function mlover(e){
	if(!e){
		var the_e = window.event;
	}
	else{
		var the_e = e;
	}
	the_e.cancelBubble = true;
	if(the_e.stopPropagation){
		the_e.stopPropagation();
	}
	clearTimeout(window.mlTimeout);
	if(window.mlLast && window.mlLast != this && ancestor(this,window.mlLast) == false){
		mlout();
	}
	else{
		window.mlLast = null;
	}
	var reltg = (the_e.relatedTarget) ? the_e.relatedTarget : the_e.fromElement;
	var ob = this.getElementsByTagName('ul');
		var under = ancestor(reltg,this);
		if(ob[0] && under == false){
			if(window.lastover != ob[0]){
				this.className = 'haschild';
				mlEffectOver(ob[0],this);
				window.lastover = ob[0];
			}
		}
}
function mlEffectOver(ob,parent){
	parent.className="hover";
	ob.className = "show";
}
function mlEffectOut(ob){
	var parent = ob.parentNode;
	parent.className="";
	ob.className = "";
}
function mlEffectLoad(ob){
	var parent = ob.parentNode;
	while(parent.parentNode && parent.className.indexOf('mlmenu') == -1){
		parent = parent.parentNode;
	}
}
function ancestor(child, parent){
	if(child==null)return false;
	if(navigator.userAgent.indexOf('Gecko') != -1 && navigator.userAgent.indexOf('Opera') == -1){
		var allc = parent.getElementsByTagName('*');
		for(var i= 0;i<allc.length;i++){
			if(allc[i] == child){
				return true;
			}
		}
	}
	else{
		for(; child.parentNode; child = child.parentNode){
			if(child.parentNode === parent) return true;
		}
	}
	return false;
}

function initFormfields(){
	var els = document.getElementsByTagName("input");
	for (i=0; i<els.length; i++){
		if (els[i].getAttribute("type") == "text" || els[i].getAttribute("type") == "password") els[i].className += " text";
		else if (els[i].getAttribute("type") == "button" || els[i].getAttribute("type") == "submit") els[i].className += " button";
		else if (els[i].getAttribute("type") == "image") els[i].className += " imgbutton";
		else if (els[i].getAttribute("checkbox") == "text") els[i].className += " checkbox";
	}
}

function initButtons(){
	var inputs = document.getElementsByTagName("input");
	for (var i = 0; i<inputs.length; i++){
		if(inputs[i].type=="image"){
			addAnEvent(inputs.item(i),'mouseover',buttonHover);
			addAnEvent(inputs.item(i),'mouseout',buttonHoverout);
			inputs.item(i).style.borderWidth = "0px"
		}
		if(inputs[i].disabled == true){
			inputs[i].className = "disabled";
		}
	}
	
	var images = document.getElementsByTagName("img");
	for (var i = 0; i<images.length; i++){
		if(images[i].className=="button"){
			addAnEvent(images.item(i),'mouseover',buttonHover);
			addAnEvent(images.item(i),'mouseout',buttonHoverout);
		}
	}
}
function buttonHover(){
	if (this.src.indexOf(".png") > 1) ext = ".png";
	else ext = ".gif";
	if (this.src.indexOf("1") == -1){
		var file = this.src.split(ext);
		this.src = file[0] + "1" + ext;
	}
}
function buttonHoverout(){
	if (this.src.indexOf(".png") > 1) ext = ".png";
	else ext = ".gif";
	var file = this.src.split("1"+ext);
	this.src = file[0] + ext;
}

//tracking and receipt pop ups
function initLinks(){
    var as = document.getElementsByTagName('a');
    for (var i=0; i<as.length; i++){
        if (as[i].className.indexOf("popup2") != -1){
            addAnEvent(as.item(i),'click',popupWin2);
        } else if (as[i].className.indexOf("popup") != -1){
	        addAnEvent(as.item(i),'click',popupWin);
	    } else if (as[i].className.indexOf("mediapop") != -1){
		    addAnEvent(as.item(i),'click',popupWin3);
		}    
    }
}
function popupWin(){
    appWindow = window.open (this.href,"appWindow","width=800,height=500,toolbar=0,resizable=1,scrollbars=1");
    appWindow.focus ();
    return false;
}

function popupWin2(){
    appWindow = window.open (this.href,"appWindow","width=500,height=270,toolbar=0,resizable=1,scrollbars=1");
    appWindow.focus ();
    return false;
}

function popupWin3(){
    appWindow = window.open (this.href,"appWindow","width=420,height=600,toolbar=0,resizable=1,scrollbars=1");
    appWindow.focus ();
    return false;
}

function initExtra(){}

function addAnEvent(target, eventName, functionName){
	eval('target.on'+eventName+'=functionName');
}

function initPage(){
	if (document.getElementById){
		if (document.getElementById("nav")) initNav(document.getElementById("nav"));
		if (document.getElementById("nav-cs")) initNav(document.getElementById("nav-cs"));		
		initFormfields();
		initButtons();
    	initExtra();
		initLinks();
	}
}

window.onload=initPage;
