var btn1okGo = true;
var btn2okGo = true;
var btn3okGo = true;

var btnHomeOkGo = true;
var btnGamesOkGo = true;
var btnContactOkGo = true;

var okay = false;
var onHome = true;
var onGames = false;
var onContact = false;

var screenNum = 1;
var screenInt = 0;
var sX;

//var gamesIconVisible = true;
//var toggleIconsInt = 0;

var loaded = 0;
var contentHeight = 555;

window.onresize = lineUp;

// Browser Detection Javascript
// copyright 1 February 2003, by Stephen Chapman, Felgall Pty Ltd

// You have permission to copy and use this javascript provided that
// the content of the script is not changed in any way.

function whichBrs() {
	var agt=navigator.userAgent.toLowerCase();
	if (agt.indexOf("opera") != -1) return 'Opera';
	if (agt.indexOf("staroffice") != -1) return 'Star Office';
	if (agt.indexOf("webtv") != -1) return 'WebTV';
	if (agt.indexOf("beonex") != -1) return 'Beonex';
	if (agt.indexOf("chimera") != -1) return 'Chimera';
	if (agt.indexOf("netpositive") != -1) return 'NetPositive';
	if (agt.indexOf("phoenix") != -1) return 'Phoenix';
	if (agt.indexOf("firefox") != -1) return 'Firefox';
	if (agt.indexOf("safari") != -1) return 'Safari';
	if (agt.indexOf("skipstone") != -1) return 'SkipStone';
	if (agt.indexOf("msie") != -1) return 'Internet Explorer';
	if (agt.indexOf("netscape") != -1) return 'Netscape';
	if (agt.indexOf("mozilla/5.0") != -1) return 'Mozilla';
	if (agt.indexOf('\/') != -1) {
	if (agt.substr(0,agt.indexOf('\/')) != 'mozilla') {
	return navigator.userAgent.substr(0,agt.indexOf('\/'));}
	else return 'Netscape';} else if (agt.indexOf(' ') != -1)
	return navigator.userAgent.substr(0,agt.indexOf(' '));
	else return navigator.userAgent;
}

var browserName = whichBrs();

