$(document).ready(function(){
	// scroll parent
	var $par = $('#gallery .gallery-block ul');
	// scroll element
	var $el = $('#gallery .gallery-block ul li');
	// scroll element width
	var $elWidth = $('#gallery .gallery-block li').width();
	// buttons 'prev','next'
	var $btNext = $('#gallery a.next');
	var $btPrev = $('#gallery a.previous');
	// top tab links
	var $tabItem = $('#gallery ul.pages a');
	// duration
	var $duration = 1000;
	var $stopped = false;
	
	var $autoSlideTime = 7000; //every 7 seconds
	
	var $anmationOptions =  { "duration": 1000, "easing": "swing" };
	
	startAutoSlide();

	// clear active classes in top and bottom tabs
	function clearActiveClasses(){
		$tabItem.parent().removeClass('active');
	}
	
	$("div.gallery-block").mouseover(function(){
      $(document).stopTime();	
    }).mouseout(function(){
		if(!$stopped){
			startAutoSlide();
		}
    });
    
	
	// top tabs click
	$tabItem.click(function(){
		stopAutoSlide();	
		if(!$(this).parent().is('.active')) {
			//var _activeIndex = $tabItem.index($tabItem.filter('.active:eq(0)'));
			var _activeIndex = $tabItem.index($tabItem.filter(function(index) {
			  		return $(this).parent().hasClass("active");
				})
			);
			clearActiveClasses();
			$(this).parent().addClass('active');
			var _index = $tabItem.index(this);
			//$tabItem2.eq(_index).addClass('active');
			//console.log('_index: ' + _index + ' | _activeindex: ' + _activeIndex);
			if(_index>_activeIndex) {
				animation(1,_index - _activeIndex);
			} else {
				animation(0,_activeIndex - _index);
			}
		}
		return false;
	});
	
	// 'Next' button click
	$btNext.click(function(){
		//var _index = $tabItem.index($tabItem.filter('.active:eq(0)'));
		stopAutoSlide();	
		moveForward();
	});
	
	function moveForward(){
		var _index = $tabItem.index($tabItem.filter(function(index) {
		  		return $(this).parent().hasClass("active");
			})
		);
		clearActiveClasses();
		if (_index+1<$el.length) {
			$tabItem.eq(_index+1).parent().addClass('active');
			//$tabItem2.eq(_index+1).addClass('active');
			animation(1,1);
		} else {
			$tabItem.parent().eq(0).addClass('active');
			//$tabItem2.eq(0).addClass('active');
			animation(0,$el.length-1);
		}
		return false;
	}

	// 'Prev' button click
	$btPrev.click(function(){
		stopAutoSlide();	
		//var _index = $tabItem.index($tabItem.filter('.active:eq(0)'));
		var _index = $tabItem.index($tabItem.filter(function(index) {
		  		return $(this).parent().hasClass("active");
			})
		);
		clearActiveClasses();
		if (_index>0) {
			$tabItem.eq(_index-1).parent().addClass('active');
			//$tabItem2.eq(_index-1).addClass('active');
			animation(0,1);
		} else {
			$tabItem.eq($el.length-1).parent().addClass('active');
			//$tabItem2.eq($el.length-1).addClass('active');
			animation(1,$el.length-1);
		}
		return false;
	});
	
	function startAutoSlide(){
		$(document).everyTime($autoSlideTime, function(i) {
		  moveForward();
		});	
	}
	function stopAutoSlide(){
		$(document).stopTime();
		$stopped = true;
	}
	
	// gallery animation
	function animation(_direction,_el) {
		if (_direction==0) {
			// diraction 'left'
			$par.animate({marginLeft:'+='+_el*$elWidth},$duration);
			
		} else {
			// diraction 'right'
			$par.animate({marginLeft:'-='+_el*$elWidth},$duration);
			
		}
	}
	
});