/***************************************************************************************
Copyright (C) 2001 ab_ditto
This script is made by and copyrighted to ab_ditto at back.to/ab or ab_ditto@hotmail.com
This may be used freely as long as this msg is intact!
***************************************************************************************
excepted from this copyright is the (seperatly marked) draggable-code by Glen Murphy
***************************************************************************************/
//to call the script from your page insert <script language="JavaScript" type="text/javascript" src="puzzle.js"></script>
//into the head-section of the page
//*****ok, letīs start. the first part of the script contains the puzzlecenter
//SLICEBORDER1!!!
//*****variables to set (only if you want to use the puzzlecenter, otherwise go to sliceborder2)
respic=1;//the number of the pic, shown when the page is opened
rescol=5;//the number of columns to use when the page is opened
resrow=5;//the number of rows to use when the page is opened
picvarbn=new Array('pic01.jpg','pic02.jpg','pic03.jpg');//fill this array with the paths to your pics
picvarbw=new Array(291,300,301);//this array with the width of each pic to be displayed(sorted like the pics)
picvarbh=new Array(325,350,271);//and this with the height of the pics
posxa=150;//set this to the left position of your pics to be displayed
posya=150;//set this to the top position of your pics to be displayed
centerleft=460;//set this to the left position of your puzzlecenter to be displayed
centertop=150;//set this to the top position of your puzzlecenter to be displayed
//*****btw. the puzzlecenter is draggable. holding down your mouse over the field "PuzzleCenter" you can drag it around ;)
opaz=10;//this is the value of transparency used for the background pic;
//*****set it to any value between 0 and 50 because 50 ist used for activated pieces (while draggin)
stepx=15;//set this to the width of one step (in pixel) when "help" is activated
//*********************************************************************************
//*****the next part is the stylesheet-definition for the puzzlecenter
//*****if necessary, change this to fit your page-design
document.write("<style type='text/css'>");
document.write("a:active { text-decoration:none; color:#bbcccc; }");
document.write("a:hover { text-decoration:none; color:#bbcccc; }");
document.write("a:link { text-decoration:none; color:#bbcccc; }");
document.write("a:visited { text-decoration:none; color:#bbcccc; }");
document.write(".fon01 { font-family:Verdana,Arial,sans-serif; color:#FFFFF0; font-weight:700; font-size:12px; }");
document.write("#theForm {width:110px; height:132px; background-color:#223333; layer-background-color:#FFFFCC;}");
document.write("#tex1 {position:absolute;left:1px; top:1px; width:108px; height:21px; text-align:center; background-color:#556666; layer-background-color:#556666;}");
document.write("#tex2 {position:absolute;left:3px; top:26px;height:21px;}");
document.write("#colval {position:absolute;left:74px; top:23px; height:21px; width:35px; padding:3px; vertical-text-align:top; background-color:#555555; color:#FFFFF0; font-weight:700; border-color:#556666; border-style:solid; border-width:1px;}");
document.write("#tex3 {position:absolute;left:3px; top:49px;height:21px;}");
document.write("#rowval {position:absolute;left:74px; top:45px; height:21px; width:35px; padding:3px; vertical-text-align:top; background-color:#555555; color:#FFFFF0; font-weight:700; border-color:#556666; border-style:solid; border-width:1px;}");
document.write("#tex4 {position:absolute;left:3px; top:71px;height:21px;}");
document.write("#picval {position:absolute;left:74px; top:67px; height:21px; width:35px; padding:3px; vertical-text-align:top; background-color:#555555; color:#FFFFF0; font-weight:700; border-color:#556666; border-style:solid; border-width:1px;}");
document.write("#tex5 {position:absolute;left:1px; top:89px; width:108px; height:21px; text-align:center; background-color:#555555; layer-background-color:#555555;}");
document.write("#tex6 {position:absolute;left:1px; top:111px; width:108px; height:21px; text-align:center; vertical-align:bottom; background-color:#555555; layer-background-color:#555555;}");
document.write("</style>");
//*****if you are using the script with the puzzlecenter, everthingīs done now, have fun :)
//*****************************************************************************************
var URLInhalt=location.href;
var ArrayVariable=URLInhalt.split("#");
colvar=unescape(ArrayVariable[1]);
rowvar=unescape(ArrayVariable[2]);
picvar=unescape(ArrayVariable[3]);
if(isNaN(colvar)!=true){mycols=Math.abs(Math.floor(colvar));}else{mycols=rescol;}
if(isNaN(rowvar)!=true){myrows=Math.abs(Math.floor(rowvar));}else{myrows=resrow;}
if(isNaN(picvar)!=true){picvarb=Math.abs(Math.floor(picvar));}else{picvarb=respic;}
puzpic=picvarbn[picvarb-1];
picw=picvarbw[picvarb-1];
pich=picvarbh[picvarb-1];

