// PORTIONS COPYRIGHT (c) 2007, DON MCCONNELL, DAY LATE & DOLLAR SHORT SOFTWARE
//
// All persons are free to use this file for the purposes of learning
// iPhone application operation. However, wholesale duplication of code
// should be acknowledged with the above copyright notice, as well as
// a visible link to the Day Late & Dollar Short web site:
//
//            http://www.daylateanddollarshort.com
//

var ITAROT_VERSION = "1.0";

var ASTROLOGY_OMNITURE_CODE = "?par=ast|site|itarot";
var  IVILLAGE_OMNITURE_CODE = "?par=ast|site|itarot";
var        DLDS_SENDER_CODE = "?&sender=itarot"

var DECK_ID = "ut";
var CARD_SIZE_CODE = 0;

var progTimeStamp = 0;

var amDebugging = true;

function setup()
{
	amDebugging = false;
	
	appState = "loading";
	
	setupDOMGlobals();   // in "ui.js"
	adjustSize();        // in "ui.js"
	changeOrientation(); // in "ui.js"
	
	setTimeout ( continueSetup, 100 ); // done with a timeout so that "hideAddressBar" (in changeOrientation) has a chance to finish
}

function continueSetup()
{
	currPanel = 0;
	//setSlipperForPanel( currPanel );
	
	infoState = false;
	textState = false;
	tarotType = "daily";
	
	setCopyrightYear(); // in "ui.js"
	setVersion();       // in "ui.js"
	
	progTimeStamp = ( new Date() ).getTime();
	document.getElementById("prog-splash"    ).style.display = "block";
	document.getElementById("prog-splash-ind").style.width   =    "0%";
	
	getAstroFeed( 0, checkDailyCookie() );
	startLoadImages();
	
	setTimeout( "completeSetup()", 0 );
}



function completeSetup()
{
	if ( imagesLoaded() && gotAstroFeed() )
	{
		hideAddressBar();
		
		appState = "normal";
		
		updateTarotContents();
		checkDailyCookie( "iTarotCards", "1000xxx1000" );
		
		startSlide( 1, null );
	}
	else
		setTimeout( "completeSetup()", 0 );
}

function setDailyCookie( cookieName, cookieValue )
{
	var tomorrow = new Date();
	
	tomorrow.setDate( tomorrow.getDate() + 1 );
	tomorrow.setHours  (0);
	tomorrow.setMinutes(0);
	tomorrow.setSeconds(0);
	
	document.cookie = cookieName+"="+escape(cookieValue) + ";expires="+tomorrow.toGMTString();
}

function getDailyCookie( c_name )
{
	var theResult = "";
	
	if ( document.cookie.length > 0 )
	{
		c_start = document.cookie.indexOf(c_name + "=");
		if ( c_start != -1 )
		{
			c_start = c_start + c_name.length + 1;
			c_end   = document.cookie.indexOf( ";", c_start );
			if ( c_end == -1 )
				c_end = document.cookie.length;
			
			theResult = unescape( document.cookie.substring( c_start, c_end ) );
		}
	}
	
	return theResult;
}

function cookieIsValid( aCookie )
{
	var theResult = false;
	
	if (( aCookie != null ) && ( aCookie != undefined ) && ( aCookie.indexOf("xxx") != -1 ))
	{
		aCookie = aCookie.split("xxx");
		theResult = ( 1*aCookie[0] == ( 1*aCookie[0] %  78 ) )
				&&  ( 1*aCookie[1] == ( 1*aCookie[1] % 156 ) );
	}
	return theResult;
}

function checkDailyCookie()
{
	var cookieCards = getDailyCookie('iTarotCards');
	if ( !cookieIsValid( cookieCards ) )
	{
		cookieCards = Math.round( ( Math.random()*12333 ) % 78 ) 
			+ "xxx" 
			+ Math.round( ( Math.random()*33321 ) % 156 );
		
		setDailyCookie( "iTarotCards", cookieCards );
	}
	return cookieCards;
}