function setOrangeInvisible(e)
{
	e.style.visibility="hidden";
}
function randOrd()
{
	return (Math.round(Math.random())-0.5); 
} 
function initializationText(rows,cols,imgArr,DeFaUlTiD,cOlOuR,bAcKgRoUnD,dOwNaRrOw,oVeRlAy)
{
	for(j=0;j<cols;j++)
	{
		for(i=0;i<rows;i++)
		{
			if(i==0)
			{
				k=j;
			}
			else
			{
				k=j+(cols*i);
			}
			window["tMpArR"+DeFaUlTiD].push(k);
		}
	}
	
	window["tmpArrCorrect"+DeFaUlTiD]=window["tMpArR"+DeFaUlTiD].slice();
	window["tMpArR"+DeFaUlTiD].sort( randOrd );
	
	var startTxt='<table id="insidesJp'+DeFaUlTiD+'" style="padding:0px;margin:0px;" cellpadding=0 cellspacing=0><tbody>';
	startTxt+='<tr>';
		var count=0;
		for(i=0;i<cols;i++)
		{
			startTxt+='<td style="" valign="top">';
			for(j=0;j<rows;j++)
			{
				var tmpCount=window["tMpArR"+DeFaUlTiD][count];
				startTxt+='<div oRd='+tmpCount+' style="padding:0px;margin:0px;" dO="0"><img src="'+imgArr[tmpCount]+'" onmouseup=""></div>';
				count++;
			}
			startTxt+='</td>';
		}
	startTxt+='</tr>';
	startTxt+='<tr><td colspan="'+cols+'" align="center"><span dO="r" style="color:'+cOlOuR+';font-family:verdana;font-size:11px;background:'+bAcKgRoUnD+';text-decoration:none;" onmousedown="resetSJp('+rows+','+cols+',';
	startTxt+='[';
	for(i=0;i<imgArr.length;i++)
	{
		startTxt+="'"+imgArr[i]+"'";
		if(i!=imgArr.length-1)
		{
			startTxt+=",";
		}
	}
	startTxt+=']';
	startTxt+=','+DeFaUlTiD+',\''+cOlOuR+'\',\''+bAcKgRoUnD+'\',\''+dOwNaRrOw+'\',\''+oVeRlAy+'\')">REPLAY</span></td></tr>';
	
	startTxt+='</tbody></table>';

	startTxt+='<img id="overlay'+DeFaUlTiD+'" src="'+oVeRlAy+'" style="position:absolute;top:0px;left:0px;display:none;">';
	startTxt+='<img id="downArrow'+DeFaUlTiD+'" src="'+dOwNaRrOw+'" style="z-index:300;position:absolute;top:-100px;left:0px;" onmouseover="setOrangeInvisible(this);">';
	return startTxt;
}
function getPositionInsideTable(e)
{
	var left = 0;
	var top  = 0;
	
	while (e.offsetParent && e.parentNode.tagName.toUpperCase() != 'HTML')
	{
		left += e.offsetLeft + (e.currentStyle?(parseInt(e.currentStyle.borderLeftWidth)).NaN0():0);
		top  += e.offsetTop  + (e.currentStyle?(parseInt(e.currentStyle.borderTopWidth)).NaN0():0);
		e     = e.offsetParent;
	}
	left += e.offsetLeft + (e.currentStyle?(parseInt(e.currentStyle.borderLeftWidth)).NaN0():0);
	top  += e.offsetTop  + (e.currentStyle?(parseInt(e.currentStyle.borderTopWidth)).NaN0():0);

	return {x:left, y:top};

}
function mouseCoords(ev)
{
	if(ev.pageX || ev.pageY)
	{
		return {x:ev.pageX, y:ev.pageY};
	}
	return {
		x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
		y:ev.clientY + document.body.scrollTop  - document.body.clientTop
	};
}
function getMouseOffsetInsideTable(tArGeT, ev)
{
	ev = ev || window.event;
	var docPos    = getPositionInsideTable(tArGeT);
	var mousePos  = mouseCoords(ev);
	return {x:mousePos.x - docPos.x, y:mousePos.y - docPos.y};
}
function tblMouseover(ev,DiD)
{
	showArrow(null,DiD);
}
function tblMousemove(ev, DiD)
{
	arrowsAppear(ev,DiD);
}
function arrowsAppear(ev,DiD)
{
	if(window["mOuSeDowN"+DiD]==1)
	{
		showArrow(null,DiD);
		ev=ev||window.event;
		var tArGeT=ev.target||ev.srcElement;
		if(tArGeT.getAttribute("dO") == "0" || tArGeT.getAttribute("dO") == "ar")
		{
		}
		else
		{
			tArGeT=tArGeT.parentNode;
		}
		var docPos = getPositionInsideTable(tArGeT);
		var offsetsJG  = getPositionInsideTable(document.getElementById("sJp"+DiD));
		var mouseOffset=getMouseOffsetInsideTable(tArGeT,ev);
		if(tArGeT!=window["pRevTaRgeT"+DiD] &&  tArGeT.getAttribute("dO") != "ar")
		{
			if(
			(mouseOffset.y>=0 && mouseOffset.y < parseInt(tArGeT.offsetHeight)/2 - 7)||(mouseOffset.y > parseInt(tArGeT.offsetHeight)/2 + 7 && mouseOffset.y <= parseInt(tArGeT.offsetHeight))
			||
			(mouseOffset.x>=0 && mouseOffset.x < parseInt(tArGeT.offsetWidth)/2 - 7)||(mouseOffset.x > parseInt(tArGeT.offsetWidth)/2 + 7 && mouseOffset.x <= parseInt(tArGeT.offsetWidth))
			)
			{
				showArrow("downArrow",DiD);
				document.getElementById("downArrow"+DiD).style.top=(parseInt(docPos.y)-offsetsJG.y+(parseInt(tArGeT.offsetHeight)/2 - 7))+"px";
				document.getElementById("downArrow"+DiD).style.left=(parseInt(docPos.x)-offsetsJG.x+(parseInt(tArGeT.offsetWidth)/2 - 7))+"px";
			}
		}
	}
}
function gw(txt)
{
	var tmpTxt="<br>"+document.getElementById("gW").innerHTML;
	document.getElementById("gW").innerHTML=txt+tmpTxt;
}
function showArrow(arrow,iD)
{
	var arrow=arrow+iD;
	var tmpItem="downArrow"+iD;
	var arrowArr=new Array();
	arrowArr[0]=tmpItem;
	for(i=0;i<arrowArr.length;i++)
	{
		if(arrowArr[i]==arrow)
		{
			document.getElementById(arrowArr[i]).style.visibility="visible";
			
			if(window["mObIlE"+iD]==1)
			{
				document.getElementById(arrowArr[i]).style.width=0;
				document.getElementById(arrowArr[i]).style.height=0;
			}
			
		}
		else
		{
			document.getElementById(arrowArr[i]).style.visibility="hidden";
		}
	}
}
function tblMouseout(ev,DiD)
{
	ev=ev||window.event;
	var tArGeT=ev.target||ev.srcElement;
	if(tArGeT!=window["pRevTaRgeT"+DiD])
	{
	}
}

