/** Copyright (c) 2006 UPT Ltd. Версия $Id: xsight.cFloatingDiv.js,v 1.8 2008/05/06 15:26:05 andy Exp $ */

/**
 * Класс плавающего контейнера
 * @class
 * @extends cObject
 */
cFloatingDiv = newClass(cObject, {

	/**
	* Объект плавающего контейнера
	* @protected
	* @type object
	*/
	_div:null,

	/**
	* Имя плавающего контейнера
	* @protected
	* @type string
	*/
	_divName:null,

	/**
	* Смещение сверху
	* @protected
	* @type integer
	*/
	_top: 0,

	/**
	* Старое смещение сверху
	* @protected
	* @type integer
	*/
	_old_top: 0,

	/**
	* Смещение слева
	* @protected
	* @type integer
	*/
	_left: 0,

	/**
	* Старое смещение слева
	* @protected
	* @type integer
	*/
	_old_left: 0,

	/**
	* Смещение сверху по-умолчанию
	* @protected
	* @type integer
	*/
	_top_offset: 0,

	/**
	* Смещение слева по-умолчанию
	* @protected
	* @type integer
	*/
	_left_offset: 0,

	/**
	* Смещение снизу по-умолчанию
	* @protected
	* @type integer
	*/
	_bottom_offset: 0,

	/**
	* Ширина по-умолчанию
	* @protected
	* @type integer
	*/
	_width: 0,

	/**
	* Флаг видимости контейнера
	* @protected
	* @type boolean
	*/
	_visible: false,

	_mouseX: 0,

	_mouseY: 0,

	_isDrag: false,

	/**
	* Конструктор класса
	* @public
	* @constructor
	* @param {string} pName имя контейнера
	* @param {integer} pWidth ширина контейнера по-умолчанию
	* @param {integer} pOffsetTop смещение сверху по-умолчанию
	* @param {integer} pOffsetLeft смещение слева по-умолчанию
	* @param {integer} pOffsetBottom смещение снизу по-умолчанию
	* @param {boolean} pVisible видимость
	*/
	constructor:function(pName, pWidth, pOffsetTop, pOffsetLeft, pOffsetBottom, pVisible)
	{
		// вызов родительского конструктора
		this.constructor.prototype.constructor.call(this);
		// инит переменных класса
		this._top_offset = pOffsetTop;
		this._left_offset = pOffsetLeft;
		this._bottom_offset = pOffsetBottom;
		this._width = pWidth;
		this._divName = pName;
		this._visible = pVisible;
		// добавление обработчика на onload
		this.fSetOnloadHandler(function(){this.fSetDiv(pName);}.bind(this));
	},

	/**
	* Метод нахождения объекта плавающего контейнера по имени
	* и установки свойства visibility в visible, а также выполнения
	* автоперемещения
	* @public
	* @param {string} pName имя контейнера
	*/
	fSetDiv:function(pName)
	{
		//alert('float_on_load');
		this._div = this.fGetObj(pName);		
		
		if (typeof(this._div != 'undefined'))
		{
			this.fSetDivVisibility(this.fGetDivIsVisible());

			this.fAddHandler(
				this._div,
				'mousemove',
				function(){this.fOnMouseXY();}.bind(this)
			);


			/*this.fAddHandler(
				this._div,
				'mouseover',
				function(){this.fOnMouseOver();}.bind(this)
			);*/

			this.fAddHandler(
				this._div,
				'mouseout',
				function(){this.fOnMouseOut();}.bind(this)
			);

			this.fAddHandler(
				this._div,
				'mouseup',
				function(){this.fOnMouseUp();}.bind(this)
			);

			this.fAddHandler(
				this._div,
				'selectstart',
				function(){
						event.returnValue = false;
						return false;
				}.bind(this)
			);

			//cFloatingDiv.fStaticScroll(this);

			with (this._div)
			{
	            style.setExpression(
	            	"left",
	            	"scrollLeft-scrollLeft+((document.compatMode && document.compatMode != 'BackCompat')?document.documentElement.scrollLeft:document.body.scrollLeft)+"
	            	+ this._left_offset,
	            	"JavaScript"
	            );
	            style.setExpression(
	            	"top",
	            	"scrollTop-scrollTop+((document.compatMode && document.compatMode != 'BackCompat')?document.documentElement.scrollTop:document.body.scrollTop)+"
	            	+ this._top_offset,
	            	"JavaScript"
	            );
	            
	            style.setExpression(
	            	"height",
	            	"scrollTop-scrollTop+((document.compatMode && document.compatMode != 'BackCompat')?document.documentElement.clientHeight:document.body.clientHeight)-" +
	            	(parseInt(this._top_offset) + parseInt(this._bottom_offset) + 2),
	            	"JavaScript"
	            );
			}

		}
	},

	/**
	* Метод установки видимости плавающего контейнера
	* @public
	* @param boolean pVisible флаг видимости
	*/
	fSetDivVisibility:function(pVisible)
	{
		var v = (pVisible == true)?'visible':'hidden';
		var d = (pVisible == true)?'':'none';
		var th = this;

		setTimeout(
			function() {
				th._div.style.visibility = v;
				th._visible = pVisible;
			},
			10
		);
	},

	/**
	* Метод получения значения флага видимости контейнера
	* @public
	* @return boolean состояние флага
	*/
	fGetDivIsVisible:function()
	{
		return this._visible;
	},

	fOnMouseXY:function(e)
	{
		var e = e || window.event;
		try
		{
			this._mouseX = e.clientX - ((document.documentElement)?document.documentElement.scrollLeft:document.body.scrollLeft);
			this._mouseY = e.clientY - ((document.documentElement)?document.documentElement.scrollTop:document.body.scrollTop);
		}
		catch(er)
		{
			this._mouseX = e.pageX;
			this._mouseY = e.pageY;
		}
		this.fOnMouseOver();

		if (e.button == 1)
		{
			if (typeof(this._div.setCapture) != 'undefined')
			this._div.setCapture();
			this.fDrag();
		}
	},

	fOnMouseOver:function()
	{
		if (this._width + this._left - 5 <= this._mouseX)
		this._div.style.cursor = "w-resize";
		else
		this._div.style.cursor = "default";
	},

	fOnMouseOut:function()
	{
		this._div.style.cursor = "default";
	},

	fOnMouseUp:function()
	{
		if (typeof(this._div.releaseCapture) != 'undefined')
		this._div.releaseCapture();
		this._isDrag = false;
	},

	fDrag:function()
	{
		if (this._mouseX >= 50 &&
			(this._div.style.cursor == 'w-resize' || this._isDrag)
		)
		{
			this._isDrag = true;
			this._width = this._mouseX + this._left;
			this._div.style.width = this._width + 'px';
		}
	},

	/**
	* Метод позиционирования плавающего контейнера
	* @public
	*/
	fMoveDiv:function()
	{
		if (this._isDrag) return;

		var pos_y = this.fGetViewportOffsetY();
		var pos_x = this.fGetViewportOffsetX();
		var height = this.fGetViewportHeight();
		var div_height = (height && (height - this._top_offset - this._bottom_offset)>0)?((height - this._top_offset - this._bottom_offset) + 'px'):this._div.style.height;

        if (pos_y < this._top)
        	pos_y = this._top;
        else
        	pos_y += this._top_offset;

        if (pos_x < this._left)
        	pos_x = this._left;
        else
        	pos_x += this._left_offset;

        if (pos_y == this._old_top)
			this._div.style.top = pos_y;

        if (pos_x == this._old_left)
			this._div.style.left = pos_x;

		if (div_height)
			this._div.style.height = div_height;

		if (this._width > 0)
			this._div.style.width = this._width;

        this._old_top = pos_y;
        this._old_left = pos_x;
	},

	/**
	* Метод показа контейнера
	* @public
	*/
	fShow:function()
	{
		this._div.style.display = '';
	},

	/**
	* Метод прятания контейнера
	* @public
	*/
	fHide:function()
	{
		this._div.style.display = 'none';
	},

	/**
	* Метод уменьшения ширины контейнера
	* @public
	* @param {integer} pStep шаг
	*/
	fDecrementWidth:function(pStep)
	{
		var width = this._div.style.pixelWidth;
		if (width - pStep > pStep)
		{
			this._width = width - pStep;
			this._div.style.width = this._width + 'px';
		}
	},

	/**
	* Метод увеличения ширины контейнера
	* @public
	* @param {integer} pStep шаг
	*/
	fIncrementWidth:function(pStep)
	{
		var width = this._div.style.pixelWidth;
		if (width + pStep < this.fGetViewportWidth())
		{
			this._width = width + pStep;
			this._div.style.width = this._width + 'px';
		}
	},

	/**
	* Метод установки ширины контейнера
	* @public
	* @param integer pWidth ширина
	*/
	fSetWidth:function(pWidth)
	{
		this._width = pWidth;
		this._div.style.width = this._width + 'px';
	}
});

/**
* Глобальный массив объектов для анимации
* @type object
*/
cFloatingDiv.StaticDivObject = [];

/**
* Глобальная функция перепозиционирования плавающих контейнеров
* через setTimeOut()
*/
cFloatingDiv.fStaticScroll = function()
{
	if (typeof(arguments[0]) != 'undefined')
	{
		var obj = arguments[0];
		var name = obj._divName;
		cFloatingDiv.StaticDivObject[name] = obj;
	}
	for(var name in cFloatingDiv.StaticDivObject)
	{
		cFloatingDiv.StaticDivObject[name].fMoveDiv();
	}
	setTimeout('cFloatingDiv.fStaticScroll()', 100);
}
