
/**************** begin jmf ********************/

var game_entries = new Array();
var F = new Array();
var D = new Array();

function add_game_entry(str1, str2){
  cur_idx = game_entries.length;
  game_entries[cur_idx] = new Array(str1, str2);
}


// populate F and D arrays with a simpler approach
function populate_F_and_D(){

  for(var idx in game_entries){
    cur_game_entry = game_entries[idx];
    F[idx] = new Array();
    F[idx][0]= cur_game_entry[0];
    F[idx][1] = idx + 1;
    D[idx] = new Array();
    D[idx][0] = cur_game_entry[1];
    D[idx][1] = idx + 1;
    D[idx][2] = 0;
  }
}

/*** move this functionality to external js file to unclutter HTML *****/
function print_game(){
  if(typeof(print_full)!="undefined" && print_full){
   	var gameDiv = '<div class="StdDiv" id="CheckButtonDiv">' +
		'<input type="button" id="CheckButton1" class="FunctionButton" onmouseover="FuncBtnOver(this)"' + 
		' onfocus="FuncBtnOver(this)" onmouseout="FuncBtnOut(this)" onblur="FuncBtnOut(this)"' + 
		' onmousedown="FuncBtnDown(this)" onclick="CheckAnswer()" value="Check Answers" />' + 
		' &nbsp; <input type="button" value="Reset" onclick="javascript:location.href=location.href;" /></div>' + 
		'<div id="InstructionsDiv" class="StdDivInstruction">' + 
		'<p id="Instructions">Click and drag each item on the right to the corresponding item on the left.</p>' + 
		'</div>';
	document.write(gameDiv);
  }
  for (var i=0; i<F.length; i++){
	document.write('<div id="F' + i + '" class="LeftCardStyle"></div>');
  }
  for (var i=0; i<D.length; i++){
	document.write('<div id="D' + i + '" class="RightCardStyle" onmousedown="beginDrag(event, ' + i + ')"></div>');
  }
}


/*************** end jmf ********************/




function BrowserCheck() {
	var b = navigator.appName;
	this.mac = (navigator.appVersion.indexOf('Mac') != -1);
	if (b=="Netscape") this.b = 'ns';
	else if (b=="Microsoft Internet Explorer") this.b = 'ie';
	else this.b = b;
	this.ns = (this.b == 'ns');
	this.ie = (this.b == 'ie');
	this.version = navigator.appVersion;
	if (this.b == 'ie'){
		var x = this.version.indexOf('MSIE ') + 5;
		var vnum = '';

		while ((this.version.charAt(x) > -1)&&(this.version.charAt(x) < 10)){
			vnum += this.version.charAt(x);
			x++;
		}
		this.v = parseInt(vnum);
	}
	else{
		this.v = parseInt(this.version);
	}
	if (this.ns&&(this.v == 5)){
		this.v = 6;
	}
	this.min = (this.v > 5);
}

is = new BrowserCheck();

if ((is.min == false)||(is.ie5mac)){
	alert('Your browser can\'t handle this page. You need NS6+ or IE6+ on Windows, or NS6+ on Mac.');
}

function PageDim(){
//Get the page width and height
	//this.W = 600;
	//this.H = 400;
	this.W = 800;
	this.H = 600;
	if (is.ns) this.W = window.innerWidth;
	if (is.ie) this.W = document.body.clientWidth;
	if (is.ns) this.H = window.innerHeight;
	if (is.ie) this.H = document.body.clientHeight;
}

var pg = null;

function SuppressBackspace(e){ 
	 if (is.ns) {
		thisKey = e.keyCode;
	}
	else {
		thisKey = window.event.keyCode;
	}

	var Suppress = false;

	if (thisKey == 8) {
		Suppress = true;
	}

	if (Suppress == true){
		if (is.ie){
			window.event.returnValue = false;	
			window.event.cancelBubble = true;
		}
		else{
			e.preventDefault();
		}
	}
}

var NavLightColor = '#7f7fbf';
var NavShadeColor = '#000040';
var NavBarColor = '#000080';
var FuncLightColor = '#ffffcb';
var FuncShadeColor = '#7f7f4b';
var ExBGColor = '#ffff97';

