!function ($) {
	$(function(){
		var   hash = window.location.hash
			, photoMainID = hash.match(/^#media-overlay_(.*)$/)
			, overlaySelector = photoMainID && photoMainID[1] ? '.media-overlay_type_single[data-hash="' + photoMainID[1] + '"]' : null
			, overlay = overlaySelector && $(overlaySelector);

		if (overlay) { overlay.show(); }
	});

	$(document).ready(function(){
		var b = new $.Bem('photo-gallery');
		var o = new $.Bem('media-overlay');


		var slides = '#gallery-slides';
		var previews = '#gallery-previews';
		var descriptions = '#gallery-descriptions';
		var p = {
			slide:   '#photo-',
			preview: '#preview-',
			desc:    '#description-',
			overlay: '#overlay-'
		};
		var slides_count = 9;

		if (!$(slides).length) return;

		/* main carousel */
		$(slides).carouFredSel({
			circular: false,
			infinite: false,
			auto: false,
			prev: b.s('photo-prev'),
			next: b.s('photo-next'),
			scroll: {
				onBefore: function(data){
					setSlider( data.items.visible.attr('data-slide-num') );
				}
			},
			onCreate: function(){
				b.j('photo-prev, photo-next, zoom').hide();
			}
		}, {
			classnames: {
				disabled: b.c('photo-nav-disabled')
			}
		});


		/* descriptions carousel */
		$(descriptions).carouFredSel({
			circular: false,
			infitite: false,
			auto: false,
			scroll: {
				fx: 'crossfade'
			}
		});


		/* previews carousel */
		$(previews).carouFredSel({
			circular: false,
			infinite: false,
			auto: false,
			prev: b.s('slider-prev'),
			next: b.s('slider-next')
		});


		/* init */
		onHashChange();
		var $max_descr_height = 0;
		b.j('description').each(function(i, e){
			$max_descr_height = $(e).height() > $max_descr_height ? $(e).height() : $max_descr_height;
		}).each(function(i, e){
			$(e).css('height', $max_descr_height);
		});
		$(b.s('descriptions') + ' .caroufredsel_wrapper').css('height', b.j('description').eq(0).outerHeight());

		
		function setSlide(n) {
			$(slides).trigger('slideTo', p.slide + n);
			setSlider(n);
			return false;
		}
		function setSlider(n) {
			var activePreview = $(p.preview + n);

			$(b.s('preview')).removeClass(b.c('preview_state_selected'));
			activePreview.addClass(b.c('preview_state_selected'));

			$(previews).trigger('slideTo', p.preview + Math.floor(n / slides_count) * slides_count);
			$(descriptions).trigger('slideTo', p.desc + n);

			window.location.hash = '#photo_' + n;
			return false;
		}


		$(previews).on('click', b.s('preview'), function(e) {
			e.preventDefault();
			var $n = $(this).attr('data-preview-num');
			return setSlide($n);
		});
		b.j('w-photos-container').hover(function(){
			b.j('photo-prev, photo-next, zoom').show();
		}, function(){
			b.j('photo-prev, photo-next, zoom').hide();
		});

		/* overlay */
		var overlay = '#media-overlay-1';
		var overlay_slides = '#overlay-slides';
		

		$(overlay).appendTo($('body'));
		var overlay_init = false;

		
		$(overlay).find('.close').on('click', function(){
			$(overlay).hide();
		});


		b.j('zoom').on('click', function(){
			$(overlay).show().find('.overlay-offset').css('margin-top', $(slides).offset().top - 100);

			if(!overlay_init) {
				$(overlay_slides).carouFredSel({
					circular: false,
					infinite: false,
					auto: false,
					prev: o.s('prev'),
					next: o.s('next'),
					scroll: {
						fx: 'crossfade'
					}
				}, {
					classnames: {
						disabled: o.c('nav-disabled')
					}
				});
				overlay_init = true;
			}

			$(slides).trigger('currentVisible', function(item){
				$(overlay_slides).trigger('slideTo', p.overlay + item.attr('data-slide-num'));
			});
		});

		var $social_show = $(overlay).find('.social-show');
		var $social_text = $(overlay).find('.social-text');
		var $social_buttons = $(overlay).find('.social-buttons');
		$social_show.hover(function(){
			$social_text.hide();
			$social_buttons.show();
		}, function(){
			$social_text.show();
			$social_buttons.hide();
		});

		function onHashChange() {
			var   hash = window.location.hash
				, photo = hash.match(/^#photo_(\d+)$/)
				, photoNum = photo ? Number(photo[1]) : null;


			if (photoNum && $(slides).find('li[data-slide-num=' + photoNum + ']').length) {
				$(slides).trigger('currentPosition', function(curPhotoNum){
					if (photoNum != curPhotoNum) setSlide(photoNum);
				})
			} else {
				setSlide(0);
			}
		}

		$(window).on('hashchange', onHashChange);
	});
}(window.jQuery);
