var tyreShots_moveTimeout = null;
var tyreShots_scrollSpeed = 2;
var tyreShots_totalWidth = 0;
var tyreShots_barWidth = 0;
var tyreShots_currImage = '';

var tyreShots_timoutSpeed = 10;
var tyreShots_maxTimeoutSpeed = 70;
var tyreShots_currTimeoutSpeed = tyreShots_maxTimeoutSpeed;

$(document).ready(function() {
	
	tyreShots_barWidth = $('#tyre-thumbnails').width();
	
	$('#tyre-shots .items').children().each(function(){ 
		tyreShots_totalWidth += $(this).outerWidth(true);
		if (!tyreShots_currImage) {
			tyreShots_currImage = $(this).find('a').attr('href');
			tyreShots_setImage(tyreShots_currImage);
			$(this).addClass('active');
		}
		$(this).find('a').tooltip({ 
			track: false,
			showURL: false,
			left: -20 
		});
	});
	$('#tyre-shots .items').css('width', tyreShots_totalWidth);			
	tyreShots_totalWidth--;
	
	if (tyreShots_totalWidth <= tyreShots_barWidth) {
	
		$('#tyre-shots .browse').css('cursor', 'default');
		$('#tyre-shots .browse').css('background-image', 'none');
		
		$('#tyre-shots .items').children().click(function() {
			$('#tyre-shots .items').children().removeClass('active');
			$(this).addClass('active');
			newImg = $(this).find('a').attr('href');
			if (newImg != tyreShots_currImage) {
				tyreShots_currImage = newImg;
				tyreShots_setImage(tyreShots_currImage);
			}
			return false;
		});
		
		tyreShots_totalWidth += $('#tyre-shots .items .item').length -1;
		$('#tyre-shots .items').css('width', tyreShots_totalWidth + 1);
		
		$('#tyre-shots .items .item').css('border', '1px solid ' + $('#tyre-shots .items .item').css('borderRightColor'));
		$('#tyre-shots .items .item').css('borderLeft', 'none');
		$('#tyre-shots .items .item:first').css('borderLeft', '1px solid ' + $('#tyre-shots .items .item').css('borderRightColor'));
		$('#tyre-shots .items').css('marginLeft', (tyreShots_barWidth - tyreShots_totalWidth)/2)
		$('#tyre-thumbnails').css('borderColor', '#ffffff');
		$('#tyre-thumbnails').css('height', parseInt($('#tyre-thumbnails').css('height'))+2);
		$('#tyre-thumbnails').css('border', 'none');
		$('#tyre-thumbnails').css('padding', '0px 1px 2px');
		
		return;
	}
	
	tyreShots_moveRight();
	
	$('#tyre-shots .prevPage').mouseover(function(){
		tyreShots_moveRight();
		$('#tyre-shots .nextPage').removeClass('rightDisabled');
	}).mouseout(function(){
		tyreShots_currTimeoutSpeed = tyreShots_maxTimeoutSpeed;
		clearTimeout(tyreShots_moveTimeout);
	});
	
	$('#tyre-shots .nextPage').mouseover(function(){
		tyreShots_moveLeft();
		$('#tyre-shots .prevPage').removeClass('leftDisabled');
	}).mouseout(function(){
		tyreShots_currTimeoutSpeed = tyreShots_maxTimeoutSpeed;
		clearTimeout(tyreShots_moveTimeout);
	});
	
	$('#tyre-shots .items').children().click(function() {
		$('#tyre-shots .items').children().removeClass('active');
		$(this).addClass('active');
		marginLeft = parseInt($('#tyre-shots .items').css('margin-left'));
		left = $(this).position().left + marginLeft - tyreShots_barWidth/2;
		marginLeft -= left + $(this).width()/2;
		if (marginLeft >= 0) {
			marginLeft = 0;
			$('#tyre-shots .prevPage').addClass('leftDisabled');
		}
		else if (tyreShots_totalWidth + marginLeft <= tyreShots_barWidth) {
			marginLeft =  - tyreShots_totalWidth + tyreShots_barWidth;
			$('#tyre-shots .nextPage').addClass('rightDisabled');
		}
		else {
			$('#tyre-shots .prevPage').removeClass('leftDisabled');
			$('#tyre-shots .nextPage').removeClass('rightDisabled');
		}
		newImg = $(this).find('a').attr('href');
		if (newImg != tyreShots_currImage) {
			tyreShots_currImage = newImg;
			tyreShots_setImage(tyreShots_currImage);
		}
		$('#tyre-shots .items').stop().animate({marginLeft: marginLeft}, 1000, 'swing', function(){$.tooltip.forceUpdate();});
		return false;
	});
	
	$('#tyre-shots').mousewheel(function(objEvent, intDelta){
		marginLeft += (intDelta > 0) ? 24 : -24;
		if (marginLeft >= 0) {
			marginLeft = 0;
			$('#tyre-shots .prevPage').addClass('leftDisabled');
		}
		else if (tyreShots_totalWidth + marginLeft <= tyreShots_barWidth) {
			marginLeft =  - tyreShots_totalWidth + tyreShots_barWidth;
			$('#tyre-shots .nextPage').addClass('rightDisabled');
		}
		else {
			$('#tyre-shots .prevPage').removeClass('leftDisabled');
			$('#tyre-shots .nextPage').removeClass('rightDisabled');
		}
		$('#tyre-shots .items').stop().animate({marginLeft: marginLeft}, 100, 'swing', function(){$.tooltip.forceUpdate();});
		return false;
	});
	
});

function tyreShots_moveLeft() {
	clearTimeout(tyreShots_moveTimeout);
	marginLeft = parseInt($('#tyre-shots .items').css('margin-left'));
	marginLeft = isNaN(marginLeft) ? 0 : marginLeft;
	marginLeft -= tyreShots_scrollSpeed;
	if (tyreShots_totalWidth + marginLeft <= tyreShots_barWidth) {
		marginLeft =  - tyreShots_totalWidth + tyreShots_barWidth;
		$('#tyre-shots .nextPage').addClass('rightDisabled');
	}
	else {
		tyreShots_moveTimeout = setTimeout(tyreShots_moveLeft, tyreShots_getSpeed());
	}
	$('#tyre-shots .items').stop();
	$('#tyre-shots .items').css('margin-left', marginLeft + 'px');
}	

function tyreShots_getSpeed() {
	tyreShots_currTimeoutSpeed = parseInt((tyreShots_timoutSpeed + tyreShots_currTimeoutSpeed * 3) / 4);
	return tyreShots_currTimeoutSpeed;
}

function tyreShots_moveRight() {
	clearTimeout(tyreShots_moveTimeout);
	marginLeft = parseInt($('#tyre-shots .items').css('margin-left'));
	marginLeft = isNaN(marginLeft) ? 0 : marginLeft;
	marginLeft += tyreShots_scrollSpeed;
	if (marginLeft >= 0) {
		$('#tyre-shots .prevPage').addClass('leftDisabled');
		marginLeft = 0;
	}
	else {
		tyreShots_moveTimeout = setTimeout(tyreShots_moveRight, tyreShots_getSpeed());
	}
	$('#tyre-shots .items').stop();
	$('#tyre-shots .items').css('margin-left', marginLeft);
}

function tyreShots_setImage(img) {
	$('#tyre-shot img').attr('src', img);
}