function NavBtnOver(Btn){
	Btn.style.padding = '1px 3px 3px 1px';
	Btn.style.borderLeftColor = NavLightColor;
	Btn.style.borderTopColor = NavLightColor;
	Btn.style.borderRightColor = NavShadeColor;
	Btn.style.borderBottomColor = NavShadeColor;
}

function NavBtnOut(Btn){
	Btn.style.borderColor = NavBarColor;
	Btn.style.padding = '2px 2px 2px 2px';
}

function NavBtnDown(Btn){
	Btn.style.borderLeftColor = NavShadeColor;
	Btn.style.borderTopColor = NavShadeColor;
	Btn.style.borderRightColor = NavLightColor;
	Btn.style.borderBottomColor = NavLightColor;
	Btn.style.padding = '3px 1px 1px 3px';
}

function FuncBtnOver(Btn){
/*
	Btn.style.padding = '2px 7px 4px 5px';
	Btn.style.borderLeftColor = FuncLightColor;
	Btn.style.borderTopColor = FuncLightColor;
	Btn.style.borderRightColor = FuncShadeColor;
	Btn.style.borderBottomColor = FuncShadeColor;
*/
}

function FuncBtnOut(Btn){
/*
	Btn.style.borderColor = '#eeeeee';
	Btn.style.padding = '3px 6px 3px 6px';
*/
}

function FuncBtnDown(Btn){
/*
	Btn.style.borderLeftColor = FuncShadeColor;
	Btn.style.borderTopColor = FuncShadeColor;
	Btn.style.borderRightColor = FuncLightColor;
	Btn.style.borderBottomColor = FuncLightColor;
	Btn.style.padding = '4px 5px 2px 7px';
*/
}

function Card(ID){
	this.elm=document.getElementById(ID);
	this.name=ID;
	this.css=this.elm.style;
	this.elm.style.left = 0 +'px';
	this.elm.style.top = 0 +'px';
	this.HomeL = 0;
	this.HomeT = 0;
	this.tag=-1;
	this.index=-1;
}

function CardGetL(){return parseInt(this.css.left)}
Card.prototype.GetL=CardGetL;

function CardGetT(){return parseInt(this.css.top)}
Card.prototype.GetT=CardGetT;

function CardGetW(){return parseInt(this.elm.offsetWidth)}
Card.prototype.GetW=CardGetW;

function CardGetH(){return parseInt(this.elm.offsetHeight)}
Card.prototype.GetH=CardGetH;

function CardGetB(){return this.GetT()+this.GetH()}
Card.prototype.GetB=CardGetB;

function CardGetR(){return this.GetL()+this.GetW()}
Card.prototype.GetR=CardGetR;

function CardSetL(NewL){this.css.left = NewL+'px'}
Card.prototype.SetL=CardSetL;

function CardSetT(NewT){this.css.top = NewT+'px'}
Card.prototype.SetT=CardSetT;

function CardSetW(NewW){this.css.width = NewW+'px'}
Card.prototype.SetW=CardSetW;

function CardSetH(NewH){this.css.height = NewH+'px'}
Card.prototype.SetH=CardSetH;

function CardInside(X,Y){
	var Result=false;
	if(X>=this.GetL()){if(X<=this.GetR()){if(Y>=this.GetT()){if(Y<=this.GetB()){Result=true;}}}}
	return Result;
}
Card.prototype.Inside=CardInside;

function CardSwapColours(){
	var c=this.css.backgroundColor;
	this.css.backgroundColor=this.css.color;
	this.css.color=c;
}
Card.prototype.SwapColours=CardSwapColours;

function CardHighlight(){
	this.css.backgroundColor='#000000';
	this.css.color='#eeeeee';
}
Card.prototype.Highlight=CardHighlight;

function CardUnhighlight(){
	this.css.backgroundColor='#eeeeee';
	this.css.color='#000000';
}
Card.prototype.Unhighlight=CardUnhighlight;

