var app = function()
{
	var SLIDE_WIDTH = 1003;
	var slides_count = 0;
	var width = 0;
	var pos = 0;
	var fx = null;
	var in_move = false;
	var prev_slide_num = 0;
	var is_first = true;

	function update_href(pos)
	{
		var slide_num = Math.round((width - SLIDE_WIDTH - pos) / SLIDE_WIDTH);
		prev_slide_num = slide_num;

		window.location.hash = '#slide-' + slide_num;
		is_first = false;
	}

	function monitor_href()
	{
		if (in_move) return;
		if (!slides_count) return;

		var mt = window.location.hash.match(/#slide\-([0-9])/);

		if (mt || window.location.hash=='')
		{
			var slide_num = (window.location.hash=='' ? 1 : mt[1]);

			if (slide_num != prev_slide_num)
			{
				prev_slide_num = slide_num;
				var newpos = - (slide_num - 1) * 1003;

				if (newpos > 0) return;
				if (newpos < -(width-SLIDE_WIDTH)) return;

				pos = newpos;

				if (is_first)
				{
					$('slides').style.left = String(pos) + 'px';
					is_first = false;
				}
				else
				{
					in_move = true;
					fx.start({ 'left': pos });
				}
			}
		}
	}

	function slide_left()
	{
		if (in_move) return false;
		if (pos >= 0) return false;

		pos += SLIDE_WIDTH;
		in_move = true;
		fx.start({ 'left': pos });
		update_href(pos);

		return false;
	}

	function slide_right()
	{
		if (in_move) return false;
		if (pos <= -(width-SLIDE_WIDTH)) return false;

		pos -= SLIDE_WIDTH;
		in_move = true;
		fx.start({ 'left': pos });
		update_href(pos);

		return false;
	}

	function slide_to()
	{
		if (in_move) return false;
		if (!this.getAttribute('slideto')) return false;

		var newpos = - (Number(this.getAttribute('slideto')) - 1) * 1003;
		if (newpos == pos) return false;

		pos = newpos;
		in_move = true;
		fx.start({ 'left': pos });
		update_href(pos);

		return false;
	}

	function on_complete()
	{
		in_move = false;
	}

	function init()
	{
		var slides_els = $$('#slides .slide');

		slides_count = slides_els.length;
		width = slides_count * SLIDE_WIDTH;
		pos = 0;

		if (!slides_count) return;

		$('slides').style.width = width + 'px';

		fx = $('slides').effects({ duration: 500, onComplete: on_complete })

		$A($$('.slide-lt')).each(function(el) { el.onclick = slide_left; });
		$A($$('.slide-rt')).each(function(el) { el.onclick = slide_right; });
		$A($$('.slide-to')).each(function(el) { el.onclick = slide_to; });

		setInterval(monitor_href, 500);
	}

	window.addEvent('domready', init);
	return null;
}();

function track_it(url)
{
	if (typeof(pageTracker)!='undefined' && typeof(pageTracker._trackPageview)!='undefined') {
		pageTracker._trackPageview(url);
	}
}