function start() {
		
	// preload images
	var img1 = new Image(); img1.src = '_assets/img/input_bg.gif';
	var img2 = new Image(); img2.src = '_assets/img/input_bg_name.gif';
	var img3 = new Image(); img3.src = '_assets/img/input_bg_email.gif';
	var img4 = new Image(); img4.src = '_assets/img/input_bg_subject.gif';
	var img5 = new Image(); img5.src = '_assets/img/textarea_bg.gif';
	var img6 = new Image(); img6.src = '_assets/img/glass_bg.png';
	var img7 = new Image(); img7.src = '_assets/img/glass_bg_home.png';

	if (browserName == 'Internet Explorer')	{
		$('games-screens').style.top = '22px';
		setTimeout(checkLoad, 1000);
	} else {
		img1.onload = img2.onload = img3.onload = img4.onload = img5.onload = img6.onload = img7.onload = checkLoad;
	}

	$('hit1').onmouseover = function() { if (btn1okGo) new Effect.Morph('btn1', { style:'border-width:7px; border-color:#9f0; opacity:0.5', duration:0.3 }); };
	$('hit1').onmouseout = function() { btn1okGo = false; window.setTimeout(function() { new Effect.Morph('btn1', { style:'border-width:1px; border-color:#00aeef; opacity:1', duration:0.3 }); btn1okGo = true; }, 200); };
	$('hit2').onmouseover = function() { if (btn2okGo) new Effect.Morph('btn2', { style:'border-width:7px; border-color:#9f0; opacity:0.5', duration:0.3 }); };
	$('hit2').onmouseout = function() { btn2okGo = false; window.setTimeout(function() { new Effect.Morph('btn2', { style:'border-width:1px; border-color:#00aeef; opacity:1', duration:0.3 }); btn2okGo = true; }, 200); };
	$('hit3').onmouseover = function() { if (btn3okGo) new Effect.Morph('btn3', { style:'border-width:7px; border-color:#9f0; opacity:0.5', duration:0.3 }); };
	$('hit3').onmouseout = function() { btn3okGo = false; window.setTimeout(function() { new Effect.Morph('btn3', { style:'border-width:1px; border-color:#00aeef; opacity:1', duration:0.3 }); btn3okGo = true; }, 200); };

	$('pos-name').onfocus = function() { this.style.backgroundImage = 'url(_assets/img/input_bg.gif)'; };
	$('pos-name').onblur = function() { if (this.value == '') this.style.backgroundImage = 'url(_assets/img/input_bg_name.gif)'; };
	$('pos-email').onfocus = function() { this.style.backgroundImage = 'url(_assets/img/input_bg.gif)'; };
	$('pos-email').onblur = function() { if (this.value == '') this.style.backgroundImage = 'url(_assets/img/input_bg_email.gif)'; };
	$('pos-regard').onfocus = function() { this.style.backgroundImage = 'url(_assets/img/input_bg.gif)'; };
	$('pos-regard').onblur = function() { if (this.value == '') this.style.backgroundImage = 'url(_assets/img/input_bg_subject.gif)'; };

	$('btn-home').onmouseover = function() {
		if (btnHomeOkGo) {
			new Effect.Move('btn-home', { x:386, y:20, duration:.2, mode:'absolute' });
			if (browserName != 'Internet Explorer') new Effect.Appear('btn-home-glow', { duration:0.25 });
		}
	};
	$('btn-home').onmouseout = function() {
		btnHomeOkGo = false;
		window.setTimeout(function() {
			new Effect.Move('btn-home', { x:386, y:23, duration:.2, mode:'absolute' });
			btnHomeOkGo = true;
		}, 200);
		if (browserName != 'Internet Explorer') new Effect.Fade('btn-home-glow', { duration:0.25 });
	};
	$('btn-home').onclick = function() {
		if (onContact) returnHomeFromContact();
		else if (onGames) returnHomeFromGames();
	};
	$('btn-games').onmouseover = function() {
		if (btnGamesOkGo) {
			new Effect.Move('btn-games', { x:486, y:20, duration:.2, mode:'absolute' });
			if (browserName != 'Internet Explorer') new Effect.Appear('btn-games-glow', { duration:0.25 });
		}
	};
	$('btn-games').onmouseout = function() {
		btnGamesOkGo = false;
		window.setTimeout(function() {
			new Effect.Move('btn-games', { x:486, y:23, duration:.2, mode:'absolute' });
			btnGamesOkGo = true;
		}, 200);
		if (browserName != 'Internet Explorer') new Effect.Fade('btn-games-glow', { duration:0.25 });
	};
	$('btn-games').onclick = games;
	$('btn-contact').onmouseover = function() {
		if (btnContactOkGo) {
			new Effect.Move('btn-contact', { x:586, y:20, duration:.2, mode:'absolute' });
			if (browserName != 'Internet Explorer') new Effect.Appear('btn-contact-glow', { duration:0.25 });
		}
	};
	$('btn-contact').onmouseout = function() {
		btnContactOkGo = false;
		window.setTimeout(function() {
			new Effect.Move('btn-contact', { x:586, y:23, duration:.2, mode:'absolute' });
			btnContactOkGo = true;
		}, 200);
		if (browserName != 'Internet Explorer') new Effect.Fade('btn-contact-glow', { duration:0.25 });
	};
	$('btn-contact').onclick = contact;
	
}

function slideScreens() {
	sX = 697;
	screenInt = setInterval(function() {
		sX = (sX == -439) ? 696 : sX-1;
		$('games-screens').style.left = sX+'px';
	}, 60);
}
/*
function toggleIcons() {
	if (gamesIconVisible) {
		gamesIconVisible = false;
		new Effect.Fade('games-icn', { duration:0.25 });
		new Effect.Appear('games-updateicn', { duration:0.25 });
	} else {
		gamesIconVisible = true;
		new Effect.Appear('games-icn', { duration:0.25 });
		new Effect.Fade('games-updateicn', { duration:0.25 });
	}
}
*/
function checkLoad() {
	loaded++;
	if (browserName == 'Internet Explorer' || loaded == 7) {
		lineUp();
		new Effect.Appear('outer-content', { duration:1.0 });
		setTimeout(function() { okay = true; }, 1000);
	}
}

function lineUp() {
	var cw = document.documentElement.clientWidth;
	var ch = document.documentElement.clientHeight;
	var cl = Math.ceil(cw/2)-497;
	var ct = Math.ceil(ch/2)-contentHeight/2;
	$('outer-content').style.left = ((cl < 0) ? 0 : cl)+'px';
	$('outer-content').style.top = ((ct < 0) ? 0 : ct+((ch%2 == 0) ? -1 : -1))+'px';
}

