/*****
 * TEPE e-Learning Interactions
 * JavaScript Library
 * Author: GB
 * Date: 15 March 2004
 ***/

/*****
 * getObj(sId, [sNest])
 * Description:	Returns the appropriate object reference, cross-browser.
 * 				If NN4 and you wish to reference a nested layer, pass the parent layer id as well (nest), otherwise omit.
 * Parameters:	sId - id of the layer you wish to return
 * 				sNest - optional, required for NN4 only. id of container layer if target layer is nested.
 * Author: GB
 * Date: 15 March 2004
 ***/

function getObj(sId, sNest) {
	if (document.getElementById) {
		//return document.getElementById(sId)
		if (document.getElementById(sId)!==null && document.getElementById(sId)!==undefined) return document.getElementById(sId)
		// else alert("Object '" + sId + "' not found");
 	}
 	else if (document.all)
 		return document.all[sId];
 	else if (document.layers)
 		if (sNest != ''){
 			return eval('document.'+sNest+'.document.layers["'+sId+'"]');
 		} else {
 			return document.layers[sId];
 		}
 	}

/*****
 * hideLayer(sId, [bPersists])
 * Description:	Hides a specified object
 * 				You may optionally persist the space left by the object
 * Parameters:	sId - id of the object you wish to hide
 * 				bPersists - optional, set to true to persist space consumed by the object. default action is to collapse space.
 * Author: GB
 * Date: 15 March 2004
 ***/
function hideLayer(sId, bPersists) {
	targetLayer = getObj(sId);
	if (bPersists==true) {
		targetLayer.style.visibility='hidden';
	} else {
		targetLayer.style.display='none';
	}
}

/*****
 * showLayer(sId)
 * Description:	Displays a specified object
 * 				Space is always expanded (there is no point in setting an object to 'block' if it is invisible!)
 * Parameters:	sId - id of the object you wish to show
 * Author: GB
 * Date: 15 March 2004
 ***/
function showLayer(sId) {
	targetLayer = getObj(sId);

	if (targetLayer!==null && targetLayer!==undefined) {
		targetLayer.style.display='block';
		targetLayer.style.visibility='visible';
		if(document.all && document.getElementById) targetLayer.filters.alpha.opacity=100;
		if(!document.all && document.getElementById) targetLayer.style.MozOpacity=100;
	}
}

/*****
 * toggleLayer(sId, bPersists)
 * Description:	Displays a specified object
 * 				Space is always expanded (there is no point in setting an object to 'block' if it is invisible!)
 * Parameters:	sId - id of the object you wish to show
 * Author: GB
 * Date: 15 March 2004
 ***/
function toggleLayer(sId, bPersists) {
	targetLayer = getObj(sId);

	// if we're persisting the space occupied by the object, alter visibility. otherwise alter display, which is the default.
	if (bPersists==true) {
		if (targetLayer.style.visibility=='' || targetLayer.style.visibility=='visible') {
				targetLayer.style.visibility='hidden';
		} else {
			targetLayer.style.visibility='visible';
		}
	} else {
		if (targetLayer.style.display=='' || targetLayer.style.display=='block') {
				targetLayer.style.display='none';
		} else {
			targetLayer.style.display='block';
		}
	}
}

/*****
 * initMouseLayer()
 * Description:	Sets up page to deal with a mouse layer object.
 * 				<div id="mouselayer"></div> - This must be inserted below the <body> tag
 * Parameters:	None
 * Author: GB
 * Date: 16 March 2004
 ***/
function initMouseLayer() {
	if (document.layers) document.captureEvents(Event.MOUSEMOVE);
	document.onmousemove = setMouseLayerPosition;
}

/*****
 * setMouseLayerPosition(e)
 * Description:	Sets the position of the mouselayer object
 * Parameters:	None (I believe the e is an event passed for netscape compatibility)
 * Author: GB
 * Date: 16 March 2004
 ***/
