// PORTIONS COPYRIGHT (c) 2008, 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 APP_SHORT_NAME = "iching";
var APP_VERSION    = "1.0";

var ASTROLOGY_OMNITURE_CODE = "?par=ast|site|iphone";
var  IVILLAGE_OMNITURE_CODE = "?par=ast|site|iphone";
var DLDS_SENDER_CODE        = "?ref="+APP_SHORT_NAME;


/*********************************
************* SETUP **************
*********************************/

function setupCustomAppParams() // Overridden from setup.js
{
	appParams.tapOnSpinner  = "nothing";
	appParams.liveSpinners  = "";
	appParams.spinTimeoutID = "";
	appParams.hexagramType  = "original";
		
	appParams.anim = new Object();
		appParams.anim.slider   = new multiAnimObject( "slider"   );
		appParams.anim.actor    = new multiAnimObject( "actor"    );
		appParams.anim.informer = new multiAnimObject( "informer" );
}

function setupCustomDOMGlobals() // Overridden fom setup.js
{	
	var aIdx;
	
	domGlobals.hintBar          = new Object();
		domGlobals.hintBar.div  = document.getElementById("hintbar");
		domGlobals.hintBar.text = document.getElementById("hintbar-text");
	
	domGlobals.uniGrams = new Array();
	for ( aIdx = 0; aIdx < 6; aIdx++ )
	{
		domGlobals.uniGrams[aIdx] = new Object();
			domGlobals.uniGrams[aIdx].div     = document.getElementById("uniGram-"+aIdx        );
			domGlobals.uniGrams[aIdx].span    = document.getElementById("uniGram-"+aIdx+"-text");
			domGlobals.uniGrams[aIdx].isYin   = "unknown";
			domGlobals.uniGrams[aIdx].isFixed = "unknown";
	}
	domGlobals.spinners = new Array();
	for ( aIdx = 0; aIdx < 4; aIdx++ )
	{
		domGlobals.spinners[aIdx] = new Object();
			domGlobals.spinners[aIdx].div   = document.getElementById("coin-"+aIdx);
			domGlobals.spinners[aIdx].value = -1;
	}
	
	domGlobals.popbox  = document.getElementById( "popbox");
	domGlobals.spinbox = document.getElementById("spinbox");
	domGlobals.readbox = document.getElementById("readbox");
	domGlobals.helpbox = document.getElementById("help-box");
}

function fillImageLoader() // OVERRIDDEN from setup.js
{
	appParams.imageLoader.images.push(
		"images/logos/astrologo-120x36.png",
		"images/logos/ivillogo-80x27.png",
		"images/coin.png",
		"images/buttons/inset-btn-bg-0.png",
		"images/buttons/inset-btn-bg-1.png",
		"images/buttons/inset-ltab-bg-0.png",
		"images/buttons/inset-ltab-bg-1.png",
		"images/buttons/inset-rtab-bg-0.png",
		"images/buttons/inset-rtab-bg-1.png"
		);
}

function afterSplash() // OVERRIDDEN from setup.js
{
	appParams.tapOnSpinner = "fade-0";
}

function postSetOrientation() // OVERRIDDEN from setup.js
{ 
	reflowText(); 
	
	if ( domGlobals.spinners )
	{
		updateImageForSpinner( 0 );
		updateImageForSpinner( 1 );
		updateImageForSpinner( 2 );
		updateImageForSpinner( 3 );
	}
}


/******************************************
********* APP-SPECIFIC ROUTINES ***********
******************************************/


/**** MANAGING SPINNERS ****/

function setupSpinners()
{
	// Populate spinners with random values from 0 to 11
	for ( var aIdx = 0; aIdx < 4; aIdx++ )
		domGlobals.spinners[aIdx].value = parseInt( 1234.56*Math.random() / 31.0 ) % 12;
}

function preShowSpinnersForUniGram( aIdx )
{
	if ( aIdx > 0 )
		startMultiAnim
		(
			"actor",
			20,
			"cube",
			new Array
			( 
	  	   		new multiAnimTarget( domGlobals.hintBar.div,     "top", "current", -24, "px" ),
				new multiAnimTarget( domGlobals.hintBar.div, "opacity", "current", 0.0,   "" )
			),
			"showSpinnersForUniGram("+aIdx+")"
		);
	else
		showSpinnersForUniGram(aIdx);
}

function showSpinnersForUniGram( aIdx )
{
	appParams.tapOnSpinner = "nothing";
	
	setHintBarHTML( "Tap the spinners to cast the " + ordinalString( aIdx + 1 ).toLowerCase() + " line." );
	domGlobals.hintBar.div.style.top = "-24px";
	
	if ( !domGlobals.helpbox.style.opacity != 0.0 )
		domGlobals.helpbox.style.opacity = 1.0;

	startMultiAnim
	(
		"actor",
		20,
		"cube",
		new Array
		( 
	  	    new multiAnimTarget( domGlobals.helpbox,     "opacity", "current", 0.0, "" ),
			new multiAnimTarget( domGlobals.popbox,       "bottom", (-1*domGlobals.popbox.offsetHeight ), 0, "px" ),
			new multiAnimTarget( domGlobals.hintBar.div,     "top", -24,   0, "px" ),
			new multiAnimTarget( domGlobals.hintBar.div, "opacity", 0.0, 1.0,   "" )
		),
		"postShowSpinnersForUniGram("+aIdx+")"
	);
}
function postShowSpinnersForUniGram( aIdx ) { appParams.liveSpinners = "0123"; appParams.tapOnSpinner = "stop-0123"; }

