var TIME = 200;
function alphaIn(object, time, alpha) {
	if(!alpha) {
		var alpha = 1
	}
	$(object).animate({
		opacity: alpha
	}, time)
}
function alphaOut(object, time, alpha) {
	if(!alpha) {
		var alpha = 0.3
	}
	$(object).animate({
		opacity: alpha
	}, time)
}
function imageSliding(imgNum){
	var thisPic = '#galery #images #imagesContainer img:nth-child(' + (imgNum + 1) + ')';
	var thisThumb = '#galery #galeryContainer img:nth-child(' + (imgNum + 1) + ')';
	
	$('html, body').animate({
		scrollTop: $(document).height() - $(window).height() + 349
		}, TIME);
	$('#galery #galeryContainer img').each(function(){
		$(this).removeClass('clicked');
		$(this).addClass('inactive');
	});
	$(thisThumb).removeClass('inactive');
	$(thisThumb).addClass('clicked');
	$('#galery #galeryContainer img.inactive').each(function(){
		alphaOut(this, TIME);
	})
	
	var imagePosition =  195 -(imgNum * 478) - (imgNum * 14);
	$('#galery #images #imagesContainer').animate({
		marginLeft: imagePosition
		}, TIME);
	alphaIn(thisThumb, TIME);
	$('#galery #images #imagesContainer img').each(function(){
		$(this).addClass('inactive');
		$(this).removeClass('active');
	})
	$(thisPic).removeClass('inactive');
	$(thisPic).addClass('active');
	alphaOut('#galery #images #imagesContainer img.inactive', TIME);
	alphaIn('#galery #images #imagesContainer img.active', TIME);
	var thumbsWidth = $('#galery #galeryContainer #thumbs').width();
	var thumbsMargin = parseInt($('#galery #galeryContainer #thumbs').css('marginLeft'));
	var thumbsConWidth = $('#galery #galeryContainer').width();
	var thisCol = (((thumbsMargin - thumbsConWidth) * -1) / 820) * 5 - 1;
	if(thisCol < imgNum) {
		thumbsSliding('right');
	}
	if(thisCol - 4 > imgNum){
		thumbsSliding('left');
	}
}
function thumbsArrow(){
	var thumbs = $('#galery #galeryContainer #thumbs');
	var thumbsCon = $('#galery #galeryContainer');
	var leftArrow = $('#galery #prev');
	var rightArrow = $('#galery #next');
	if(thumbs.width() > thumbsCon.width()) {
		if(parseInt(thumbs.css('marginLeft')) < 0 && parseInt(thumbs.css('marginLeft')) + thumbs.width() <= thumbsCon.width()) {
			thumbsCon.css('marginLeft', '2px')
			rightArrow.fadeOut(TIME);
			leftArrow.fadeIn(TIME);
		}
		else if(parseInt(thumbs.css('marginLeft')) < 0){
			thumbsCon.css('marginLeft', '2px');
			rightArrow.fadeIn(TIME);
			leftArrow.fadeIn(TIME);
		}
		else if(parseInt(thumbs.css('marginLeft')) == 0){
			leftArrow.fadeOut(TIME, function(){thumbsCon.css('marginLeft', '31px')});
			rightArrow.fadeIn(TIME);
		}
	}
	else {
		leftArrow.hide();
		rightArrow.hide();
		thumbsCon.css('marginLeft', '33px');
	}
}
function thumbsSliding(side){
	var thumbsConWidth = $('#galery #galeryContainer').width();
	var thumbsMargin = parseInt($('#galery #galeryContainer #thumbs').css('marginLeft'));
	var left = (thumbsMargin + thumbsConWidth) + 'px';
	var right = (thumbsMargin - thumbsConWidth) + 'px';
	
	if(side == 'right'){
		$('#galery #galeryContainer #thumbs').animate({
			marginLeft: right
		}, TIME, thumbsArrow);
	}
	if(side == 'left'){
		$('#galery #galeryContainer #thumbs').animate({
			marginLeft: left
		}, TIME, thumbsArrow);
	}
}
$(function() {
	var picLength = $('#galery #images #imagesContainer img').length;
	var picConLen = (picLength * 478) + (picLength * 14);
	$('#galery #images #imagesContainer').width(picConLen);
	var thumbLength = $('#galery #galeryContainer #thumbs img').length;
	var thumbConLen =  (thumbLength * 150) + (thumbLength * 14);
	$('#galery #galeryContainer #thumbs').width(thumbConLen);
	thumbsArrow();
	$('#galery a#prev').click(function(){thumbsSliding('left')});
	$('#galery a#next').click(function(){thumbsSliding('right')});
	alphaOut('#galery a', 1);
	$('#galery a').hover(function(){
		alphaIn(this, TIME);
	}, function(){
		alphaOut(this, TIME);
	})
	
	$('#galery #galeryContainer').hover(function(){return;}, function(){
		if($('#galery').hasClass('closed')) {
			$('#galery #galeryContainer img').each(function(){alphaIn(this, TIME);
		})
		}
	})
	$('#galery #galeryContainer img').each(function(){
		$(this).click(function(){
			if($('#galery').hasClass('closed')){
				$('#galery').removeClass('closed');
				$('#galery').addClass('opened');
				$('#galery #images').slideDown(TIME);
			}
			
			var imgNum = parseInt($(this).attr('num'));
			imageSliding(imgNum);
		})
		$(this).hover(function(){
			if($(this).hasClass('inactive')){
				alphaIn(this, TIME);
			}
			if($('#galery').hasClass('closed')) {
				$('#galery #galeryContainer img').each(function(){
					alphaOut(this, TIME);
				})
				alphaIn(this, TIME);
			}
		},function(){
			if($(this).hasClass('inactive')){
				alphaOut(this, TIME);
			}
			if($('#galery').hasClass('closed')) {
				alphaOut(this, TIME);
			}
		})
	})
	$('#galery #images #imagesContainer img').each(function(){
		$(this).hover(function(){
			if($(this).hasClass('inactive')){
				alphaIn(this, TIME);
			}
		},function(){
			if($(this).hasClass('inactive')){
				alphaOut(this, TIME);
			}
		})
		$(this).click(function(){
			if($(this).hasClass('inactive')){
				var imgNum = parseInt($(this).attr('num'));
				imageSliding(imgNum);
			}
		})
	})
	$('#galery #images').hide();
})
