var PRELOAD_XML_FEED = true;

var appParams;
var domGlobals;


function setup()
{
	setupDefaultAppParams();
	setupDefaultDOMGlobals();
	
	changeOrientation(); // calls 'hideAddressBar'
	setTimeout ( continueSetup, 100 );
}

function continueSetup()
{
	setupCustomAppParams();
	setupCustomDOMGlobals();
	
	setSlipperForPanel( appParams.currentPanel );
	resizeWindowForOrientation( appParams.orientation );

	setCopyrightYear();
	
	startXMLFeeder();
	startImageLoader();
}

function completeSetup()
{
	updateImageLoaderUIForProgress( -1 );

	appParams.state = "normal";
	startSlide( 1, null, "afterSplash();" );
}

// function afterSplash() {} // OVERRIDE in script.js




// ************************************* //
// ************************************* //

function setupDefaultAppParams()
{
	appParams = new Object();
	
	appParams.debugging    = false;
	appParams.orientation  = "portrait";
	appParams.state        = "loading";
	
	if ( PRELOAD_XML_FEED )
		appParams.xmlPreloaded = false;
		
	appParams.currentPanel = 0;
	
	appParams.infoState = false;
	appParams.popState  = false;
}
// function setupCustomAppParams() {} // OVERRIDE in script.js


function setupDefaultDOMGlobals()
{
	var aIdx = 0, aDummy;

	domGlobals = new Object();

	domGlobals.clipper = document.getElementById("clipper");
	domGlobals.slipper = document.getElementById("slipper");	
	
	domGlobals.panels  = new Array();
	for ( aIdx = 0; aDummy = document.getElementById("panel-"+aIdx); aIdx++ )
		domGlobals.panels.push( aDummy );
	
	domGlobals.info = new Object();
		domGlobals.info.state    = false; // Okay, so it's not a DOM element ... sue me!
		domGlobals.info.panel    = document.getElementById("panel-i");
		domGlobals.info.hideBtn  = document.getElementById("btn-done");
		domGlobals.info.showBtns = new Array();
		for ( aIdx = 0; aDummy = document.getElementById("btn-info-"+aIdx); aIdx++ )
			domGlobals.info.showBtns.push( aDummy );
}
// function setupCustomDOMGlobals() // OVERRIDE in script.js



// *** HOUSEKEEPING SETUP ***//

function startXMLFeeder()
{
	if ( !PRELOAD_XML_FEED ) return;
	
	appParams.xmlPreloaded = false;
	
	sendXMLRequest( "" ); // xmlProcessing.js
}

function updateImageLoaderUIForProgress( aProg ) // OVERRIDE in script.js
{
	document.getElementById("prog-splash"    ).style.display = ( aProg == -1 ? "none" : "block" );
	document.getElementById("prog-splash-ind").style.width   = ( 100*aProg + "%" );	
}

function startImageLoader()
{
	initImageLoader();
	fillImageLoader();
	updateImageLoaderUIForProgress( appParams.imageLoader.progress );
	
	rLoadImageLoaderImageAtIndex( 0 );
	setTimeout( "checkImageLoader()", 0 );
}
function initImageLoader()
{
	appParams.imageLoader = new Object();
	
	appParams.imageLoader.timeStamp = ( new Date() ).getTime();
	appParams.imageLoader.count     = 0;
	appParams.imageLoader.progress  = 0;
	appParams.imageLoader.images    = new Array();
}
// function fillImageLoader() // OVERRIDE in script.js

function rLoadImageLoaderImageAtIndex( aIdx )
{
	if ( aIdx == appParams.imageLoader.images.length )
		return;
	
	var aImgSrc = appParams.imageLoader.images[aIdx];
	appParams.imageLoader.images[aIdx]     = new Image();
	appParams.imageLoader.images[aIdx].src = aImgSrc;
	
	rLoadImageLoaderImageAtIndex( 1+aIdx );
}
function rUpdateImageLoaderProgressWithIndex( aIdx )
{
	if ( aIdx == appParams.imageLoader.images.length )
		return;
	
	if ( aIdx == 0 )
		appParams.imageLoader.progress = 0;
	
	appParams.imageLoader.progress += ( appParams.imageLoader.images[aIdx].complete ? 1 : 0 );
	rUpdateImageLoaderProgressWithIndex( 1+aIdx );
}
function checkImageLoader()
{
	rUpdateImageLoaderProgressWithIndex( 0 );
	
	if (( PRELOAD_XML_FEED ? appParams.xmlPreloaded : true ) && ( appParams.imageLoader.progress == appParams.imageLoader.images.length ))
		completeSetup();
	else
	{
		var timeElapsed = Math.max( 0, Math.min( 3000, ( new Date() ).getTime() - appParams.imageLoader.timeStamp ) );
		var theProgress = appParams.imageLoader.progress + timeElapsed / 3000.0;
		theProgress /= ( appParams.imageLoader.images.length == 0 ? 1 : appParams.imageLoader.images.length );
		theProgress  = Math.min( theProgress, 1.0 );
	
		updateImageLoaderUIForProgress( theProgress );
		
		setTimeout( "completeSetup()", 0 );
	}
}




function setCopyrightYear()
{
	var theSpan = document.getElementById("copy-year");
	
	if ( theSpan )
		theSpan.innerHTML = ( new Date() ).getFullYear();
		
	theSpan = document.getElementById("version");
	if ( theSpan )
		theSpan.innerHTML = APP_VERSION;
}
