/*	ICC-ThrowCoins.js

	JavaScript functions to be used in the MOOdalBox.
	Apparantly, you can't define js in the html (but you can call stuff from there).
*/

if (typeof isMobile == "undefined") isMobile = false; 

if (!isMobile)
{	//	Asset from mootools: include moodalbox stuff
	//Asset.javascript( '/javascripts/moodalbox/moodalbox.js');
	//	IE(6) doesn't like the Asset use in this case, so do it the poor-man's way:
	document.write('<script type="text/javascript" src="' + '/javascripts/moodalbox/moodalbox.js' + '"></scr'+'ipt>');
	Asset.css( '/javascripts/moodalbox/moodalbox.css');
}

var ICC_Throw_imgroot = Joomla_root + 'images/icc-generator/throw/';

var init_throw_img = ICC_Throw_imgroot + 'bl.gif';

//	*** Object Coin ***

//	constructor
function Coin( imgElemID)
{
	this.state = 0;	//	0=init, 1=turning, 2/3=outcome
	this.imgElemID = imgElemID;
	//	methods
	this.Throw = CoinThrow;
	this.Stop = CoinStop;
	this.Display = CoinDisplay;

}	//	Coin


function CoinThrow()
{	if (this.state != 1)	//	not already turning
	{	//	start turning
		this.state = 1;
		this.Display();
	}
	//	now, let it turn between 1 and 5 seconds
//	setTimeout( "this.Stop()", (1 + Math.floor( 5*Math.random())*1000);		//	doesn't work
	//	so apply a little bind trick
	//	see http://www.klevo.sk/javascript/javascripts-settimeout-and-how-to-use-it-with-your-methods/
	setTimeout( this.Stop.bind(this),  (1 + 4*Math.random())*1000);
	
}	//	CoinThrow

function CoinStop()
{	this.state = 2 + Math.round( Math.random());	//	determine outcome
	this.Display();

}	//	CoinStop

function CoinDisplay()
{	//	when turning, choose from 3 speeds (0 => 0.060 sec/frame, 1 => 0.070, 2 => 0.080)
	document.getElementById( this.imgElemID).src = ICC_Throw_imgroot + 'coin_' + this.state + (this.state == 1 ? '_' + Math.floor( Math.random()*3) : '') + '.gif';

}	//	CoinDisplay

//	*** end of Coin ***

var theCoins = [ new Coin('coinImgL'), new Coin('coinImgM'), new Coin('coinImgR')];

var thrown_actualVal = 0;
var thrown_futureVal = 0;
var thrown_time;
var throwComplete = false;

//	returns 6 - 9
//	line is 0 - 5
function ThrowLine( line)
{	var c;

	for (c = 0 ; c < theCoins.length ; c++)
	{	theCoins[ c].Throw();
	}
	setTimeout( 'Wait4LineThrow('+line+')',  500);

}	//	ThrowLine

function Wait4LineThrow( line)
{
//	var act = 6 + Math.round( Math.random()) + Math.round( Math.random()) + Math.round( Math.random());
	var act = 0;

	for (c = 0 ; c < theCoins.length ; c++)
	{	if (theCoins[ c].state == 1)	//	still turning
		{	break;
		}
		act += theCoins[ c].state;
	}
	if (c < theCoins.length)		//	some coin was still turning
	{	//	wait some more
		setTimeout( 'Wait4LineThrow('+line+')',  500);
		return;
	}	
	
	var actImgID = 'actImg' + line;
	SetThrowImage( actImgID, ICC_Throw_imgroot + 't' + act + '.gif');
	thrown_actualVal |=  ((act % 2) ? 0 : (1 << line));	//	uneven -> no bit
	
	fut = (act == 6 ? 7 : act == 9 ? 8 : act);
	var futImgID = 'futImg' + line;
	SetThrowImage( futImgID, ICC_Throw_imgroot + 't' + fut + '.gif');
	thrown_futureVal |=  ((fut % 2) ? 0 : (1 << line));	//	uneven -> no bit
	
	if (line < 5)
	{	//	next line, after a little while
		setTimeout( 'ThrowLine(' + (line+1) + ')',  500);
	}
	else
	{	//	done
		ThrowAllComplete();
	}

}	//	Wait4LineThrow