function setMouseLayerPosition(e) {
	var x = (document.layers) ? e.pageX : event.x+document.body.scrollLeft;
	var y = (document.layers) ? e.pageY : event.y+document.body.scrollTop;
	getObj('mouseLayer').style.left = x - 60;
	getObj('mouseLayer').style.top = y + 20;
}

/*****
 * showMouseLayer(sText,sClassName)
 * Description:	Displays the appropriate mouseover text and sets the layer to the specified class
 * Parameters:	sText - HTML to write to the layer
 *						sClassName - Name of the CSS class to use for the layer
 * Author: GB
 * Date: 16 March 2004
 ***/
function showMouseLayer(sText,sClassName) {
	if (document.layers)  {
		getObj('mouseLayer').document.write(sText);
		getObj('mouseLayer').document.close();
		getObj('mouseLayer').style.visibility = "visible";
		(sClassName==null)? getObj('mouseLayer').className='default' : getObj('mouseLayer').className=sClassName;
	} else if (document.all) {
		getObj('mouseLayer').innerHTML = sText;
		getObj('mouseLayer').style.visibility = "visible";
		(sClassName==null)? getObj('mouseLayer').className='default' : getObj('mouseLayer').className=sClassName;
	}
}

/*****
 * hideMouseLayer()
 * Description: Hides the mouse layer
 * Parameters:	None
 * Author: GB
 * Date: 16 March 2004
 ***/
function hideMouseLayer()  {
	getObj('mouseLayer').style.visibility = "hidden";
}

/*****
 * fadeLayersIn(sId/asId, [lDelay])
 * Description:	Fades in layer
 * Parameters:	sId - string/array of the layer(s) you wish to reveal
 *				lDelay - delay in milliseconds between displaying the next layer. default is 1 second.
 *				Ignore lFadeLayerLoop/lOpacityCount - these are used internally as the func is called recursively
 * Author: GB
 * Date: 18 March 2004
 ***/
function fadeLayersIn(asId,lDelay, lFadeLayerLoop, lOpacityCount) {
	// if only a string is passed rather than the preferred array, it cleverly handles it!
	if(typeof(asId)=='string') asId=[asId];
	// sets default values if this is the first time the function has been run.
	if (lDelay==null) lDelay=1000;
	if (lFadeLayerLoop==null) lFadeLayerLoop=0;
	targetLayer = getObj(asId[lFadeLayerLoop]);
	if (lOpacityCount==null) {
		if (targetLayer!==null && targetLayer!==undefined) {
			if(document.all && document.getElementById) lOpacityCount=targetLayer.filters.alpha.opacity;
			if(!document.all && document.getElementById) lOpacityCount=targetLayer.style.MozOpacity;
		}
	}

	// Creates a string based version of the array, ready to pass back to fadeLayersIn
	// This is basically because setTimeout is a bit of a hack - you can't pass a reference to an existing variable
	sIds="";
	for (j=0;j<asId.length;j++) {
		if (j>0) sIds=sIds+",";
		sIds=sIds+"'"+asId[j] +"'";
	}

	// if you're fading layers in, you're going to want to show them
	// this makes sure they're visible the first time this loop is run
	if (lOpacityCount==0) showLayer(asId[lFadeLayerLoop]);

	if(lOpacityCount!=100){
		lOpacityCount+=2;

		if (targetLayer!==null && targetLayer!==undefined) {
			if(document.all && document.getElementById) targetLayer.filters.alpha.opacity = lOpacityCount;
			if(!document.all && document.getElementById) targetLayer.style.MozOpacity = lOpacityCount/100;
			setTimeout("fadeLayersIn([" + sIds + "],"+lDelay+","+lFadeLayerLoop+","+lOpacityCount+")");
		}
	} else {
		lOpacityCount=0;
		lFadeLayerLoop++;
		if (lFadeLayerLoop<asId.length) setTimeout("fadeLayersIn([" + sIds + "],"+lDelay+","+lFadeLayerLoop+","+lOpacityCount+")",lDelay);
		// else i=0;

	}
}

