/*
 * Copyright (c) 2000, 2001 OutStart, Inc. All rights reserved.
 *
 * $Id: os_waldo.js,v 1.24 2002/12/10 19:04:06 vaishnavi Exp $
 */
 
function findWaldo (contentid,base,tryCnt,resetButton,aryAreas,tries,ordered) {
    this.finalCorrect	= false;	
    this.appendFeedbk   ='';
    this.base           = base;
    this.contentid      = contentid;
    this.attempts       = 0;
    this.numClicks	= 0;
    this.tries          = tries;
    this.tryCnt         = tryCnt;
    this.ordered        = new Boolean(ordered);
    this.state          = false;
    this.finished       = false;
    this.matches        = 0;
    this.areasFound     = 0;
    this.orderArray     = new Array();
    this.clickArray     = new Array();
    this.numArray	= new Array();  
    this.reset          = resetWaldo;
    this.feedback       = showWaldoFeedback;
    this.links          = getWaldoLinks;
    this.hint           = showWaldoHint;
    this.submit		= submitWaldo;
    this.judge          = judgeWaldoClick;
    this.getArea        = getWaldoArea;
    this.getNextArea    = getNextWaldoArea;
    this.updateTries    = updateWaldoTries;
    this.showClick      = showWaldoClick;
    this.showClickNumber= showWaldoClickNumber; 
    this.hideClicks     = hideWaldoClicks;
    this.checkState     = checkWaldoState;
    this.solve          = solveWaldo;
    this.resetFeedback	= resetWaldoFeedback;
    this.appendFeedback = appendWaldoFeedback;
    this.addAllFeedbk   = addAllWaldoFeedbk;
    this.resetButtonDiv	= resetButton;
    
    if (aryAreas != '') {
        this.areasArray = new Array(aryAreas.length);
        for (var i=1; i<aryAreas.length; i++) {
            var parms = aryAreas[i].split(',');
            this.areasArray[i] = new findWaldoArea(this,i,parms[0],parms[1],parms[2],parms[3],parms[4],parms[5],parms[6]);
            if (parms[6] == true) {             
            	this.orderArray[this.matches] = parms[5];
                this.matches++;
            }
        }
        this.orderArray = this.orderArray.sort(waldoCompare);
           
    }
    
    this.loaded = true;
    return this;
}

function findWaldoArea (parent,id,x,y,w,h,feedback,order,correct) {
  
    this.parent         = parent;
    this.base           = parent.base; 
    this.id             = id;
    this.x              = parseInt(x);
    this.y              = parseInt(y);
    this.w              = parseInt(w);
    this.h              = parseInt(h);
    this.bx             = (this.x + this.w);
    this.by             = (this.y + this.h);
    this.feedback       = feedback;
    this.order          = order;
    this.correct        = correct;
    this.found          = false;
    this.state          = false;

    
    this.show           = showWaldoArea;
    
    if (this.correct == 1) { 
        var borderstyle = "waldocorrect"; 
        var image = CkMark;
    }
    else {                   
        var borderstyle = "waldoincorrect";
 
        var image = XMark;
    }

    this.area = new DynLayer(null,this.x-4,this.y-4);
    this.area.setHTML('<div class="'+borderstyle+'"><img src="'+Blank.src+'" width="'+(this.w-4)+'" height="'+(this.h-4)+'"></div>');
    this.area.setVisible(false);
    this.base.addChild(this.area);
    
    this.mark = new DynLayer(null,1,1,image.width,image.width);//alert ("image.width="+image.width);
    this.mark.setHTML('<img src="'+image.src+'" width="'+image.width+'" height="'+image.width+'">');//alert ("image.height="+image.height);
    this.mark.setVisible(true);
    this.area.addChild(this.mark);
        
    return;
}
function submitWaldo(){
		
	this.attempts++;
	this.updateTries();
	if(this.attempts>=this.tries){
		 
             if(!(document.layers)){ 
		this.resetButtonDiv.setHTML("<input value=\"Remise\" disabled=\"true\" class=\"assessmentbuttonstyle\" name=\"Reset"+this.contentid+" \" onclick=\"javascript:findWaldo00B0D0B5DF1C0662129A020100000232.reset();\" type=\"button\"/>");
	     }
	     this.solve();
	}
	
	// ********* do SCORM stuff here  ***********************
	// right now we are scoring twice.  once for scorm and once for normal functionality.
	// we will need to rewrite code to separate hint,feedback,*remediation presentation from scoring 
    cloneScore(this.contentid);	
	// **********************************************************
	
	
	this.feedback();
	return;
}

function resetOrNot(reButton){

	resetButton=reButton;
}