function CardOverlap(OtherCard){
	var smR=(this.GetR()<(OtherCard.GetR()+10))? this.GetR(): (OtherCard.GetR()+10);
	var lgL=(this.GetL()>OtherCard.GetL())? this.GetL(): OtherCard.GetL();
	var HDim=smR-lgL;
	if (HDim<1){return 0;}
	var smB=(this.GetB()<OtherCard.GetB())? this.GetB(): OtherCard.GetB();
	var lgT=(this.GetT()>OtherCard.GetT())? this.GetT(): OtherCard.GetT();
	var VDim=smB-lgT;
	if (VDim<1){return 0;}
	return (HDim*VDim);	
}
Card.prototype.Overlap=CardOverlap;

function CardDockToR(OtherCard){
	this.SetL(OtherCard.GetR() + 5);
	this.SetT(OtherCard.GetT());
}

Card.prototype.DockToR=CardDockToR;

function CardSetHome(){
	this.HomeL=this.GetL();
	this.HomeT=this.GetT();
}
Card.prototype.SetHome=CardSetHome;

function CardGoHome(){
	this.SetL(this.HomeL);
	this.SetT(this.HomeT);
}

Card.prototype.GoHome=CardGoHome;

var CorrectResponse = 'Excelente! Well done.';
var IncorrectResponse = 'Sorry! Try again. Incorrect matches have been removed.';
var YourScoreIs = 'Your score is';
var Correction = '[strCorrection]';
var DivWidth = 600; //default value
var FeedbackWidth = 200; //default
var ExBGColor = '#eeeeee';
var PageBGColor = '#ffffff';
var TextColor = '#000000';
var TitleColor = '#000000';
var Penalties = 0;
var Score = 0;

var CurrDrag = -1;
var topZ = 100;

var DragWidth = 200;
var LeftColPos = 250;
var RightColPos = 650;
var DragTop = 200;
var pg = new PageDim();
if(pg.W >= 800) {
  LeftColPos = 350;
  RightColPos = 850;
}
var StartTime = (new Date()).toLocaleString();
var HPNStartTime = (new Date()).getTime();
var Finished = false;
var SubmissionTimeout = 30000;



//Fixed and draggable card arrays
var FC = new Array();
var DC = new Array();

function doDrag(e) {
	if (CurrDrag == -1) {return};
	if (is.ie){var Ev = window.event}else{var Ev = e}
	var difX = Ev.clientX-window.lastX; 
	var difY = Ev.clientY-window.lastY; 
	var newX = DC[CurrDrag].GetL()+difX; 
	var newY = DC[CurrDrag].GetT()+difY; 
	DC[CurrDrag].SetL(newX); 
	DC[CurrDrag].SetT(newY);
	window.lastX = Ev.clientX; 
	window.lastY = Ev.clientY; 
	return false;
} 


function beginDrag(e, DragNum) { 
	CurrDrag = DragNum;
	if (is.ie){
		var Ev = window.event;
		document.onmousemove=doDrag;
		document.onmouseup=endDrag;
	}
	else{
		var Ev = e;
		window.onmousemove=doDrag; 
		window.onmouseup=endDrag;
	} 
	DC[CurrDrag].Highlight();
	topZ++;
	DC[CurrDrag].css.zIndex = topZ;
	window.lastX=Ev.clientX; 
	window.lastY=Ev.clientY;
	return false;  
} 


function endDrag(e) { 
	if (CurrDrag == -1) {return};
	DC[CurrDrag].Unhighlight();
	if (is.ie){document.onmousemove=null}else{window.onmousemove=null;}
	onEndDrag();	
	CurrDrag = -1;
	return true;
} 


function onEndDrag(){ 
//Is it dropped on any of the fixed cards?
	var Docked = false;
	var DropTarget = DroppedOnFixed(CurrDrag);
	if (DropTarget > -1){
//If so, send home any card that is currently docked there
		for (var i=0; i<DC.length; i++){
			if (DC[i].tag == DropTarget+1){
				DC[i].GoHome();
				DC[i].tag = 0;
				D[i][2] = 0;
			}
		}
//Dock the dropped card
		DC[CurrDrag].DockToR(FC[DropTarget]);
		D[CurrDrag][2] = F[DropTarget][1];
		DC[CurrDrag].tag = DropTarget+1;
		Docked = true;
	}

	if (Docked == false){
		DC[CurrDrag].GoHome();
		DC[CurrDrag].tag = 0;
		D[CurrDrag][2] = 0;
	}
} 

