get = function (id)
{
	return document.getElementById(id) || false;
}


blockEvent = function (event)
{
	event = event || window.event;
	if(event.stopPropagation) event.stopPropagation();
	else event.cancelBubble = true;
	if(event.preventDefault) event.preventDefault();
	else event.returnValue = false;
}


Tracer = function (elemId, shift, buttons, timeout, duration)
{
	this.canTrace = false;
	this.prepared = false;
	
	this.direction = 1;

	this.elem = get(elemId);
	this.startShift;
	this.currentPosition = 0;
	
	this.menuTrackWidth;

	this.menuWidth;

	this.defShift = shift ? shift :100;
	this.shift = shift ? shift :100;
	
	this.momentTime = 0;
	this.duration = timeout ? timeout : 25;
	this.timeout = duration ? duration : 2;

	this.dontmove = false;
	
	this.buttons = buttons;
	
	this.setShift = function ()
	{
		this.momentTime = 0;
		this.currentPosition = parseInt(this.elem.style.marginLeft);
		this.shift = Math.abs(this.defShift) * this.direction;
		if (parseInt(this.elem.style.marginLeft) + this.shift < -1 * this.menuTrackWidth)
		{
			this.shift = -1 * this.menuTrackWidth - parseInt(this.elem.style.marginLeft) + this.startShift;
		}
		if (parseInt(this.elem.style.marginLeft) + this.shift > this.startShift)
		{
			this.shift = this.startShift - parseInt(this.elem.style.marginLeft);
		}
		if (this.shift < 100  && this.shift > -100) 
		{
			this.shift = 0;
		}
		return true;
	} 	
	
	this.prepare = function ()
	{
		if(this.elem)
		{
			this.startShift = parseInt(this.elem.style.marginLeft);
			this.menuTrackWidth = this.elem.offsetWidth + this.startShift;
			this.menuWidth = this.elem.parentNode.clientWidth;
			
			this.menuTrackWidth -= this.menuWidth;
			
			this.checkButtons();
			this.prepared = true;
			this.canTrace = true;
		}
	}
	
	this.go = function (e)
	{
		if (this.prepared && this.canTrace)
		{
			this.canTrace = false;
			var target = e ? e.target : window.event.srcElement;
			this.direction = target.rel ? target.rel : target.parentNode.rel
			
			blockEvent(e);
			if (this.setShift())
			{
				thiss = this;
				a = function () {thiss.show()};
				this.interval = setInterval("a()", this.timeout);
			}
		}
	}
	
	this.checkButtons = function ()
	{
		var left = false;
		var right = false;
		this.direction = 1;
		if (this.setShift())
		{
			if (this.shift)
			{
				left = true;
			}
		}
		this.direction = -1;
		if (this.setShift())
		{
			if (this.shift)
			{
				right = true;
			}
		}
		
		for (var i = 0; i < this.buttons.length; i++)
		{
			if (this.buttons[i].rel == 1)
			{
				this.buttons[i].className = left ? 'active' : 'passive';
			}
			if (this.buttons[i].rel == -1)
			{
				this.buttons[i].className = right ? 'active' : 'passive';
			}
		}
	}
	
	this.show = function ()
	{
		if (this.momentTime++ > this.duration)
		{
			this.canTrace = true;
			clearInterval(this.interval);
			this.checkButtons();
			return true;
		}
		else
		{
			this.setPosition();
		}
	}

	this.setPosition = function() 
	{ 
		var t = this.momentTime / this.duration;
		this.elem.style.marginLeft = Math.round(-this.shift * t*(t-2) + this.currentPosition) + 'px';
		return true;
	}
	
}

window.onload = function ()
{
	var table = get('mover');
	var maintr = get('maintr');
	if (table && maintr)
	{
		if (!table.style.marginLeft) table.style.marginLeft = '-25px';
		var tds = table.getElementsByTagName('TD');
		for (var i = 0; i < tds.length; i++)
		{
			maintr.appendChild(tds[i]);
		}
		var trs = table.getElementsByTagName('TR');
		var badTrs = Array();
		for (i in trs)
		{
			if (trs[i].id !== maintr.id)
			{
				badTrs[i] = trs[i];
			}
		}
		
		for (i in badTrs)
		{
			if (badTrs[i].parentNode) badTrs[i].parentNode.removeChild(badTrs[i]);
		}
		
		var buttons = Array();
		buttons[0] = get('left');
		buttons[1] = get('right');
		get('left').rel = 1;
		get('right').rel = -1;

		
		var i = new Tracer ('mover', 273, buttons);
		i.prepare();
		
		get('left').onclick = get('right').onclick = function (e) {i.go(e)};
	}
	if (get('smile-button'))
	{
		get('smile-button').onclick = function ()
		{
			get('smile').style.display = 'block';
			get('nosmile').style.display = 'none';
			return false;
		}
	}
}

