var scroller = null;
var scrollbar = null;
var buttonMoved = false;
var isFirefox = navigator.appName.indexOf('Netscape') != -1;

window.onload = function() 
{	
	soundManager.onready(function()
	{
		// set the position of the objects in the page (book, buttons, ...)
		setObjectPos();

		// setup the CSS of the list elements depending on the browser
		setupCSS();

		// initialize the book's scrollbar
		initScrollbar();

		// initialize the page sounds
		initSounds();
	});
}

window.onresize = function() 
{
	setObjectPos();
}

function setObjectPos()
{
	var objBookFrame = document.getElementById('book_frame');
	var objBookContent = document.getElementById('book_content');
	var objBookBackTop = document.getElementById('book_back_top');
	var objIntroMovie = document.getElementById('intro_movie');
	var objEarth = document.getElementById('earth');
	var objEnterSite = document.getElementById('enter_site');

	if (document.body && document.body.clientWidth)
	{
		objBookFrame.style.left = ((document.body.clientWidth / 2) + 20);
	}
	else
	{
		objBookFrame.style.left = 400;
	}

	objBookFrame.style.top = 270;
	objBookContent.style.left = parseInt(objBookFrame.style.left) + 20;
	objBookContent.style.top = parseInt(objBookFrame.style.top) + objBookBackTop.offsetHeight + 8;

	objBookFrame.style.visibility = "visible";
	objBookContent.style.visibility = "visible";

	objEnterSite.style.top = objIntroMovie.offsetHeight + objIntroMovie.offsetTop + 10;
	objEnterSite.style.zIndex = 3;
	
	if (buttonMoved)
	{
		objEnterSite.style.left = objEarth.offsetLeft;
	}
	else
	{
		objEnterSite.style.left = objEarth.offsetLeft + ((objEarth.offsetWidth - objEnterSite.offsetWidth) / 2);
	}

	objEnterSite.style.visibility = "visible";
}

function initScrollbar()
{
	scroller = new Scroller(document.getElementById("scroller-1"), 400, 295);
	scrollbar = new Scrollbar(document.getElementById("scrollbar-container"), scroller, new ScrollTween());
}

function initSounds()
{
	// init bgm music
	initSound(bgm);

	// init sound effects
	for (i = 0; i < sounds.length; i++)
	{
		initSound(sounds[i]);
	}
}

function initSound(objSound)
{
	soundManager.createSound(
	{
		id: objSound['id'],
		url: objSound['url'],
		volume: objSound['volume'],
		autoLoad: objSound['autoLoad'],
		stream: objSound['stream']
	});
}

function embedFlashMovie()
{
	document.write('<div id="intro_movie" align="center">');
	document.write('    <object codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="646" height="526" align="middle" id="earth" name="earth" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000">');
	document.write('        <param name="movie" value="movies/preloader.swf"/>');
	document.write('		<param name="quality" value="high"/>');
	document.write('		<param name="play" value="true"/>');
	document.write('		<param name="loop" value="false" />');
	document.write('		<param name="scale" value="showall"/>');
	document.write('		<param name="wmode" value="opaque"/>');
	document.write('		<param name="devicefont" value="false"/>');
	document.write('		<param name="bgcolor" value="#000000"/>');
	document.write('		<param name="menu" value="false"/>');
	document.write('		<param name="allowFullScreen" value="false"/>');
	document.write('		<param name="allowScriptAccess" value="always"/>');
	document.write('		<param name="salign" value=""/>');
	document.write('		<embed width="646" height="526" src="movies/preloader.swf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" align="middle" play="true" loop="false" scale="showall" wmode="opaque" devicefont="false" bgcolor="#000000" name="earth" menu="false" allowFullScreen="false" allowScriptAccess="always" salign="" type="application/x-shockwave-flash"></embed>');
	document.write('	</object>');
	document.write('</div>');
}

function showBook()
{
	var objEarth = document.getElementById('earth');
	var objEnterSite = document.getElementById('enter_site');

	soundManager.play('enterMove');
	moveX('enter_site', objEnterSite.offsetLeft, objEarth.offsetLeft);
	buttonMoved = true;

	opacity(['book_frame', 'book_content'], [0,0], [85,75], 5000);
	setTimeout("soundManager.play(bgm['id']);", 2000);
}

function setupCSS()
{
	// if Firefox, modify the CSS of the list elements to match IE displayed style
	if (isFirefox)
	{
		var unordlist = document.getElementsByTagName("ul");
		var list = document.getElementsByTagName("li");

		for (var i = 0; i < unordlist.length; i++)
		{
			unordlist[i].style.margin = "-15px 20px -25px -10px";
		}


		for (var j = 0; j < list.length; j++)
		{
			list[j].style.backgroundPosition = "0px 6px";
			list[j].style.paddingLeft = "20px";
			list[j].style.marginBottom = "-20px";
		}
	}
}
