// 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
//

//
// SIGNS pane
//

var ISCOPES_VERSION = "1.2";
var currPanel = 0;

var ASTROLOGY_OMNITURE_CODE = "?par=ast|site|iphone";
var IVILLAGE_OMNITURE_CODE  = "?par=ast|site|iphone";

var appState  = "loading";
var appOrient = "portrait";

var amDebugging = true;

function setup()
{
	amDebugging = false;
	
	appState = "loading";
	setupDOMGlobals();
	adjustSize();
	changeOrientation();
	
	setTimeout ( continueSetup,  100 );
}

function adjustSize()
{
	if ( window.innerWidth > 480 )
		resizeInnerTo( 320, 416 );
}

function hideAddressBar()
{
//	setTimeout( function() { window.scrollTo(0, 1); }, 10 );
	window.scrollTo(0, 1);
}

function continueSetup()
{
//	if ( !amDebugging )
//		getScopesForSignId( 0 );
	
	currPanel = 0;
	setSlipperForPanel( currPanel );
	
	if ( amDebugging ) // for debugging
	{
		stopSpinAnim();
		appState = "normal"; // MINI
		buildDefaultScopeObject();
		startSlide( 1, null );
	}
	setCopyrightYear();
	
	startLoadImages();
	setTimeout( "completeSetup()", 0 );
}

var imagesToLoad, imagesLoadCount, imageLoadTimeStamp;
function startLoadImages()
{
	imageLoadTimeStamp = ( new Date() ).getTime();
	imageLoadCount     = 0;
	
	document.getElementById("prog-splash"    ).style.display = "block";
	document.getElementById("prog-splash-ind").style.width   = "0%";

	var srcArray = new Array
	(
		"images/buttons/wfigures/sign-stack.png",
		
		"images/list-lite.png",
		"images/buttons/btn-bg-0.png",
		"images/buttons/btn-bg-1.png",
		"images/buttons/ltab-bg-0.png",
		"images/buttons/ctab-bg-0.png",
		"images/buttons/rtab-bg-0.png",
		"images/buttons/ltab-bg-1.png",
		"images/buttons/ctab-bg-1.png",
		"images/buttons/rtab-bg-1.png",
		"images/buttons/inset-btn-bg-0.png",
		"images/buttons/inset-btn-bg-1.png",
		"images/buttons/inset-larr-bg-0.png",
		"images/buttons/inset-larr-bg-1.png"
	);
	
	imagesToLoad = new Array();
	for ( var aIdx = 0; aIdx < srcArray.length; aIdx++ )
	{
		imagesToLoad[aIdx]        = new Image();
		imagesToLoad[aIdx].src    = srcArray[aIdx];
	}
}

function completeSetup()
{
	var imagesLoaded = true;
	var imageCount = 0;
	
	for ( var aIdx = 0; aIdx < imagesToLoad.length; aIdx++ )
		imageCount += ( imagesToLoad[aIdx].complete ? 1 : 0 );
	
	if ( imageCount > imageLoadCount )
	{
		imageLoadCount = imageCount;
		imageLoadTimeStamp = ( new Date() ).getTime();
	}
	
	var theProgress = ( 100 * imageCount / imagesToLoad.length );
	theProgress += 100 * Math.min( 3000, ( new Date() ).getTime() - imageLoadTimeStamp ) / 3000.0 / imagesToLoad.length;
	theProgress = Math.min( theProgress, 100 );
	
	document.getElementById("prog-splash-ind").style.width = ( theProgress + "%" );
	
	if ( imageCount == imagesToLoad.length )
	{
		window.scrollTo(0, 1);
		
		document.getElementById("prog-splash").style.display = "none";
		
		stopSpinAnim("splash");
		highlightSignButtonWithId(-1);
		
		appState = "normal";
		
		startSlide( 1, null );
	}
	else
		setTimeout( "completeSetup()", 0 );
}

var domGlobals;

