/*
* Mover class for PowerPHP
* 
* ver 0.7
* Histroy:
*	0.7	24.07.09/goshi	using pseudo var sel instead of selElement
*	0.6	23.07.09/goshi	using MouseX instead element position
*	0.5	23.07.09/goshi	add callback, add drop rules etc.
*	0.01	06.07.09/goshi	...
*/

function objMoverController(params){
	this._init(params);
	this._objid = objMoverController.Instances.length;
	objMoverController.Instances[this._objid] = this;
}

objMoverController.prototype = {

	_SelectedItem : null,
	_SelectedX : null,
	_SelectedY : null,
	_MouseX : null,
	_MouseY : null,
	_MoveEnabled : null,
	_OverEnabled : null,
	
	_wndWidth : 0,
	_wndHeight : 0,
	
	// parameter for outer bound for the acceptor
	_bound : 10,
	
	_holder: null, 
	
	// object for manipulating
	_oContainer : null,
	_oLeftFrame : null,
	_oMover : null,

	_allItems: null,
	// transparent object for good moving
	_oTranspDiv : null,
	
	// callback function
	_callback : null,
	
	// aceptor for move
	_oAcceptor: null,
	_oAcceptorClass : null,
	_oAcceptorPos : {},
	
	// id of the instance
	_objid : null,
	
	_init : function (params){
			
		with (this){
			// getting client width
			_wndWidth = document.body.clientWidth;
			_wndHeight = document.body.clientHeight;
			
			// prepare main containers
			_oContainer = params.container;
			//_oLeftFrame = params.left_frame;
			_oMover = params.splitter;
			//_oRightFrame = params.right_frame;
			_oAcceptor = $_(params.acceptor);
			_oAcceptorClass = _oAcceptor.className;
						
			_holder = params.holder;
				
			if (typeof params.callback == "function"){
				_callback = params.callback;
			}
		}
		
		// restore position of the splitter
		//  getting cookie value
		/*var _left = portal.storage.get('pphp_spltr_' + this._oMover.id);
		if (_left){
			this._oLeftFrame.style.width = _left + 'px';
			this._oMover.style.left = _left + 'px';
		}*/
					
		// setting resizer and setting up autoupdate
		var athis = this;
				
		portal.ready(function (){
		
			athis._allItems = new Array();
			
			if (typeof params.holder != "undefined" && params.holder){
				var i = 0;
				var obj = $_(params.holder+'_'+i);
				var num = 0;
				
				/*while (obj != "undefined" && obj){
					
					num = athis._allItems.length;
					
					athis._allItems[num] = obj;
					//athis._attachListener(num);
					i++;
					
					obj = $_(params.holder+'_'+i);
				}*/
			
			}
		
			//  attach to window
			//portal.events.attach(window, "resize", function (evt){
			//	athis.resizeFrames(athis._objid);
			//});
			// attach to spliter
			if (document.attachEvent)
				// for IE attach to the current document
				portal.events.attach(document, "mouseup", function (evt){
					athis.remove(evt, athis);
				});
			else
				// attach to document window mouse up - because there is bug in Opera and sometimes in other browsers
				portal.events.attach(window, "mouseup", function (evt){
					athis.remove(evt, athis);
				});
			
			
			
			// finally - resize frames
			//athis.resizeFrames(athis._objid);
		});
			
	},
	
	_attachListener: function (objnum){
		var athis = this;
		portal.events.attach(athis._allItems[objnum], "mousedown", function (evt){
			
			athis.prepare(evt, objnum);
		});
	},
	
	// function toggle height of the frames
	toggle : function(top,bot,param){
		var add = 0;
		if (top == '0'){
			//this._oLeftFrame.style.display="none";
		}else
			this._oLeftFrame.style.display="";
				
		if (bot == '0'){
			top = this._wndWidth - this._oMover.offsetWidth + 'px';
			// adding some width for fucking browsers
			add = 2;
		} else
			this._oRightFrame.style.display="";
		
		this._oLeftFrame.style[param] = top;
		this._oRightFrame.style[param] = bot;
				
		this._oMover.style.left = this._oLeftFrame.offsetWidth - add + 'px';
		
		this.resizeFrames(this._objid);
		
		portal.storage.set('pphp_spltr_' + this._oMover.id, parseInt(this._oMover.style.left));
		
	
	},
	
	_getAbsPosition : function(obj){
		var x = 0,y = 0;
		while(obj) { 
			x += obj.offsetLeft; 
			y += obj.offsetTop;
			obj = obj.offsetParent; 
		}
		return  {'x' : x, 'y' : y};
	},	
	
	drag : function(event){
		
		if (!this._MoveEnabled) return false;
		
		var clientX = event ? event.clientX : window.event.clientX;
		var clientY = event ? event.clientY : window.event.clientY;
		
		this._SelectedItem.style.left = this._SelectedX + (clientX - this._MouseX) + 'px';
		// move invisible layer
		//this._oTranspDiv.style.left = this._SelectedX + (clientX - this._MouseX) - 100 + 'px';
		this._SelectedItem.style.top = this._SelectedY + (clientY - this._MouseY) + 'px';
		
		// check for move over acceptor
		var pos = this._getAbsPosition(this._SelectedItem);
		//$_('selChannelsResults').innerHTML = (pos.x + 'x' + pos.y + ':  ' + (this._oAcceptorPos.x) + '---' + (this._oAcceptorPos.x+this._oAcceptor.offsetWidth) + 'x' + (this._oAcceptorPos.y) + '---' + (this._oAcceptorPos.y+this._oAcceptor.offsetHeight));
		
		if (pos.y >= this._oAcceptorPos.y-this._bound && pos.y <= this._oAcceptorPos.y+this._oAcceptor.offsetHeight+this._bound &&
			pos.x >= this._oAcceptorPos.x-this._bound && pos.x <= this._oAcceptorPos.x+this._oAcceptor.offsetWidth+this._bound){
			this._oAcceptor.className = this._oAcceptorClass + ' bounded';
			this._OverEnabled = true;
		} else {
			this._oAcceptor.className = this._oAcceptorClass;
			this._OverEnabled = false;
		}
		
		
		return false;
	},
	
	drop : function(event){
		// turn on frames after toggle off
		//this._oLeftFrame.style.display = "";
		//this._oRightFrame.style.display = "";
		//portal.storage.set('pphp_spltr_' + this._oMover.id, parseInt(this._oMover.style.left));
		// check -  if we drag on the table - then connect it!
		if (this._OverEnabled){
			var el = elem('li', false, false, false);
			el.appendChild(this._SelectedItem);
			this._oAcceptor.appendChild(el);
			this._SelectedItem.style.left = 0+'px';
			this._SelectedItem.style.top = 0+'px';
			
			if (this._callback != null)
				this._callback('add');
		} else {
			// if base conainer enabled
			// check for selElement
			var sel = selElement || 0;
			if ($_('chnl_'+sel)){
				// check -if base position is full
				if ($_('chnl_'+sel).firstChild.firstChild != null && $_('chnl_'+sel).firstChild.firstChild.nodeName == 'INS'){
					// destroy current element
					this._SelectedItem.parentNode.removeChild(this._SelectedItem);
					this._SelectedItem = null;
				} else {
					$_('chnl_'+sel).firstChild.appendChild(this._SelectedItem);
										
					this._SelectedItem.style.left = 0+'px';
					this._SelectedItem.style.top = 0+'px';
					
				}
			} else {
			// destroy element
				this._SelectedItem.parentNode.removeChild(this._SelectedItem);
				this._SelectedItem = null;
			}
			if (this._callback != null)
				this._callback('remove');
			
		}
		
		if (this._MoveEnabled){
			portal.events.remove(document, "mousemove", document['mover_drag']);
			portal.events.remove(document, "mouseup", document['mover_drop']);
		}

		this._oAcceptor.className = this._oAcceptorClass;
		this._OverEnabled = false;
		document.onmousemove = null;
		document.onmouseup = null;
		this._MoveEnabled = false;
	},
	
	prepare : function(event, objnum){
		
		this._SelectedItem = $_(this._holder+'_'+objnum);//objMoverController.Instances[0]._allItems[objnum];
		if (this._SelectedItem){
			
			this._MouseX = event.clientX;
			this._MouseY = event.clientY;
			
			// now using relative coordinates
			
			if (this._SelectedItem.parentNode.nodeName == 'SPAN'){
				this._oAcceptorPos = this._getAbsPosition(this._oAcceptor);
				//this._SelectedX = this._SelectedItem.parentNode.parentNode.offsetLeft + 36; // get from CSS
				this._SelectedX = this._MouseX - $_('mainLayout').offsetLeft; // get from CSS
				this._SelectedY = this._SelectedItem.parentNode.parentNode.offsetTop + this._SelectedItem.parentNode.parentNode.parentNode.parentNode.parentNode.offsetTop;
				$_('orderContainer').appendChild(this._SelectedItem);
			} else if (this._SelectedItem.parentNode.nodeName == 'LI'){
				//alert();
				this._SelectedX = this._MouseX - $_('mainLayout').offsetLeft; // get from CSS
				this._SelectedY = this._SelectedItem.parentNode.parentNode.parentNode.parentNode.offsetTop;
				var old_parent = this._SelectedItem.parentNode;
				$_('orderContainer').appendChild(this._SelectedItem);
				// delete old parent node
				old_parent.parentNode.removeChild(old_parent);
			} else {
				this._SelectedX = this._SelectedItem.offsetLeft;
				this._SelectedY = this._SelectedItem.offsetTop;
			}
			//this._SelectedItem.style.position = 'absolute';
			this._SelectedItem.style.left = this._SelectedX + 'px';
			this._SelectedItem.style.top = this._SelectedY + 'px';
			
			// clone Node
			//var div = document.createElement("div");
			//var newNode = this._SelectedItem.cloneNode(true);
			
			//this._SelectedItem.parentNode.replaceChild(div, this.image);
			/*portal.events.attach(athis._allItems[objnum], "mousedown", function (evt){
				athis.prepare(evt, objnum);
			});*/
			//this._SelectedItem = div.firstChild;
		
			// adding transparent div
			/*this._oTranspDiv = document.createElement('div');
		     	this._oTranspDiv.id = 'trnspDiv';
		     	this._oTranspDiv.style.backgroundColor = '#fff';
			this._oTranspDiv.className = 'img-op50';
			this._oTranspDiv.style.position = 'absolute';
			this._oTranspDiv.style.top = this._oContainer.offsetTop + 'px';
			this._oTranspDiv.style.left = 0 + 'px';
			this._oTranspDiv.style.width = this._wndWidth + 'px';
			this._oTranspDiv.style.height = this._oContainer.style.height;
			this._oTranspDiv.style.zIndex = 999;	// use value < then splitter
				
			this._oContainer.insertBefore(this._oTranspDiv, this._SelectedItem.nextSibling);
			*/
			
		
			var athis = this;
			document['mover_drag'] = function (evt){
					athis.drag(evt);
			};
			document['mover_drop'] = function (evt){
					athis.drop(evt);
			}
			portal.events.attach(document, "mousemove", document['mover_drag']);
			portal.events.attach(document, "mouseup", document['mover_drop']);
	
			this._MoveEnabled = true;
			return this.stopEvent(event);
			
		}
	},
	
	stopEvent: function (event){
		if (event.preventDefault) {
			event.preventDefault();
			event.stopPropagation();
		} else {
			event.returnValue = false;
			event.cancelBubble = true;
		}
		return false;
	},
	
	remove : function(event, obj){
		// remove transparent div
		//if (obj._oTempDiv){
			//obj._oContainer.removeChild(obj._oTranspDiv);
			//obj._oTranspDiv = null;
			//obj._oLeftFrame.style.width = obj._SelectedItem.offsetLeft + 'px';
			//obj._SelectedItem.className = 'splitter';
			
			
			//this._oRightFrame.style.left = parseInt(this._SelectedItem.style.left) + parseInt(this._SelectedItem.style.width) + 1 + 'px';
			//this._oRightFrame.style.width = this._wndWidth - this._oRightFrame.style.left + 'px';
			
		//}
		
		
	}

}

objMoverController.Instances = new Array();