function DroppedOnFixed(DNum){
	var Result = -1;
	var OverlapArea = 0;
	var Temp = 0;
	for (var i=0; i<FC.length; i++){
		Temp = DC[DNum].Overlap(FC[i]);
		if (Temp > OverlapArea){
			OverlapArea = Temp;
			Result = i;
		}
	}
	return Result;
}


Imgs = new Array();

function PreloadImages(){
	var a = PreloadImages.arguments;
	for (var i=0; i<a.length; i++){
		Imgs[i] = new Image();
		Imgs[i].src = a[i];
	}
}

function RefreshImages(){
	for (var i=0; i<document.images.length; i++){
		if (document.images[i].name.substring(0,6) != 'NavBar'){
			document.images[i].src = document.images[i].src;
		}
	}
}



function StartUp(){
	if (is.ie){
		document.attachEvent('onkeydown',SuppressBackspace);
		window.attachEvent('onkeydown',SuppressBackspace);
	}
	else if (is.ns){
		window.addEventListener('keypress',SuppressBackspace,false);
	}




	// jmf commented out
	// PreloadImages('II/Deckel.jpg','II/Kochloeffel.jpg','II/Pfanne.jpg','II/Spatel.jpg','II/Topf.jpg');


//Calculate page dimensions and positions
	pg = new PageDim();
/**** comment out to go with defaults
	DivWidth = Math.floor((pg.W*2)/6);
	DragWidth = Math.floor((DivWidth*3)/10);
	LeftColPos = Math.floor(pg.W/4);
	RightColPos = pg.W - (DragWidth + LeftColPos);
*************************************/
	DragTop = parseInt(document.getElementById('CheckButtonDiv').offsetHeight) + parseInt(document.getElementById('CheckButtonDiv').offsetTop) + 40;




	if (is.ie){
		DragTop += 15;
	}

//Shuffle the items on the right
	D = Shuffle(D);

	var CurrTop = DragTop;
	var TempInt = 0;
	var DropHome = 0;
	var Widest = 0;

	for (var i=0; i<F.length; i++){
		FC[i] = new Card('F' + i);
//		FC[i].SetW(DragWidth);
		FC[i].elm.innerHTML = F[i][0] + '<br clear="all" />'; //required for Navigator rendering bug with images
		
		if (FC[i].GetW() > Widest){
			Widest = FC[i].GetW();
		}
	}

	if (Widest > DragWidth){Widest = DragWidth;}

	CurrTop = DragTop;
	var Highest = 0;

	DragWidth = Math.floor((DivWidth-Widest)/2) - 24;
	RightColPos = DivWidth + LeftColPos - (DragWidth + 14);
	var WidestRight = 0;

	for (i=0; i<D.length; i++){
		DC[i] = new Card('D' + i);
		DC[i].elm.innerHTML = D[i][0] + '<br clear="all" />'; //required for Navigator rendering bug with images
		if (DC[i].GetW() > DragWidth){DC[i].SetW(DragWidth);}
		DC[i].css.cursor = 'move';
		DC[i].css.backgroundColor = '#eeeeee';
		DC[i].css.color = '#000000';
		TempInt = DC[i].GetH();
		if (TempInt > Highest){Highest = TempInt;}
		TempInt = DC[i].GetW();
		if (TempInt > WidestRight){WidestRight = TempInt;}
	}

	var HeightToSet = Highest;
	if (is.ns||is.ie5mac){HeightToSet -= 12;}
	var WidthToSet = WidestRight;
	if (is.ns||is.ie5mac){WidthToSet -= 12;}

	for (i=0; i<D.length; i++){
		DC[i].SetT(CurrTop);
		DC[i].SetL(RightColPos);
		if (DC[i].GetH() < Highest){
			DC[i].SetH(HeightToSet);
		}
		if (DC[i].GetW() < WidestRight){
			DC[i].SetW(WidthToSet);
		}
		DC[i].SetHome();
		DC[i].tag = -1;
		CurrTop = CurrTop + DC[i].GetH() + 5;
	}

	CurrTop = DragTop;

	for (var i=0; i<F.length; i++){
		FC[i].SetW(Widest+70);   /// jmf: add 70px to width since it is not being calculated properly
		if (FC[i].GetH() < Highest){
			FC[i].SetH(HeightToSet);
		}
		FC[i].SetH(100);   /// jmf: hard-coded value set 1/20/2009 to fix FireFox 3 bug(?)
		FC[i].SetT(CurrTop);
		FC[i].SetL(LeftColPos);
		FC[i].SetHome();
		TempInt = FC[i].GetH();
		CurrTop = CurrTop + TempInt + 5;
	}


}


