<!--
//----- 12-03-02, 12-05-02; 07-04-05, 07-07-05
//----- guess02.js: Code under development/test...

var contentType = 1;            // Default Text=1, images=0
var biggestTerm = 0;            // Field size for largest term/word

//----- Defaults: Check on these vars... better method?
if (myWidth  == null) var myWidth  = 10;
if (myHeight == null) var myHeight = 10;

//----- Move/split the string data into the arrays...
var theItems = new Array();
var theDimns = new Array();
var theDesc  = new Array();

theItems     = myData.split(",");
theDimns     = myDimns.split(",");
theDesc      = myDesc.split(",");

//----- If dot, then match for image file types...
//----- Is it a text-based or is it an image-based game?
var tvar = theItems[0];
if ( tvar.indexOf(".") + 1 )
{
//----- If ends in .jpg or .gif, must be an image type file... IE?
        if ( tvar.match(/\.(jpg|gif)$/) ) contentType = 0;
}

//----- Data Inits...
var n         = theItems.length;
var upperbnd  = n - 1;           // Upper bound slot in array
var lowerbnd  = 0;               // Lower bound slot
var myURLpath = new String(document.location);          // Create object
var loc       = 0;
var sequence  = new Array(n);
var count     = 0;
var errors    = 0;
var whichItem = 0;
var seq_cnt   = 0;
var direction = 0;


//----- MAIN: Begin processes...
build_seq(n);

if ( contentType )
{
        for (var i = 0; i < n; i++)
        {
                if (theItems[i].length > biggestTerm)
                {
                        biggestTerm = theItems[i].length;
                }
        }

        biggestTerm++;          // Make it one space bigger...
}

if ( ! contentType )
{
//----- There is myWidth and myHeight in accompanying data js file...
//----- If images, loadup the prefetch buffer...
        preLoadImages(n);

//----- erase.jpg holds a blank, white space for a clear in image area...
        var clearScreen    = new Array();
        clearScreen[0]     = new Image(myWidth, myHeight);

//----- NOTE: Watch path! erase.jpg must exixt at URLs!
//----- Must have something here to clear screen to a blank...
        if ( (loc = myURLpath.lastIndexOf("/")) > 0 )
        {
                myURLpath = myURLpath.substr(0, loc) + "/ltgrey.jpg";
                clearScreen[0].src = myURLpath;
        }
}

function getMatch()
{
        var mychecked;

        for (var i = 0; i < n; i++)
        {
//----- Find out which one has been checked... and exit loop
                if (document.game.choices[i].checked)
                {
                        mychecked = i;
                        break;
                }
        }

        if ( whichItem == mychecked)
        {
//----- Ack the correct choice, could be alot more...
//  alert("OK, you got it!");
                
//----- Check the box if OK and mark as done...
                document.game.done[whichItem].checked = 1;
        }
        else
        {
//----- Move the dot to the correct choice...
                document.game.choices[whichItem].checked = 1;
                alert("Correct choice is shown...");
                errors++;
                document.game.errors.value = errors;
        }

//----- Always inc event counter... and display
        count++;
        document.game.counter.value = count;
}

function getNext()
{
//----- Select a random choice to display in window...
       	whichItem = sequence[seq_cnt];

//----- Move up or down for random sequence...
        (direction) ? seq_cnt++ : seq_cnt-- ;

//----- Reset to top of array
        if (seq_cnt > upperbnd) seq_cnt = 0;

//----- Reset to bottom of array
        if (seq_cnt < 0) seq_cnt = upperbnd;

//----- ... now we are done.
        if (count > upperbnd)
        {
                document.game.counter.value = "Done!";
                return;
        }

//----- contentType is either text=1 or images=0...
//----- This is where it, text or image, is displayed.
        if ( contentType )
        {
                document.game.myDisplay.value = theItems[whichItem];
        }
        else
        {
//----- Can be written dynamically in IE 4+ or Netscape 6+ only! Before 
//----- that, it is just readonly. But, if right browser, then it should go!

//----- Get the dimensions of the jpg image
		var pos       = theDimns[whichItem].indexOf(':');
		var adjWidth  = theDimns[whichItem].substr(0,pos);
		var adjHeight = theDimns[whichItem].substr(pos+1);
		var ratio     = 0;

//----- Adjust image to fit inside and on the "screen"
//----- NOTE: myWidth and myHeight belong to the screen
		if ( adjHeight >= adjWidth )
		{
//----- If bigger, scale down...
			if ( adjHeight >= myHeight )
			{
				ratio     = myHeight / adjHeight;
				adjHeight = myHeight;
				adjWidth  = Math.round(adjWidth * ratio);
			}
		}
		else
//----- Implied opposite case: width > height
		{
			if ( adjWidth >= myWidth )
			{
				ratio     = myWidth / adjWidth;
				adjWidth  = myWidth;
				adjHeight = Math.round(adjHeight * ratio); 
			}
		}

//----- Test point
// alert("Height is: "+adjHeight+" Width is: "+adjWidth);

//----- Write these vars just before the display to dynamically adjust...
		document.game.myImages.width  = adjWidth;
		document.game.myImages.height = adjHeight;
                document.game.myImages.src    = theItems[whichItem];
        }
}

