$(document).ready(function() {
	var velocity = 0.1;
	var velocity_fast = 0.3;
	velocity = 2;
	var CatFixed = $('#fixed');
	var ViewWidth = parseInt(CatFixed.width());
	var CatWidth = 0;
	var CatContainer = $('#hscroller');
	var Categories = CatContainer.find('div');
	var CategoriesCount = Categories.length;
	var startMarginLeft = 0;
	var hasSelected = false;
	Categories.each(function() {
		var w = parseInt($(this).width())+11;
		if ($(this).hasClass('selected')) {
			hasSelected = true;
		}
		if (!hasSelected) {
			startMarginLeft += w;
		}
		CatWidth += w;
	});
	if (!hasSelected) {
		startMarginLeft = 0;
	}
	var timer = window.setInterval(hscroll, 50);
	var direction = 1, prev_direction, t = 0, s = 0, offset = 5;
	if (-startMarginLeft < (ViewWidth-CatWidth)) {
		startMarginLeft = (ViewWidth-CatWidth);
		CatContainer.css('margin-left', startMarginLeft+'px');
	}
	else {
		CatContainer.css('margin-left', '-'+startMarginLeft+'px');
	}
	$('#left').hover(
		function()
		{
			offset = 10;
			direction = 0;
		},
		function()
		{
			offset = 5;
			direction = 0;
		}
	);	
	$('#right').hover(
		function()
		{
			offset = 10;
			direction = 1;
		},
		function()
		{
			offset = 5;
			direction = 1;
		}
	);
	$('#left').click(function() { return false; });
	$('#right').click(function() { return false; });
	$('#hscroller div').hover(
		function()
		{
			prev_direction = direction;
			direction = 2;
		},
		function()
		{
			 direction = prev_direction;
		}
	);	
	function hscroll() {
		if (direction==1) {
			if (CatWidth-ViewWidth > 0) {
				s = parseInt(CatContainer.css('margin-left'));
				if (s >= (ViewWidth-CatWidth)) {
					s -= offset;
					if (s < (ViewWidth-CatWidth)) {
						s = (ViewWidth-CatWidth);
					}
					CatContainer.css('margin-left', s+'px');
				}
			}
			else {
				window.clearInterval(timer);
			}
		}
		else if (direction == 0) {
			s = parseInt(CatContainer.css('margin-left'));
			if (s < 0) {
				s += offset;
				if (s > 0) {
					s = 0;
				}
				CatContainer.css('margin-left', s+'px');
			}
		}
	}
});