function setupDOMGlobals()
{
	var aIdx;
	
	domGlobals = new Object();
	
	domGlobals.clipper        = document.getElementById("clipper");
	domGlobals.slipper        = document.getElementById("slipper");
	domGlobals.slipperStyle   = document.getElementById("slipper").style;
	domGlobals.infoPanelStyle = document.getElementById("info"   ).style;
	
	domGlobals.busys = new Object();
	domGlobals.busys.signs  = document.getElementById("busy-signs"    );
	domGlobals.busys.splash = document.getElementById("splash-loading");
	
	domGlobals.panels = new Array();
	domGlobals.panels[0] = document.getElementById("splash");
	domGlobals.panels[1] = document.getElementById("signs" );
	domGlobals.panels[2] = document.getElementById("types" );
	domGlobals.panels[3] = document.getElementById("scopes");
	
	domGlobals.typeTitle      = document.getElementById("titlebox-types" );
	domGlobals.scopeTitle     = document.getElementById("titlebox-scopes");
	
	domGlobals.typeBox        = document.getElementById("typebox" );
	domGlobals.scopeBox       = document.getElementById("scopebox");
	
	domGlobals.signButtons    = new Array();
	domGlobals.typeButtons    = new Array();
	domGlobals.scopeButtons   = new Array();
	domGlobals.scopeFields    = new Array();
	
	for ( aIdx = 0; aIdx < 12; aIdx++ )
	{
		domGlobals.signButtons      [aIdx] = document.getElementById("sign-"+aIdx+"-btn");
		domGlobals.signButtons[aIdx].style.backgroundImage       = "url('images/buttons/wfigures/sign-stack.png')";
	}
	
	domGlobals.typeButtons[0] = document.getElementById( "type-1-0" );
	domGlobals.typeButtons[1] = document.getElementById( "type-7-0" );
	domGlobals.typeButtons[2] = document.getElementById( "type-30-0" );
	domGlobals.typeButtons[3] = document.getElementById( "type-1000-0" );
	
	for ( aIdx = 0; aIdx < 3; aIdx++ )
	{
		domGlobals.scopeButtons[aIdx] = document.getElementById( "tabForScopeId-"+( 99+aIdx ) );
		domGlobals.scopeFields [aIdx] = document.getElementById("textForScopeId-"+( 99+aIdx ) );
	}
	
	domGlobals.infoButtons = new Array();
	domGlobals.infoButtons[0] = document.getElementById("info-done"       );
	domGlobals.infoButtons[1] = document.getElementById("head-info-signs" );
	domGlobals.infoButtons[2] = document.getElementById("head-info-types" );
	domGlobals.infoButtons[3] = document.getElementById("head-info-scopes");
	
}
function setupImages()
{
}

/*
var loadingImagesCount;
var imagesToLoad;

function preLoadImages()
{
	// Just load a couple of images that sometimes don't appear properly.
	
	var srcArray = new Array
		(
//			"images/busy-32.png",
//			"images/logos/astrologo-200x60.png",
//			"images/head.png",
//			"images/buttons/stars-v.png",
			"images/buttons/wfigures/sign-stack.png"a
//			"images/buttons/wfigures/sign-0-0.png",
//			"images/buttons/wfigures/sign-1-0.png",
//			"images/buttons/wfigures/sign-2-0.png",
//			"images/buttons/wfigures/sign-3-0.png",
//			"images/buttons/wfigures/sign-4-0.png",
//			"images/buttons/wfigures/sign-5-0.png",
//			"images/buttons/wfigures/sign-6-0.png",
//			"images/buttons/wfigures/sign-7-0.png",
//			"images/buttons/wfigures/sign-8-0.png",
//			"images/buttons/wfigures/sign-9-0.png",
//			"images/buttons/wfigures/sign-10-0.png",
//			"images/buttons/wfigures/sign-11-0.png"
			
//			"images/list-lite.png",
//			"images/buttons/btn-bg-0.png",
//			"images/buttons/btn-bg-1.png",
//			"images/buttons/ltab-bg-0.png",
//			"images/buttons/ctab-bg-0.png",
//			"images/buttons/rtab-bg-0.png",
//			"images/buttons/ltab-bg-1.png",
//			"images/buttons/ctab-bg-1.png",
//			"images/buttons/rtab-bg-1.png",
//			"images/buttons/inset-btn-bg-0.png",
//			"images/buttons/inset-btn-bg-1.png",
//			"images/buttons/inset-larr-bg-0.png",
//			"images/buttons/inset-larr-bg-1.png"
		);
		
	imagesToLoad = new Array();
	for ( var aIdx = 0; aIdx < srcArray.length; aIdx++ )
	{
		imagesToLoad[aIdx]        = new Image();
		imagesToLoad[aIdx].src    = srcArray[aIdx];
	}
	
	startImageCheck();
}

var imagesLoaded;
var imagesLoadedTimeStamp;

function startImageCheck()
{
	imagesLoadedTimeStamp = ( new Date() ).getTime();
	setTimeout( "stepImageCheck()", 0 );
}

function stepImageCheck()
{
	imagesLoaded = true;
	
	for ( var aIdx = 0; aIdx < imagesToLoad.length; aIdx++ )
		imagesLoaded &= imagesToLoad[aIdx].complete;
	
	if ( !imagesLoaded )
		setTimeout( "stepImageCheck()", 0 );
	else
		alert( ( new Date() ).getTime() - imagesLoadedTimeStamp );
}

*/

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


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

