
var intervalId = 0;
var positionValue = 0;
var maxPosition = 0;

var scrollDestination = "";

var rollDestination = "";

var destinationMap = "";

function clearMenu() {
	var menuDiv = document.getElementById('menuDiv');
	var mainFrame = document.getElementById('mainFrame');
	mainFrame.setAttribute('src', '');
	removeChildrenFromNode(menuDiv);
}

function removeChildrenFromNode(node)
{
	if(node == null)
	{
		return;
	}

	var len = node.childNodes.length;

	while (node.hasChildNodes())
	{
		node.removeChild(node.firstChild);
	}
}

function animate(imageName, maxWidth) {
	var evilDivButton = document.getElementById('evilDivButton');
	var lithDivButton = document.getElementById('lithDivButton');
	evilDivButton.style.display = "None";
	lithDivButton.style.display = "None";
	maxPosition = maxWidth-980;
	var animDiv = document.getElementById('animDiv');
	animDiv.style.opacity = 1;
	animDiv.style.backgroundImage = "url('"+imageName+"')";
	animDiv.style.backgroundPosition = "0px 0px";
	if(imageName == 'lithStart.jpg') {
		rollDestination = "lith";
	}
	intervalId = setInterval ( "nextFrame('animDiv', 980)", 40 );
}

function nextFrame(divName, width) {
	var animDiv = document.getElementById('animDiv');
	var currentStyle = animDiv.style.backgroundPosition;
	positionValue = parseInt(currentStyle.match(/(\d+)px\ 0px/)[1]);
	if(positionValue < maxPosition) {
		animDiv.style.backgroundPosition = "-"+(positionValue+width)+"px 0px";
	} else {
		clearInterval ( intervalId );
		fadeDownAnimate();
	}
}

function fadeDownAnimate() {
	document.body.style.opacity = 1;
	//var animDiv = document.getElementById('animDiv');
	intervalId = setInterval ( "fadeDownStep()", 40 );
}

function fadeDownStep() {
	current = parseFloat(document.body.style.opacity);
	if(current > 0) {
		document.body.style.opacity = current - 0.04;
	} else {
		clearInterval ( intervalId );
		if(rollDestination == "") {
			document.location.href = "home.html";
		} else {
			document.location.href = "home.html#"+rollDestination;
		}
	}
}

function homeStart() {
	fadeUpAnimate();
}

function fadeUpAnimate() {
	var backgroundDiv = document.getElementById('backgroundDiv');
	var dummy = document.getElementById('dummy');
	switch(window.location.hash) {
		case "#lith":
			backgroundDiv.style.backgroundPosition = "-2560px 0px";
			dummy.useMap = "#lithMap";
			break;
		case "#versus":
			backgroundDiv.style.backgroundPosition = "-1280px 0px";
			dummy.useMap = "#versusMap";
			break;
		default:
			break;
	}
	
	backgroundDiv.style.opacity = 0;
	intervalId = setInterval ( "fadeUpStep()", 40 );
}

function fadeUpStep() {
	var backgroundDiv = document.getElementById('backgroundDiv');
	current = parseFloat(backgroundDiv.style.opacity);
	if(current != 1) {
		backgroundDiv.style.opacity = current + 0.04;
	} else {
		clearInterval ( intervalId );
		endFade();
	}
}

function endFade() {
	switch(window.location.hash) {
		case "#lith":
			lithStart();
			break;
		case "#evil":
			evilStart();
			break;
		case "#versus":
			versusStart();
			break;
		default:
			evilStart();
	}
}

function evil2Lith() {
	clearMenu();
	var backgroundDiv = document.getElementById('backgroundDiv');
	maxPosition = 2560;
	backgroundDiv.style.backgroundPosition = "0px 0px";
	intervalId = setInterval ( "scrollLithStep(80)", 40 );
	destinationMap = "#lithMap";
}

function versus2Lith() {
	clearMenu();
	var backgroundDiv = document.getElementById('backgroundDiv');
	maxPosition = 2560;
	backgroundDiv.style.backgroundPosition = "-1280px 0px";
	intervalId = setInterval ( "scrollLithStep(80)", 40 );
	destinationMap = "#lithMap";
}

function evil2Versus() {
	clearMenu();
	var backgroundDiv = document.getElementById('backgroundDiv');
	maxPosition = 1280;
	backgroundDiv.style.backgroundPosition = "0px 0px";
	intervalId = setInterval ( "scrollLithStep(80)", 40 );
	destinationMap = "#versusMap";
}

