var recBar_moveTimeout = null;
var recBar_totalWidth = 0;
var recBar_barWidth = 0;

var recBar_maxTimeoutSpeed = 55;//  scrollSpeed (maxTimeoutSpeed is old name) established number of pixels in one move (timeout)
var recBar_minScrollSpeed = 5;// starting number of pixels in one move
var recBar_timoutSpeed = 180;//180 timeout for mouseover
var recBar_acceleration = 4;//less - faster going to scrollSpeed
var recBar_currTimeoutSpeed = recBar_maxTimeoutSpeed;

var recBar_img = null;

$('.scroller').ready(function() {
	recBar_barWidth = $('.horizontal-slider .scroller').width();
	$('.horizontal-slider .items .item:last').css('background', 'none');
	$('.horizontal-slider .items .item:last').css('padding-right', 
		parseInt( $('.horizontal-slider .items .item:last').css('padding-right') ) - 1
	);
	$('.horizontal-slider .items').children().each(function(){ recBar_totalWidth += $(this).outerWidth(true); });
	$('.horizontal-slider .items').css('width', recBar_totalWidth + 1);
	recBar_totalWidth--;


	if (recBar_totalWidth <= recBar_barWidth) {
		$('.horizontal-slider .prev').hide();
		$('.horizontal-slider .next').hide();
		$('.horizontal-slider .items').children().click(function() {
			$('.horizontal-slider .items').children().removeClass('active');
			$(this).addClass('active');
		});
		return;
	}

	recBar_moveRight();

	$('.horizontal-slider .items a').tooltip({
		track: false,
		showURL: false,
		left: -20,
		top: 5
	});

	$('.horizontal-slider .prev').mouseover(function(){
		recBar_moveRight();
	}).mouseout(function(){
		recBar_currTimeoutSpeed = recBar_maxTimeoutSpeed;
		clearTimeout(recBar_moveTimeout);
	});

	$('.horizontal-slider .next').mouseover(function(){
		recBar_moveLeft();
	}).mouseout(function(){
		recBar_currTimeoutSpeed = recBar_maxTimeoutSpeed;
		clearTimeout(recBar_moveTimeout);
	});
	
	$('.horizontal-slider .next, .horizontal-slider .prev').click(function(){
		return false;
	});

	if (!$('.horizontal-slider').hasClass('not-selectable')) {
		$('.horizontal-slider .items').children().click(function() {
			$('.horizontal-slider .items').children().removeClass('active');
			$(this).addClass('active');
			marginLeft = parseInt($('.horizontal-slider .items').css('margin-left'));
			left = $(this).position().left + marginLeft - recBar_barWidth/2;
			marginLeft -= left + $(this).width()/2;
			if (marginLeft >= 0) {
				marginLeft = 0;
			}
			else if (recBar_totalWidth + marginLeft <= recBar_barWidth) {
				marginLeft =  - recBar_totalWidth + recBar_barWidth;
			}
			if (recBar_img) {
				recBar_img.onload = null;
			}
			$('.horizontal-slider .items').stop().animate({marginLeft: marginLeft}, 1000, function(){$.tooltip.forceUpdate();});
			if ($('.horizontal-slider').hasClass('popup')) {
				recBar_img = new Image();
				var jItem = this;
				recBar_img.onload = function() {
					var link = $('<div>').append($('a:last', jItem).clone().show()).remove().html();
					recBar_boxy = new Boxy(
						'<div><p><img src="' + $('a:first', jItem).attr('href') + '" height="' + this.height + '"  width="' + this.width +'" />' + 
						'</p><p class="button-next" style="text-align: right">' + link + '</p></div>', {
							title: "&nbsp;",
							modal: true,
							closeText: getCloseText()
						}
					);
				};
				recBar_img.src = $('a:first', jItem).attr('href');
			}
			return false;
		});
	}

	$('.horizontal-slider').mousewheel(function(objEvent, intDelta){
		marginLeft += (intDelta > 0) ? 40 : -40;
		if (marginLeft >= 0) {
			marginLeft = 0;
		}
		else if (recBar_totalWidth + marginLeft <= recBar_barWidth) {
			marginLeft =  - recBar_totalWidth + recBar_barWidth;
		}
		$('.horizontal-slider .items').stop().animate({marginLeft: marginLeft}, 100, function(){$.tooltip.forceUpdate();});
		return false;
	});

	$('#tyres-intro-content .horizontal-slider .item').hover( function(){
		if (marginLeft < 0) {
			var widthItems = 0;
			var count = $('.item').length;
			for (var i = parseInt($(this).attr('rel')); i<count; i++) {
				widthItems += parseInt($('.item[rel="' + i + '"]').width());
			}
			if ((widthItems + 50*count) > parseInt($('.scroller').width())) {
				var movePosition = (widthItems + 50*count) - parseInt($('.scroller').width());
				var margin = marginLeft  + movePosition;
				$('.horizontal-slider .items').stop().animate({marginLeft: margin}, 400, function(){$.tooltip.forceUpdate();});
			}
		}
	});

});

function recBar_moveLeft() {
	clearTimeout(recBar_moveTimeout);
	marginLeft = parseInt($('.horizontal-slider .items').css('margin-left'));
	marginLeft = isNaN(marginLeft) ? 0 : marginLeft;
	var scrollSpeed = recBar_getSpeed();
	marginLeft -= recBar_maxTimeoutSpeed-scrollSpeed;
	if (recBar_totalWidth + marginLeft <= recBar_barWidth) {
		marginLeft = - recBar_totalWidth + recBar_barWidth;
	} else {
		recBar_moveTimeout = setTimeout(recBar_moveLeft, recBar_timoutSpeed);
	}
	$('.horizontal-slider .items').stop().animate({marginLeft: marginLeft}, recBar_timoutSpeed, 'linear');
}

function recBar_getSpeed() {
	recBar_currTimeoutSpeed = parseInt((recBar_minScrollSpeed + recBar_currTimeoutSpeed * recBar_acceleration) / (recBar_acceleration+1) );
	return recBar_currTimeoutSpeed;
}

function recBar_moveRight() {
	clearTimeout(recBar_moveTimeout);
	marginLeft = parseInt($('.horizontal-slider .items').css('margin-left'));
	marginLeft = isNaN(marginLeft) ? 0 : marginLeft;
	var scrollSpeed = recBar_getSpeed();
	marginLeft += recBar_maxTimeoutSpeed-scrollSpeed;
	if (marginLeft >= 0) {
		marginLeft = 0;
	} else {
		recBar_moveTimeout = setTimeout(recBar_moveRight, recBar_timoutSpeed);
	}
	$('.horizontal-slider .items').stop().animate({marginLeft: marginLeft}, recBar_timoutSpeed, 'linear');
}

function getParams(link) {
	var params = new Object;
	var paramString = link.substring(link.indexOf('?')+1)
	if (paramString) {
		var singleParams = paramString.split('&');
		for (i = 0; i < singleParams.length; i++) {
			var keyValue = singleParams[i].split('=');
			params[keyValue[0]] = keyValue[1];
		}
	}
	return params;
}