function highlightElement( aEl, aState )
{
	var bState = ( aState ? "-off" : "-onn" );
	aState     = ( aState ? "-onn" : "-off" );
	aEl.className = replaceAll( aEl.className, bState, aState );
}

function highlightElementConditionally( aEl, aState )
{
	// Stupid hack to prevent highlighting "Last" and "Next" tabs for promo items
	if ( aState && scopeObjectIsPromo( currScopeObj ) && ( indexFromHyphenatedId( aEl.id ) != 100 ) )
		return;
	else
		highlightElement( aEl, aState );
}


function clickSign( aBtn )
{
	if ( appState == "sliding" )
		return;
	
	var theIdx = indexFromHyphenatedId( aBtn.id );
	
	if ( !currScopeObj || ( currScopeObj && ( theIdx != signIdFromScopeObject( currScopeObj ) )) )
	{
		highlightSignButtonWithId( theIdx )
		//startSpinAnim( "signs" );
		//startSignProgressAnim();
		getScopesForSignId( theIdx );
	}
	else
	{
		zapScopeXMLRequest( scopeXMLRequest );
		//stopSpinAnim("signs");
		stopSignProgressAnim();
		
		updatePanesAndSlide( 
			theIdx,
			ID_NULL,
			ID_NULL,
			1 );
	}
	
//	currScopeObj = scopeObjectFromIds( 
//		indexFromHyphenatedId( aBtn.id ),
//		ID_INITIAL,
//		ID_INITIAL );
//	
}

var signProgressAnim = { timeout:null, bar:null, indicator:null, startMSecs:0, expDurationMSecs:0 };
function startSignProgressAnim()
{
	signProgressAnim.bar       = document.getElementById("prog-signs"    );
	signProgressAnim.indicator = document.getElementById("prog-signs-ind");
	
	signProgressAnim.indicator.style.width   = "0%";
	signProgressAnim.indicator.style.opacity = 1.0;
	signProgressAnim.bar.style.display     = "block";
	
	signProgressAnim.startMSecs       = ( new Date() ).getTime();
	signProgressAnim.expDurationMSecs = 5000;
	
	signProgressAnim.timeout = setTimeout( "stepSignProgressAnim()", 0 );
}

function stepSignProgressAnim()
{
	var theProgress = ( 100*( ( new Date() ).getTime() - signProgressAnim.startMSecs )/signProgressAnim.expDurationMSecs );
	
	signProgressAnim.indicator.style.width = ( Math.min( 100, theProgress ) + "%" );
	
	if ( theProgress > 100 )
		signProgressAnim.indicator.style.opacity = ( 0.75 + 0.25*Math.cos( ( theProgress - 100 ) * Math.PI / 30.0 ) );
	
	signProgressAnim.timeout = setTimeout( "stepSignProgressAnim()", 0 );
}

function stopSignProgressAnim()
{
	clearTimeout( signProgressAnim.timeout );
	signProgressAnim.bar.style.display = "none";
}

function clickType( aBtn )
{
	if ( appState == "sliding" )
		return;
	
	currScopeObj = scopeObjectFromIds( 
		signIdFromScopeObject( currScopeObj ), 
		indexFromHyphenatedId( aBtn.id ), 
		100 );

	updatePanesAndSlide(
		signIdFromScopeObject( currScopeObj ),
		indexFromHyphenatedId( aBtn.id ),
		100,
		1 );
}

function clickScope( aBtn )
{
	if ( scopeObjectIsPromo( currScopeObj ) )
		aBtn = domGlobals.scopeButtons[1];
	
	currScopeObj = scopeObjectFromIds(
		signIdFromScopeObject( currScopeObj ),
		typeIdFromScopeObject( currScopeObj ),
		indexFromHyphenatedId( aBtn.id ) );
	
	highlightScopeButtonWithId( indexFromHyphenatedId( aBtn.id ) );
	
	domGlobals.scopeBox.scrollTop = 0;
}