function setOpacity(obj, value) {
	obj.style.opacity = value/100;
	obj.style.filter = 'alpha(opacity=' + value + ')';
}

function setOpacityNEW(elem, opacityAsInt)
{
	var opacityAsDecimal = opacityAsInt;

	if (opacityAsInt > 100)
		opacityAsInt = opacityAsDecimal = 100; 
	else if (opacityAsInt < 0)
		opacityAsInt = opacityAsDecimal = 0; 

	opacityAsDecimal /= 100;
	if (opacityAsInt < 1)
		opacityAsInt = 1; 

	elem.style.opacity = (opacityAsDecimal);
	elem.style.filter  = "alpha(opacity=" + opacityAsInt + ")";
}

function FadeOpacity(elemId, fromOpacity, toOpacity, time, fps)
{
    var steps = Math.ceil(fps * (time / 1000));
    var delta = (toOpacity - fromOpacity) / steps;
    
    FadeOpacityStep(elemId, 0, steps, fromOpacity, delta, (time / steps));
}

function FadeOpacityStep(elemId, stepNum, steps, fromOpacity, delta, timePerStep)
{
	setOpacity(elemId, Math.round(parseInt(fromOpacity) + (delta * stepNum)));

    if (stepNum < steps)
	{
		setTimeout(function(){FadeOpacityStep(elemId, stepNum+1, steps, fromOpacity, delta, timePerStep)},timePerStep);		   
	}
}