function judgeWaldoClick (e)
{
    this.numClicks++;
    if (this.attempts <this.tries)
    {   
    	this.click  = new waldoClick(e);
        var area    = this.getArea();
   		this.showClickNumber();
   		
   		// need to add to form so SCORM can score practice question
   		if (document.all) {
	        var x           = e.offsetX;
	        var y           = e.offsetY;
	    }
	    else {
	        var x           = e.layerX;
	        var y           = e.layerY;
	    }
	    var elem = eval("document.Question"+this.contentid+"Form.Answer"+this.numClicks+this.contentid+";");
	    if (elem!=null)
	    	elem.value = x+","+y;
   		// end practice question SCORM support
   	// the following is when the hotspot is ordered		
    	if (this.ordered == true)
    	{
    		var nextArea = this.getNextArea();
        	var lastOrder = this.orderArray[(this.orderArray.length-1)];
		// the below condition is when the user doesnt click on the areas authored 
       		if (area == false) {
            		this.showClick(false); 
            		this.finalCorrect=false;
            	}
            	else {// the user has clicked on the areas authored by  the course developer
            		if (area.correct == 1) {//now the chance is the area he clicked is either correct or incorrect.
            			if (this.orderArray[this.numClicks-1]==area.order){
            			// if he clicked on correct area 
            			// he can click in either correct order  or incorrect order.
                    			//if (area.order == nextArea.order) { vg
             	    	    		//this.hideClicks(); vg
             	    	    		// area.show(); vg
 					this.finalCorrect=true;	                      
             				//this.showClick(true);
                	       		area.found = true;
                	        	area.state = true;
                	        	this.areasFound++;
                	        	//if (area.order < lastOrder) {vg
                	        		//    this.attempts = 0;vg
                	        	//}
                    		}
                    		else {// the following represents that the user has clicked on the right area but in the wrong order
                        		this.showClick(false);
                    		}	
                    	}
                    	else {
				// here the user selected a distractor or a wrong area 
				 area.found = true;
                		//area.show();//vg 
                    	}
            	}
    	}
    	else
    	{
      		if (this.attempts <= this.tries)
      		{
      			if (area == false)
      			{
      				this.finalCorrect=false;
      		      	this.showClick(false);
      		    }
            	else
            	{
            		// area.show();
            		area.found = true;
                	if (area.correct == 1)
                	{
                		area.state = true;
                		this.areasFound ++;
                	}
            	}
        	}
        	else
        	{
        		if (area == false)
        		{
        			this.solve();
        	    }
            	else
            	{
            		// area.show();
            		area.found = true;
            		if (area.correct == 1)
            		{
            			area.state = true;
            			this.areasFound ++;
            		}
              		//this.solve();
            	}
        	}
   		}

	    this.state = this.checkState();
	   
	    if ((this.attempts >= this.tries) || (this.finalCorrect==true))
	    {
	         this.finished = true;
	    }
	    this.addAllFeedbk(area);
	    
	    /*
	    if (parent.LEARNING_OBJECT != null) {
	      var assessment = parent.LEARNING_OBJECT.getAssessment();
	      assessment.addItem(this.contentid,this.state);
	    }
	    */
	} 
    return;
}



function checkWaldoState() {
    for (var i=1;i<this.areasArray.length;i++) {
        if (this.areasArray[i].correct == 1) {
            if (this.areasArray[i].state == false) { 
                return false;
            }
        }
    }
    return true;
}

function getWaldoArea () {
    var click = this.click;
    if (!this.areasArray) { return false; }
    for (var i=1;i<this.areasArray.length;i++) {
        var area = this.areasArray[i];
        if ((!area.found) && (click.x >= area.x) && (click.x <= area.bx) && (click.y >= area.y) && (click.y <= area.by)) {
            return area;
        }
    }
    return false;
}

function getNextWaldoArea() {
  // if ((this.areasFound < this.matches)&&(this.matches>=this.numClicks)) {vg
  
        var nextOrder = this.orderArray[this.areasFound];
        for (var i=1;i<this.areasArray.length;i++) {
            if (nextOrder == this.areasArray[i].order) {
                return this.areasArray[i];
            }
        }
    //}vg
    return false;
}

function showWaldoArea (solve) {

    this.area.setVisible(true);
    return;
}


function showWaldoClickNumber(){
    var x = this.click.x;
    var y = this.click.y;
    var i = this.numArray.length;
    
    this.numArray[i]= new DynLayer(null,x,y,null,null);
    this.numArray[i].setHTML('<div class="waldoClick">'+this.numClicks+'</div>');
    this.base.addChild(this.numArray[i]);
    return;
}