function highlightSignButtonWithId( aId )
{
	for ( var bIdx = 0; bIdx < domGlobals.signButtons.length; bIdx++ )
		highlightElement( domGlobals.signButtons[bIdx], indexFromHyphenatedId( domGlobals.signButtons[bIdx].id ) == 1*aId );
}

function highlightTypeButtonWithId( aId )
{
	for ( var aIdx = 0; aIdx < domGlobals.typeButtons.length; aIdx++ )
		highlightElement( domGlobals.typeButtons[aIdx], indexFromHyphenatedId( domGlobals.typeButtons[aIdx].id ) == 1*aId );
}

function highlightScopeButtonWithId( aId )
{
	var theState;
	
	for ( var aIdx = 0; aIdx < domGlobals.scopeButtons.length; aIdx++ )
	{
		theState = ( indexFromHyphenatedId( domGlobals.scopeButtons[aIdx].id ) == 1*aId );
		highlightElement( domGlobals.scopeButtons[aIdx], theState );
		highlightElement( domGlobals.scopeFields [aIdx], theState );
		
		domGlobals.scopeButtons[aIdx].innerHTML =
				genericTabLabelForTypeIdAndScopeId
				(
					100,
					indexFromHyphenatedId( domGlobals.scopeButtons[aIdx].id )
				);
		
		if ( theState )
			domGlobals.scopeButtons[aIdx].innerHTML = 
				tabLabelForTypeIdAndScopeDate
				(
					typeIdFromScopeObject( currScopeObj ), 
					scopeObjectFromIds
					(
						signIdFromScopeObject( currScopeObj ), 
						typeIdFromScopeObject( currScopeObj ), 
						indexFromHyphenatedId( domGlobals.scopeButtons[aIdx].id )
					).date
				);
		else
			domGlobals.scopeButtons[aIdx].innerHTML =
				genericTabLabelForTypeIdAndScopeId
				(
					1*currScopeObj.parent.id,
					indexFromHyphenatedId( domGlobals.scopeButtons[aIdx].id )
				);
	}
}



function updatePanesAndSlide( aSignId, aTypeId, aScopeId, aSlideDir )
{
	updateSignsPane ( aSignId, aTypeId, aScopeId );
	updateTypesPane ( aSignId, aTypeId, aScopeId );
	updateScopesPane( aSignId, aTypeId, aScopeId );
	
	startSlide( aSlideDir, null );
}

function updateSignsPane( aSignId, aTypeId, aScopeId )
{
	highlightSignButtonWithId( aSignId );
}

function updateTypesPane( aSignId, aTypeId, aScopeId )
{
	domGlobals.typeTitle.innerHTML = domGlobals.signButtons[ 1*aSignId ].name;
	
	highlightTypeButtonWithId( aTypeId );
}

function updateScopesPane( aSignId, aTypeId, aScopeId )
{
	var theScopeObj;
	
	domGlobals.scopeTitle.innerHTML = "";
	
	for ( var aIdx = 0; aIdx < domGlobals.typeButtons.length; aIdx++ )
	{
		if ( indexFromHyphenatedId( domGlobals.typeButtons[aIdx].id ) == 1*aTypeId )
			domGlobals.scopeTitle.innerHTML = domGlobals.typeButtons[aIdx].innerHTML;
	}
	
	for ( var aIdx = 0; aIdx < 3; aIdx++ )
	{
		domGlobals.scopeFields[aIdx].innerHTML = "";
		
		theScopeObj = scopeObjectFromIds( aSignId, aTypeId, 99+aIdx );
		if ( theScopeObj )
			domGlobals.scopeFields[aIdx].innerHTML 
				= replaceAll( theScopeObj.text, "PROMOSCOPEID", aIdx );
				//PROMOSCOPEID is placeholder in id of promo button; replace with 'aIdx' to distinguish copies
	}
	
	if ( typeIdIsPromoId( aTypeId ) )
		highlightScopeButtonWithId ( 100 );
	else
		highlightScopeButtonWithId ( aScopeId );
		
	
	showScopeScrollNotice();
}

function showScopeScrollNotice()
{
	document.getElementById("scopeScrollNotice").style.display = "none";
	
	for ( var aIdx = 0; aIdx < 3; aIdx++ )
	{
		if ( domGlobals.scopeFields[aIdx].className.indexOf("-onn") != -1 )
		{
			if ( domGlobals.scopeFields[aIdx].offsetHeight > domGlobals.scopeBox.offsetHeight )
				document.getElementById("scopeScrollNotice").style.display = "block";
		}
	}
}








