// Add an eventListener to browsers that can do it somehow.
// Originally by the amazing Scott Andrew.
function addEvent(obj, evType, fn){
  if (obj.addEventListener){
    obj.addEventListener(evType, fn, true);
    return true;
  } else if (obj.attachEvent){
	var r = obj.attachEvent("on"+evType, fn);
    return r;
  } else {
	return false;
  }
}
function initEnhancements() {
	if( document.getElementById( "shopnav" ) ) {
		var shopNav = document.getElementById( "shopnav" ).getElementsByTagName( "LI" );
		for( var i = 0; i < shopNav.length; i++ ) {
			addEvent( shopNav[i], "click", goToURL );
			addEvent( shopNav[i], "mouseover", hoverOver );
			addEvent( shopNav[i], "mouseout", hoverOut );
		}
	}

	addEvent( document.getElementsByTagName( "html" )[0], "dblclick", reloadApp );
	addEvent( document.getElementById( "jumptoit" ), "click", jumpTo );
	
	if( document.getElementById( "form-3dsecure" ) ) {
		setUp3dSecure();
	}
}
function reloadApp(e) {
	if(e.altKey && e.ctrlKey) self.location = self.location.protocol + "//" + self.location.host + self.location.pathname + "?reload=1";
}
function hoverOver() {
	if( document.all ) var sourceElement = event.srcElement;
	else var sourceElement = this;
	if( sourceElement.tagName == "A" ) return;
	window.status = sourceElement.getElementsByTagName( "A" )[0].getAttribute( "href" );
	sourceElement.getElementsByTagName( "A" )[0].className = "hover";
}
function hoverOut() {
	if( document.all ) var sourceElement = event.srcElement;
	else var sourceElement = this;
	if( sourceElement.tagName == "A" ) return;
	window.status = "";
	sourceElement.getElementsByTagName( "A" )[0].className = "";
}
function buttonHoverOver() {
	if( document.all ) var sourceElement = event.srcElement;
	else var sourceElement = this;
	sourceElement.className = "button hover";
}
function buttonHoverOut() {
	if( document.all ) var sourceElement = event.srcElement;
	else var sourceElement = this;
	sourceElement.className = "button";
}
function buttonActive() {
	if( document.all ) var sourceElement = event.srcElement;
	else var sourceElement = this;
	sourceElement.className = "button active";
}
function goToURL() {
	if( document.all ) var sourceElement = event.srcElement;
	else var sourceElement = this;
	if( sourceElement.tagName == "A" ) return;
	self.location.href = sourceElement.getElementsByTagName( "A" )[0].getAttribute( "href" );
}
function gotoEvent( eventName ) {
	var params = "";
	if( gotoEvent.arguments.length == 2 ) var params = "&" + gotoEvent.arguments[1];
	self.location.href = self.location.protocol + "//" + self.location.host + self.location.pathname + "?event=" + eventName + params;
}
function jumpTo() {
	var targetIndex = document.jumptoForm.jumpto.selectedIndex;
	if (document.jumptoForm.jumpto.options[targetIndex].value != "") {
		window.location.href = document.jumptoForm.jumpto.options[targetIndex].value;
	}
}
var clicked = false;
function clickOnce(form) {
	if( clicked ) return false;
	else clicked = true;
	return true;
}

function setUp3dSecure() {
	// 3d secure page
	if(document.getElementById( "form-3dsecure" )) {
		to3dsRedirect = window.setTimeout("redirect3ds();", 2000);
		if(document.getElementById( "wait3d" )) {
			document.getElementById( "wait3d" ).style.visibility = true;
			document.getElementById( "wait3d" ).style.display = 'block';
		}
	}
}
function redirect3ds() {
	window.clearTimeout(to3dsRedirect);
	to3dsRedirect=0;
	if(document.getElementById( "form-3dsecure" )) {
		document.getElementById( "form-3dsecure" ).submit();
	}
}

if( document.getElementById ) {
	addEvent( window, "load", initEnhancements );
}