/*
// 2007-09-14
// Copyright (c) Art. Lebedev | http://www.artlebedev.ru/
// Author - Vladimir Tokmakov
*/

if( !window.Overflow ){
	var Overflow = {
		oCurrent : false
	}
}

Overflow.Drag = {
	bInited: false
	, bDisabled : false
	, hMouse : false
	, hThis : false

	, Object : function( eOn, hParams ){
		this.create( eOn, hParams );
		if( !Overflow.Drag.bInited ){
			Overflow.Drag.init();
			Overflow.Drag.bInited = true;
		}
	}

	, init : function(){
		Common.Event.add( document, 'mouseup', function(){
			Overflow.Drag.finish();
		} );
	}

	, finish : function(){
		if( Overflow.oCurrent ){
			Overflow.oCurrent.eThis.style.cursor = '';
			Overflow.oCurrent.eParent.style.cursor = '';
			Overflow.oCurrent = false;
			Common.Event.remove( document, 'selectstart', function(){ return false; } );
		}
	}

}

Overflow.Drag.Object.prototype.create = function( eOn, hParams ){
	if( !eOn ){
		return this;
	}
	var oThis = this;
	oThis.eThis = eOn;
	oThis.eThis.style.position = 'absolute';
	if( hParams ){
		oThis.eParent = hParams.eParent ? hParams.eParent : oThis.eThis.parentNode;
	}else{
		oThis.eParent = oThis.eThis.parentNode;
	}
	if( oThis.eParent.style.position != 'absolute' ){
		oThis.eParent.style.position = 'relative';
	}
	oThis.eParent.style.overflow = 'hidden';

	for( var i = 0 ; i < oThis.eThis.childNodes.length ; i++ ){
		if( oThis.eThis.childNodes[i].nodeType != 3 && !oThis.eThis.childNodes[i].tagName.match( /img/i ) ){
			Common.Event.add( oThis.eThis.childNodes[i], 'mousedown', function( oEvent ){
				Overflow.Drag.bDisabled = true;
			} );
			Common.Event.add( oThis.eThis.childNodes[i], 'mouseup', function( oEvent ){
				Overflow.Drag.bDisabled = false;
			} );
		}
	}

	Common.Event.add( oThis.eThis, 'dragstart', function(){ return false; } );

	Common.Event.add( oThis.eThis, 'mousedown', function( oEvent ){
		if( !Overflow.Drag.bDisabled ){
			Overflow.oCurrent = oThis;
			Overflow.Drag.hMouse = Common.Event.getAbsoluteCoords( oEvent || window.event );
			Overflow.Drag.hThis = { iLeft : oThis.eThis.offsetLeft, iTop : oThis.eThis.offsetTop };
			Common.Event.add( document, 'selectstart', function(){ return false; } );
			Common.Event.cancel( oEvent );
		}
	} );

	/*Common.Event.add( oThis.eParent, 'mousemove', function( oEvent ){
		var hMouse = Common.Event.getAbsoluteCoords( oEvent || window.event );
		var hParent = Common.Dom.getAbsoluteCoords( oThis.eParent );
		if(
			hMouse.iTop < hParent.iTop + 20
			|| ( hMouse.iTop > hParent.iTop + oThis.eParent.offsetHeight - 20 )
			|| hMouse.iLeft < hParent.iLeft + 20
			|| ( hMouse.iLeft > hParent.iLeft + oThis.eParent.offsetWidth - 20 )
		){
			oThis.eParent.style.cursor = 'move';
		}else{
			oThis.eParent.style.cursor = '';
		}
	} );*/
	
	Common.Event.add( document, 'mousemove', function( oEvent ){
		if( Overflow.oCurrent ){
			/*oThis.eThis.style.cursor = 'move';
			oThis.eParent.style.cursor = 'move';*/
			var hMouse = Common.Event.getAbsoluteCoords( oEvent || window.event );
			var iX = Overflow.Drag.hThis.iLeft + hMouse.iLeft - Overflow.Drag.hMouse.iLeft;
			var bCan = Overflow.oCurrent.can();
			if( iX <= 0 && iX >= Overflow.oCurrent.eParent.offsetWidth - Overflow.oCurrent.eThis.offsetWidth ){
				Overflow.oCurrent.eThis.style.left = iX;
			}else if( bCan ){
				Overflow.oCurrent.eThis.style.left = iX >= 0 ? 0 : Overflow.oCurrent.eParent.offsetWidth - Overflow.oCurrent.eThis.offsetWidth;
			}
			var iY = Overflow.Drag.hThis.iTop + hMouse.iTop - Overflow.Drag.hMouse.iTop;
			if( iY <= 0 && iY >= Overflow.oCurrent.eParent.offsetHeight - Overflow.oCurrent.eThis.offsetHeight ){
				Overflow.oCurrent.eThis.style.top = iY;
			}else if( bCan ){
				Overflow.oCurrent.eThis.style.top = iY >= 0 ? 0 : Overflow.oCurrent.eParent.offsetHeight - Overflow.oCurrent.eThis.offsetHeight;
			}
		}
	} );

	Common.Event.add( oThis.eThis, 'mouseup', function(){
		Overflow.Drag.finish();
	} );

	return this;
}


Overflow.Drag.Object.prototype.can = function(){
	return ( this.eThis.offsetWidth > this.eParent.offsetWidth || this.eThis.offsetHeight > this.eParent.offsetHeight );
}