function ThrowAll()
{	//	first, blank existing outcome
	SetElementHTML( 'actualhex', '');
	SetElementHTML( 'futurehex', '');
	document.getElementById( 'throwCoins').blur();
	document.getElementById( 'throwCoins').disabled = true;
	document.getElementById( 'throwOK').disabled = true;
	for (var i = 0 ; i < 6 ; i++)
	{
		SetThrowImage( 'actImg' + i, ICC_Throw_imgroot + 'bl.gif');
		SetThrowImage( 'futImg' + i, ICC_Throw_imgroot + 'bl.gif');
	}
	//	init
	throwComplete = false;	
	thrown_actualVal = 0;
	thrown_futureVal = 0;
	thrown_time = new Date();		//	now
//	for (var i = 0 ; i < 6 ; i++)
//	{
//		ThrowLine( i );
//	}
	ThrowLine( 0);
	
}	//	ThrowAll

function ThrowAllComplete()
{
	throwComplete = true;	
	
	SetElementHTML( 'actualhex', HTMLWrap( Hexagrams[ thrown_actualVal].GetDescription()));
	SetElementHTML( 'futurehex', HTMLWrap( Hexagrams[ thrown_futureVal].GetDescription()));
// leave it disabled	document.getElementById( 'throwCoins').disabled = false;
	document.getElementById( 'throwOK').disabled = false;
	document.getElementById( 'throwOK').focus();
	
}	//	ThrowAllComplete

function HTMLWrap( str)
{
	return str.replace( ' - ', '<br />');
	
}	//	HTMLWrap


function SetThrowImage( lineID, newurl)
{
	var imgObj = document.getElementById( lineID);
	if (imgObj != null)
	{	imgObj.src = (newurl == null ? init_throw_img : newurl);
	}

}	//	setImage


function ThrowOK()
{
	if (throwComplete == true)
	{	//	var oldQuest = currentDivination.question;

		currentDivination = new Divination();
		currentDivination.actualVal = thrown_actualVal;
		currentDivination.futureVal = thrown_futureVal;
		currentDivination.divTime = thrown_time;
		currentDivination.question = document.getElementById( 'inputNewQuest').value;
		document.getElementById( 'inputNewQuest').value = '';

		currentDivination.Display();
//		SelectQuestion();
		
		GetNextConnexion();
	}

}	//	ThrowOK


function ButtonNewDivination()
{
	MOOdalBox.open( '/javascripts/ICC-ThrowCoins.html', ' ', '360 428');
	//	wait for throwCoins to exist
	FocusElement_Delayed( 'throwCoins');

}	//	ButtonNewDivination


//	we want to focus a button in the MOOdalBox, but don't know
//	how long it takes to become available
function FocusElement_Delayed( elemID)
{	var elem = document.getElementById( elemID);

	if (isVisible( elem))
	{	elem.focus();
	}
	else	//	wait some more
	{	setTimeout( "FocusElement_Delayed( '" + elemID + "')",  500);
	}

}	//	FocusElement_Delayed


//	source: http://snippets.dzone.com/posts/show/5757
//	Determines if a given element is visible, by checking a variety of things. 
//	Will work for CSS or inline style declarations of visible:hidden or display: none. 
//	Will check if it's inside of an invisible element, as well.
function isVisible(obj)
{
    if (obj == document) return true;
    
    if (!obj) return false;
    if (!obj.parentNode) return false;
    if (obj.style) {
        if (obj.style.display == 'none') return false;
        if (obj.style.visibility == 'hidden') return false;
    }
    
    //Try the computed style in a standard way
    if (window.getComputedStyle) {
        var style = window.getComputedStyle(obj, "");
        if (style.display == 'none') return false;
        if (style.visibility == 'hidden') return false;
    }
    
    //Or get the computed style using IE's silly proprietary way
    var style = obj.currentStyle;
    if (style) {
        if (style['display'] == 'none') return false;
        if (style['visibility'] == 'hidden') return false;
    }
    
    return isVisible(obj.parentNode)
    
}	//	isVisible

