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

WBTMouseEvent = function() { 
	this.objmousedown = null;
	this.isDragging = false;
	this.elementToDrag = null;
	this._lastX = null;
	this._lastY = null;  
	this._lastDragX = 0;
	this._lastDragY = 0;
};

var isDragging = false;
var wasDragging = false;
var elementToDrag=null;
var _cX = null;
var _cY = null;

WBTMouseEvent.prototype.eventMethod = function(e) {
	if (iambusy) return; //pg2-bi_audioUPFrame
	try {
		if (typeof(page) == "undefined" || page == null) return;
		iambusy = true; //pg2-bi_audioUPFrame
		if (is.def) {
			if (is.ie) {
				var e = event;
			};
			e.cancelBubble = true;
			if (e.stopPropagation) e.stopPropagation();
		};
		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) {
			iambusy = false; //pg2-bi_audioUPFrame
			return false;
		};
		el = rsrc.owner;
		if (!el && typeof(rsrc) != 'undefined' && typeof(rsrc.parentElement) != 'undefined') { //pg2-giub - drag feedback by its text
			el = rsrc.parentElement.owner;
			if (typeof(el) != 'undefined' && (el.type == 'componentPopup' || el.type == 'componentFeedback')) { //pg2-itwl
				var val = el.w - (e.clientX - el.x);
				if (val >= 0 && val <= 20) el = null; else {
					val = el.h - (e.clientY - el.y);
					if (val >= 0 && val <= 40) el = null;
				};
				
			} else el = null;
		};
		if (!el) {
			iambusy = false; //pg2-bi_audioUPFrame
			return;
		};
		
		if (el.locked) {
			iambusy = false; //pg2-bi_audioUPFrame
			return; //pg2-coig_page_print
		};
		
		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" || e.type == 'touchstart') {
			if (e.type == 'touchstart') { //pg2-iPad - if element is one that checks answer or checkAnswer action is assigned to it, checking answer is invoked twice...
				if (el.confirmAnswer) {
					iambusy = false; //pg2-bi_audioUPFrame
					return;
				};
				if (el.onClick != 'undefined' && el.onClick.toString().indexOf('.actionCheckAnswer()') > -1) {
					iambusy = false; //pg2-bi_audioUPFrame
					return;
				};
				if (is.mobile && is.safari && el.onClick != 'undefined') { //pg2-iPad - single fast click invokes action twice
					//var str = el.onClick.toString();
					//if (str.indexOf('.next') > -1 || str.indexOf('.prev') > -1 || str.indexOf('.goto') > -1) return;
					if (!el.canDragging && !el.parentDragging) {
						iambusy = false; //pg2-bi_audioUPFrame
						return; //pg2-test
					};
				};
			};
			
			if (typeof(el) != 'undefined') {
				if (el.type == 'componentDDElement' && el.parent && el.parent.type == 'componentContainer') el.stopPropagation = false; //pg2-FPC -DnD within container
				else
				if (el.parent.type == 'componentContainer' && el.canDragging && !el.parentDragging) el.stopPropagation = false; //pg2-FPC -dragging other components within container
			};
			
			if (typeof(el) != "undefined" && !el.stopPropagation && (el.canDragging || el.parent.canDragging)) {
				wasDragging = false;
				isDragging = true;
				elementToDrag = (el.canDragging)?el:el.parent;
				if (elementToDrag.beginDrag && !elementToDrag.locked) { //pg2-giub_cai_dnd -if locked, do not start drag nor hide cai
					elementToDrag.beginDrag();
				};
				if (elementToDrag.obj) { //pg2-otherwise if browser != IE, plain image component (not DDElement) ain't dragged- is selected
					elementToDrag.obj.onselectstart = function(e) {	return false; };
					elementToDrag.obj.ondragstart = function(e) { return false; };
				};
				if (is.mobile) {
					if (e.touches.length > 0) {
						var touch = e.touches[0];
						_cX = touch.pageX - elementToDrag.x;
						_cY = touch.pageY - elementToDrag.y;
					};
				} else
				if (is.ns4) {
					_cX = e.pageX - elementToDrag.x;
					_cY = e.pageY - elementToDrag.y;
				} else
				if (is.moz) {
					_cX = e.pageX - elementToDrag.x;
					_cY = e.pageY - elementToDrag.y;
				} else {
					_cX = e.clientX - elementToDrag.x; //e.x - elementToDrag.x; //pg2
					_cY = e.clientY - elementToDrag.y; //e.y - elementToDrag.y;
				};
				if (is.ns4) document.captureEvents(Event.MOUSEMOVE);

				if (!is.mobile) {
					document.onmousemove = mouseEvent.drag;
				};
			} else {
				if (typeof(el) != "undefined") {
					el.eventMouseDown(e);
					mouseEvent.objmousedown = el;
				};
			};
		} else
		if (e.type == "mouseup" || e.type == 'touchend') {
			PlayerInterface.SetValue("studentActive"); //pg2-knf -inactivity
			if (e.type == 'touchend') { //pg2-iPad - if element is one that checks answer or checkAnswer action is assigned to it, checking answer is invoked twice...
				if (el.confirmAnswer) {
					iambusy = false; //pg2-bi_audioUPFrame
					return;
				};
				if (el.onClick != 'undefined' && el.onClick.toString().indexOf('.actionCheckAnswer()') > -1) {
					iambusy = false; //pg2-bi_audioUPFrame
					return;
				};
				if (is.mobile && is.safari && el.onClick != 'undefined') { //pg2-iPad - single fast click invokes action twice
					//var str = el.onClick.toString();
					//if (str.indexOf('.next') > -1 || str.indexOf('.prev') > -1 || str.indexOf('.goto') > -1) return;
					if (!el.canDragging && !el.parentDragging) {
						iambusy = false; //pg2-bi_audioUPFrame
						return; //pg2-test
					};
				};
			};
			if (isDragging) {
				if (!is.mobile) {
					if (typeof(onMouseMoveFromPage) != 'undefined') document.onmousemove = onMouseMoveFromPage; //pg2-d'n'd feedback and mousemove restore
					else document.onmousemove = null; //pg2-old one
				};
				isDragging = false;
				wasDragging = false;
				_cX = 0;
				_cY = 0;
				var docCurX = 0;
				var docCurY = 0;
				if (is.mobile) {
					docCurX = mouseEvent._lastDragX;
					docCurY = mouseEvent._lastDragY;
				} else
				if (is.ns4||is.moz) {
					docCurX = e.pageX;
					docCurY = e.pageY;
				} else {
					docCurX = e.clientX; //pg2-DnD_puzzle_part1 -otherwise DnDBaskets one next to another in 'puzzle' exercises won't work correctly
					docCurY = e.clientY;
					//docCurX = document.body.scrollLeft + e.clientX; //e.x; //pg2
					//docCurY = document.body.scrollTop + e.clientY; //e.y;
				};
				var dropObj = null;
				//top.window.document.title = elementToDrag.parent.id; //pg2-test
				
				//pg2-fpc_dnd
				var dropBasket = null;
				var dropDW1 = 0; //ile obszaru na baskecie
				
				/* //pg2-fpc_dnd -removed, replaced with the block below it
				if (elementToDrag && elementToDrag.parent.type == 'componentContainer') { //pg2-FPC -in case of container
					docCurX = elementToDrag.x;
					docCurY = elementToDrag.y;
					for (var i=0; i<elementToDrag.parent.children.length; i++) {
						var subChild = elementToDrag.parent.children[i];
						if (docCurX >= subChild.x && docCurX <= subChild.x+subChild.w &&
							docCurY >= subChild.y && docCurY <= subChild.y+subChild.h &&
							elementToDrag && subChild.id != elementToDrag.id && subChild.dockSite) //pg2- if elementToDrag != null
						{
							if (!dropObj || (dropObj && subChild.z >= dropObj.z)) {
								dropObj = subChild;
							};
						};
					};
				} else*/
				
				//pg2-fpc_dnd -removed, replaced with the block below it
				var parent = page;
				if (elementToDrag && elementToDrag.parent.type == 'componentContainer') parent = elementToDrag.parent;
				for (var i=0; i<parent.children.length; i++) {
					var child = parent.children[i];
					if (!child.visible) continue; //pg2-otherwise if two baskets are located one over another and - for example - one is not visible, then docking wouldn't be possible
					
					if (typeof(child.puzzleMode) != 'undefined' && child.puzzleMode) { //pg2-puzzleMode_dndComponents -for Drag'n'drop elements, docking mechanism - not based on elements' corners- is different and checking answer as well
						//pg2-fpc_dnd -removed, replaced with the block below it
						if (docCurX >= child.x && docCurX <= child.x+child.w &&
							docCurY >= child.y && docCurY <= child.y+child.h &&
							elementToDrag && child.id != elementToDrag.id && child.dockSite) //pg2- if elementToDrag != null
						{
							if (!dropObj || (dropObj && child.z >= dropObj.z)) {
								dropObj = child;
							};
						};// else
					} else {
						//---------------------------------
						if ( //pg2-fpc_dnd
								((elementToDrag.x >= child.x && elementToDrag.x <= child.x+child.w) || (elementToDrag.x+elementToDrag.w >= child.x && elementToDrag.x+elementToDrag.w <= child.x+child.w)) &&
								((elementToDrag.y >= child.y && elementToDrag.y <= child.y+child.h) || (elementToDrag.y+elementToDrag.h >= child.y && elementToDrag.y+elementToDrag.h <= child.y+child.h))
							)
						{
							if (child.type == 'componentDDBasket' && elementToDrag.type == 'componentDDElement') {
								var dropDW2 = 0;
								if (elementToDrag.x >= child.x && elementToDrag.x <= child.x+child.w) dropDW2 = child.x + child.w - elementToDrag.x; else
								if (elementToDrag.x+elementToDrag.w >= child.x && elementToDrag.x+elementToDrag.w <= child.x+child.w) dropDW2 = elementToDrag.x+elementToDrag.w-child.x;
								if (dropBasket == null) {
									dropBasket = child;
									dropDW1 = dropDW2;
								} else {
									if (dropDW1 < dropDW2) {
										dropBasket = child;
										dropDW1 = dropDW2;
									};
								};
							} else
							if (!dropObj || (dropObj && child.z >= dropObj.z)) {
								dropObj = child;
							};
						};
						//---------------------------------
					};
				};
				
				if (dropBasket != null) dropObj = dropBasket; //pg2-fpc_dnd
				
				//top.window.document.title = elementToDrag.id + ', ' + dropObj; //pg2-test
				if (elementToDrag && elementToDrag.endDrag && elementToDrag.endDrag(dropObj)) { //pg2- if elementToDrag != null
					if (dropObj) dropObj.ddeDrop(elementToDrag);
				};
				if (typeof(el)!="undefined") {
					el.eventMouseUp(e);
				};
			} else {
				el.eventMouseUp(e);
				if (mouseEvent.objmousedown == wbtelem) {
					el.eventClick(e);
				};
				//mouseEvent.objmousedown=null; //pg2-replaced with the block below (from MK)
				if (typeof(mouseEvent)!="undefined") {
					mouseEvent.objmousedown = null;
				};
			};
		} else {
			if (e.type == "mouseover" && !el.imWarningPopup) { //pg2-knf
				el.eventMouseOver(e);
				PlayerInterface.SetValue("studentActive");
			} else
			if (e.type == "mouseout") el.eventMouseOut(e); else
			if (e.type == "dblclick") el.eventDblClick(e); else
			if (e.type == "touchmove" && isDragging) mouseEvent.drag(e);
			//if (((e.type == "mousedown") || (e.type == 'click') /*||(e.type == "mouseover")*/) && (!el.imWarningPopup)) { //pg2-knf -mouseover moved above, here added 'click'
			if (e.type == "mousedown" || e.type == 'click') { //pg2-knf -mouseover moved above, here added 'click'
				PlayerInterface.SetValue("studentActive");
			};
		};
	} catch(e) {};
	iambusy = false;
	return true;
};

WBTMouseEvent.prototype.drag = function(e) {
	try { //pg2-bi_audioUPFrame
		wasDragging = true;
		if (is.ie) var e = event;
		if (is.mobile) e.preventDefault();
		var cursorX = 0;
		var cursorY = 0;
		if (is.ns4) {
			cursorX = e.pageX;
			cursorY = e.pageY;
		} else
		if (is.mobile) {
			var touch = e.touches[0];
			cursorX = touch.pageX;
			cursorY = touch.pageY;
			mouseEvent._lastDragX = touch.pageX;
			mouseEvent._lastDragY = touch.pageY;
		} else
		if (is.moz) {
			cursorX = e.pageX;
			cursorY = e.pageY;
		} else {
			cursorX = e.clientX; //e.x; //pg2
			cursorY = e.clientY; //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 - _cX;
		var elY = posY - _cY;
		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();
	} catch(e) {};
	return false;
};

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

mouseEvent = new WBTMouseEvent();
