if (!window.console) {
	window.console = {
		log: function() {
		}
	};
}

$.domReady(function() {
	var elem = $(this);

	// Текст по умолчанию в полях формы
	if ($.fn.placeholder) {
		elem.find('input[placeholder], textarea[placeholder]').placeholder();
	}

	// Ссылки в тексте
	if ($.fn.linkify) {
		elem.find(".linkify").linkify(function(links) {
			links.attr("target", "_blank");
		});
	}

	// timeago
	if ($.fn.timeago) {
		elem.find("time.timeago").timeago();
	}

	// Ссылки на ролики YouTube
	if ($.fn.youtubin) {
		$.youtubin({
			scope: elem
		});
	}

	// chosen
	if ($.fn.chosen) {
		elem.find("form select").chosen();
	}

	// Кнопки submit
	elem.find("a.submit").click(function(e) {
		e.preventDefault();
		$(this).closest("form").submit();
	});

	// Блокируем кнопку отправки формы после отправки формы
	elem.find("form").submit(function() {
		$(this).find(":submit").attr("disabled", "disabled");
	});

	// Colorbox
	elem.find("a").filter(function() {
		var href = $(this).attr("href");
		return href && /\.(png|gif|jpg)$/i.test(href);
	}).each(function() {
		$(this).colorbox({
			returnFocus: false,
			initialWidth: 300,
			initialHeight: 200,
			current: "изображение {current} из {total}"
		});

		var $a   = $(this);
		var $img = $a.children("img:only-child");
		if (!$img.length && $a.text() === "") return;

		if ($a.is(":hidden")) return;

		$a.css({
			position: "relative",
			display: "inline-block"
		});

		var $zoom = $("<img src='/images/zoom.png' class='zoom'>").hide();
		$a.append($zoom);

		$zoom.attr("title", "Увеличить");

		$a.on({
			mouseenter: function() {
				$(this).find("img.zoom").stop(true, true).fadeIn(200);
			},
			mouseleave: function() {
				$(this).find("img.zoom").stop(true, true).fadeOut(200);
			}
		});
	});

	// Раскрывающийся блок
	if (1) {
		elem.find("div.fold").each(function() {
			var $trigger = $("<span></span>").addClass("trigger");
			
			var $body = $("<div></div>").addClass("body");
			$body.append($(this).next());

			$trigger.click(function() {
				if ($body.is(":visible")) {
					$body.slideUp("fast", function() {
						$body.closest(".fold").removeClass("fold-open");
					});
				} else {
					$body.closest(".fold").addClass("fold-open");
					$body.slideDown("fast");
				}
			});

			$(this).wrapInner($trigger);
			$(this).append($body);
		});
		// Якорь на раскрывающийся блок
		var openFoldByHash = function() {
			if (!window.location.hash) return;
			
			var hash = window.location.hash.substr(1);
			
			elem.find("div.fold").each(function() {
				var $prev = $(this).prev("p");
				if (!$prev.length) return;
				
				var $a = $prev.children("a:only-child");
				if (!$a.length) return;

				if ($a.attr("name") === hash) {
					$(this).children(".trigger").trigger("click");
				}
			});
		};
		$(window).hashChange(openFoldByHash);
		openFoldByHash();
		elem.find("div.fold.open").children(".body").show();
	};

	// Красивые таблички
	elem.find("table[border=1]").removeAttr("border").addClass("table").each(function() {
		$(this).find("tbody tr").each(function(i) {
			if (i % 2) {
				$(this).addClass("row2");
			} else {
				$(this).addClass("row1");
			}
		});
	});

});

$(function() {
	// Ajax-блоки
	if ($.eAjax) {
		$.eAjax({"url": "/ajax"});
	}

	// Окошки
	if ($.eOverlay) {
		$.eOverlay.styles.grey = function() {};
		$.eOverlay();
	}

	// Картинки справа/слева
	$(".content").find("img").each(function() {
		var align = $(this).attr("align");
		if ($(this).css("float") === "left") align = "left";
		if ($(this).css("float") === "right") align = "right";
		
		if ($.inArray(align, ["left", "right"]) === -1) return;

		if (align) {
			$(this).removeAttr("align");
			
			if ($(this).is(":only-child") && $(this).parent().is("a")) {
				$(this).parent().addClass("align-" + align);
			} else {
				$(this).addClass("align-" + align);
			}
		}
	});

	// Красивые ссылки на файлы
	$(".content").find("p > a[href]:only-child").each(function() {
		var html = $(this).html();
		var href = $(this).attr("href");
		var size = $(this).data("size");

		// Размер
		if (!size) {
			size = null;
			if (href.lastIndexOf("?") > -1) {
				var size = href.substr(href.lastIndexOf("?") + 1);
				href = href.substr(0, href.lastIndexOf("?"));
			}
		}

		// Расширение
		if ($(this).data("extension")) {
			var ext = $(this).data("extension");
		} else {
			var ext = href.substr(href.lastIndexOf(".") + 1);
		}

		var valid = {
//			"gif"  : true,
//			"jpg"  : true,
//			"png"  : true,
			"doc"  : true,
			"docx" : true,
			"rtf"  : true,
			"xls"  : true,
			"xlsx" : true,
			"ppt"  : true,
			"pptx" : true,
			"pdf"  : true,
			"txt"  : true,
			"exe"  : true,
			"avi"  : true,
			"mpg"  : true,
			"zip"  : true,
			"7z"   : true,
			"rar"  : true,
			"swf"  : true
		};
		if (!valid[ext]) return;

		$(this).attr("href", href);
		$(this).wrap($("<table class='download'><tr><td class='" + ext + "'></td></tr></table>"));

		if (size != null) {
			$(this).after("<span class='size'>(" + size + ")</span>");
		}
	});

	// Попапы
	$(document.body).click(function() {
		$(".popup").hide();
	});
	$(document.body).on("click", ".popup", function(e) {
		e.stopPropagation();
	});

});

