var newClass = function() {
	return function() {
		return this.init.apply(this, arguments);
	}
}

var Smena = newClass();

Smena.prototype = {

init: function() {

	$(window).load(function() {
		arr = [];
		$("#js-lgi").find(".element-lgi").each(function(i){
			cur_href_ci = $(this).attr("href");
			arr.push(cur_href_ci);
		});
		lengthListImg = arr.length;
		sizeListImg = $('#js-lgi').find('a.element-lgi').length;
		lineListImg = (sizeListImg-4) * 155;
		
		if (sizeListImg < 5) {
			$('.to-left-lgi').hide();
			$('.to-right-lgi').hide();
		}
		
		
		selectBlockUrl = window.location.hash.replace('#', '');
		if (selectBlockUrl != '') {
			selectBlock = $('#js-lgi .' + selectBlockUrl).find('a');
			changeBigImg = selectBlock.attr('href');
			$('#js-lgi a').removeClass('curr-img-lgi');
			selectBlock.addClass('curr-img-lgi');
			var showImg = $(".detail-img-team img").attr('src', changeBigImg).load (function () {
				$('.detail-img-team img').fadeIn();
			});
			
			indexSelectBlock = selectBlock.parents('li').index();
			if (indexSelectBlock < 3) {
				return false;
			} else {
				startPosition = (indexSelectBlock - 3)*155;
				$('#js-lgi').animate({left:'-='+startPosition+'px'}, 500);
			}
			
		}
		
	});
	
	$('.element-lgi').click(function() {

		$('.element-lgi').removeClass('curr-img-lgi');
		$(this).addClass('curr-img-lgi');
		
		curBigImg = $(".detail-img-team img").attr('src');
		var curHrefImg = $(this).attr('href');

		if (curBigImg == curHrefImg) {
			return false;
		} else {
			$('.detail-img-team img').hide();
			showImg = $(".detail-img-team img").attr('src', curHrefImg).load (function () {
				$('.detail-img-team img').fadeIn(500);
			});
			indexSelectElement = $(this).index();
			$('.description-select-image').find('.imade-description').removeClass('cur-id').fadeOut(300);
			$('.description-select-image').find('.imade-description:eq('+indexSelectElement+')').addClass('cur-id').fadeIn(300);
			return false;
		}
		
		
		
		return false;
	});
	
	$('#js-dit').mouseenter(function() {
		$('#js-dsi').fadeIn(300);
	});
	
	$('#js-dit').mouseleave(function() {
		$('#js-dsi').fadeOut(300);
	});
	
	$('#js-left-select-lgi').click(function() {
		selectBigImg = $(".detail-img-team img").attr('src');
		indexImgArr = jQuery.inArray(selectBigImg, arr);
		if (indexImgArr == 0) {
			indexChangeImg = lengthListImg - 1;
		} else {
			indexChangeImg = indexImgArr - 1;
		}
		$('.detail-img-team img').hide();
		var showImg = $(".detail-img-team img").attr('src', arr[indexChangeImg]).load (function () {
			$('.detail-img-team img').fadeIn();
		});
		
		$('.description-select-image').find('.imade-description').removeClass('cur-id').fadeOut(300);
		$('.description-select-image').find('.imade-description:eq('+indexChangeImg+')').addClass('cur-id').fadeIn(300);
		$('.element-lgi').removeClass('curr-img-lgi');
		$('.element-lgi:eq('+indexChangeImg+')').addClass('curr-img-lgi');
		
		var coordsGaleryLeft = $('#js-lgi').position().left;
		var maxNotMove = Math.abs(coordsGaleryLeft/155);
		var indexCurImg = $('.curr-img-lgi').parents('li').index();
		if ((indexCurImg+1) == maxNotMove) {
			$('#js-lgi').animate({left:'+=155px'}, 500);
		} else if (indexCurImg == (sizeListImg-1)) {
			$('#js-lgi').animate({left:'-' + lineListImg + 'px'}, 1000);
		}
		
	});
	
	$('#js-right-select-lgi').click(function() {
		selectBigImg = $(".detail-img-team img").attr('src');
		indexImgArr = jQuery.inArray(selectBigImg, arr);
		if (indexImgArr == (lengthListImg-1)) {
			indexChangeImg = 0;
		} else {
			indexChangeImg = indexImgArr + 1;
		}
		$('.detail-img-team img').hide();
		var showImg = $(".detail-img-team img").attr('src', arr[indexChangeImg]).load (function () {
			$('.detail-img-team img').fadeIn();
		});
		
		$('.description-select-image').find('.imade-description').removeClass('cur-id').fadeOut(300);
		$('.description-select-image').find('.imade-description:eq('+indexChangeImg+')').addClass('cur-id').fadeIn(300);
		$('.element-lgi').removeClass('curr-img-lgi');
		$('.element-lgi:eq('+indexChangeImg+')').addClass('curr-img-lgi');
		
		var coordsGaleryLeft = $('#js-lgi').position().left;
		var maxNotMove = Math.abs(coordsGaleryLeft/155) + 4;
		var indexCurImg = $('.curr-img-lgi').parents('li').index();
		if (indexCurImg == maxNotMove) {
			$('#js-lgi').animate({left:'-=155px'}, 500);
		} else if (indexCurImg == 0) {
			$('#js-lgi').animate({left:'0'}, 1000);
		}
		
		
	});
	
	$('#js-right-home-lgi').live('click', function() {
		
		if ($('#js-lbi').find('div.cur-img-home').is(":last-child")) { 
			nextHomeImg = $('#js-lbi div:first-child');
			$('#js-lbi div').removeClass("cur-img-home").fadeOut();
			srcNextHomeImg = nextHomeImg.find("input").val();
			nextHomeImg.find("input").replaceWith("<img src="+srcNextHomeImg+" />");
			nextHomeImg.addClass("cur-img-home").fadeIn();
		} else {
			nextHomeImg = $('.cur-img-home').next("div");
			srcNextHomeImg = nextHomeImg.find("input").val();
			$('#js-lbi div').removeClass("cur-img-home").fadeOut();
			nextHomeImg.find("input").replaceWith("<img src="+srcNextHomeImg+" />");
			nextHomeImg.addClass("cur-img-home").fadeIn();
		}
		
			textDescImage = nextHomeImg.index();
			
			$('.description-select-image .imade-description').removeClass('cur-id').fadeOut(300);
			$('.description-select-image').find('.imade-description:eq('+textDescImage+')').addClass('cur-id').fadeIn(300);
	});
	
	$('#js-left-home-lgi').live('click', function() {
		
		if ($('#js-lbi').find('div.cur-img-home').is(":first-child")) { 
			nextHomeImg = $('#js-lbi div:last-child');
			$('#js-lbi div').removeClass("cur-img-home").fadeOut();
			srcNextHomeImg = nextHomeImg.find("input").val();
			nextHomeImg.find("input").replaceWith("<img src="+srcNextHomeImg+" />");
			nextHomeImg.addClass("cur-img-home").fadeIn();
		} else {
			nextHomeImg = $('.cur-img-home').prev("div");
			srcNextHomeImg = nextHomeImg.find("input").val();
			$('#js-lbi div').removeClass("cur-img-home").fadeOut();
			nextHomeImg.find("input").replaceWith("<img src="+srcNextHomeImg+" />");
			nextHomeImg.addClass("cur-img-home").fadeIn();
		}
		
			textDescImage = nextHomeImg.index();
			
			$('.description-select-image .imade-description').removeClass('cur-id').fadeOut(300);
			$('.description-select-image').find('.imade-description:eq('+textDescImage+')').addClass('cur-id').fadeIn(300);
	});
	
	
	$('.to-right-lgi').click(function() {
		if ($('div#js-lgi').hasClass('block')) {
			return false;
	} else {
		
		coordsGaleryLeft = $('#js-lgi').position().left;
		movedElements = Math.abs(coordsGaleryLeft/155) + 4;
		freeMove = (sizeListImg - movedElements)*155;
		
		if (freeMove == 0) {
			return false;
		} else if (freeMove > 620) {
			$('#js-lgi').addClass("block");
			lengthFM = 620;
		}	else {
			$('#js-lgi').addClass("block");
			lengthFM = freeMove;
		}
		
		$('#js-lgi').animate({left:'-=' + lengthFM + 'px'}, 500);
		$('#js-lgi').queue(function () {
			$('#js-lgi').removeClass("block");
			$(this).dequeue();
		});
	}
	});
	
	$('.to-left-lgi').click(function() {
	if ($('div#js-lgi').hasClass('block')) {
			return false;
	} else {
		
		coordsGaleryLeft = $('#js-lgi').position().left;
		freeMove = Math.abs(coordsGaleryLeft);
		if (freeMove == 0) {
			return false;
		} else if (freeMove > 620) {
			$('#js-lgi').addClass("block");
			lengthFM = 620;
		}	else {
			$('#js-lgi').addClass("block");
			lengthFM = freeMove;
		}
		
		$('#js-lgi').animate({left:'+=' + lengthFM + 'px'}, 500);
		$('#js-lgi').queue(function () {
			$('#js-lgi').removeClass("block");
			$(this).dequeue();
		});

	}
	});

}

};

$(document).ready(function(){
	new Smena();
	
});