function stepSpinners()
{
	var theSpinSpeed = 1;
	var cIdx, theVOffset, theStopCounter = 0;
	
	for ( cIdx = 0; cIdx < 4; cIdx++ )
	{
		var the100s = parseInt( domGlobals.spinners[cIdx].value / 100.0 );
		var the1s   = domGlobals.spinners[cIdx].value % 100;
		
		if ( the100s == 0 )
			domGlobals.spinners[cIdx].value =               ( 2 + the1s ) % 12;
		else if ( the100s % 12 != the1s % 12 )
			domGlobals.spinners[cIdx].value = 100*the100s + ( 1 + the1s ) % 12;
		else
			theStopCounter += 1;
		
		updateImageForSpinner( cIdx );
	}
	
	if ( theStopCounter == 4 )
		endCastUniGram();
	else
		appParams.spinTimeoutID = setTimeout( stepSpinners, 0 );
}

function updateImageForSpinner( aIdx )
{
		var theMag = ( appParams.orientation == "portrait" ? 1.0 : 0.7 );
		
		var theVOffset = -80*theMag*( 11 - ( domGlobals.spinners[aIdx].value % 12 ) );
		domGlobals.spinners[aIdx].div.style.backgroundPosition = (-5*theMag)+"px " + ((-3*theMag)+ theVOffset ) + "px";
}

function stopSpinner( aIdx )
{
	if ( appParams.liveSpinners.indexOf( aIdx ) == -1 )
		return;
	
	if ( parseInt( 6543.21*Math.random() ) % 12 > 5 )
		domGlobals.spinners[aIdx].value += 1200;
	else
		domGlobals.spinners[aIdx].value +=  600;
	
	appParams.liveSpinners = appParams.liveSpinners.split(aIdx).join("");
				
	appParams.tapOnSpinner = "stop-0123";
}



//
// GRAM MISCELLANEA
//

function setHintBarHTML( aHTML ) { domGlobals.hintBar.div.innerHTML = aHTML; }
function setGramType ( aType  ) { appParams.gramType = aType;  }

function binaryValueOfHexagram()
{
	var thePlaceVal = 1;
	var theResult   = 0;
	
	for ( var aIdx = 0; aIdx < 6; aIdx++ )
	{
		theResult += thePlaceVal*( 1*domGlobals.uniGrams[aIdx].isYin ? 0 : 1 );
		thePlaceVal *= 2;
	}
	
	return theResult;
}

function binaryValueOfDuoHexagram()
{
	var thePlaceVal = 1;
	var theResult   = 0;
	
	for ( var aIdx = 0; aIdx < 6; aIdx++ )
	{
		if ( domGlobals.uniGrams[aIdx].isFixed )
			theResult += thePlaceVal*( 1*domGlobals.uniGrams[aIdx].isYin ? 0 : 1 );
		else
			theResult += thePlaceVal*( 1*domGlobals.uniGrams[aIdx].isYin ? 1 : 0 );
		
		thePlaceVal *= 2;
	}
	
	return theResult;
}

function uniGramTypeFromSpins( aSpin0, aSpin1, aSpin2, aSpin3 )
{
	var theTotal = 0;
	var theResult;
		
	theTotal += ( ( aSpin0 % 100 ) % 12 == 6 ? 0 : 8 ); // 8 or 0
	theTotal += ( ( aSpin1 % 100 ) % 12 == 6 ? 0 : 4 ); // 4 or 0
	theTotal += ( ( aSpin2 % 100 ) % 12 == 6 ? 0 : 2 ); // 2 or 0
	theTotal += ( ( aSpin3 % 100 ) % 12 == 6 ? 0 : 1 ); // 1 or 0

	// solid  = yang = 1
	// broken = yin  = 0
	
	if ( theTotal == 0 )          // prob 1/16 = 0.0625
		theResult = "01"; // 01:  Yin Old
	else if ( theTotal < 4 )      // prob 3/16 = 0.1825
		theResult = "11";  // 11: Yang Old
	else if ( theTotal < 9 )      // prob 5/16 = 0.3125
		theResult = "10";  // 10: Yang Young
	else                          // prob 7/16 = 0.4375
		theResult = "00";  // 00:  Yin Young
		
	return theResult;
}

function duoGramTypeFromGramType( aGramType )
{
	var theResult = aGramType;
	
	switch ( aGramType )
	{
		case "01": theResult = "11"; break;
		case "11": theResult = "01"; break;
	}
	
	return theResult;
}

function gramObject(
	aId, aDecimal, aBinString, aGram,
	aPictoName, aDiacName, aTextName,
	aReadObj )
{
	this.id           = aId;        // Number in traditional ordering (1-8 or 1-64)

	// The trigram itself.
	this.gram         = aGram;      // UTF-8 for gram symbol
	this.binaryString = aBinString; // Binary for gram symbol; 1=solid, 0=broken
	this.decimal      = aDecimal;   // Decimal value of binary symbol
	
	// What the trigram represents
	this.pictogram    = aPictoName; // UTF-8 for pictographic gram name
	this.name         = aDiacName;  // UTF-8 for diacriticaled gram name
	this.textName     = aTextName;  // Text for gram name
	
	// The "readings" ...
	this.readings     = new Object();
	for ( var aProp in aReadObj )
		this.readings[aProp] = aReadObj[aProp];
	
	return this;
}