document.write("<div id='theForm' style='position:absolute;left:"+centerleft+"px; top:"+centertop+"px;'><font class='fon01'>");
document.write("<div id='tex1' onmousedown=\"engage('theForm');\" onmouseup=\"release('theForm');\"><b>PuzzleCenter</b></div>");
document.write("<div id='tex2'>Columns</div>");
document.write("<form name='newset'>");
document.write("<input type=text id='colval' name='colval' size=3 value='"+mycols+"'>");
document.write("<div id='tex3'>Rows</div>");
document.write("<input type=text id='rowval' name='rowval' size=3 value='"+myrows+"'>");
document.write("<div id='tex4'>Pic 1..."+picvarbn.length+"</div>");
document.write("<input type=text id='picval' name='picval' size=3 value='"+picvarb+"'>");
document.write("</form>");
document.write("<div id='tex5' onclick='rel()'><b>new puzzle</b></a></div>");
document.write("<div id='tex6' onclick='slide()'><b>need help?</b></a></div>");
document.write("</font></div>");
//var puzzlecenter=1;
function rel(){
colvalv=document.newset.colval.value;rowvalv=document.newset.rowval.value;picvalv=document.newset.picval.value;
document.location.href=unescape(ArrayVariable[0])+'#'+colvalv+'#'+rowvalv+'#'+picvalv;location.reload();}
//*********************************************************************************
//SLICEBORDER2!!!
//*****common section
//*********************************************************************************
//found it? fine :) ...so, if you intend to use the script with one fixed pic and fixed rows/columns
//at first take out the complete part between the two marks //SLICEBORDER1!!! and //SLICEBORDER2!!!
puzzlecenter=1;//and set this to 0;
//*********************************************************************************
//variables to set (only if you want to use NO puzzlecenter)
if(!puzzlecenter){
puzpic='pic01.jpg';//the path to your pic
picw=294;//the width you want you pic be displayed on your page
pich=326;//the height you want you pic be displayed on your page
posxa=300;//set this to the left position of your pic to be displayed
posya=150;//set this to the top position of your pic to be displayed
mycols=5;//the number of columns to use
myrows=5;//the number of rows to use
opaz=20;//this is the value of transparency used for the background pic;
//*****set it to any value between 0 and 50 because 50 ist used for activated pieces (while draggin)
stepx=10;//set this to the width of one step (in pixel) when "help" is activated
showlink=1;//set this to 1 if you want to use the integrated link which starts the "help"-function
linkposl=700;//set this to the left position of the link to be displayed
linkpost=100;//set this to the top position of the link to be displayed
linktext="... need help?"//set this to text you want to use for your link
}
modpic=puzpic;//if you want to use a second pic as background for the puzzle,
//*****change this to its path; sample: modpic='mypicnameandpath.jpg';
//*****ok, thatīs it, everthingīs done now, have fun :)
//*****************************************************************************************
stepy=stepx*pich/picw;dragb=picw/mycols;dragh=pich/myrows;

function rand(top,bottom) {if(!bottom) bottom=0;num=Math.random();
  num=bottom+Math.floor(num*(top-bottom+1));return num;}

//function test2(){
if(!puzzlecenter){if(showlink){
document.write("<div id='tex' style='position:absolute;left:"+linkposl+"px; top:"+linkpost+"px;><a href='#' onclick='slide()'>"+linktext+"</a></div>")
}}
document.write("<div id='model' style='position:absolute;left:"+posxa+"px; top:"+posya+"px; width:"+picw+"px; height:"+pich+"px; filter:\"Alpha(Opacity="+opaz+")\";'><img src='"+modpic+"' width="+picw+"px height="+pich+"px></div>")
widfac=0;heifac=0;vonlinksa=new Array();vonobena=new Array();
for(a = 1; a <= mycols*myrows; a++) {
ranx=rand(picw+(posxa*1)-dragb,posxa);
rany=rand(pich+(posya*1)-dragh,posya);
picid="picnum"+a;
pleft=widfac*-1*dragb;
bclip=(widfac+(1*1))*dragb;
dclip=widfac*dragb;
ptop=heifac*-1*dragh;
aclip=heifac*dragh;
cclip=(heifac+(1*1))*dragh;
ie5exepa=vonlinksa.length;vonlinksa[ie5exepa]=dclip+(posxa*1);
ie5exepb=vonobena.length;vonobena[ie5exepb]=aclip+(posya*1);
	document.write("<div id='"+picid+"' style='position:absolute;left:"+ranx+"px; top:"+rany+"px; width:"+dragb+"px; height:"+dragh+"px; clip:rect(0,"+dragb+","+dragh+",0);overflow:hidden; z-index: 3'><img src="+puzpic+" width="+picw+"px height="+pich+"px style='position:absolute;left:"+pleft+"px; top:"+ptop+"px;clip:rect("+aclip+"px,"+bclip+"px,"+cclip+"px,"+dclip+"px);overflow:hidden;' onmousedown=\"engage('"+picid+"');\" onmouseup=\"release('"+picid+"');\"></div>");
widfac+=1; if(widfac>=mycols){widfac=0;heifac+=1;}
}
domoz('model');
//ende von function test2}

