

// Copyright (c) 2006-2007, Powermand Inc. All Rights Reserved.

// Draggable Layers
// Use class=draggable, and optionally class=handle for child trigger/control elements
// Requires js.toolbox.js & draggable_init() on window.onload
// Ex: Constraint(OBJ,'off','NA','none','NA') Horz moves are off -  No Vert Constraint
		var dragObj=0;
		var Xoffset=0;
		var Yoffset=0;
		var dragoutTimer=0;
		//var startMoveEvents = new EventCache();
		//var stopMoveEvents = new EventCache();		

		function draggable_init(withinObj){
				
				//DragEvents.add('watchDragObj()');
				DragEvents.subscribe(function(){watchDragObj()});
				document.onmouseup= stopMove;
				if(withinObj) dragObjs = $$TB("draggable",withinObj); 
				else dragObjs = $$TB("draggable");	
				for (var i=0;i<dragObjs.length;i++){
					if(withinObj) Handles = $$TB("handle",withinObj);
					else Handles = $$TB("handle");
					if(Handles.length==0){
					  dragObjs[i].onmousedown=function(){startMove(this)}
					  dragObjs[i].onmouseup=function(){stopMove(this)}	
					  dragObjs[i].style.cursor="move";
					 }else{
					   dragParent=dragObjs[i]
						for (j=0;j<Handles.length;j++){
					  	  Handles[j].onmousedown=function(){startMove(dragParent)}
					  	  Handles[j].onmouseup=function(){stopMove(dragParent)}
						}
					}
				}
			}		
		function watchDragObj(){
		  if(dragObj!=0){
			lm=dragObj.LMin;
			rm=dragObj.RMax;
			tm=dragObj.TMin;
			bm=dragObj.BMax;
			w=width(dragObj);
			h=height(dragObj);
			leftPos=leftCoord(dragObj);
			topPos=topCoord(dragObj);			
			x=mouseX-Xoffset;
			y=mouseY-Yoffset;
			if(lm!='off'){
				if (rm < (x+w) && rm!=null){
					leftPos=rm-w;
				}else if (lm > x && lm!=null){
					leftPos=lm;
				}else if((lm <= x || lm==null) && (rm >=(x+w) || rm==null)){
					leftPos=x;
				}
			}
			if(tm!='off'){
				if(bm < (y+h) && bm!=null)
					topPos=bm-h;
				if(tm > y && tm!=null)
					topPos=tm;	
				if((tm <= y || tm==null) &&
					(bm >=(y+h) || bm==null)
					|| tm=='none')
					topPos=y;				
			}
			reposition(dragObj,leftPos,topPos);
			if(typeof(dragObj.dragEvent)=='function')
					dragObj.dragEvent();			
		  }
		}		
		function startMove(O){
				dragObj=$(O);
				Xoffset=mouseX-leftCoord(O);
				Yoffset=mouseY-topCoord(O);
				dragObj.fireEvent('onStart'); 
				//startMoveEvents.execute();
			}
		function stopMove(O){ 
				if(dragObj) 
					dragObj.fireEvent('onComplete'); 
				dragObj=0;
				//stopMoveEvents.execute();			
			}
		function Constraint(O,l,r,t,b){
			 $TB(O).LMin=l;
			 $TB(O).RMax=r;
			 $TB(O).TMin=t;
			 $TB(O).BMax=b;		 
		}
function deselect() {
  if (document.selection)
    document.selection.empty();
  else if (window.getSelection)
    window.getSelection().removeAllRanges();
} 	