//\//////////////////////////////////////////////////////////////////////////////////
//\  Plug-in module for support Draggable popups. Portions of this code adapted from
//\
//\  overLIB 3.50  --  This notice must remain untouched at all times.
//\  Copyright Erik Bosrup 1998-2001. All rights reserved.
//\
//\  By Erik Bosrup (erik@bosrup.com).  Last modified 2002-08-10.
//\  Portions by Dan Steinman (dansteinman.com). Additions by other people are
//\  listed on the overLIB homepage.
//\
//\//////////////////////////////////////////////////////////////////////////////////
//\proposed
////////////////////////////////////////////////////////////////////////////////////
// CONSTANTS
/////////////////////////////////////////////////////////////////////////////////////
registerCommands('draggable,altcut,dragimg');
////////////////////////////////////////////////////////////////////////////////////
// DEFAULT CONFIGURATION
// You don't have to change anything here if you don't want to. All of this can be
// changed on your html page or through an overLIB call.
////////////////////////////////////////////////////////////////////////////////////
// Set default values for draggable capability.
if (typeof ol_draggable=='undefined') var ol_draggable=0;
if (typeof ol_altcut=='undefined') var ol_altcut=0;
if (typeof ol_dragimg=='undefined') var ol_dragimg='';
////////////////////////////////////////////////////////////////////////////////////
// END CONFIGURATION
// Don't change anything below this line,all configuration is above.
////////////////////////////////////////////////////////////////////////////////////
// INIT
////////////////////////////////////////////////////////////////////////////////////
// Runtime variables initialization for draggable support. Don't change,not for config!
// Make sure they have some value in case not set any where else.
var o3_draggable=0;
var o3_altcut=0;
var o3_dragimg='';
var olImgLeft,olImgTop,olImgRight,olImgBottom;
var olImgObj;
var olMseMv;  // hold mouseMove name
////////////////////////////////////////////////////////////////////////////////////
// PUBLIC FUNCTIONS
////////////////////////////////////////////////////////////////////////////////////
// Function which sets runtime variables to their default values
function setDragVar() {
	o3_draggable=ol_draggable;
	o3_altcut=ol_altcut;
	o3_dragimg=ol_dragimg;
	olImgObj=null;
}
////////////////////////////////////////////////////////////////////////////////////
//  FUNCTION WHICH IS CALLED TO PARSE ASSOCIATED PARAMETERS RELATED TO CSS SUPPORT
//
// returns the index within the argument array where a match was found.
// Arguments are current index value in the argument array and the argument array
// Returns index within that array where a match is found,-1 if no match.
////////////////////////////////////////////////////////////////////////////////////
function parseDragExtras(pf,i,ar) {
	var k=i;
	if (k < ar.length) {
		if (ar[k]==DRAGGABLE) { eval(pf+'draggable=('+pf+'draggable==0) ? 1 : 0'); return k; }
		if (ar[k]==ALTCUT) { eval(pf+'altcut=('+pf+'altcut==0) ? 1 : 0'); return k; }
		if (ar[k]==DRAGIMG) { eval(pf+'dragimg="'+ar[++k]+'"'); return k; }
	}
	return -1;
}
////////////////////////////////////////////////////////////////////////////////////
//  PRESHOW PROCESSING FOR DRAGGABLE POPUPS
////////////////////////////////////////////////////////////////////////////////////
function startDrag() {
 // Initiate dragging if in same frame and its a sticky
	if (o3_draggable) {
		if (o3_sticky&&(o3_frame==ol_frame)) initDrag();
		else o3_draggable=0;
	}
}
////////////////////////////////////////////////////////////////////////////////////
//  POSTHIDE PROCESSING FOR DRAGGABLE POPUPS
////////////////////////////////////////////////////////////////////////////////////
function stopDrag() {
	if (o3_draggable) endDrag();
}
////////////////////////////////////////////////////////////////////////////////////
// DRAGGABLE FUNCTIONS
////////////////////////////////////////////////////////////////////////////////////
function initDrag() {
	olMseMv=capExtent.onmousemove;
	if(olNs4) {
		document.captureEvents(Event.MOUSEDOWN | Event.CLICK);
		document.onmousedown=grabEl;
		document.onclick=function(e) {return routeEvent(e);}
	} else {
		over.onmousedown=grabEl;
	}
	if (o3_dragimg) chkForSupport(o3_dragimg);
	return true;
}
function chkForSupport(dragImg) {
	if (dragImg) {
		if (typeof getRmrkObjRef!='undefined') olImgObj=getRmrkObjRef(dragImg);
		if (olImgObj==null) o3_dragimg='';
	}
}
function setCursor(on) {
	if (olNs4) return;
	over.style.cursor=(on ? 'move' : 'auto');
}
function chkCursorPosition(Obj,XPos,YPos) {
	if (Obj) {
		o3_rmrkx=o3_rmrky=0;
		o3_rmrkpt='LR';
		getRmrkLocation(Obj);
		if (XPos < olImgLeft||XPos > olImgRight||YPos < olImgTop||YPos > olImgBottom) return false;
	}
	return true;
}
function grabEl(e) {
	var e=(e) ? e : event;
	var X,Y;
	var cKy=(olNs4 ? e.modifiers & Event.ALT_MASK : (!olOp ? e.altKey : e.ctrlKey));
	if ((o3_altcut ? !cKy : cKy)) {
		//   get mouse's current x,y location
		X=(e.pageX) ? e.pageX : eval('e.clientX+o3_frame.'+docRoot+'.scrollLeft');
		Y=(e.pageY) ? e.pageY : eval('e.clientY+o3_frame.'+docRoot+'.scrollTop');
	  if (chkCursorPosition(olImgObj,X,Y)) {
	 		if (olNs4) document.captureEvents(Event.MOUSEUP);
	 		capExtent.onmousemove=moveEl;
	 		document.onmouseup=function() {setCursor(0); if (olIe4) over.onselectstart=null; capExtent.onmousemove=olMseMv;}
	 		setCursor(1);
	 		if (olIe4) over.onselectstart=function() {return false;}
	 		if (olNs4) {
	  		cX=X
	  		cY=Y
	 		} else {
	  		// get offsets from upper left hand corner of popup to keep popup from jummping
	  		// when first starting to drag
	  		cX=X-(olNs4 ? over.left : parseInt(over.style.left));
	  		cY=Y-(olNs4 ? over.top : parseInt(over.style.top)); 
	 		}
	 		return (olNs4 ? routeEvent(e) : false);
	  }
	} else setCursor(0);
}
function moveEl(e) {
	var e=(e) ? e : event;
	var dX,dY,X,Y;
	//  get new mouse location
	X=(e.pageX) ? e.pageX : eval('e.clientX+o3_frame.'+docRoot+'.scrollLeft');
	Y=(e.pageY) ? e.pageY : eval('e.clientY+o3_frame.'+docRoot+'.scrollTop');
	if (chkCursorPosition(olImgObj,X,Y)){
		if (olNs4) {
			dX=X-cX; cX=X;
			dY=Y-cY; cY=Y;
			over.moveBy(dX,dY);
		} else 
			repositionTo(over,X-cX,Y-cY);  // move popup to that position
	}
}
function endDrag(obj) {
	if (olNs4) {
		document.releaseEvents(Event.MOUSEDOWN | Event.MOUSEUP | Event.CLICK);
		document.onmousedown=document.onclick=null;
	} else {
		if(!obj) obj=over;
		obj.onmousedown=null;
	}
	document.onmouseup= null;
}
//////////////////////////////////////////////////////////////////////////////////////
//  REGISTER FUNCTIONS -- MUST BE DONE LAST SO THAT FUNCTIONS ARE KNOWN
//////////////////////////////////////////////////////////////////////////////////////
registerRunTimeFunction(setDragVar);
registerCmdLineFunction(parseDragExtras);
registerFunction("disp",startDrag,FBEFORE);
registerFunction("hideObject",stopDrag,FAFTER);
//end 

