var homePage = getURLVar('start');
var inited = false;
var splashFadeTimer;

function splashEnterPage() { 
	var objMain = document.getElementById('splashMainContainer');
	objMain.opacityTween.begin = 0;
	objMain.opacityTween.setFinish(100);
	objMain.opacityTween.setDuration(1.5);
	objMain.opacityTween.onMotionFinished = function() { 
		//splashFadeTimer = setTimeout(splashLeavePage,3000);
	};
	objMain.opacityTween.start();
}

function splashLeavePage() {
	var objMain = document.getElementById('splashMainContainer');
	objMain.opacityTween.begin = 100;
	objMain.opacityTween.setFinish(0);
	objMain.opacityTween.setDuration(4);
	objMain.opacityTween.onMotionFinished = function() { 
		//nothing 
	};
	objMain.opacityTween.start();
}

function init() {
	
	//alert('go');
	
	if (homePage == 1) {
		var objMain = document.getElementById('splashMainContainer');
		objMain.opacityTween = new OpacityTween(objMain,Tween.strongEaseInOut, 0, 100, 1.5);
		//splashEnterPage();
	}
	if (curPageName != 'home') {
		document.getElementById('coverUp').style.display = 'none';
		document.getElementById('menuActiveBG').style.backgroundPosition = lastPos + 'px 0px';
		//document.getElementById('innerContainer').style.backgroundImage = "url('/images/pagebodyBG_bare.jpg')";
	} else {
		document.getElementById('pagebody').style.background = "transparent url('/images/scrollBG.png') no-repeat top right";
	}
	//enterPage();
	inited = true;
	if (menuItemActive) {
		document.getElementById(menuItemActive).src = document.getElementById(menuItemActive).src.replace('.png','_on.png');
	}
}

function findPosX(obj) {
    var curleft = 0;
    if(obj.offsetParent)
        while(1) 
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
}

function getURLVar(urlVarName) {
	//divide the URL in half at the '?'
	var urlHalves = String(document.location).split('?');
	var urlVarValue = '';
	if(urlHalves[1]){
		//load all the name/value pairs into an array
		var urlVars = urlHalves[1].split('&');
		//loop over the list, and find the specified url variable
		for(i=0; i<=(urlVars.length); i++){
			if(urlVars[i]){
				//load the name/value pair into an array
				var urlVarPair = urlVars[i].split('=');
				if (urlVarPair[0] && urlVarPair[0] == urlVarName) {
					//I found a variable that matches, load it's value into the return variable
					urlVarValue = urlVarPair[1];
				}
			}
		}
	}
	return urlVarValue;   
}

var curPageName = location.href.replace(/.*\//,'');
curPageName = curPageName.replace(/(.*).cfm.*/g,'$1');

var lastPos = 0;
var menuItemActive = '';

switch(curPageName) {
	case 'about':
		lastPos = 13;
		menuItemActive = 'menu1';
		break;
	case 'appointments':
		lastPos = 132;
		menuItemActive = 'menu2';
		break;
	case 'gallery':
		lastPos = 265;
		menuItemActive = 'menu3';
		break;
	case 'contact':
		lastPos = 398;
		menuItemActive = 'menu4';
		break;
	case 'testimonials':
		lastPos = 520;
		menuItemActive = 'menu5';
		break;
	case 'pricing':
		lastPos = 670;
		menuItemActive = 'menu6';
		break;
	default:
		lastPos = -100;
}

function setMenu() {
	if	(curPageName == 'home') {
		//to run mouse events remotely
		var obj = document.getElementById('menu1');
		var func = obj.onmouseout;
		//scopes function to element
		//setTimeout(function() { func.call(obj) },3000);
		func.call(obj);
	}	
}

var timer = 0;
var timerInterval;

function timerFunc() {
	timer = timer + 1;
	
	if (timer >= 3 && inited == true) {
		splashLeavePage();
	}
	if (timer >= 5 && inited == true) {
		enterPage();
		clearInterval(timerInterval);
	}
}

function startSplashTimer() {
	if(homePage == 1) {
		timerInterval = setInterval(timerFunc,1000);
	}
}
function enterPage() {
	var obj = document.getElementById('coverUp');
	opacityTween = new OpacityTween(document.getElementById('coverUp'),Tween.strongEaseInOut, 100, 0, 2); 
	opacityTween.onMotionFinished = function() { 
		obj.style.display = 'none';
		setMenu();
	};
	opacityTween.start();
}

function returnMenuBG(obj) {
	var objX = findPosX(obj) - findPosX(obj.parentNode.parentNode);
	var curPos = document.getElementById('menuActiveBG').style.backgroundPosition;
	var curPosNew = Math.floor(curPos.replace(/(\d.*)px\ .*/g, '$1'));
	if(obj.parentNode.parentNode.tweenTA1) {
		obj.parentNode.parentNode.tweenTA1.begin = eval(curPosNew);
		obj.parentNode.parentNode.tweenTA1.setFinish(lastPos);
		obj.parentNode.parentNode.tweenTA1.func = Tween.strongEaseOut;
		obj.parentNode.parentNode.tweenTA1.setDuration(1.5);
	} else {
		obj.parentNode.parentNode.tweenTA1 = new Tween(new Object(),'xyz',Tween.strongEaseInOut,eval(curPosNew),lastPos,1.5);
		obj.parentNode.parentNode.tweenTA1.onMotionChanged = function(event){
			document.getElementById('menuActiveBG').style.backgroundPosition = event.target._pos + 'px 0px';
		};
	}
	obj.parentNode.parentNode.tweenTA1.start();
}

function moveMenuBG(obj) {
	//alert(obj.parentNode.parentNode.id + ": " + findPosX(obj.parentNode.parentNode));
	var objX = findPosX(obj) - findPosX(obj.parentNode.parentNode);
	var objWidth = obj.clientWidth;
	var newPos = Math.floor(objX + (objWidth / 2 - (121 / 2))) ;
	var curPos = document.getElementById('menuActiveBG').style.backgroundPosition;
	//var re = new RegExp(/(\d.*)px\ .*/g);
	var curPosNew = curPos.replace(/(\d.*)px\ .*/g, '$1');
	//lastPos = eval(curPosNew);
	if(obj.parentNode.parentNode.tweenTA1) {
		obj.parentNode.parentNode.tweenTA1.begin = eval(curPosNew);
		obj.parentNode.parentNode.tweenTA1.setFinish(newPos);
		obj.parentNode.parentNode.tweenTA1.func = Tween.strongEaseOut;
		obj.parentNode.parentNode.tweenTA1.setDuration(0.5);
	} else {
		obj.parentNode.parentNode.tweenTA1 = new Tween(new Object(),'xyz',Tween.strongEaseOut,eval(curPosNew),newPos,0.5);
		obj.parentNode.parentNode.tweenTA1.onMotionChanged = function(event){
			document.getElementById('menuActiveBG').style.backgroundPosition = event.target._pos + 'px 0px';
		};
	}
	obj.parentNode.parentNode.tweenTA1.start();
}

window.onload = init;