// F and D array allocations were here



function Shuffle(InArray){
	Temp = new Array();
	var Len = InArray.length;

	var j = Len;

	for (var i=0; i<Len; i++){
		Temp[i] = InArray[i];
	}

	for (i=0; i<Len; i++){
		Num = Math.floor(j  *  Math.random());
		InArray[i] = Temp[Num];

		for (var k=Num; k < j; k++) {
			Temp[k] = Temp[k+1];
		}
		j--;
	}
	return InArray;
}

function TimerStartUp(){
	setTimeout('StartUp()', 300);
}

function CheckAnswer(){
//Set the default score and response
	var TotalCorrect = 0;
	Score = 0;
	var Response = '';

//for each fixed, check to see if the tag value for the draggable is the same as the fixed
	var i, j;
	for (i=0; i<D.length; i++){
		if ((D[i][2] == D[i][1])&&(D[i][2] > 0)){
			TotalCorrect++;
		}
		else{
			DC[i].GoHome();
		}
	}

	Score = Math.floor((100*(TotalCorrect-Penalties))/F.length);

	if (TotalCorrect == F.length) {
		Response = YourScoreIs + ' ' + Score + '%.';
		WriteFeedback(Response + '<br />' + CorrectResponse);

//Bug avoider for ie5mac
		if ((is.ie)&&(!is.mac)){
			WriteToInstructions(Response);
		}

		Finished = true;
		setTimeout('Finish()', SubmissionTimeout);
	}
	else {
		Response = IncorrectResponse + '<br />' + YourScoreIs + ' ' + Score + '%.';
		WriteFeedback(Response);
		//Penalties++; // Penalty for inaccurate check <!-- no more penalties JMF (jmf)
	}
}

function WriteFeedback(Feedback){
	var Output = Feedback + '<br /><br />';
	document.getElementById('FeedbackContent').innerHTML = Output;
	var FDiv = document.getElementById('FeedbackDiv');
//Calculate and set its left setting
	topZ++;
	FDiv.style.zIndex = topZ;

//Position the feedback div
	pg = new PageDim();
	FDiv.style.left = Math.floor(pg.W/3) + 'px';
	FDiv.style.width = Math.floor(pg.W/3) + 'px';
//Set the top again in case the window has been resized
	if (is.ns){
		FDiv.style.top = Math.floor(pg.H/3) + window.pageYOffset + 'px';
	}
	else{
		FDiv.style.top = Math.floor(pg.H/3) + document.body.scrollTop + 'px';
	}
	FDiv.style.display = 'block';
	document.getElementById('FeedbackOKButton').focus();


	RefreshImages();

}

function HideFeedback(){
	document.getElementById('FeedbackContent').innerHTML = '';
	document.getElementById('FeedbackDiv').style.display='none';
	FocusAButton();
	if (Finished == true){
		Finish();
	}
}

function FocusAButton(){
	if (document.getElementById('CheckButton1') != null){
		document.getElementById('CheckButton1').focus();
	}
	else{
		document.getElementsByTagName('button')[0].focus();
	}
}

function WriteToInstructions(Feedback) {
	Feedback = '<span class="FeedbackText">' + Feedback + '</span>';
	document.getElementById('InstructionsDiv').innerHTML = Feedback;

	RefreshImages();

}



function Finish(){
//If there's a form, fill it out and submit it
	if (document.store != null){
		Frm = document.store;
		Frm.starttime.value = HPNStartTime;
		Frm.endtime.value = (new Date()).getTime();
		Frm.mark.value = Score;
		Frm.submit();
	}
}