/*****
 * fadeLayersOut(sId/asId, [lDelay])
 * Description:	Fades in layer
 * Parameters:	sId - string/array of the layer(s) you wish to reveal
 *				lDelay - delay in milliseconds between displaying the next layer. default is 1 second.
 *				Ignore lFadeLayerLoop/lOpacityCount - these are used internally as the func is called recursively
 * Author: GB
 * Date: 23 March 2004
 ***/
function fadeLayersOut(asId,lDelay, lFadeLayerLoop, lOpacityCount) {
	// if only a string is passed rather than the preferred array, it cleverly handles it!

	if(typeof(asId)=='string') asId=[asId];

	// sets default values if this is the first time the function has been run.

	if (lDelay==null) lDelay=1000;
	if (lFadeLayerLoop==null) lFadeLayerLoop=0;
	targetLayer = getObj(asId[lFadeLayerLoop]);
	if (lOpacityCount==null) {
		if (targetLayer!==null && targetLayer!==undefined) {

			if(document.all && document.getElementById) lOpacityCount=targetLayer.filters.alpha.opacity;
			if(!document.all && document.getElementById) lOpacityCount=targetLayer.style.MozOpacity;
		}
	}

	// Creates a string based version of the array, ready to pass back to fadeLayersIn
	// This is basically because setTimeout is a bit of a hack - you can't pass a reference to an existing variable
	sIds="";
	for (j=0;j<asId.length;j++) {
		if (j>0) sIds=sIds+",";
		sIds=sIds+"'"+asId[j] +"'";
	}

	// if you're fading layers in, you're going to want to show them
	// this makes sure they're visible the first time this loop is run
	//if (lOpacityCount==0) showLayer(asId[lFadeLayerLoop]);

	if(lOpacityCount !==0){
		lOpacityCount-=4;
		if (targetLayer!==null && targetLayer!==undefined) {
			if(document.all && document.getElementById) targetLayer.filters.alpha.opacity = lOpacityCount;
			if(!document.all && document.getElementById) targetLayer.style.MozOpacity = lOpacityCount/100;
			setTimeout("fadeLayersOut([" + sIds + "],"+lDelay+","+lFadeLayerLoop+","+lOpacityCount+")");
		}
	} else {
		lOpacityCount=0;
		lFadeLayerLoop++;
		targetLayer.style.display='none';
		if (lFadeLayerLoop<asId.length) setTimeout("fadeLayersOut([" + sIds + "],"+lDelay+","+lFadeLayerLoop+","+lOpacityCount+")",lDelay);
		// else i=0;

	}
}


// *********************************************************** DRAG DROP ******************************************

var DragEl;
var chosenOption;
var targetImgSrc='target';
var transparentImgSrc='trans.gif';
var sDndLayerClassName = 'dnd';
var sTargetLayerId = 'target';

function initDrapDrop() {
	// this is where you may need to specifiy all the targets

	targetImgSrc=document.all('target').children[0].src;
	// if internet explorer

	if(document.all){
		document.onmousedown=DragStart;
		document.onmouseup=DragEnd;
		document.onmousemove=DoDrag;
	}
}

function getRealPos(i,which) {
	iPos = 0;
	while (i!=null) {
	 	iPos += i["offset" + which];
		i = i.offsetParent;
	}
	return iPos;
}

function DoDrag(){
	// if we are moving a draggable element ie. DragEl not null

	if(DragEl){
		dndlayer.style.pixelLeft=event.clientX+lOriginalLeft-lMousePosLeft;
		dndlayer.style.pixelTop=event.clientY+lOriginalTop-lMousePosTop;
		return false;
	}
}

