// JavaScript Document

$.fn.pause = function(duration) {
    $(this).animate({ dummy: 1 }, duration);
    return this;
};

$(document).ready(function(){
	var holderWidth;
	var currLeft;
	var animation=0;
	var direction;
	var smallholderHeight;
	var smallcurrTop;
	var smalldirection;
	var smallanimation=0;
	var countSmall=0;
	var timerval;
	 
	$('#main-image-slider').each(function() {
		//var count = $(this).children('a').children('img').length;
		var count = $(this).find('img').length;
		$(this).css({'width':(count*764)+'px'});
	});

	if ($('#main-image-slider-cont img').length > 1) {
		$('#main-image').each(function() {
			$(this).append('<div class="move-left"><img src="/images/race-collection/left-arrow.png" alt="move left" /></div><div class="move-right"><img src="/images/race-collection/right-arrow.png" alt="move right" /></div>');
		});
	}

	$('.small-image-slider').each(function(){
		$(this).addClass('small-slider-'+countSmall);
		countSmall++;
	});
	
	$('#main-image').mouseenter(function(){
		$('.move-left').fadeIn();
		$('.move-right').fadeIn();
		if ($.browser.msie && $.browser.version.substr(0,1)<7) {
        	$(document).pngFix(); 
		}
	}).mouseleave(function(){
		$('.move-left').fadeOut();
		$('.move-right').fadeOut();
	});
	
	$('.move-right').click(function(){
		if ($('.small-image-slider').length > 0) {
			smallholderHeight = $('.small-image-slider').height();
			smallcurrTop = $('.small-image-slider').css('top').replace('px','');
			if(smallcurrTop <= 0 && smallcurrTop > -(smallholderHeight - 175) && smallanimation==0){
				smalldirection= '-';
				moveSmallImage()
			} else {
				resetSmallImageLeft()
			}
		}
	
		holderWidth = $('#main-image-slider').width();
		currLeft = $('#main-image-slider').css('left').replace('px','');
		if(currLeft <= 0 && currLeft > -(holderWidth - 764) && animation==0){
			direction= '-';
			moveImage()
		} else {
			resetImageLeft()
		}
		timeron();
	});
	
	$('.move-left').click(function(){
		if ($('.small-image-slider').length > 0) {
			smallholderHeight = $('.small-image-slider').height();
			smallcurrTop = $('.small-image-slider').css('top').replace('px','');
			if(smallcurrTop < 0 && smallcurrTop >= -(smallholderHeight - 175) && smallanimation==0){
				smalldirection= '+';
				moveSmallImage()
			} else {
				resetSmallImageLeft()
			};
		}
	
		holderWidth = $('#main-image-slider').width();
		currLeft = $('#main-image-slider').css('left').replace('px','');
		if(currLeft < 0 && currLeft >= -(holderWidth - 764) && animation==0){
			direction= '+';
			moveImage()
		}else{
			resetImageLeft()
		}
		timeron();
	});
	
	timeron();
	
	function timeron(){
		clearInterval(timerval);
		timerval = setInterval(autoMoveImage,6000);
	} 
	
	function autoMoveImage() {
		autoMoveSmallImage();
		holderWidth = $('#main-image-slider').width();
		currLeft = $('#main-image-slider').css('left').replace('px','');
		if(currLeft <= 0 && currLeft > -(holderWidth - 764) && animation==0){
			direction= '-';
			moveImage()
		} else {
			resetImageLeft()
		}
	}
	
	function autoMoveSmallImage() {
		if ($('.small-image-slider').length > 0) {
			smallholderHeight = $('.small-image-slider').height();
			smallcurrTop = $('.small-image-slider').css('top').replace('px','');
			if(smallcurrTop <= 0 && smallcurrTop > -(smallholderHeight - 175) && smallanimation==0){
				smalldirection= '-';
				moveSmallImage()
			} else {
				resetSmallImageLeft()
			}	
		}
	}

	function resetImageLeft() {
			if(currLeft==-(holderWidth - 764) && animation==0){
			animation =	1;
			$('#main-image-slider').animate({'left':'0px'},500, function() {
				animation =0;
			});
			}
			if(currLeft==0 && animation==0){
			animation =	1;
			$('#main-image-slider').animate({'left':-(holderWidth - 764)+'px'},500, function() {
				animation =0;
			});
			}
	}
	function moveImage() {
			animation =	1;
			$('#main-image-slider').animate({'left':direction+'='+'764px'},500, function() {
				animation =0;
			});
	}
	
	function moveSmallImage() {
		if(smallcurrTop <= 0 && smallcurrTop > -(smallholderHeight - 175) && smallanimation==0){
			smallanimation =	1;
			$('.small-slider-0').pause(400).animate({'top':smalldirection+'='+'175px'},500);
			$('.small-slider-1').pause(800).animate({'top':smalldirection+'='+'175px'},500);
			$('.small-slider-2').pause(1200).animate({'top':smalldirection+'='+'175px'},500, function() {
				smallanimation =0;
			});
		}
	}
	
	function resetSmallImageLeft() {
			if(smallcurrTop==-(smallholderHeight - 175) && smallanimation==0){
			smallanimation =	1;
			$('.small-image-slider').animate({'top':'0px'},500, function() {
				smallanimation =0;
			});
			}
			if(smallcurrTop==0 && smallanimation==0){
			smallanimation =	1;
			$('.small-image-slider').animate({'top':-(smallholderHeight - 175)+'px'},500, function() {
				smallanimation =0;
			});
			}
	}

	$(document).keyup(function(event){
		if (event.keyCode == 37) {
			$('.move-left').trigger('click');
		}
		if (event.keyCode == 39) {
			$('.move-right').trigger('click');
		}
	});
	
});