function goToForm( aURL ) { window.open( aURL ); }
function goToSite( aSiteDotCom ) {
	
	switch ( aSiteDotCom )
	{
		case "www.astrology.com": aSiteDotCom += ASTROLOGY_OMNITURE_CODE; break;
		case "www.ivillage.com":  aSiteDotCom += IVILLAGE_OMNITURE_CODE;  break;
		
		case "www.daylateanddollarshort.com":
		case "www.iconicon.net":
			aSiteDotCom += "?sender=iscopes";
			break;
	}
	
	aSiteDotCom  = "http://"+aSiteDotCom;
	
	window.open( aSiteDotCom );
}

//
// TYPES pane
//

var typePanelHeight;

function buildTypePopup()
{
	var numTypes = 0;
	var theTypeObj;
	var typeFam  = -1; // typeFam will tick up when we go from "daily" to "weekly" to "monthly" to "offers".
	
	// Clear the div.
	clearDiv( domGlobals.typeBox );
	
	// Get number of horotypes.
	if ( currScopeObj.parent.parent.horotypeArray )
		numTypes = currScopeObj.parent.parent.horotypeArray.length;
		
	// Iterate through the horotypes.
	domGlobals.typeButtons.length = 0;
	for ( var aIdx = 0; aIdx < numTypes; aIdx++ )
	{
		theTypeObj = currScopeObj.parent.parent.horotypeArray[aIdx];
		
		if ( parseInt( 1*theTypeObj.id /100.0 ) > typeFam )
		// Current type has a longer interval (daily/weekly/...) than previous,
		// so we need to add a divider to separate the groupings.
		{
			typeFam = parseInt( 1*theTypeObj.id / 100.0 );
			appendedChildWithIdClassHTML( domGlobals.typeBox, "typeFam-"+typeFam, "type type-head click-only", 
				( typeFam > 2000 ? "Special Offers" : theTypeObj.name.split(" ")[0] ) );
		}
		
		// Add data for current type to list.
		
		domGlobals.typeButtons.push
		(
			appendedChildWithIdClassHTML( domGlobals.typeBox, "type-"+theTypeObj.id, "type type-off click-only", 
				( typeFam > 2000
					? "<span class=\"type\">" + theTypeObj.name               + "</span>"
					: "<span class=\"dwm\" >" + theTypeObj.name.split(" ")[0] + "</span> "
						+ "<span class=\"type\">" + theTypeObj.name.split(" ").slice(1).join(" ") + "</span>" ) )
		);
		domGlobals.typeButtons[-1+domGlobals.typeButtons.length].setAttribute(    "onClick",        "clickType( this );"      );
		domGlobals.typeButtons[-1+domGlobals.typeButtons.length].setAttribute("xonMouseDown", "highlightElement( this, true );");
	}
}

function appendedChildWithIdClassHTML( aParent, aChildId, aChildClass, aChildHTML )
{
	var theChild  = document.createElement("div");
	
	if ( theChild )
	{
		theChild.id        = aChildId;
		theChild.className = aChildClass;
		theChild.innerHTML = aChildHTML;
		
		if ( aParent )
			aParent.appendChild(theChild);
	}
	
	return theChild;
}


//
// SCOPES pane
//

function currentPanelWidth() { return body.offsetWidth; } //( appOrient == "landscape" ? 480 : 320 ); }

function indexFromHyphenatedId( aId ) { return 1*aId.split("-")[1]; }
function scopeObjectIsPromo( aScopeObj ) { return typeIdIsPromoId( typeIdFromScopeObject( aScopeObj ) ); }
function typeIdIsPromoId( aId ) { return 1*aId >= PROMO_THRESHOLD; }
	
function replaceAll( aSrcStr, aFindStr, aReplaceStr ) { return aSrcStr.replace( new RegExp( aFindStr, "g" ), aReplaceStr ); }

function clearDiv( aDiv )
{
	var theChildren = aDiv.childNodes;
	
	while ( theChildren.length > 0 )
		aDiv.removeChild( theChildren[0] );
}



