var list; // global list variable cache
var tickerObj; // global tickerObj cache
var opacity = 0;
var interval = 15; // interval in seconds
var fadeSpeed = 15; // fade speed, the lower the speed the faster the fade.  40 is normal.
var alerted;
var inkonami = false;

function fadeIn(divId) {
	if(tickerObj) {
		for (i = 0; i <= 100; i++) {
			setTimeout("changeOpac(" + i + ",'" + divId + "')", fadeSpeed * i);
		}
	}
}

function fadeOut(divId) {
	var timer = 0;
	if(tickerObj) {
		for (i = 100; i >= 0; i--) {
			timer++;
			setTimeout("changeOpac(" + i + ",'" + divId + "')", (timer) * (fadeSpeed/2));
		}
	}
}

function changeOpac(opacity, id) {
	if (opacity > 100) {opacity = 100;}
	var object = document.getElementById(id).style;
	setOpac(object, opacity);
}

function setOpac(object, opacity){
	object.opacity = (opacity / 100);
	object.MozOpacity = (opacity / 100);
	object.KhtmlOpacity = (opacity / 100);
	object.filter = "alpha(opacity=" + opacity + ")";
}

function initialiseList(divId) {
  tickerObj = document.getElementById(divId);
  if (!tickerObj) {
  // reportError("Could not find a div element with id \"" + divId + "\"");
 	 return;
  }
  list = tickerObj.childNodes;
  if(list.length <= 0)
    reportError("The div element \"" + divId + "\" does not have any children");
  for (var i=0; i<list.length; i++) {
    var node = list[i];;
    if (node.nodeType == 3 && !/\S/.test(node.nodeValue)) 
        tickerObj.removeChild(node);
  }
  run(divId, 0);
}


function run(divId, count){
	changeOpac(0, divId);
	list[count].style.display = "block";
	fadeIn(divId);
	setTimeout("fadeOut('" + divId + "')", ((interval-1)*1000));
	if (count > 0) {
		list[count - 1].style.display = "none";
	}
	else {
		list[list.length - 1].style.display = "none";
	}
	count++;
	if (count == list.length) {
		count = 0;
	}

	window.setTimeout("run('" + divId + "', " + count+ ")", interval*1000);
}
function reportError(error) {
  alert("The script could not run because you have errors:\n\n" + error);
  return false;
}

String.prototype.repeat = function(l){
	return new Array(l+1).join(this);
};

function $(el) {
	return document.getElementById(el);
}

window.onload = function(){
	initialiseList('testitext');
	var kkeys = [], 
    konami = "38,38,40,40,37,39,37,39,66,65";
	Event.add(document, "keydown", function konamiListen(e) {
		var evt = window.event || e;
  		kkeys.push(evt.keyCode);
		if ( kkeys.toString().indexOf(konami) >= 0 && !inkonami){
			inkonami = true;
			konamiCode();        
		}
	});
};

function getSize(x) {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  if (x) {
  	return [myWidth];
  } else {
  	return [myHeight];
  }
}

function getScrollXY(x) {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  if (x) {
  	return [ scrOfX ];
  } else {
	return [ scrOfY ];
  }
}

function moveHide() {
	var el = Dom.get("hide");
	var ws = Dom.get("warp");
	if (el!=null) {
		var height = getSize();
		var top = getScrollXY();
		el.style.top = top+"px";
		el.style.height = height+"px";
		el = Dom.get("causeform");
	}
	if (ws!=null) {
		var height = getSize();
		var top = getScrollXY();
		ws.style.top = top+"px";
		ws.style.height = height+"px";
		ws = Dom.get("causeform");
	}
}

function konamiCode() {
	var hide = document.createElement('div');
	hide.id = 'hide';
	var hs = hide.style;
	hs.position = 'absolute';
	hs.top = getScrollXY()+"px";
	hs.left = '0';
	hs.width = '100%';
	hs.height = getSize()+"px";
	hs.backgroundColor = 'black';
	hs.zIndex = '98';
	hs.padding = '0';
	hs.margin = '0';
	setOpac(hs, 0);
	Dom.add(hide, document.body);
	moveint = setInterval("moveHide()", 100);
	fadeIn("hide");
	setTimeout("window.location='zork.html'", fadeSpeed*150);
}