function DragStart(){
	el=event.srcElement;

	// if the classname of the container div is dnd and we're not trying to drag a target or empty placeholder image...
	if(el.parentElement.className==sDndLayerClassName && (el.src).indexOf(targetImgSrc)==-1 && (el.src).indexOf(transparentImgSrc)==-1) {
		// if we are dragging an option to a target (and NOT an option from a target back to it's original position)
		if ((el.parentElement.id).indexOf(sTargetLayerId)==-1){
			// if we've already chosen an option, redisplay it and set chosen option to the one we're currently dragging
			if (chosenOption!==undefined) {
					document.all(chosenOption).children[0].src=dndlayer.children[0].src;
			}
			chosenOption = el.parentElement.id; // can this be moved to dragend
		}
		// Sets several values (current mouse pos, initial element position)
		DragEl=el;
		lMousePosLeft=event.clientX;
		lMousePosTop=event.clientY;
		lOriginalLeft=getRealPos(el,"Left");
		lOriginalTop=getRealPos(el,"Top");
		// Sets dnd layer to appear exactly on top of layer where we've started dragging
		dndlayer.style.pixelTop=lOriginalTop;
		dndlayer.style.pixelLeft=lOriginalLeft;

		// sets the image src to be that of the element you clicked when you started dragging
		dndlayer.children[0].src=el.src;
		// sets the other one to be blank so that the image doesn't stay there when you start dragging
		el.src=transparentImgSrc;
		// sets our new draggable layer to be visible
		dndlayer.style.visibility='visible';
		// sets the target image back to the default one, just incase any target has previously been placed
		document.all('target').children[0].src=targetImgSrc;

	}
}

function DragEnd(){
	var i;
	// If you're not dragging, return.
	if(!DragEl) return;

	// Sets variables and says that at least a quarter of the draggable object must be over the target object in order for it to snap
	X1=dndlayer.style.pixelLeft;
	Y1=dndlayer.style.pixelTop;
	requiredAreaOverlap=50 //(dndlayer.offsetHeight*dndlayer.offsetWidth)/4
	TargetElem=null;

	// Gets the amount that the draggable layer is over a target layer
	X2=getRealPos(document.all('target').children[0],"Left");
	Y2=getRealPos(document.all('target').children[0],"Top");
	hOverlap=dndlayer.offsetWidth-Math.abs(X2-X1);
	vOverlap=dndlayer.offsetHeight-Math.abs(Y2-Y1);
	areaOverlap=((hOverlap>0)&&(vOverlap>0))?hOverlap*vOverlap:0;

	// if the amount that is overlapping is more than our target amount (a quarter)...
	if(areaOverlap>requiredAreaOverlap){
		// if the layer we're dragging is currently visible...
		if (dndlayer.style.visibility=='visible') {
			requiredAreaOverlap=areaOverlap;
			dndlayer.style.visibility='hidden';
			document.all('target').children[0].src=dndlayer.children[0].src;
			document.all('target').style.cursor='move';
			DragEl.style.cursor='';
			if (DragEl.parentElement.id!=='target')
				chosenOption=DragEl.parentElement.id;
		}
	} else {
		dndlayer.style.visibility='hidden';
		if (chosenOption!==undefined) {
			document.all(chosenOption).children[0].src=dndlayer.children[0].src;
			document.all(chosenOption).style.cursor='move';
		}
		document.all('target').style.cursor='';
		chosenOption=undefined;
		return;
	}

	DragEl=null;
}

function submitAnswer() {
	if (chosenOption!==undefined) {
		//document.all("answer1").value=chosenOption;
		alert(chosenOption);
		// frmQuestion.submit();
	} else {
		alert("Please drag your impression of the individual to the 'DRAG HERE!' box before clicking 'Next'.");
		return false;
	}
}




/*** Deprecated Functions ***/

/*****
 * setLayerView(sId, sDisplay, sVisibility)
 * Description:	Displays a layer. This layer does not consume screen space.
 * Parameters:	sID - name of the layer you wish to reveal
 * Author: GB
 * Date: 15 March 2004
 ***/
function setLayerView(sId, sDisplay,sVisibility) {
	targetLayer = getObj(sId);
	targetLayer.style.display=sDisplay;
	targetLayer.style.visibility=sVisibility;
}