$(function () {
	
	$('.preview img').click( function() {
		
		var h = $('#big_img').height();
		
		$('#viewport').css('display','none');
		
		$('.preview img').removeClass('active');
		
		var x = $('.preview img').index(this);
		$(this).addClass('active');
		
		$('#gallery_2').css({
			'display':'block',
			'position':'relative',
			'z-index':'100',
			'left':'0px',
			'top':'0px',
			'visibility':'visible'
			});
		
		$('#gallery_2 img').removeClass('act');
		
		$($('#gallery_2 img').get(x)).addClass('act');
		
	});
	
	$('#simplePrevious').click( function() {
		$('#gallery_2').css('display','none');
		$('#viewport').css('display','block');
		
	});
	$('#simpleNext').click( function() {
		$('#gallery_2').css('display','none');
		$('#viewport').css('display','block');
	});
	
	
	$('.hovered').hover(function () {
		$(this).addClass('hover');
	},
	function () {
		$(this).removeClass('hover'); 
	});
		
	$('#img_menu li:first').addClass('li_act');
	
	$('#img_menu li').click(function () {
		
		var _index = $('#img_menu li').index(this);
		var activeImg = $('#image img').get(_index);
		
		slide(null, $(activeImg), 400);
		
		clearInterval(interval);
		interval = setInterval(slide, 5000);

	});

	var slide = function (e, nextParam, speed) {	
		
		$('#img_menu li.li_act').removeClass('li_act');
		
		var activeImg = $('#image img:visible');	
			activeImg.fadeOut(speed || 2000);
		var activeText = $('.img_text:visible');	
			activeText.fadeOut(speed || 2000);

		var next = activeImg.next();

		if (!next.is('img')) {
			next = nextParam || $('#image img:first');
		}												

		next = nextParam || next;

		next.fadeIn(speed || 2000, function () {
			
			var current_index = $('#image img').index(this);
			$($('#img_menu li').get(current_index)).addClass('li_act');
		});
		
		var _index = $('#image img').index(next);
		$('.img_text').fadeOut(speed || 2000);
		$($('.img_text').get(_index)).fadeIn(speed || 2000);
		
	};

	var interval = setInterval(slide, 6000);
	
	$(".slider").jCarouselLite({
   		btnNext: ".next",
   		btnPrev: ".prev",
   		visible: 4
	});
	
	$('#viewport a').lightBox();
	$('#gallery_2 a').lightBox();
	$('#big_img a').lightBox();

});