function tblMousedown(ev,DiD)
{
	ev=ev||window.event;
	var tArGeT=ev.target||ev.srcElement;
	ev.cancelBubble = true || ev.stopPropagation() || ev.preventDefault();
	
	showArrow(null,DiD);

	if(window["mOuSeDowN"+DiD]==1)
	{
		if(tArGeT.getAttribute("dO") == "0")
		{
			if(tArGeT!=window["pRevTaRgeT"+DiD])
			{
				window["mOuSeDowN"+DiD]=0;
			}		
		}
		else
		{
			tArGeT=tArGeT.parentNode;
			if(tArGeT.getAttribute("dO") == "0")
			{
				if(tArGeT!=window["pRevTaRgeT"+DiD])
				{
					window["mOuSeDowN"+DiD]=0;
				}		
			}
		}
		if(window["mOuSeDowN"+DiD]==0)
		{
			var mouseOffset=getMouseOffsetInsideTable(tArGeT,ev);
			var tmpNode=null;
			window["pRevTaRgeT"+DiD].style.cssText="";
			var prevTargetParent=window["pRevTaRgeT"+DiD].parentNode;
			
			if(prevTargetParent.childNodes.length==1)
			{
				alert("You cannot remove the last one in a column!");
			}
			else
			{
				var tmpClone1=tArGeT.cloneNode(true);
				var tmpClone2=window["pRevTaRgeT"+DiD].cloneNode(true);
				tmpNode=tArGeT.parentNode.replaceChild(tmpClone2, tArGeT);
				var tmpNodeImgArr=tmpClone2.getElementsByTagName("IMG");
				var tempNode=tmpNodeImgArr[0];
				FadeOpacity(tempNode,0,100,2000,100);
				
				tmpNode2=window["pRevTaRgeT"+DiD].parentNode.replaceChild(tmpClone1, window["pRevTaRgeT"+DiD]);
				var tmpNodeImgArr=tmpClone1.getElementsByTagName("IMG");
				var tempNode=tmpNodeImgArr[0];
				FadeOpacity(tempNode,0,100,2000,100);
				
				document.getElementById("overlay"+DiD).style.display="none";
				
				tblMouseup(ev,DiD);
			}
		}
		else
		{
			window["mOuSeDowN"+DiD]=0;
			window["pRevTaRgeT"+DiD].style.cssText="";
			document.getElementById("overlay"+DiD).style.display="none";
		}
		return false;
	}
	else if(window["mOuSeDowN"+DiD]==0)
	{
		if(tArGeT.getAttribute("dO") == "0")
		{
			window["mOuSeDowN"+DiD]=1;
		}
		else
		{
			tArGeT=tArGeT.parentNode;
			if(tArGeT.getAttribute("dO") == "0")
			{
				window["mOuSeDowN"+DiD]=1;
			}
		}
		if(window["mOuSeDowN"+DiD]==1)
		{
			window["pRevTaRgeT"+DiD]=tArGeT;
			document.getElementById("overlay"+DiD).style.width=window["pRevTaRgeT"+DiD].offsetWidth;
			document.getElementById("overlay"+DiD).style.height=window["pRevTaRgeT"+DiD].offsetHeight;

			var e=window["pRevTaRgeT"+DiD];
			var left=0;
			var top=0;
			while (e.offsetParent.id.indexOf("sJp"+DiD)<0)
			{
				left += e.offsetLeft + (e.currentStyle?(parseInt(e.currentStyle.borderLeftWidth)).NaN0():0);
				top  += e.offsetTop  + (e.currentStyle?(parseInt(e.currentStyle.borderTopWidth)).NaN0():0);
				e     = e.offsetParent;
			}
			left += e.offsetLeft + (e.currentStyle?(parseInt(e.currentStyle.borderLeftWidth)).NaN0():0);
			top  += e.offsetTop  + (e.currentStyle?(parseInt(e.currentStyle.borderTopWidth)).NaN0():0);
			
			document.getElementById("overlay"+DiD).style.display="";
			document.getElementById("overlay"+DiD).style.left=left;
			document.getElementById("overlay"+DiD).style.top=top;
			
			showArrow(null,DiD);
		}
	}
}
function tblMouseup(ev, DiD)
{
	showArrow(null,DiD);
	var tmpArr2=window["tbl"+DiD].getElementsByTagName("DIV");
	for(i=0;i<window["tMpArR"+DiD].length;i++)
	{
		var iT=window["tmpArrCorrect"+DiD][i]+"";
		if(i==window["tMpArR"+DiD].length-1 && iT == tmpArr2[window["tMpArR"+DiD].length-1].getAttribute("oRd"))
		{
			javascriptOnComplete(DiD);
		}
		if(iT != tmpArr2[i].getAttribute("oRd"))
		{
			break;
		}
	}
}
function initSwapJumpPuzzle(rowS,colS,imageArray,DiD,jsTxt,cOlOuR,bAcKgRoUnD,dOwNaRrOw,oVeRlAy)
{
	var cOlOuR=cOlOuR;
	var bAcKgRoUnD=bAcKgRoUnD;
	if(typeof(cOlOuR)=="undefined")
	{
		cOlOuR="white";
	}
	if(typeof(bAcKgRoUnD)=="undefined")
	{
		bAcKgRoUnD="transparent";
	}
	
	var dOwNaRrOw=dOwNaRrOw;
	var oVeRlAy=oVeRlAy;
	if(typeof(dOwNaRrOw)=="undefined")
	{
		dOwNaRrOw="http://cache.trafficmp.com/iris/tc/mid.gif";
	}
	if(typeof(oVeRlAy)=="undefined")
	{
		oVeRlAy="http://www.virtualiris.com/tc/overlaySwapImage.png";
	}
	
	window["jsText"+DiD]=jsTxt;
	window["DeFaUlTiD"+DiD]=DiD;
	window["mOuSeDowN"+DiD]=0;
	window["mObIlE"+DiD]=0;
	window["pRevTaRgeT"+DiD]=null;
	window["rWs"+DiD]=rowS;
	window["cLs"+DiD]=colS;
	window["tMpArR"+DiD]=[];
	window["tmpArrCorrect"+DiD]=[];
	Number.prototype.NaN0=function(){return isNaN(this)?0:this;};
	window["tbl"+DiD]=document.getElementById("sJp"+DiD);
	window["tbl"+DiD].onmousemove=function(ev){tblMousemove(ev,DiD)};
	window["tbl"+DiD].onmouseover=function(ev){tblMouseover(ev,DiD)};
	window["tbl"+DiD].ontouchend=function(ev){window["mObIlE"+DiD]=1;tblMouseup(ev,DiD);};
	window["tbl"+DiD].onmouseout=function(ev){tblMouseout(ev,DiD)};
	window["tOuChStArT"] = 0;
	window["tbl"+DiD].ontouchstart=function(ev)
	{
		window["tOuChStArT"] = 1;
		tblMousedown(ev,DiD)
	};
	window["tbl"+DiD].onmousedown=function(ev)
	{
		if(window["tOuChStArT"] == 0)
		{
			tblMousedown(ev,DiD)
		}
	};
	document.getElementById("sJp"+DiD).innerHTML=initializationText(rowS,colS,imageArray,DiD,cOlOuR,bAcKgRoUnD,dOwNaRrOw,oVeRlAy);
	showArrow(null,window["DeFaUlTiD"+DiD]);
}
function resetSJp(rowS,colS,imageArray,DeFaUlTiD,cOlOuR,bAcKgRoUnD,dOwNaRrOw,oVeRlAy)
{
	window["tMpArR"+DeFaUlTiD]=[];
	window["tmpArrCorrect"+DeFaUlTiD]=[];
	window["mOuSeDowN"+DeFaUlTiD]=0;
	document.getElementById("sJp"+DeFaUlTiD).innerHTML=initializationText(rowS,colS,imageArray,DeFaUlTiD,cOlOuR,bAcKgRoUnD,dOwNaRrOw,oVeRlAy);
}
function javascriptOnComplete(DiD)
{
	eval(window["jsText"+DiD]);
}