function games() {
	if (okay && !onGames) {
		okay = false;

		// start sliding screens and make them immediately visible
		slideScreens();
		
		// if we're on the homepage...
		if (onHome) {
			onHome = false;
			
			new Effect.Fade('ipod', { duration:0.5 });
			new Effect.Fade('intro', { duration:0.5 });
			setTimeout(function() {
			
				new Effect.Appear('games-screens', { duration:0.0 });
				if (browserName != 'Internet Explorer') $('glass').style.backgroundImage = 'url(_assets/img/glass_bg.png)';
				new Effect.Move('glass', { x:-247, y:0, mode:'absolute' });
				new Effect.Scale('content', 125, { scaleX:false, scaleContent:false });
				new Effect.Scale('outer-content', 115, { scaleX:false, scaleContent:false, scaleFromCenter:true });
				contentHeight = 638;
				
				setTimeout(function() {
				
					// fade in games content
					new Effect.Appear('games', { duration:0.5 });
//					toggleIconsInt = setInterval(toggleIcons, 5000);
					onGames = true;
					okay = true;
				}, 1000);
			}, 500);
			
		// if we're on the contact page
		} else if (onContact) {
			onContact = false;
			
			// fade out contact content
			new Effect.Fade('contact', { duration:0.5 });
			setTimeout(function() {

				// scale content to normal size and move glass back to default position
				new Effect.Scale('content', 125, { scaleX:false, scaleContent:false });
				new Effect.Scale('outer-content', 115, { scaleX:false, scaleContent:false, scaleFromCenter:true });
				contentHeight = 638;
				if (browserName != 'Internet Explorer') $('glass').style.backgroundImage = 'url(_assets/img/glass_bg.png)';
				new Effect.Move('glass', { x:-247, y:0, mode:'absolute' });
				setTimeout(function() {
				
					// fade in games content
					new Effect.Appear('games', { duration:0.5 });
					new Effect.Appear('games-screens', { duration:0.5 });
//					toggleIconsInt = setInterval(toggleIcons, 5000);
					onContact = false;
					onGames = true;
					okay = true;
				}, 1000);
			}, 500);
		}
	}
}

function contact() {
	if (okay && !onContact) {
		okay = false;
		if (onHome) {
			onHome = false;
			new Effect.Fade('intro', { duration:0.5 });
			setTimeout(function() {
				new Effect.Fade('ipod', { duration:0.5 });
				setTimeout(function() {
					new Effect.Appear('contact', { duration:0.5 });
					onContact = true;
					okay = true;
				}, 500);
			}, 500);
		} else if (onGames) {
			onGames = false;
			new Effect.Fade('games-screens', { duration:0.5 });
			setTimeout(function() {
				clearInterval(screenInt);
				$('games-screens').style.left = '708px';
				new Effect.Fade('games', { duration:0.5 });
//				clearInterval(toggleIconsInt);
				new Effect.Scale('content', 80, { scaleX:false, scaleContent:false });
				new Effect.Scale('outer-content', 87, { scaleX:false, scaleContent:false, scaleFromCenter:true });
				contentHeight = 555;
				setTimeout(function() {
					if (browserName != 'Internet Explorer') $('glass').style.backgroundImage = 'url(_assets/img/glass_bg_home.png)';
					new Effect.Move('glass', { x:0, y:0, mode:'absolute' });
					new Effect.Appear('contact', { duration:0.5 });
					onGame = false;
					onContact = true;
					okay = true;
				}, 1000);
			}, 600);
		}
	}
}

function returnHomeFromContact() {
	if (okay) {
		okay = false;
		onContact = false;
		new Effect.Fade('contact', { duration:0.5 });
		setTimeout(function() {
			new Effect.Appear('ipod', { duration:0.5 });
			setTimeout(function() {
				new Effect.Appear('intro', { duration:0.5 });
				onHome = true;
				okay = true;
			}, 500);
		}, 500);
	}
}

function returnHomeFromGames() {
	if (okay) {
		okay = false;
		onGames = false;
		new Effect.Fade('games-screens', { duration:0.5 });
		setTimeout(function() {
			clearInterval(screenInt);
			$('games-screens').style.left = '708px';
			new Effect.Fade('games', { duration:0.5 });
//			clearInterval(toggleIconsInt);
			new Effect.Scale('content', 80, { scaleX:false, scaleContent:false });
			new Effect.Scale('outer-content', 87, { scaleX:false, scaleContent:false, scaleFromCenter:true });
			contentHeight = 555;
			setTimeout(function() {
				if (browserName != 'Internet Explorer') $('glass').style.backgroundImage = 'url(_assets/img/glass_bg_home.png)';
				new Effect.Move('glass', { x:0, y:0, mode:'absolute' });
				new Effect.Appear('intro', { duration:0.5 });
				setTimeout(function() {
					new Effect.Appear('ipod', { duration:0.5 });
					onHome = true;
					okay = true;
				}, 500);
			}, 1000);
		}, 600);
	}
}

