﻿var mask_in_speed = 'normal';
var mask_out_speed = 'slow';
var hover_in_speed = 'normal';
var hover_out_speed = 'fast';
var imagine_in_speed = 'slow';
var imagine_out_speed = 'normal';

var show_title = 'кликните, чтобы увидеть в большом размере';
var close_title = 'кликните, чтобы закрыть';
var loading_message = 'Идёт загрузка…';

var imagined = false;

$(document).ready(function() {
	$('#body').parent()
	.append('<div id="mask"></div>')
	.append('<div id="hover">' + loading_message + '</div>');

	$("a[href$='.jpg'] img, a[href$='.jpeg'] img, a[href$='.png'] img, a[href$='.gif'] img", $('.pageContent')).each(function(index) {
		$(this).attr('id', 'imagine_link_' + index).addClass('imagination');
		var imagine_link = $(this);

		$(this).parent()
		.attr('title', show_title)
		.click(function(event) {
			event.preventDefault();

			var link = $(this).attr('href');
			var id = 'imagine_' + index;

			var imagine = $('#' + id);

			if(!$(imagine).length > 0) {
				showHover(imagine_link);

				$(this).after('<img id="' + id + '" title="' + close_title + '" class="imagine" alt=""/>');
				var imagine = $('#' + id);
			}{
				$(imagine).load();
			}

			$(imagine).load(function(){
				hideHover();
				posImagines();
				sizeMask();

				$(this)
				.fadeIn(imagine_in_speed)
				.click(function (event) {hideImagine()});

				imagined = true;

				if(pageTracker){pageTracker._trackPageview(link)};
				if(yaCounter){yaCounter.hit(link, document.title, document.URL);console.log([link, document.title, document.URL])};
			}).attr('src', link);

			showMask();

			return false;
		});
	});

	$('#mask').click(function(event) {hideImagine()});

	$(window).resize(function() {
		if(imagined){
			posImagines();
			sizeMask();
		}
	});
});

function showMask() {
	$('#mask').fadeIn(mask_in_speed);
}
function hideMask() {
	$('#mask').fadeOut(mask_out_speed);
}
function sizeMask() {
	$('#mask').hide().css({'width': $(document).width(), 'height': $(document).height()}).show();
}

function showHover(imagine_link) {
	var pos = $(imagine_link).offset();
	var h = $(imagine_link).height();
	var w = $(imagine_link).width();

	var hover = $('#hover');

	$(hover).css({
		'top': Math.round(pos.top + h / 2 - $(hover).height() / 2),
		'left': Math.round(pos.left + w / 2 - $(hover).width() / 2)
	});
	$(hover).fadeIn(hover_in_speed);
}
function hideHover() {
	$('#hover').fadeOut(hover_out_speed);
}

function posImagines() {
	$('.imagine').each(function(index) {
		var imagine_link = $('#imagine_link_' + $(this).attr('id').substring(8));
		var pos = $(imagine_link).offset();
		var h = $(imagine_link).height();
		var w = $(imagine_link).width();

		$(this).css({
			'top': Math.round(pos.top + h / 2 - $(this).height() / 2),
//			'left': Math.round(pos.left + w / 2 - $(this).width() / 2)
			'left': Math.round($(window).width() / 2 - $(this).width() / 2)
		});
	});
}

function hideImagine() {
	$('.imagine').fadeOut(imagine_out_speed);
	hideMask();

	imagined = false;
}