var t;
var timer_is_on=0;



$(function() {
    init_slider();
	d=setTimeout("doTimer()",6000);
});



function init_slider() {
	// thumbs selectors
	var thumbs = $('.slider-thumbs li');
	var thumbs_ul = thumbs.parent();
	var thumbs_width = thumbs.eq(0).outerWidth();
	
	// big slide selectors
	var big_slides = $('.slider-container li');
	
	var i=0;
	big_slides.each(function(){
		$(this).attr('_index', i);
		i++;
	});
	
	var big_ul = big_slides.parent();
	var big_width = big_slides.eq(0).outerWidth();
	
	// indexes
	var num_show = 6, active_pos = 0, index = 0, num_slides = big_slides.length;
	
	var active_span = $('<span class="active"></span>').appendTo(thumbs_ul.parent());
	
	// clone the last thumbs and place them on the first place
	var num_to_clone = 1;
	for( var i=0; i<num_to_clone; i++ ){
		//thumbs_ul.prepend(thumbs_ul.find('li:last').remove());
		big_ul.prepend(big_ul.find('li:last').remove());
		index++;
	}
	
	// set width
	thumbs.parent().css('width', thumbs_width * num_slides);
	big_slides.parent().css('width', big_width * num_slides);
	
	// set left
	//thumbs.parent().css('left', -1 * index * thumbs_width);
	big_slides.parent().css('left', -1 * index * big_width);
	
	// append overlays
	var overlay_left = $('<span class="overlay" style="left:0"></span>').appendTo($('.slider-container')).css('opacity', 0.75);
	var overlay_right = $('<span class="overlay" style="right:0"></span>').appendTo($('.slider-container')).css('opacity', 0.75);
	set_overlay_width();
	$(window).resize(set_overlay_width);
	
	function get_overlay_width(){
		return ($(window).width() - big_width) / 2;
	}
	function set_overlay_width(){
		$('.slider-container .overlay').css('width', get_overlay_width());
	}
	
	// buttons events
	$('#slider .next').live('click', function() { next_slide(); return false; });
	$('#slider .prev').live('click', function() { prev_slide(); return false; });
	
	// thumbs click events
	thumbs.live('mouseup', function(){
		var thumbs_index = $(this).parent().find('li').index(this);
		
		
		index = big_slides.parent().find('li').index( big_slides.parent().find('li[_index='+thumbs_index+']') );
		
		
		//console.log(index2);
		var num_invisible = Math.abs(parseInt(thumbs_ul.css('left').replace('px', ''))) / thumbs_width;
		
		active_pos = thumbs_index-num_invisible;
		
		//move_active_span();
		//show_big_slide();
		
		active_pos--;
		index--;
		next_slide();
		
		//console.log(index);
		

		
		return false;
		
		
	});
	
	 activeThumbnail(active_pos);
	
	// show next slides
	function next_slide(){
		if(active_pos < num_show-1 ) active_pos++;
		else active_pos = 0;
	activeThumbnail(active_pos);
		
		if( index == num_slides-2 ){
			
			// clone the first thumb and put it on the last place
			/*
			var thumb_clone = thumbs_ul.find('li:first').remove();
			var new_left = parseInt(thumbs_ul.css('left').replace('px', '')) + thumbs_width;
			thumbs_ul.append(thumb_clone).css('left', new_left);
			*/
			
			// clone big slide
			var big_clone = big_ul.find('li:first').remove();
			var new_left = parseInt(big_ul.css('left').replace('px', '')) + big_width;
			big_ul.append(big_clone).css('left', new_left);
			
			index--;
		}
		
		index++;
		show_thumb_slide();
		show_big_slide();
	}
	
	timedNext = function() {
		if(active_pos < num_show-1 ) active_pos++;
		else active_pos = 0;
	activeThumbnail(active_pos);
		
		if( index == num_slides-2 ){
			
			// clone the first thumb and put it on the last place
			/*
			var thumb_clone = thumbs_ul.find('li:first').remove();
			var new_left = parseInt(thumbs_ul.css('left').replace('px', '')) + thumbs_width;
			thumbs_ul.append(thumb_clone).css('left', new_left);
			*/
			
			// clone big slide
			var big_clone = big_ul.find('li:first').remove();
			var new_left = parseInt(big_ul.css('left').replace('px', '')) + big_width;
			big_ul.append(big_clone).css('left', new_left);
			
			index--;
		}
		
		index++;
		show_thumb_slide();
		show_big_slide();
	}
	
	// show previous slides
	function prev_slide(){
		if(active_pos > 0) active_pos --;
		else active_pos = num_show-1;
		
	    activeThumbnail(active_pos);
		
		if( index == 1 ){
			// clone last thumb and place it on first place
			/*
			var thumb_clone = thumbs_ul.find('li:last').remove();
			var new_left = parseInt(thumbs_ul.css('left').replace('px', '')) - thumbs_width;
			thumbs_ul.prepend(thumb_clone).css('left', new_left);
			*/
			
			// clone big slide
			var big_clone = big_ul.find('li:last').remove();
			var new_left = parseInt(big_ul.css('left').replace('px', '')) - big_width;
			big_ul.prepend(big_clone).css('left', new_left);
			
			index++;
		}
		
		index--;
		show_thumb_slide();
		show_big_slide();
	}
	
	
	// move thumbnails
	function show_thumb_slide(){
		return;
		
		if(active_pos == num_show-1 )
		thumbs.parent().stop(true, true).animate({'left' : -1 * (index-num_show+1) * thumbs_width});
		
		if(active_pos == 0 )
		thumbs.parent().stop(true, true).animate({'left' : -1 * index * thumbs_width});
	}
	
	function activeThumbnail(a) {
		
		
		for (i=0;i<6;i++) {
			
			var cycleT = "thumbnail" + i;	
			var thumbC = document.getElementById(cycleT);
   			thumbC.style.borderColor="#000000";
			
		}
		
		
		var activeT = "thumbnail" + a;
		var thumb = document.getElementById(activeT);
   		thumb.style.borderColor="#ac1f30";
		
		
		
	}
	
	// move big slides
	function show_big_slide(){
		big_slides.parent().stop(true, true).animate({ 'left' : -1 * index * big_width });
	}
	
	
	
	
}



	
	
function timedCount()
{
timedNext();
t=setTimeout("timedCount()",6000);
}

function doTimer()
{
if (!timer_is_on)
  {
  timer_is_on=1;
  timedCount();
  }
}

function stopCount()
{
clearTimeout(t);
timer_is_on=0;
}