function genericTabLabelForTypeIdAndScopeId( aTypeId, aScopeId )
{
	var theResult = "";
	var theFamily = parseInt( aTypeId / 100.0 );
	
	switch ( theFamily )
	{
		case 1: // Daily
			switch( aScopeId )
			{
				case  99: theResult = "Yesterday"; break;
				case 100: theResult =     "Today"; break;
				case 101: theResult =  "Tomorrow"; break;
			}
			break;
		
		case  7: // Weekly
		case 30: // Monthly
		{
			switch( aScopeId )
			{
				case  99: theResult = "Last "; break;
				case 100: theResult = "This "; break;
				case 101: theResult = "Next "; break;
			}
			theResult += ( theFamily == 7 ? "Week" : "Month" );
		}
			break;
		
		default: // Promotional or Unknown (presumed Today-only)
			if ( aScopeId == 100 )
				theResult = "Today";
			break;
	}
	
	return theResult;
}

function tabLabelForTypeIdAndScopeDate( aTypeId, aYYYYMMDD )
{
	var theResult = "";
	var theYYYY = parseInt(   1*aYYYYMMDD                   / 10000 );
	var theMM   = parseInt( ( 1*aYYYYMMDD - 10000*theYYYY ) /   100 );
	var theDD   = parseInt(   1*aYYYYMMDD - 10000*theYYYY - 100*theMM );
	
	var theDate = new Date( theYYYY, theMM-1, theDD );
	
	switch ( parseInt( aTypeId/100.0 ) )
	{
		case    1: // Daily
		case 2001: // Promotional
			theResult = dayAtIndex( theDate.getDay() );
			break;
		
		case 7: // Weekly
			theResult  = monthAtIndex( theDate.getMonth() ).substring(0,3) + " " + theDate.getDate() + " - ";
			
			theDate.setDate(theDD+6);
			theResult += monthAtIndex( theDate.getMonth() ).substring(0,3) + " " + theDate.getDate();
			break;
		
		case 30: // Monthly
			theResult = monthAtIndex( theDate.getMonth() );
			break;
			
		default: // Unknown
			theResult = "Today"; break;
	}
	
	return theResult;
}

function monthAtIndex( aIdx ) { return "January,February,March,April,May,June,July,August,September,October,November,December".split(",")[aIdx]; }
function   dayAtIndex( aIdx ) { return "Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday".split(",")[aIdx]; }

function clickPrevNextScope( aId )
{
	currScopeObj = scopeObjectFromIds(
		signIdFromScopeObject( currScopeObj ),
		( aId.split("-")[1] == "prev"
			? prevTypeIdFromScopeObject( currScopeObj )
			: nextTypeIdFromScopeObject( currScopeObj ) ),
		currScopeObj.id );
	
	updatePanesAndSlide( 
		signIdFromScopeObject( currScopeObj ),
		typeIdFromScopeObject( currScopeObj ),
		currScopeObj.id,
		0 );
}



var slideAnim = { timer:null, counter:0, fromPanel:0, toPanel:0, panelWidth:0 };

function clickBackArrow( aBtn )
{
	aBtn.className = replaceAll( aBtn.className, "-off", "-onn" );
	startSlide( -1, aBtn );
}

function startSlide( aDir, aBtn )
{
	if ( appState == "sliding" )
		return;
	
	if ( aDir == 0 )
		return;
	
	
	appState = "sliding";
	
	slideAnim.button     = aBtn;
	slideAnim.counter    = 0;
	
	slideAnim.fromPanel  =          currPanel;
	slideAnim.toPanel    = 1*aDir + currPanel;
	slideAnim.panelWidth = currentPanelWidth();
	
	var theHeight = heightForOrientation( appOrient ) + "px";
	if ( slideAnim.toPanel == 2 )
	{
		theHeight  = document.getElementById("head-types").offsetHeight;
		theHeight += domGlobals.typeBox.scrollHeight;
		theHeight += "px";
	}
	
	body.style.height               = theHeight;
	domGlobals.typeBox.style.height = theHeight;
	domGlobals.clipper.style.height = theHeight;
	domGlobals.slipper.style.height = theHeight;
	
	domGlobals.panels[slideAnim.toPanel].style.opacity = 1.0;
	
//	hideAddressBar();
	slideAnim.timer = setTimeout ( "stepSlide()", 0 );
}

