$(document).ready(function() {
        
	// VARS
	var auto_slide_seconds = 30; // MAIN SPEED
	var normal_slide_scroll = 3;
	var normal_slide_scroll_IE8 = 3;
	var speed_slide_scroll = 10;
	var default_scroll_direction = "right";

	// IE 8 SETINTERVAL FIX
	if (navigator.appName == 'Microsoft Internet Explorer') {
		var ua = navigator.userAgent;
        	var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
		if (re.exec(ua) != null){
			ver = parseFloat(RegExp.$1);
			if (ver >= 8.0){	
				var normal_slide_scroll = normal_slide_scroll_IE8;
	}}}

	// CREATE VARS CONTAINERS
	$('#carousel_container').append('<input type="hidden" id="scroller_speed" value="" />');
	$('#carousel_container').append('<input type="hidden" id="scroller_direction" value="" />');
	$('#carousel_container').append('<input type="hidden" id="scroller_counter" value="-164" />');

	// SET VARS IN CONTAINERS
	$('#scroller_speed').val(normal_slide_scroll);
	$('#scroller_direction').val(default_scroll_direction);

	// STOP / RESUME ON HOVER
	$('#carousel_ul').hover(function(){
		clearInterval(timer);
	},function(){
		timer = setInterval('slide()', auto_slide_seconds); 
	});
  
	// HOVER FUNCTION
	$('#carousel_ul li').hover(function () {
		var a = $(this).find('img').attr("alt");
		$(this).find('img').css({ 'height' : '132px', 'width' : '200px', 'margin-top' : '-12px', 'margin-left' : '-18px', 'z-index' : '10001'});
		$(this).css("z-index", "10000");
		$(this).find('a').append('<span class="title">'+a+'</span>');
		$(this).find('.title').animate({bottom:0});
		$(this).find('.date').css("display", "inline");
	},function () {
		$(this).find('img').css({ 'height' : '108px', 'width' : '164px', 'margin-top' : '0px', 'margin-left' : '0px', 'z-index' : '101'});
		$(this).css("z-index", "100");
		$(this).find('.title').remove();
		$(this).find('.date').css("display", "none");		
  	});

	// LEFT RIGHTS
	$('#right_scroll').hover(function () { $('#scroller_direction').val("right"); });
	$('#left_scroll').hover(function () { $('#scroller_direction').val("left"); });

	// SPEED UP ON CLICK
	$('#right_scroll, #left_scroll').mouseup(function(){ $('#scroller_speed').val(normal_slide_scroll); }).mousedown(function(){ $('#scroller_speed').val(speed_slide_scroll); });

	// IPAD FIX
	$('#left_scroll, #right_scroll').bind( "touchstart", function(e){ $('#scroller_speed').val(speed_slide_scroll); });
	$('#left_scroll, #right_scroll').bind( "touchend", function(e){ $('#scroller_speed').val(normal_slide_scroll); });

	// STARTING IT UP
	var timer = setInterval('slide()', auto_slide_seconds);
});

//slide function  
function slide(){

	var direction = $('#scroller_direction').val();
	var speed = parseInt($('#scroller_speed').val());
	var left_indent = parseInt($('#carousel_ul').css('left')) + speed;
	var right_indent = parseInt($('#carousel_ul').css('left')) - speed;
				 
      	if(direction == 'left'){
		var counter = parseInt($('#scroller_counter').val()) + speed;
		if(counter > 164){
			$('#carousel_ul li:first').before($('#carousel_ul li:last'));
			var counter = 0;
			$('#carousel_ul').css('left',-164 );
		}else{
			$('#carousel_ul').css({'left' : left_indent});
		}
	}else{
      		var counter = parseInt($('#scroller_counter').val()) - speed;
		if(counter < -164){
			$('#carousel_ul li:last').after($('#carousel_ul li:first'));
			var counter = 0;
			$('#carousel_ul').css('left', -164 );
		}else{
			$('#carousel_ul').css({'left' : right_indent});
		}
       }
	
	$('#scroller_counter').val(counter);
}