function showWaldoClick (state) {

    if (!state) {
    	var image = XMark;
    } 
    var sx = this.click.x-(image.width/2);
    var sy = this.click.y-(image.height/2);
    var i = this.clickArray.length;
   
    this.clickArray[i] = new DynLayer(null,sx,sy,image.width,image.height);
    this.clickArray[i].setHTML(' <img src="'+image.src+'" width="'+image.width+'" height="'+image.height+'">');
    return;

}

function hideWaldoClicks () {
	    
    for(var el in this.numArray) {
         this.base.removeChild(this.numArray[el])
    }
    
    for (var et in this.clickArray){
    	    this.base.removeChild(this.clickArray[et]);
    }	    
    return;
}

function resetWaldo () {
    //this.attempts  = 0;vg
    this.areasFound = 0;
    this.numClicks = 0;
   // this.finished = false;vg
    this.state = false;
    for (var i=1;i<this.areasArray.length;i++) {
        this.areasArray[i].found = false;
        this.areasArray[i].state = false;
        this.areasArray[i].area.setVisible(false);
    }
    
    this.updateTries();
    this.resetFeedback();
    
    this.hideClicks();
    this.clickArray = new Array();
    return;
}

function updateWaldoTries() {
    var currentCount = this.tries - this.attempts;
    if (currentCount >= 0) {
        this.tryCnt.setHTML('<span class="trycounter">Essais Restants : '+currentCount+'</span>');
    }
    return;
}

function waldoCompare (y,z) { return y - z; }

function waldoClick (e) {
    if (document.all) { this.x = e.offsetX; this.y = e.offsetY; }
    else {              this.x = e.layerX; this.y = e.layerY; }
    return this;
}

function solveWaldo() {

    for (var i=1;i<this.areasArray.length;i++) {
        var area = this.areasArray[i];
        if (area.found == false){ // && (area.correct == 1)) { vg to show the incorrect areas also at the end of the tries//
            area.show(true); 
        }
    }
    return;
}
function resetWaldoFeedback(){
	
	this.appendFeedbk='';
	
}
function appendWaldoFeedback(area){
 ls_html='';
 	 
		
  	if (!area.state) {
 		var feedback =  "Incorrect";
 	}
 	else {
 		var feedback = "Correct!"; 
  	}
  	ls_html += '  <tr><td colspan="2"><b>'+this.numClicks + ':'+unescape(feedback)+'</b></td></tr>';
  	
  	
  	if (area) {
  		if ((area.state) || (area.correct == 0)) {
  			if ((area.feedback != '') && (area.feedback != 'null') && (area.feedback != null)) {
  				ls_html += '  <tr>\n';
        			ls_html += '    <td><table><tr><td bgcolor="#cc3300"><img src="../themes/thumb_clear.gif" width="3" height="3"></td></tr></table></td>\n';
        		        ls_html += '    <td width="100%">'+unescape(area.feedback)+'</td>\n';
        		        ls_html += '  </tr>\n';
   		       	}
   		 }
        }
                 
       return ls_html;
} 



function addAllWaldoFeedbk(area){
	
 	this.appendFeedbk += this.appendFeedback(area);
 	return this.appendFeedbk;
 	
 }


function showWaldoFeedback() {
    
    var numFeedback     = 0;
    var ls_html         = '';

    var feedback_html   = '';
    
    ls_html += addPopupSyles("R&eacute;troaction");   
    ls_html += addPopupHeader("R&eacute;troaction");
    ls_html += '<table width="100%" class="yellowbox" border="0" cellpadding="5" cellspacing="0">\n';
    ls_html += '  <tr class="yellowbox">\n';
    ls_html += '    <td colspan="2" class="yellowbox">&nbsp;</td>\n';
    ls_html += '  </tr>\n';
    
    if ((this.numClicks==0)&&(this.attempts<this.tries)) {
        		ls_html+='<tr class="yellowbox"><td colspan="2" class="yellowbox"><b>Essayez de nouveau</b></td></tr>';
    }else
    if (this.attempts > this.tries){ 
          ls_html += '  <tr class="yellowbox"><td colspan="2" class="yellowbox"><b>D&eacute;sol&eacute;, vous avez exc&eacute;d&eacute; le nombre d&#39;essais permis.</b></td></tr>';
    }
    else{
    	
    	
    
    	if( (this.areasFound==this.matches)&&(this.numClicks==this.matches)){
                   var feedback = eval('Correctfdbk' + this.contentid);
                  
                   ls_html += '  <tr class="yellowbox"><td colspan="2" class="yellowbox"><b>'+unescape(feedback)+'</b></td></tr>';
                    ls_html += '<tr class="yellowbox"><td colspan="2" class="yellowbox"><hr width="80%"></hr></td></tr><tr class="yellowbox"><td colspan="2" class="yellowbox">&nbsp;</td></tr>'
                   ls_html += this.appendFeedbk;
                   ls_html += this.links(2);
              	   
        } 
        else {
        	    	
            	if (this.attempts >= this.tries) {
            		
            		if ((this.areasFound<this.matches)||(this.numClicks >this.matches)) {
            		
                	  		 var feedback = eval('Incorrectfdbk' + this.contentid);
                	  		 
                	  		 
                	  		 ls_html += '  <tr><td colspan="2"><b>'+unescape(feedback)+'</b></td></tr>';
                	  		 ls_html += '<tr><td colspan="2"><hr width="80%"></hr></td></tr><tr><td colspan="2">&nbsp;</td></tr>'
                	  		 ls_html += this.appendFeedbk;
                	  		 ls_html += this.links(3);
              	   		  	 
              	   	} 
              	   	
        	}
        	
        	else {
        		ls_html += this.appendFeedbk;
        	}
    	}
    	
    }
    
    ls_html += '</table>\n';
    ls_html += '<p align="center"><a href="javascript: window.opener.focus(); window.close();">Fermer</a></p>\n';
     	   
    for (var i=0;i<this.clickArray.length;i++){
       this.base.addChild(this.clickArray[i]); 
    }
    
    for (var i=1;i<this.areasArray.length;i++) {
//trev - to show both boxes removed if true.
    	     if (this.areasArray[i].found == true) { // && this.areasArray[i].state==true){
                  this.areasArray[i].area.setVisible(true);
              }      
    }
    ShowFeedback(ls_html);
    
    return;
}