function clearChoices()
{
//----- Clear the last item selected... before the next choosing/scramble
        document.game.choices[whichItem].checked = 0;
}

function write_it()
{
//----- Write in the radio buttons and the descriptions...
        var radioButtons = '<input type="radio" name="choices"> ';
        var checkBoxes   = ' <input type="checkbox" name="done"> ';

        for (var x = 0; x < n; x++)
        {
                document.writeln("<li> " + radioButtons + checkBoxes + theDesc[x] + ' <br>');
        }
}

function build_seq(max)
{
//----- Load up this sequencing array...
        for (var j = 0; j < max; j++)
        {
                sequence[j] = j;
        }
}

function clearAll()
{
//-----Clear all check boxes...
        for (var k = 0; k < n; k++) document.game.done[k].checked = 0;

//----- Reset all counters...
        seq_cnt   = 0;
        count     = 0;
        errors    = 0;
        whichItem = 0;

//----- Reset window field displays...
        document.game.reset();

//----- OK, this is better. It wipes image area to white...
        if ( ! contentType )
        {
                document.myImages.src = clearScreen[0].src;
        }
}

function scramble()
{
//----- Rearrange an ordered list to a scrambled list...
//      Do only once at the start of each "session:
        var tmp   = "";
        var cnt   = 0;
        var mid   = 0;

        mid = Math.round( (n/2) );

        for (var y = 0; y < mid; y++)
        {
                for (var x = 0; x < n; x++)
                {
                        if (sequence[cnt+1])
                        {
                                tmp             = sequence[cnt];
                                sequence[cnt]   = sequence[cnt+1];
                                sequence[cnt+1] = tmp;
                        }
                        else break;
                        cnt = cnt + 2;
                }
//----- Shift left and append first to end...
                tmp = sequence[0];
                for (x = 1; x < n; x++) sequence[x-1] = sequence[x];
                sequence[n-1] = tmp;
        }

//----- Derive random number from the clock in seconds
        var now  = new Date();
        var secs = Number(now.getSeconds());
        seq_cnt  = 2;                   // Assumes at least 2 answers

        if (secs > 9) secs    = Math.round(secs % 10);
        if (secs > 0) seq_cnt = Math.round( (secs/10) * (n - 1) );

//----- Then see if number is odd or even and go by that!
        ( seq_cnt % 2 ) ? direction = 1 : direction = 0;
}

function preLoadImages(max)
{
//----- Load the prefetch buffer...
//----- NOTE: By specifying a size, they display *MUCH* faster!
        var preBuffer   = new Array()

        for (var i = 0; i < max; i++)
        {
                preBuffer[i]     = new Image(myWidth, myHeight);
                preBuffer[i].src = theItems[i];
        }
}

function setDisplayType()
{
//----- Write into the doc so that you have the matching choice..
        var textType  = '<input type="text" name="myDisplay" value="" size="'+ biggestTerm + '">';
	var imageType = '<img name="myImages" width="' + myWidth + '" height="' + myHeight + '">';

        (contentType) ? document.write(textType) : document.write(imageType);
}

function setInputLine(input_name)
{
//----- This is for a field display item.
//----- You could overide this from other local data...
        var defInputSize = "5";

        document.write('<input type="text" value="0" name="' + input_name + '" size="' + defInputSize + '" >');
}

function reentry()
{
//----- When re-entering page, test and or set data or flags...
	if ( document.game.counter.value > 0 )
	{
		count = document.game.counter.value;
	}
}

//-->