function stepSlide()
{
	slideAnim.counter       += 1;
	
	var theT = animRatioForCounter( slideAnim.counter );
	
	var aLeft = -1*slideAnim.fromPanel;
	var bLeft = -1*slideAnim.toPanel;
	var cLeft = aLeft + ( bLeft - aLeft)*theT;
	
	domGlobals.slipperStyle.left = ( cLeft * 100 ) + "%"; //( cLeft * slideAnim.panelWidth ) + "px";
	
	if ( theT >= 1.0 )
		postSlide();
	else
		slideAnim.timer = setTimeout( "stepSlide()", 0 );
}

function postSlide()
{
	currPanel = slideAnim.toPanel;
	
	domGlobals.panels[slideAnim.fromPanel].style.opacity = 0.0;
	if ( slideAnim.button )
		slideAnim.button.className = replaceAll( slideAnim.button.className, "-onn", "-off" );
	
	setSlipperForPanel( currPanel );
	
	appState = "normal";
}

function setSlipperForPanel( aPanel ) { domGlobals.slipperStyle.left = -100*currPanel + "%"; } //( -1*currPanel*currentPanelWidth() ) +"px"; }

function showInfo() { startInfo( 1); }
function hideInfo() { startInfo(-1); }

var infoAnim = { timer:null, counter:0, direction:0, currentTop:0, fromTop:0, toTop:0 };

function startInfo( aDir )
{
	if ( aDir == 0 )
		return;
	
	domGlobals.infoPanelStyle.display = "block";
	
	infoAnim.counter   = 0;
	infoAnim.direction = aDir;
	
	infoAnim.fromTop = 50*( 1 + aDir ); // * domGlobals.clipper.offsetHeight;
	infoAnim.toTop   = 50*( 1 - aDir ); // * domGlobals.clipper.offsetHeight;
	
	domGlobals.infoPanelStyle.top = ( infoAnim.fromTop+"%" );
	
	domGlobals.infoButtons[0].className = replaceAll( domGlobals.infoButtons[0].className, 
		( aDir == 1 ? "-onn" : "-off" ), 
		( aDir == 1 ? "-off" : "-onn" ) );
	for ( var aIdx = 1; aIdx < domGlobals.infoButtons.length; aIdx++ )
		domGlobals.infoButtons[aIdx].className = replaceAll( domGlobals.infoButtons[aIdx].className, 
			( aDir == 1 ? "-off" : "-onn" ),
			( aDir == 1 ? "-onn" : "-off" ) );
	
	infoAnim.timer = setTimeout ( "stepInfo()", 0 );
}

function animRatioForCounter( aCounter )
{
	var theResult = 4;
	
	if ( aCounter > theResult )
	{
		theResult = ( 2*theResult - aCounter )/theResult;
		theResult = 1.0 - theResult*theResult*theResult/2.0;
	}
	else
	{
		theResult = aCounter / theResult;
		theResult = theResult*theResult*theResult;
	}
	
	return theResult;
}

function stepInfo()
{
	infoAnim.counter       += 1;
	
	var theT = animRatioForCounter( infoAnim.counter );
	
	var aTop = infoAnim.fromTop;
	var bTop = infoAnim.toTop;
	var cTop = aTop + ( bTop - aTop )*theT;
	
	domGlobals.infoPanelStyle.top = ( cTop + "%" );
	
	if ( theT >= 1.0 )
		domGlobals.infoPanelStyle.top = ( infoAnim.toTop + "%" );
	else
		infoAnim.timer = setTimeout( "stepInfo()", 0 );
}

var zodiacStyle = "wfigures";

function clickZodiacRadio( aId )
{
//	zodiacStyle = aId.split("-")[1];
//	
//	highlightElement( document.getElementById("zodiacRadio-wstars"  ), ( zodiacStyle == "wstars"   ) );
//	highlightElement( document.getElementById("zodiacRadio-wsymbols"), ( zodiacStyle == "wsymbols" ) );
//	highlightElement( document.getElementById("zodiacRadio-wfigures"), ( zodiacStyle == "wfigures" ) );
//	
//	for ( var aIdx=0; aIdx < 12; aIdx++ )
//		document.getElementById("sign-"+aIdx).src="images/buttons/"+zodiacStyle+"/"+aIdx+".png";
}


//
// ID ACCESSORS ***************************************************
//

function typeIdFromScopeObject( aScopeObj )
{
	var theResult = ID_NULL;
	
	if ( aScopeObj.parent )
		theResult = aScopeObj.parent.id;
	
	return theResult;
}

function signIdFromScopeObject( aScopeObj )
{
	var theResult = ID_NULL;
	
	if ( aScopeObj.parent )
		theResult = signIdFromTypeObject( aScopeObj.parent );
	
	return theResult;
}

