
_dragObject				= null;
_mouseOffset			= null;

window.ondragdrop = stopEvent;
window.ondragstart = stopEvent;

addEvent(document, 'mousemove', _mouseMove);
addEvent(document, 'mouseup', _mouseUp);


function addEvent(element, evname, func) {
	if (document.all) {
		element.attachEvent('on' + evname, func);
	} else {
		element.addEventListener(evname, func, true);
	}
}

function removeEvent(element, evname, func) {
	if (document.all) {
		element.detachEvent('on' + evname, func);
	} else {
		element.removeEventListener(evname, func, true);
	}
}

function stopEvent(event) {
	if (document.all) {
		event = window.event;
		event.cancelBubble = true;
		event.returnValue = false;
	} else {
		event.preventDefault();
		event.stopPropagation();
	}
	return false;
}

function makeDraggable(item, code) {
//		log('makeDraggable', item, false);
	item.onmousedown = function(ev) {
//				log('mouse down', 'ok', false);
		_fullScreenDivShow(1);
		var clone = this.cloneNode(true);
		document.body.appendChild(clone); 
		_dragObject = clone;
		_dragObject.code = code;
		_mouseOffset = _getMouseOffset(this, ev);
		_mouseMove(ev);
//				return true;
}
}
//
function _fullScreenDivShow(fl) {
		var x, y;
		if (self.innerHeight) {
		x = self.innerWidth;
		y = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) {
				x = document.documentElement.clientWidth;
				y = document.documentElement.clientHeight;
		} else if (document.body) {
				x = document.body.clientWidth;
				y = document.body.clientHeight;
		}
		var el=document.getElementById('fullScreenDiv');
		if(null!=el) {
				el.style.visibility = (fl==1)?'visible':'hidden';
				el.style.display = (fl==1)?'block':'none';
				//rgrigorov@ :ToDo: remove (-20 hack)
				el.style.width = x - 20 + "px";
				el.style.height = y  - 20 + "px";
				el.style.zIndex = 999;
		}
}
//


function getEditAreaPosition() {	
	var	el = top.document.getElementById('wysiwyg___Frame');
	var x = 0;
	var y = 0;
	do {
		x += el.offsetLeft;
		y += el.offsetTop;
		var oldEl = el;
		el = el.offsetParent;
	} while(el != null)


	el = document.getElementById('wysiwyg___Frame').contentWindow.document;
	var el = el.getElementById('eEditorArea');


	var orEl = el;

	do {
		x += el.offsetLeft;
		y += el.offsetTop;
		var oldEl = el;
		el = el.offsetParent;
	} while(el != null)

	return {
		X: x,
		Y: y,
		maxX: x + orEl.offsetWidth,
		maxY: y + orEl.offsetHeight
	}
}



function _mouseUp() {
	if (_dragObject) {
		_fullScreenDivShow(0);
		var mEditor = FCKeditorAPI.GetInstance('wysiwyg') ;
		var ofs = getEditAreaPosition();
		var mousePosX = _dragObject.offsetLeft;
		var mousePosY = _dragObject.offsetTop;
		var date = new Date();
		var tmpFraimeId = "newModuleInstance_" + mousePosX + mousePosY + date.getYear() + date.getMonth() + date.getDate() + date.getHours() + date.getMinutes() + date.getSeconds() + Math.random();
		document.body.removeChild(_dragObject);
		if ((mousePosX > ofs.X) && (mousePosX < ofs.maxX) && (mousePosY > ofs.Y) && (mousePosY < ofs.maxY)) {
			mEditor.InsertHtml('<iframe id="'+tmpFraimeId+'" src="'+baseUrl+'/Wizard/Edit/Module/Preview?module=' + _dragObject.code + '&frameId='+tmpFraimeId+'" frameborder="0" style="overflow: hidden; width: 100%;" scrolling="no" onload="this.style.height=this.contentWindow.document.body.scrollHeight+\'px\';"></iframe>');
			/*
 			 * :NOTE: epalchukovsky@: Workaround for events in new Mozilla, see Bug # 101190
 			 */
			var blockModuleRegister = function() {
				var modFrame = mEditor.EditorDocument.getElementById(tmpFraimeId);
				var idControl = modFrame
					?	modFrame.contentWindow.document.getElementById("newInstnaceId")
					:	null;
				if (idControl) {
					modFrame.id = "moduleInstance_"+idControl.value;
					modFrame.src = modFrame.src.replace(/^([^\?]+\?).*$/, "$1moduleInstance="+idControl.value);
					mEditor.SetBlockModulesEvents();
					mEditor.Commands.GetCommand('SbTranslator').Execute();
				} else {
					setTimeout(blockModuleRegister, 500);
				}
			}
			setTimeout(blockModuleRegister, 500);
		}
		_dragObject = null;
	}
}
//
function _mouseMove(ev){
	ev           = ev || window.event;
	var mousePos = _mouseCoords(ev);

	if(_dragObject){
		_dragObject.style.position = 'absolute';
		_dragObject.style.top      = mousePos.y - _mouseOffset.y;
		_dragObject.style.left     = mousePos.x - _mouseOffset.x;		
		stopEvent(ev);
	}
}
//
function _mouseCoords(ev){
	if(ev.pageX || ev.pageY){
		return {x:ev.pageX, y:ev.pageY};
	}
	return {
		x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
		y:ev.clientY + document.body.scrollTop  - document.body.clientTop
	};
}
//
function _getMouseOffset(target, ev){
	ev = ev || window.event;

	var docPos    = _getPosition(target);
	var mousePos  = _mouseCoords(ev);
	return {x:mousePos.x - docPos.x, y:mousePos.y - docPos.y};
}

function _getPosition(e){
	var left = 0;
	var top  = 0;	
	do {
		left += e.offsetLeft;
		top  += e.offsetTop;
		e     = e.offsetParent;
	}
	while (e.offsetParent)
	return {x:left, y:top};
}
///////////////
/*
var logs;

function log(key, val, clear) {
	if (null != logs) {
		if (clear) {
			logs.innerHTML = '';
		}
		logs.innerHTML = logs.innerHTML + '<br>' + key  + '==' + val;
	} else {
		SbInitLogging();
	}
}

function SbInitLogging() {
	logs = document.getElementById('temp');
}
*/