/*
 * Copyright (c) 1995-2011 by 4system Polska Sp. z o.o.
 * All Rights Reserved.
 */
if (typeof(moduleDone)!="undefined") moduleDone("mouseevent.js");

WBTMouseEvent = function()
{ 
	this.objmousedown = null;

	//Drag & Drop
	this.isDragging = false;
	this.elementToDrag = null;
	this._lastX = null;
	this._lastY = null;  
}

var isDragging = false;
var wasDragging = false;
var elementToDrag = null;
var _corX = null;
var _corY = null;

WBTMouseEvent.prototype.eventMethod = function(e)
{
	if (typeof(page) == "undefined" || page == null) return;
	//handle event
	if (is.def) {
		if (is.ie) {
			var e = event
		}
		e.cancelBubble = true;
		if (e.stopPropagation) e.stopPropagation();
	}
	
	//set cursor position
	if(is.ns4) {
		var cursorX = e.pageX;
		var cursorY = e.pageY;
  	} else if (is.moz) {
		var cursorX = e.pageX;
		var cursorY = e.pageY;  
  	} else {
		var cursorX = e.x;
		var cursorY = e.y;
  	}
	
	var wbtelem = null;
	var el = null;
	var rsrc = null;
	if (is.def) {
		rsrc = (is.ie) ? e.srcElement : e.target;
	} else {
		if (is.ns4) rsrc = e.target;
	}		
	if (!rsrc) return false; 
	el = rsrc.owner;
	if (!el) return;
	//looking foe wbtelement
	var wbtelem = el;
	
	if (!isDragging) {
		if (!el.stopPropagation) {
			if (!wbtelem.isComponentElement) {
				while (wbtelem.parent.type != page.type) {
					wbtelem = wbtelem.parent;
					if (wbtelem.isComponentElement) {
						break;
					}
				}
			}
		}
	}
	
	if (e.type == "mousedown" ) {
		// Element is able to move, so drag it
		if (typeof(el) != "undefined" && !el.stopPropagation && (el.canDragging || el.parent.canDragging)) {
			//page.setOnTop(el);
			wasDragging = false;
			isDragging = true;
			elementToDrag = (el.canDragging) ? el : el.parent;
			if (elementToDrag.beginDrag) {
				elementToDrag.beginDrag();
			} 
			if (is.ns4) {
				_corX = e.pageX - elementToDrag.x;
				_corY = e.pageY - elementToDrag.y;
			} else if (is.moz) {
				_corX = e.pageX - elementToDrag.x;
				_corY = e.pageY - elementToDrag.y;
			} else {
				_corX = e.x - elementToDrag.x;
				_corY = e.y - elementToDrag.y;
			}
			if (is.ns4) document.captureEvents( Event.MOUSEMOVE );
			document.onmousemove = mouseEvent.drag;
		} else {
			if (typeof(el) != "undefined") {
				//wbtelem.eventMouseDown(e);
				el.eventMouseDown(e);
				mouseEvent.objmousedown = el;
			}
		}
	}  // END: e.type == "mousedown"
	else if (e.type == "mouseup") {
		if (isDragging) {
			// end moving element
			document.onmousemove = null;
			isDragging = false;
			wasDragging = false;
			_corX = 0;
			_corY = 0;
			// say to DDBaskets that there was end of draging
			// if can do somthnig with it 
			if (is.ns4 || is.moz) {
				var docCurX = e.pageX;//document.body.scrollLeft + e.pageX;
				var docCurY = e.pageY;//document.body.scrollTop + e.pageY;
			} else {
				var docCurX = document.body.scrollLeft + e.x;
				var docCurY = document.body.scrollTop + e.y;
			}
			
			var dropObj = null;
			for (var i = 0; i < page.children.length; i++) {
				var child = page.children[i];
				if (docCurX >= child.x && docCurX <= child.x + child.w &&
						docCurY >= child.y && docCurY <= child.y + child.h &&
						child.id != elementToDrag.id && child.dockSite) {
					if (!dropObj || (dropObj && child.z >= dropObj.z)) {
						dropObj = child;
					}
				}
			}

			if (elementToDrag.endDrag && elementToDrag.endDrag(dropObj)) {
				if (dropObj) dropObj.ddeDrop(elementToDrag);
			}
			if (typeof(el) != "undefined") {
				el.eventMouseUp(e);
				//wbtelem.eventMouseUp(e);
			}
		} else {
			el.eventMouseUp(e);
			//wbtelem.eventMouseUp(e);
			if (mouseEvent.objmousedown == wbtelem) {
				//wbtelem.eventClick(e);
				el.eventClick(e);
			}  
			//mouseEvent.objmousedown = null; //pg2-replaced with the block below (from MK)
			if (typeof(mouseEvent) != "undefined") {
				mouseEvent.objmousedown = null;
			}
		}
	}  // END: e.type == "mouseup"
	else if(e.type == "mouseover") el.eventMouseOver(e);
	else if(e.type == "mouseout") el.eventMouseOut(e);
	else if(e.type == "dblclick") el.eventDblClick(e);
	
	if (e.type == "mousedown" && !el.imWarningPopup) 
	{
		PlayerInterface.SetValue("studentActive");
	}
	
  	//if (is.moz) return false;
	return true;
}

WBTMouseEvent.prototype.drag = function(e)
{
	wasDragging = true;
	
	if (is.ie) var e = event;

	if (is.ns4) {
		var cursorX = e.pageX;
		var cursorY = e.pageY;
	} else if (is.moz) { 
		var cursorX = e.pageX;
		var cursorY = e.pageY;  
	} else {
		var cursorX = e.x;
		var cursorY = e.y;
	}
	
	var posX = (cursorX < 0 ? 0:(cursorX > page.pageWidth ? page.pageWidth:cursorX));
	var posY = (cursorY < 0 ? 0:(cursorY > page.pageHeight ? page.pageHeight:cursorY));
	
	var elX = posX - _corX;
	var elY = posY - _corY;
	
	if (elementToDrag.calcPosX) elX = elementToDrag.calcPosX(elX);
	if (elementToDrag.calcPosY) elY = elementToDrag.calcPosY(elY);
	
	elementToDrag.moveTo(elX, elY);
	_lastX = posX;
	_lastY = posY;
	
	if (is.moz) mouseEvent.wait();
	return false;
};

WBTMouseEvent.prototype.wait = function()
{
	var len = window.status.length;
	if (window.status.substr((len - 1), 1) == " ") window.status = window.status.substring(0, len - 1);
	window.status = window.status + " ";
}

mouseEvent = new WBTMouseEvent();
