IM.settings.viewMode = "gray";		// Color mode
IM.settings.animationSpeed = 400;	// Скорость анимации
IM.settings.thumbOpacity = 0.3;		// Прозрачность превьюшек галереи
IM.settings.overlay_back_opacity = 0.9;		// прозрачность фона оверлея
IM.settings.overlay_back_black = "#000";	// черный цвет фона оверлея
IM.settings.overlay_back_white = "#FFF";	// белый цвет фона оверлея
IM.settings.overlay_back_gray = "#444";		// серый цвет фона оверлея
IM.settings.overlay_back_default = IM.settings.overlay_back_black;		// цвет фона оверлея по умолчанию

/*
 * Обработчик оверлэя
 * TODO: найти нормальное решение без использования JS
 */
IM.behaviors.overlay = function () {
	// Устанавливаем цвет фона и прозрачность оверлея по умолчанию
	$("#o_background").css("background", IM.settings.overlay_back_default)
					  .fadeTo(0, IM.settings.overlay_back_opacity);
}

/*
 * Закрываем превьюшку при клике по картинке
 */
IM.behaviors.op_image = function () {
	$("#op_image").click(function () {
		IM.functions.hideOverlay();
	})
}

/*
 * Обрабочтик нажатий кнопок управления оверлеем
 * color - цвет фона, в формате пригодном для стиля background
 */
IM.behaviors.op_controls = function () {
	// Кнопка закрытия оверлея
	$("#close_overlay").click(function () {
		IM.functions.hideOverlay();
	});
	
	// Кнопки смены цвета фона оверлея
	$("#op_controls area:not(#close_overlay)").click(function () {
		var color = $(this).attr("title").substring($(this).attr("title").lastIndexOf(" ") + 1);
		switch (color) {
			case 'white':
				$("#o_background").animate({backgroundColor: IM.settings.overlay_back_white}, IM.settings.animationSpeed);
				break;

			case 'gray':
				$("#o_background").animate({backgroundColor: IM.settings.overlay_back_gray}, IM.settings.animationSpeed);
				break;

			case 'black':
			default:
				$("#o_background").animate({backgroundColor: IM.settings.overlay_back_black}, IM.settings.animationSpeed);
				break;
		}
	});
}

/*
 * Обработчик клика по ссылке в тексте ошибки оверлея
 */
IM.behaviors.o_error = function () {
	$("#o_error a").click(function () {
		IM.functions.hideOverlay();
		return false;
	});
}

/*
 * Обработчик фотогалереи
 */
IM.behaviors.p_photos = function () {
	$("#p_photos img").fadeTo(0, IM.settings.thumbOpacity).hover(function () {
		// TODO: Найти нормальное решение через stopPropagation
		$(this).stop().fadeTo(IM.settings.animationSpeed, 1.0);
	}, function () {
		$(this).stop().fadeTo(IM.settings.animationSpeed, IM.settings.thumbOpacity);
	}).click(function(e) {
		var imgLink = "images/photos/"+ $(this).attr("src").substring($(this).attr("src").lastIndexOf("/") + 1);

		// Если высота оверлея оказалась меньше высоты контента,
		// то увеличиваем ее на недостающую величину
		if ($(window).height() < $("#page").height())
			$("#overlay td, #o_background").height($("#page").height() + 20);
		else
			$("#overlay td, #o_background").height("100%");
		
		$("#overlay").fadeIn(IM.settings.animationSpeed, function () {
			$("#o_loader").show();
			
			var photo = new Image();
			$(photo).load(function () {
				$("#op_image img").replaceWith(this);
				$("#o_loader").fadeOut(IM.settings.animationSpeed, function () {
					$("#o_photo").fadeIn(IM.settings.animationSpeed)
								 .css("width", $("#op_image img").outerWidth());
				});
			}).error(function () {
				$("#o_loader").fadeOut(IM.settings.animationSpeed, function () {
					$("#o_error").fadeIn(IM.settings.animationSpeed);
				})
			}).attr("src", imgLink);
		});
		e.stopPropagation();
	});
	
}

/*
 * Функция скрытия оверлея
 */
IM.functions.hideOverlay = function() {
	$("#overlay").fadeOut(IM.settings.animationSpeed, function () {
		$("#o_photo").hide();
		$("#o_background").css("background-color", IM.settings.overlay_back_default);
	});
}