function scrollLithStep(width) {
	var backgroundDiv = document.getElementById('backgroundDiv');
	positionValue = parseInt(backgroundDiv.style.backgroundPosition.match(/(\d+)px\ 0px/)[1]);
	if(positionValue < maxPosition) {
		backgroundDiv.style.backgroundPosition = "-"+(positionValue + width)+"px 0px";
	} else {
		clearInterval ( intervalId );
		endScroll();
	}
}

function lith2Evil() {
	clearMenu();
	var backgroundDiv = document.getElementById('backgroundDiv');
	maxPosition = 0;
	backgroundDiv.style.backgroundPosition = "-2560px 0px";
	intervalId = setInterval ( "scrollEvilStep(80)", 40 );
	destinationMap = "#evilMap";
}

function versus2Evil() {
	clearMenu();
	var backgroundDiv = document.getElementById('backgroundDiv');
	maxPosition = 0;
	backgroundDiv.style.backgroundPosition = "-1280px 0px";
	intervalId = setInterval ( "scrollEvilStep(80)", 40 );
	destinationMap = "#evilMap";
}

function lith2Versus() {
	clearMenu();
	var backgroundDiv = document.getElementById('backgroundDiv');
	maxPosition = 1280;
	backgroundDiv.style.backgroundPosition = "-2560px 0px";
	intervalId = setInterval ( "scrollEvilStep(80)", 40 );
	destinationMap = "#versusMap";
}

function endScroll() {
	switch(destinationMap) {
		case "#lithMap":
			lithStart();
			break;
		case "#versusMap":
			versusStart();
			break;
		case "#evilMap":
			evilStart();
			break;
		default:
			break;
	}
	setMap();
}

function scrollEvilStep(width) {
	var backgroundDiv = document.getElementById('backgroundDiv');
	positionValue = parseInt(backgroundDiv.style.backgroundPosition.match(/(\d+)px\ 0px/)[1]);
	if(positionValue > maxPosition) {
		backgroundDiv.style.backgroundPosition = "-"+(positionValue - width)+"px 0px";
	} else {
		clearInterval ( intervalId );
		endScroll();
	}
}


function setMap() {
	document.getElementById('dummy').useMap = destinationMap;
}

function lithStart() {
	var menuArray = Array(
			Array("About me", "", "pages/aboutLith.html")
		);
	showMenu(menuArray);
	window.location.hash = "#lith";
}

function versusStart() {
	var menuArray = Array(
			Array("Homepages", "", "imageGallery.php?page=versusHomepages")
		);
	showMenu(menuArray);
	window.location.hash = "#versus";
}

function evilStart() {
	var menuArray = Array(
						Array("About me", "aboutMe", "pages/aboutEap.html"),
						Array("Sewing", "", "imageGallery.php?page=sewing"),
						Array("Sculptures", "", "imageGallery.php?page=sculptures"),
						Array("Ex-3D", "", "imageGallery.php?page=ex-3d"),
						Array("Photography", "", "imageGallery.php?page=photography"),
						Array("Illustrations", "", "imageGallery.php?page=illustrations"),
						Array("Other", "", "imageGallery.php?page=other"),
						Array("Videos", "", "pages/teiaielVideos.html")
		);
	showMenu(menuArray);
	window.location.hash = "#evil";
}

function showMenu(menuArray) {
	var menuDiv = document.getElementById('menuDiv');
	document.getElementById('menuDiv').style.display = "block";
	for (menuItem in menuArray) {
		menuElement = document.createElement('a');
		// menuElement.setAttribute('href', '#');
		menuElement.setAttribute('onclick', 'setPage("'+menuArray[menuItem][2]+'", "'+menuArray[menuItem][1]+'")');
		menuElement.setAttribute('class', 'menuItem');
		menuElement.innerHTML = menuArray[menuItem][0];
		menuDiv.appendChild(menuElement);
		menuDiv.appendChild(document.createElement('br'));
	}
}

function setPage(page, className) {
	var mainFrame = document.getElementById('mainFrame');
	mainFrame.setAttribute('class', className);
	mainFrame.setAttribute('src', page);
	
}


function changeVideo(videoFile) {
	alert(thisMovie('playerID').loadFile());
	//var videoPlayer = document.getElementById('player');
	//var videoPlayerVars = document.getElementById('videoPlayerVars');
	//videoPlayerVars.value = "file=../videos/"+videoFile;
	//alert(videoPlayer.getConfig().autostart);
	//alert(videoPlayer.sendEvent("LOAD", "/videos/test.flv"));
	//alert(videoPlayerVars.getAttr);
}
