/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Written by Terry Friesen,  tfriesen@mts.net  copyright 1998-2002
http://www.mts.net/~tfriesen/

last updated: Feb 24, 2002

This script may be used freely on non-commercial web pages
It is not to be sold or profit made from it without the consent of the author
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

function utilityMethods(lay){
lay.moveNextTo=moveNextTo;
lay.insertCoverLayer=insertCoverLayer;
lay.moveToFront=moveToFront;
lay.swapPosition=swapPosition;
lay.absPosition=absPosition;
lay.setDrag=setDrag;
}

var batch_Count=0;
var batch_Running=false;

function runBatch(a1,a2,a3,a4,a5){
if(!batch_Running){
var lay=this[batch_Count];
if(lay){
batch_Running=true;
batch_Code=a1;
batch_Ident=a2||null;
batch_Ident2=a3||null;
batch_Speed=a4;
batch_NextCode=a5||null;
var truestr=lay.layerName.search(batch_Ident)>-1||batch_Ident==null;
var falsestr=lay.layerName.search(batch_Ident2)>-1;
if(truestr&&!falsestr&&!lay.is3d_bg){
var theCode="this."+lay.layerName+"."+batch_Code;
eval(theCode);
}
batch_Count++;
batch_Array=this;
setTimeout("batch_Running=false;batch_Array.runBatch(batch_Code,batch_Ident,batch_Ident2,batch_Speed,batch_NextCode)",batch_Speed||100);
}
else{
batch_Count=0;
eval(batch_NextCode);
}}
return this;
}

function moveNextTo(a1,a2,a3,a4){
var lay=a1||this.previousLayer;
if(lay){
var pl=a2||'right';
var offleft=a3||0;
var offtop=a4||0;
var pl2=pl.toLowerCase();
var lleft=lay.absLeft();
var ltop=lay.absTop();
var lheight=lay.pxHeight();
var lwidth=lay.pxWidth();
if(pl2=='top'){this.absPosition(lleft+offleft,ltop-this.pxHeight()+offtop)}
if(pl2=='right'){this.absPosition(lleft+lwidth+offleft,ltop+offtop)}
if(pl2=='bottom'){this.absPosition(lleft+offleft,ltop+lheight+offtop)}
if(pl2=='left'){this.absPosition(lleft-this.pxWidth()+offleft,ltop+offtop)}
}
return this;
}

function insertCoverLayer(a1,a2,a3){
var cl=this.coverLayer=this.insertElement(null,null,null,true);
cl.Index(a1||0);
cl.resizeTo(a2||this.pxWidth(),a3||this.pxHeight());
cl.Visibility("visible");
return this;
}

function moveToFront(a1){
var p=getParent(this);
for(var i=0;i<p.cssLayers.length;i++){var topindex=Math.max(this.Index(),p.cssLayers[i].Index()||0)}
this.Index(topindex+(a1||2));
return this;
}

function swapPosition(lay){
var left1=this.absLeft();
var left2=lay.absLeft();
var top1=this.absTop();
var top2=lay.absTop();
lay.absPosition(left1,top1);
this.absPosition(left2,top2);
return this;
}

function absPosition(a1,a2){
this.pxLeft(this.pxLeft()-this.absLeft()+a1);
this.pxTop(this.pxTop()-this.absTop()+a2);
return this;
}

function setDrag(a1){
var doc=this.owner.page;
if(!a1){
this.setEventHandler("mousedown",setup_Drag,"cb");
this.setEventHandler("mouseup",cancel_Drag,"cb");
}
else{
this.setEventHandler("mousedown",null,"cb");
doc.setEventHandler("mousemove",null,"cb",1);
this.setEventHandler("mouseup",null,"cb");

}
return this;
}

function setup_Drag(e){
var win=this.owner;
var doc=win.page;
doc.onselectstart=cancel_Select;
doc.off_DragX=e.clientX+win.offsetLeft()-this.pxLeft();
doc.off_DragY=e.clientY+win.offsetTop()-this.pxTop();
doc.setEventHandler("mousemove",do_Drag,"cb",1);
doc.drag_Layer=this;
if(doc.execCommand&&win.selectedText()){doc.execCommand("Unselect")}
}

function cancel_Select(){return false}

function cancel_Drag(e){
var doc=this.owner.page;
doc.setEventHandler("mousemove",null,"cb",1);
doc.onselectstart=null;
}

function do_Drag(e){
var win=this.owner;
var x1=Client.ns?e.pageX:win.event.clientX+this.body.scrollLeft;
var y1=Client.ns?e.pageY:win.event.clientY+this.body.scrollTop;
this.drag_Layer.moveTo(x1-this.off_DragX,y1-this.off_DragY);
if(this.drag_Layer.has3d){this.drag_Layer._3dLayer.moveTo(x1-this.off_DragX-2,y1-this.off_DragY-2)}
return false;
}