var isChanging = false;
var direction = 0;
var speed = 1;
var timer;
var totalWidth;
var ulWidth

$(function(){
	
	$numShirts = $('.slider ul.shirts li').length;
	$ulWidth = ($numShirts * 110);
	totalWidth = ($ulWidth * 3);
	ulWidth = $ulWidth;
	
	//Set the width of the BOX
	$('.slider ul.shirts').css({width: $ulWidth + 'px'});
	$('.slider .room .inner').css({width: ($ulWidth * 3) + 'px'});
	$('.slider ul.shirts').before($('.slider ul.shirts').clone());
	$('.slider ul.shirts:first').after($('.slider ul.shirts:last').clone());
	
	$('.slider .room').scrollLeft($ulWidth);
	
	//$('.slider .room').css('margin-left', '-' + $ulWidth + 'px');
	
	
	$('.slider').mousemove(function(e){	
		
		size = $(this).width();
	
		var zone = {
			1: {action: 'move', from: 0, to: 0.06 * size, direction: -1 , speed: 16},
			2: {action: 'move', from: 0.06 * size, to: 0.15 * size, direction: -1 , speed: 8},
			3: {action: 'move', from: 0.15 * size, to: 0.25 * size, direction: -1 , speed: 4},
			4: {action: 'move', from: 0.25 * size, to: 0.4 * size, direction: -1 , speed: 2},
			5: {action: 'stop', from: 0.4 * size, to: 0.6 * size},
			6: {action: 'move', from: 0.6 * size, to: 0.75 * size, direction: 1 , speed: 2},
			7: {action: 'move', from: 0.75 * size, to: 0.85 * size, direction: 1 , speed: 4},
			8: {action: 'move', from: 0.85 * size, to: 0.94 * size, direction: 1 , speed: 8},
			9: {action: 'move', from: 0.94 * size, to: size, direction: 1 , speed: 16}
		}
		
		x = e.pageX - this.offsetLeft;
				
		for (i in zone) {
			if (x >= zone[i].from && x < zone[i].to) {
				if (zone[i].action == 'move') {
					clearTimeout(timer);
					direction = zone[i].direction;
					isChanging = true;
					speed = zone[i].speed;	
					
					moveIt();			
				} else {
					clearTimeout(timer);
					isChanging = false;
					direction = 0;
					speed = 1;
				}
			}
		}
		
		
	});
	
	$('.slider').mouseout(function(){
		clearTimeout(timer);
		isChanging = false;
		direction = 0;
		speed = 1;	
	});
	
});

$('.slider a').live('mouseover', function(){
	$(this).parent().children('.clubName').stop().show();
});
$('.slider a').live('mouseout', function(){
	$('.clubName').stop().hide();
});

function moveIt(){ 

	if (isChanging == false) {return;}
	
	scrollLeft = $('.slider .room').scrollLeft();
	
	//$('.strapline h2').text(totalWidth + ' - ' + scrollLeft);
	
	if (scrollLeft == 0) {
		$('.slider .room').scrollLeft(ulWidth);
		scrollLeft = $('.slider .room').scrollLeft();
	} else if (scrollLeft == (totalWidth - 671)) {
		$('.slider .room').scrollLeft(-ulWidth);
		scrollLeft = $('.slider .room').scrollLeft();
	}
	
	scrollLeft += (direction * speed);

	$('.slider .room').scrollLeft(scrollLeft);
	
	
	
	timer = setTimeout('moveIt();', 50);

}