function domoz(id){
if(id=='model'){document.getElementById(id).style.MozOpacity=opaz+'%';}
else if(document.getElementById(id).style.MozOpacity!='50%'){
	document.getElementById(id).style.MozOpacity='50%';
	document.getElementById(id).style.filter = 'Alpha(Opacity=50)';}
else {
document.getElementById(id).style.MozOpacity='100%';
document.getElementById(id).style.filter = 'Alpha(Opacity=100)';}
}
/**************************************************
original draggable code
written by Glen Murphy : http://glenmurphy.com/ :
you may use this code if you keep this line intact.
**************************************************/
var d = document;
if(d.all) {var isIE = 1;var isNS6 = 0;}
else if (d.getElementByID) {var isNS6 = 1;var isIE = 0;}
function shiftTo(obj, x, y) {
	theObj = eval("d.getElementById('"+obj+"')");
	theObj.style.top = y;
	theObj.style.left = x;}
	var offsetX, offsetY, curz = 1;
	var mx, my, activeelement;
function engage(thing) {
	activeelement = thing;
	domoz(thing);
	eval("offsetX = mousex - parseInt(d.getElementById('"+activeelement+"').style.left)");
	eval("offsetY = mousey - parseInt(d.getElementById('"+activeelement+"').style.top)");
	theObj = eval("d.getElementById('"+activeelement+"').style");
	theObj.zIndex = curz++;}
function release(thing) {domoz(thing);activeelement = null;}
function mousemoved(evt) {
	if(isIE){mousex = window.event.clientX+document.body.scrollLeft;
		mousey = window.event.clientY+document.body.scrollTop;}
	else {mousex = evt.pageX;mousey = evt.pageY;}
    	if(activeelement) {wx=mousex-offsetX;
   		wy=mousey-offsetY;
   		wx=(wx>0)?wx:0;
   		wy=(wy>0)?wy:0;
    		shiftTo(activeelement,wx, wy);}
	return false;}
document.onmousemove = mousemoved;
/**************************************************
	 END DRAGGABLE CODE
**************************************************/
bremse=0;
function slide() {
	bremse+=1;
	blockinh=new Array();vonlinks=new Array();vonoben=new Array ();
	for(u = 1; u <= mycols*myrows; u++) {
		theId="picnum"+u;
		wertaa = parseInt(vonlinksa[u-1])
		wertbb = parseInt(vonobena[u-1])
		wertcc = parseInt(document.getElementById(theId).style.left)
		wertdd = parseInt(document.getElementById(theId).style.top)
		if (wertaa != wertcc || wertbb != wertdd) {
			blockinh.length = blockinh.length+1;
			zaehlera = blockinh.length - 1;
			blockinh[zaehlera] = theId;
			vonlinks.length = vonlinks.length+1;
			vonlinks[zaehlera] = vonlinksa[u-1];
			vonoben.length = vonoben.length+1;
			vonoben[zaehlera] = vonobena[u-1];
		}}
		if (blockinh.length > 0) {
		for(i = 1; i <= blockinh.length; i++) {
			blockinhalt = blockinh[i-1];
			vonlinksakt = vonlinks[i-1];
			vonobenakt = vonoben[i-1];
			blockxpos = parseFloat(document.getElementById(blockinh[i-1]).style.left);
			blockypos = parseFloat(document.getElementById(blockinh[i-1]).style.top);

		if (blockxpos!=vonlinksakt || blockypos!=vonobenakt){
			diffxmov=0;diffymov=0;diffx=0;diffy=0;
			if (blockxpos!=vonlinksakt){
				diffxmov=stepx;
				diffx=vonlinksakt-blockxpos;
 	 			if(diffx<=0){diffx=-diffx;}
				diffxb=vonlinksakt-blockxpos;
				if(diffxb<=0){diffxmov=-diffxmov;}
				}
  	 		if (blockypos!=vonobenakt){
  				diffymov=stepy;
				diffy=vonobenakt-blockypos;
 	 			if(diffy<=0){diffy=-diffy;}
 	 			diffyb=vonobenakt-blockypos;
				if(diffyb<=0){diffymov=-diffymov;}
				}
 	 	 	if(Math.abs(diffx)<=stepx){blockxpos=vonlinksakt;}else{blockxpos+=diffxmov;}
			if(Math.abs(diffy)<=stepy){blockypos=vonobenakt;}else{blockypos+=diffymov;}
			document.getElementById(blockinh[i-1]).style.left=blockxpos;
			document.getElementById(blockinh[i-1]).style.top=blockypos;
		}}if (bremse < 1000) {setTimeout("slide()",1)}}
}