function showWaldoHint() {
    ShowHint(this.contentid);
    return;
}

function getWaldoLinks(displayType) {
   
    ls_link='';
	var numLinks = eval("NumLinks"+this.contentid);

    var linkCnt=0;
    for (var i = 1;i<=numLinks;i++) {
        eval("linkObj = link"+this.contentid+"Array["+(i-+1)+"]");
        if ((linkObj.linkDisplay == 1) || (linkObj.linkDisplay == displayType))  {
            theLink=unescape(linkObj.theLink);
            
            if (theLink.length>34) {
                if (linkCnt==0) { ls_link = '<tr><td colspan="2"><hr width="80%"></hr></td></tr><tr><td colspan="2">Pour de plus amples renseignements :</td></tr>'; }
                ls_link += '<tr><td><table><tr><td bgcolor="#cc3300"><img src="../themes/thumb_clear.gif" width="3" height="3"></td></tr></table></td><td width="100%">'+theLink+'</td></tr>';
                linkCnt++;
            }
        }
    }
    return ls_link;
}
function waldoAssessment (contentid,base,sectionid) {
	
    this.base           = base;
    this.contentid      = contentid;
    this.clickArray     = new Array();
	 
	//this.tryLayer 	= tryLayer;
	this.reset		= waldoReset;
	this.sectionid		= sectionid;
	
       
    return this;
}

waldoAssessment.prototype.assess = function(e) {
	
    eval("currentClick"+this.contentid+"++");
    
    var numClicks       = eval("numClicks"+this.contentid);

    var currentClick    = eval("currentClick"+this.contentid);
	var fName			= "Question"+this.sectionid+"_"+this.contentid+"Form";
	var prefix			= getPrefix( fName );
    
    if (document.all) {
        var x           = e.offsetX;
        var y           = e.offsetY;
    }
    else {
        var x           = e.layerX;
        var y           = e.layerY;
    }
    
   // if (document.layers) { var trycounter  = eval("document.layers.tries"+this.contentid); }
   // else {                 var trycounter  = eval("document.getElementById('tries"+this.contentid+"')"); }
	
	
    
        var nextClick = this.clickArray.length;
        this.clickArray[nextClick] = new DynLayer(null,(x-10),(y-10),null,null);
        this.clickArray[nextClick].setHTML('<div class="waldoAssessClick">'+currentClick+'</div>');
        this.base.addChild(this.clickArray[nextClick]);
       
	  if (currentClick<=numClicks){	
         	eval(prefix+"document.Question"+this.sectionid+"_"+this.contentid+"Form.Answer"+currentClick+this.contentid+".value = '"+x+","+y+"'");
         	
	  }
	 

    return;
}
function waldoReset(){
var fName			= "Question"+this.sectionid+"_"+this.contentid+"Form";
	var prefix			= getPrefix( fName );
	for (var i=0; i<this.clickArray.length; i++) {
		this.base.removeChild(this.clickArray[i]);
	}	
	eval("currentClick"+this.contentid+" = 0");
 var numClicks=	eval("numClicks"+this.contentid);
	for(var i=1;i<=numClicks;i++) {
		eval(prefix+"document.Question"+this.sectionid+"_"+this.contentid+"Form.Answer"+i+this.contentid+".value = ''");
	}
        
}
function waldoWrite(layer,text) {
    return;
}

