function GeneralTool()
{
	this.m_currDDObj=null;
	this.m_lastEv = EV_NONE;
	this.m_lastX =0;
	this.m_lastY =0;
	
	
	
	//tooltip related members
	this.m_TTObj=null;
	this.m_TTTime=null;
	this.m_TTState= TT_STATE_NONE;
	
	//show Clippe dRelated members
	//this.m_currExpObj =null;
	
	///////////////////////////////////////////////////////////////
	//show clipped functions
	//this.getExpObjFromEventObj = function(obj)
	//{
	//	if (!IsDefined(obj)) return null;
	//	while (obj)
	//	{
	//		if (IsDefined(obj.getAttribute) && obj.getAttribute("name")=="expandedClip")
	//		 return obj;
	//		obj=obj.parentNode;
	//	}
	//	return null;
	//}
	
	//this.HandleShowClipped = function(evnt, evObj)
	//{
	//	var evSrc = IsDefined(evObj) ? evObj:g_dc.GetEventTarget(evnt);
	//	if (IsDefined(evSrc))
//		{
//			var expobj = this.getExpObjFromEventObj(evSrc);
//			if (expobj!=this.m_currExpObj)
//			{
//				if (this.m_currExpObj) this.m_currExpObj.dragContainer.SetClipping(SC_SHOW_NAKED);
//				if (expobj) expobj.dragContainer.SetClipping(SC_SHOW_ALL);
//				this.m_currExpObj = expobj;
//			}
//		}
//	}
	
	
	//////////////////////////////////////////////////////////////
	//tool interface, each tool must impleent this
	this.ClearTool = function()
	{
		this.ExitDragMod();
		this.ExitResizeMod();
	}
	
	this.GetToolName = function()
	{
		return "GeneralTool";
	}
	
	
	
	this.InitTool = function(initVal)
	{
		
	}
	///////////////////////////////////////////////
	//end tool interface
	
	this.TTReset = function()
	{
		//debugger;
		if (this.m_TTObj)
			this.m_TTObj.TTHide();
		this.m_TTObj=null;
		this.m_TTTime=null;
		this.m_TTState= TT_STATE_NONE;
	}
	
	this.TTInit = function(obj)
	{
		//alert(obj.getAttribute("name"));
		this.m_TTObj=obj;
		this.m_TTState= TT_STATE_WAITING;
		this.m_TTTime = new Date();
		
		setTimeout("if (IsDefined(g_dc.m_curEventTool.OnTTTimeout)) g_dc.m_curEventTool.OnTTTimeout();", TT_WAIT_TIME);
	}
	
	this.OnTTTimeout = function()
	{
		if (!(this.m_TTState == TT_STATE_WAITING))
		{
			return;
		}
		if (Math.abs(this.TimeFromTT() - TT_WAIT_TIME)< TT_WAIT_TOLERANCE)
		{
			this.m_TTObj.TTShow();
			this.m_TTState=TT_STATE_SHOWN;
			return;
		}
		if ((this.TimeFromTT() - TT_WAIT_TIME)>1000)
		{
			this.TTReset();
			return;
		}
	}
	
	this.TimeFromTT = function()
	{
		if (!this.m_TTTime) return 0;
		var curdt = new Date();
		return ((curdt.getMilliseconds() - this.m_TTTime.getMilliseconds()) +
				((curdt.getSeconds() - this.m_TTTime.getSeconds())*1000)	+
				((curdt.getMinutes() - this.m_TTTime.getMinutes())*1000*60));
	}
	
	this.GetTTHandler = function(obj)
	{
		while (obj)
		{
			 if (typeof(obj.TTShow)!=dd_u)
			 {
			 	return obj;
			 }
			 obj=obj.parentNode;	
		}
		return null;
	}
	
	this.HandleTT = function(evnt)
	{
		var evSrc = g_dc.GetEventTarget(evnt);
		var ttObjHandler =  this.GetTTHandler(evSrc);
		
		switch (this.m_TTState)
		{
			case TT_STATE_NONE:
				if (ttObjHandler)
				{
					this.TTInit(ttObjHandler);
				}
				break;
			case TT_STATE_WAITING:
				if (ttObjHandler)
				{
					if (this.m_TTObj && this.m_TTObj.TTIsSame(ttObjHandler))
					{
						if (this.TimeFromTT()>TT_WAIT_TIME)
						{
							this.TTReset();
							this.TTInit(ttObjHandler);
						}
					}
					else
					{
						this.TTReset();
						this.TTInit(ttObjHandler);
					}
				}
				else
				{
					this.TTReset();
				}
				break;	
			case TT_STATE_SHOWN:
			{
				var x = g_dc.getEventMouseX(evnt);
				var y = g_dc.getEventMouseY(evnt);
				if (!this.m_TTObj.TTInBounds(x,y))
				{
					this.TTReset();
				}
				break;
			}
		}
	}
	
	this.AdjustFrameCoords = function(obj, ev)
	{
		var rc  = obj.getClientPosition();
		ev.realX = ev.clientX+rc.m_left;
		ev.realY = ev.clientY+rc.m_top;
	}
	
	this.startDrag = function(dragObj, e)
	{
		this.TTReset();
		if (typeof dragObj==dd_u || dragObj==null) {alert("error20");return;}
		this.m_currDDObj = dragObj;
		this.m_lastEv= EV_DRAG_START;
		this.m_lastX = g_dc.getEventMouseX(e);
		this.m_lastY = g_dc.getEventMouseY(e);
		dragObj.maximizeZ();
	}
	
	this.startResize = function(resizeObj, e)
	{
		this.TTReset();
		if (typeof resizeObj==dd_u || resizeObj==null) {alert("error20");return;}
		try{
			this.m_currRZObj = resizeObj;
			this.m_lastEv= EV_RESIZE_START;
			this.m_lastX = g_dc.getEventMouseX(e);
			this.m_lastY = g_dc.getEventMouseY(e);
			resizeObj.maximizeZ();
		}
		catch(err){
			alert(err.description);
		}
	}
	
	this.DoResize = function(e)
	{
		if (this.m_lastEv==EV_RESIZE_START)
		{
			if (g_dc.ie) 
					g_dc.docBody.setCapture(true);
		}
		this.m_currRZObj.ResizeToDiff(g_dc.getEventMouseX(e)-this.m_lastX, 
										g_dc.getEventMouseY(e)-this.m_lastY);						
										
		this.m_lastX=g_dc.getEventMouseX(e);
		this.m_lastY=g_dc.getEventMouseY(e);
		
		this.m_lastEv = EV_RESIZE_MOVE;
	}
	
	this.DoDrag = function(e)
	{
		if (this.m_lastEv==EV_DRAG_START)
		{
			if (g_dc.ie) 
					g_dc.docBody.setCapture(true);
		}
		var retarr = this.m_currDDObj.DragElem(g_dc.getEventMouseX(e)-this.m_lastX, 
										g_dc.getEventMouseY(e)-this.m_lastY);
		this.m_lastX=g_dc.getEventMouseX(e);								
		if (IsDefined(retarr) && IsDefined(retarr['xresidue']))
		{
			this.m_lastX-=retarr['xresidue'];
		}
		this.m_lastY=g_dc.getEventMouseY(e);
		if (IsDefined(retarr) && IsDefined(retarr['yresidue']))
		{
			this.m_lastY-=retarr['yresidue'];
		}
			
		this.m_lastEv = EV_DRAG_MOVE;
	}
	
	this.ExitDragMod = function()
	{
		this.m_currRZObj = null;
		this.m_lastEv = EV_RESIZE_END;
		this.m_xDelta=0;
		this.m_yDelta=0;
		if (g_dc.ie) 
			g_dc.docBody.releaseCapture();
	}
	
	this.ExitResizeMod = function()
	{
		this.m_currDDObj = null;
		this.m_lastEv = EV_DRAG_END;
		this.m_xDelta=0;
		this.m_yDelta=0;
		if (g_dc.ie) 
			g_dc.docBody.releaseCapture();	
	}
	
	this.onClipbarMouseDown = function(e)
	{
		src = g_dc.GetEventTarget(e);
		this.startDrag(src.dragContainer, e);
		ev = g_dc.GetEvent(e);
		ev.cancelBubble=true;
		return false;
	}
	
	this.onResizeDown = function(e)
	{
		src = g_dc.GetEventTarget(e);
		this.startResize(src.dragContainer, e);
		ev = g_dc.GetEvent(e);
		ev.cancelBubble=true;
		return false;
	}
	
	this.onResizeUp = function(e)
	{
		this.ExitResizeMod();
	}
	
	this.onFrameMouseDown = function(e, frameID)
	{
		var clpObj = document.getElementById(frameID);
		if (!clpObj) {alert("error30");return;}
		var ddObj = clpObj.dragContainer;
		if (!ddObj) {alert("error30");return;}
		this.AdjustFrameCoords(ddObj, e);
		this.startDrag(ddObj, e, clpObj);
	}
	
	this.onFrameMouseUp = function(e, frameID)
	{
		this.ExitDragMod();
		this.ExitResizeMod();
	}
	
	this.onFrameMouseMove = function(e, frameID)
	{
		var clpObj = document.getElementById(frameID);
		if (!clpObj) {alert("error30");return;}
		var ddObj = clpObj.dragContainer;
		if (!ddObj) {alert("error30");return;}
		this.AdjustFrameCoords(ddObj, e);
		this.onDocumentMouseMove(e, clpObj);
	}
	
	this.onDocumentMouseMove = function(e, clpObj)
	{
		if ((this.m_lastEv==EV_DRAG_MOVE || this.m_lastEv==EV_DRAG_START)
			 && this.m_currDDObj !=null)
		{
			this.DoDrag(e);
		}
		else if ((this.m_lastEv==EV_RESIZE_MOVE || this.m_lastEv==EV_RESIZE_START)
			 && this.m_currRZObj !=null)
		 {
		 	this.DoResize(e);
		 }
		else
		{
			//this.HandleShowClipped(e, clpObj);
			this.HandleTT(e);
		}
	}
	
	this.onDocumentMouseDown = function(e)
	{
		src = g_dc.GetEventTarget(e);
		if (src && src.className && src.className=="workingArea")
			g_dc.Select(null);
		return false;
	}
	
	this.onDocKeyUp = function(e)
	{
		var ev = g_dc.GetEvent(e);
		var code = g_dc.GetEventCode(e);
		if (code==46)
		{
			g_dc.DeleteSelected();
		}
	}
	
	this.onDocumentMouseUp = function(e)
	{
		this.ExitDragMod();
		this.ExitResizeMod();
	}
	
	this.onIconizeClick = function(e)
	{
		this.TTReset();
		src = g_dc.GetEventTarget(e);
		src.dragContainer.ToIcon(true);
		ev = g_dc.GetEvent(e);
		ev.cancelBubble=true;
		return false;
	}
	
	this.onMaximizeClick = function(e)
	{
		this.TTReset();
		src = g_dc.GetEventTarget(e);
		src.dragContainer.toDefSize();
		return false;
	}
	
	
	
	this.OnHeaderMouseDown = function(e)
	{
		this.TTReset();
		src = g_dc.GetEventTarget(e);
		var dragCont = (IsDefined(src.dragContainer))? src.dragContainer:(IsDefined(src.parentNode.dragContainer)?src.parentNode.dragContainer:null) ;
		if (!dragCont) return true;
		this.startDrag(dragCont, e);
		this.Select(dragCont);
		var ev = g_dc.GetEvent(e);
		ev.cancelBubble=true;
		return false;
	}
	this.OnHeaderMouseUp = function(e)
	{
		this.ExitDragMod();
		var ev = g_dc.GetEvent(e);
		ev.cancelBubble=true;
		return false;
	}
	
	
	this.OnIcnDblClick = function(e)
	{
		src = g_dc.GetEventTarget(e);
		while (!IsDefined(src.dragContainer))
			src = src.parentNode;
		src = src.dragContainer;
		g_dc.ToggleTextEditMode(true,TXT_EDIT_TYPE_EXISTING, src);
		return false;
	}
	
	this.OnHeaderDblClick = function(e)
	{
		src = g_dc.GetEventTarget(e);
		while (!IsDefined(src.dragContainer))
			src = src.parentNode;
		src = src.dragContainer;
		g_dc.ToggleTextEditMode(true, TXT_EDIT_TYPE_EXISTING, src);
		return false;
	}
	
	//icon handlers
	this.OnIconMouseDown = function(e)
	{
		this.TTReset();
		src = g_dc.GetEventTarget(e);
		var dgcont = g_dc.getDragContainer(src);
		this.startDrag(dgcont, e);
		this.Select(dgcont);
		var ev = g_dc.GetEvent(e);
		ev.cancelBubble=true;
		return false;
	}
	
	this.OnIconMouseUp = function(e)
	{
		this.ExitDragMod();
		var ev = g_dc.GetEvent(e);
		ev.cancelBubble=true;
		return false;
	}
	
	this.onExpandClick = function(e)
	{
		this.TTReset();
		src = g_dc.GetEventTarget(e);
		src.dragContainer.ToIcon(false);
		ev = g_dc.GetEvent(e);
		ev.cancelBubble=true;
		return false;
	}
	
	this.onToSourceClick = function(e)
	{
		this.TTReset();
		src = g_dc.GetEventTarget(e);
		window.open(src.dragContainer.url,"");
		ev = g_dc.GetEvent(e);
		ev.cancelBubble=true;
		return false;
	}
	
	
	
	this.onDeleteClick = function(e)
	{
		this.TTReset();
		var answer = confirm("You are about to permanently delete the selected clip. Continue?");
		if (answer)
		{
		src = g_dc.GetEventTarget(e);
		src.dragContainer.Delete();
	}
	}
	
	this.onBtnMouseDown = function(e)
	{
		ev = g_dc.GetEvent(e);
		ev.cancelBubble=true;
		return false;
	}
	
	this.onBtnMouseUp = function(e)
	{
		ev = g_dc.GetEvent(e);
		ev.cancelBubble=true;
		return false;
	}
	
	this.OnMiniItemDown = function(e)
	{
		src = g_dc.GetEventTarget(e);
		this.startDrag(src.m_wrapper, e);
		ev = g_dc.GetEvent(e);
		ev.cancelBubble=true;
		return false;
	}

	this.OnMiniItemUp = function(e)
	{
		this.ExitDragMod();
		this.ExitResizeMod();
	}
	
	this.Select = function(obj)
	{
		g_dc.Select(obj);
	}
	
	this.onGtsMouseDown = function(e)
	{
		ev = g_dc.GetEvent(e);
		ev.cancelBubble=true;
	}
	this.onDscMouseDown = function(e)
	{
		src = g_dc.GetEventTarget(e);
		this.startDrag(src.dragContainer, e);
		ev = g_dc.GetEvent(e);
		ev.cancelBubble=true;
		return false;
	}	
}

g_GeneralTool = new GeneralTool();