function signIdFromTypeObject( aTypeObj )
{
	var theResult = ID_NULL;
	
	if ( aTypeObj.parent )
		theResult = aTypeObj.parent.id;
		
	return theResult;
}

function nextSignIdFromScopeObject( aScopeObj )
{
	var theResult = ID_NULL;
	
	if ( aScopeObj.parent )
		if ( aScopeObj.parent.parent )
			if ( aScopeObj.parent.parent.next )
				theResult = aScopeObj.parent.parent.next.id;
	
	return theResult;
}

function prevSignIdFromScopeObject( aScopeObj )
{
	var theResult = ID_NULL;
	
	if ( aScopeObj.parent )
		if ( aScopeObj.parent.parent )
			if ( aScopeObj.parent.parent.prev )
				theResult = aScopeObj.parent.parent.prev.id;
	
	return theResult;
}

function nextTypeIdFromScopeObject( aScopeObj )
{
	var theResult = ID_NULL;
	
	if ( aScopeObj.parent )
		if ( aScopeObj.parent.next )
			theResult = aScopeObj.parent.next.id;
	
	return theResult;
}

function prevTypeIdFromScopeObject( aScopeObj )
{
	var theResult = ID_NULL;
	
	if ( aScopeObj.parent )
		if ( aScopeObj.parent.prev )
				theResult = aScopeObj.parent.prev.id;
	
	return theResult;
}


/*********************************************
Apple/iPhone-Related Methods
*********************************************/
var theWidth = 0;

function changeOrientation() { handleResize(); }
function handleResize()
{
	if ( window.innerWidth != theWidth )
	{
		if (( window.orientation == undefined ) || ( window.orientation % 180 == 0 ))
			setOrientation("portrait");
		else
			setOrientation("landscape");
		theWidth = window.innerWidth;
	}
}

function clickOrienter( aId ) { setOrientation( aId.split("-")[1] ); }
function setOrientation( aOrient )
{
	var theW, theH;
	
	if ( aOrient == appOrient )
		return;
	
	appOrient = aOrient;
	body.setAttribute( "orient", aOrient );
	domGlobals.slipperStyle.left = ( -1*currPanel*widthForOrientation( aOrient ) ) + "px";
	
	showScopeScrollNotice();
	
	hideAddressBar();
}

function  widthForOrientation( aOrient ) { return ( aOrient == "portrait" ? 320 : 480 ); }
function heightForOrientation( aOrient ) { return ( aOrient == "portrait" ? 416 : 268 ); }

function resizeInnerTo(w,h)
{
	var inW = top.innerWidth;
	var inH = top.innerHeight;
	
	top.resizeBy( w - inW, h - inH );
}


function showUserAgent() {
	
	var agt = navigator.appVersion;
	
	alert ("And your user-agent string is:\n" + agt);
}

function showSafariVersion() {
	// String found if this is a AppleWebKit based product
	var theVersion = -1;
	var testFor    = "safari/";
	var tempStr    = navigator.userAgent.toLowerCase();
	var pos        = tempStr.indexOf( testFor );
	var passedTest = (pos != -1);
	if ( passedTest )
	{
		testFor    = "version/";
		pos        = tempStr.indexOf( testFor );
		passedTest = ( pos != -1 );
		
		if ( passedTest )
		{
			// Grab the version
			theVersion = tempStr.substring( pos + testFor.length, tempStr.length );
			theVersion = theVersion.substring( 0, theVersion.indexOf(" ") );
		}
	}
	
	if ( theVersion != -1 )
		alert( "You are using Safari version "+theVersion );
	else
		alert( "You are not using Safari version 3" );
}

function showIsAppleWebKit() {
	// String found if this is a AppleWebKit based product
	var kitName = "applewebkit/";
	var tempStr = navigator.userAgent.toLowerCase();
	var pos = tempStr.indexOf(kitName);
	var isAppleWebkit = (pos != -1);
	
	if (isAppleWebkit)
	{
		// Grab the version
		var kitVersion = tempStr.substring(pos + kitName.length,tempStr.length);
		kitVersion = kitVersion.substring(0,kitVersion.indexOf(" "));
		alert("Congratulations!\n You are using AppleWebKit version : " + kitVersion);
	}
	else
	{
		alert("Bummer\n You are not using AppleWebKit.");
	}
}


