/**
 * @param string | DOM element
 */
function getPageX(obj)
{
	if (!isObject(obj))
		obj = getElement(obj);
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function getPageY(obj)
{
	if (!isObject(obj))
		obj = getElement(obj);
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

/**
* @param string | DOM element
* @return int
*/
function getLeft(obj)
{
	if (!isObject(obj))
		obj = getElement(obj);
	
	var left = 0;
	if (document.defaultView) {
		var style = document.defaultView;
		var css = style.getComputedStyle(obj, "");
		left = css.getPropertyValue("left");
	} else if (obj.currentStyle) {
		left = obj.currentStyle.left;
	}
	else if (obj.style) {
		left = obj.style.left;
	}

	return parseInt(left);
}

function getTop(obj)
{
	if (!isObject(obj))
		obj = getElement(obj);
	
	var top = 0;
	if (document.defaultView) {
		var style = document.defaultView;
		var css = style.getComputedStyle(obj, "");
		top = css.getPropertyValue("top");
	} else if (obj.currentStyle) {
		top = obj.currentStyle.top;
	}
	else if (obj.style) {
		top = obj.style.top;
	}

	return parseInt(top);
}

/**
* @param string | DOM element
* @param int
* @return void
*/
function setLeft(obj, left)
{
	if (!isObject(obj))
		obj = getElement(obj);
	
	var units = (obj.style.left.indexOf('px') != -1 ? 'px' : 0);
	obj.style.left = left + units;
}

function setTop(obj, top)
{
	if (!isObject(obj))
		obj = getElement(obj);
	
	var units = (obj.style.top.indexOf('px') != -1 ? 'px' : 0);
	obj.style.top = top + units;
}

Slider = function (layerId, offset, speed, interval)
{
	this.layerId = layerId;
	
	this.offset = offset || 50;
	this.speed = speed || 3;
	this.interval = interval || 100;
}
Slider.prototype.init = function ()
{
	this.layer = getElement(this.layerId);
	this.xSlide = (this.layer.offsetWidth < this.layer.scrollWidth);
	this.ySlide = (this.layer.offsetHeight < this.layer.scrollHeight);
	if (this.xSlide || this.ySlide)
	{
		this.minX = this.layer.offsetWidth - this.layer.scrollWidth;
		this.maxX = 0;
		this.minY = this.layer.offsetHeight - this.layer.scrollHeight;
		this.maxY = 0;

		if (this.layer.captureEvents)
			this.layer.captureEvents(Event.MOUSEMOVE);
		
		var myself = this;
		
		this.layer.onmouseover = function () { myself.startSlide(); }
		this.layer.onmouseout = function () { myself.stopSlide(); }
		this.layer.onmousemove = function (e) {	myself.updateTarget(e); }
		
		this.layer.style.position = 'relative';
		
		var newHTML = '<div id="' + this.layer.id + '_wrapper" style="position:absolute;top:0px;left:0px;">' + getHTML(this.layer) + '</div>';
		setHTML(this.layer, newHTML);
		
		// reassign text to the wrapper
		this.wrapper = getElement(this.layer.id + '_wrapper');
	}
}
Slider.prototype.updateTarget = function(e)
{
	var posx = 0;
	var posy = 0;
	if (!e) var e = window.event;
	if (e.pageX || e.pageY) {
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY) {
		posx = e.clientX + document.body.scrollLeft;
		posy = e.clientY + document.body.scrollTop;
	}

	this.targetX = this.offset + (((posx - this.X) / this.layer.offsetWidth) * (this.minX - (this.offset * 2)));
	this.targetY = this.offset + (((posy - this.Y) / this.layer.offsetHeight) * (this.minY - (this.offset * 2)));
}
/**
 * @param int
 * @return void
 */
Slider.prototype.startSlide = function()
{
	if (this.xSlide)
	{
		this.X = getPageX(this.layer);
		
		var myself = this;
		function callSlideX() { myself.slideX(); }
		
		this.intervalXId = setInterval(callSlideX, this.interval);
	}
	
	if (this.ySlide)
	{
		this.Y = getPageY(this.layer);
		
		var myself = this;
		function callSlideY() { myself.slideY(); }
		
		this.intervalYId = setInterval(callSlideY, this.interval);
	}
}
/**
 * @return void
 */
Slider.prototype.stopSlide = function()
{
	if (this.intervalXId)
		clearInterval(this.intervalXId);
	if (this.intervalYId)
		clearInterval(this.intervalYId);
}
Slider.prototype.slideX = function()
{
	var newX = getLeft(this.wrapper);
	newX += (this.targetX - newX) / this.speed;
	setLeft(this.wrapper, Math.max(this.minX, Math.min(this.maxX, newX)));
}
Slider.prototype.slideY = function()
{
	var newY = getTop(this.wrapper);
	newY += (this.targetY - newY) / this.speed;
	setTop(this.wrapper, Math.round(Math.max(this.minY, Math.min(this.maxY, newY))));
}