function prepareForms() {
	var $f = $("form#regForm");
	if (!$f.length) {
		return;
	}
	$f.append('<input type="hidden" id="ua" name="ua" />');
	$f.append('<input type="hidden" id="screenSize" name="screenSize" />');
	$("#ua").val(navigator.userAgent);
	$("#screenSize").val(screen.width + "x" + screen.height);
}

function enhanceContentTypography() {
	var title = $("div#main-content h1 span").parent();
	if (!title.length) {
		return;
	}
	title.addClass("decorated").html(
		title.clone()
			.find("span.omit").remove().end()
			.find("span.line")
				.wrapInner("<span class=\"bg\"><\/span>")
				.before("<span class=\"h1-line\"><\/span>")
				.end()
			.html()
	);
	
	$("div#main-content h1.decorated + p").each(function(){
		var html = $(this).html();
		html = html.replace(/^(\S+)/, "<span class=\"first\">$1<\/span>");
		$(this).html(html);
	});
}

function dynamizeButtons() {
	$("button").hover(function(){$(this).addClass("hover")}, function(){$(this).removeClass("hover")});
}

function convertContactLinks() {
	$("a.contact-email").each(function(){
		var href = this.href.replace(new RegExp("^" + BASE_HREF), "");
		href = decodeURI(href);
		href = href
			.replace("(", "<")
			.replace(")", ">")
			.replace("[]", "@")
			.replace(/\*/g, ".");
		this.href = "mailto:" + href;
		this.innerHTML = this.innerHTML
			.replace("[]", "@")
			.replace(/\*/g, ".");
	});
}

function createPullQuotes() {
	$("div#main-content span.quote").each(function(){
		var quote = $.trim($(this).clone().find("span.omit").remove().end().html());
		quote = String(quote).substr(0,1).toUpperCase() + String(quote).substr(1);
		quote = quote.replace(/[^a-z0-9?!]$/, "") + ".";
		quote = quote.replace(/([!?])\.$/, "$1");
		$(this).parent().prepend("<div class=\"pullquote\">" + quote + "<\/div>");
	});
}

function enhanceSearchForm() {
	$("#search").submit(function(){
		location = BASE_HREF + "search/" + encodeURI($("#q").val());
		return false;
	})
	var $q = $("#q");
	if (!$q.length) {
		return;
	}
	$q.data("original", $q.val()).focus(function(){
		if (this.value == $(this).data("original")) {
			this.value = "";
		}
	}).blur(function(){
		if (!this.value.length) {
			this.value = $(this).data("original");
		}
	});
}

function hidePoorComments() {
	$("div.text.poor-comment").filter(function(){
		return !$("span.censored", this).length;
	}).each(function(){
		var $this = $(this);
		$this
			.wrapInner("<div class=\"poor-comment-content\"><\/div>")
			.prepend("<span class=\"censored\">Ezt a kommentet alacsony színvonala miatt elrejtettük. Ha látni szeretnéd, kattints ide!<\/span>")
			;
		$this.find("div.poor-comment-content").hide();
		$this.click(function(){
			if ($this.find("div.poor-comment-content:visible").length) {
				$this.find("div.poor-comment-content").hide("fast");
				$this.find("span.censored").show("fast");
			} else {
				$this.find("div.poor-comment-content").show("fast");
				$this.find("span.censored").hide("fast");
			}
		}).css("cursor", "pointer");
	});
}

function focusCommentTextfield() {
	if (location.search.match(/reply-\d+/)) {
		$("textarea#comment").focus();
	}
}

function commentInteraction() {
	$("div.user-interface span.vote-for, div.user-interface span.vote-against, div.user-interface span.flag")
		.mouseover(function(){$(this).addClass("hover")})
		.mouseout(function(){$(this).removeClass("hover")})
		.click(function(){
			var comment = this.className.match(/comment-(\d+)/)[1];
			var command = this.className.match(/(vote-for|vote-against|flag)/)[1];
			var process = false, reason = "";
			switch (command) {
				case "vote-for":
					process = confirm("Tetszik a komment vagy nagyon egyetértesz a leírtakkal.\n\nBiztos?");
					break;
				case "vote-against":
					process = confirm("Ellenszenvesnek találod a kommentet vagy kifogásolod a leírtakat.\n\nBiztos?");
					break;
				default:
					var reason = prompt("A kommentet szerinted el kellene távolítani, ezért a moderátor figyelmébe ajánlod.\nIndokold meg röviden, miért kifogásolod!", "");
					if (reason && reason.length) {
						reason = $.trim(reason);
					}
					if (reason && reason.length) {
						process = true;
					}
			}
			if (!process) {
				return;
			}
			
			location = BASE_HREF + "comment-interaction/" +command + "/" + comment + "?r=" + encodeURI(reason);
		});
}

function enableTabbing() {
	if (!$("ul.tabs").length) {
		return;
	}
	var $activeTabLi;
	if (!location.hash.length || !location.hash.match(/^#tab-/)) {
		$activeTabLi = $("ul.tabs li:first");
	} else {
		$activeTabLi = $("ul.tabs a[href$=" + location.hash +"]").parents("li:first");
	}
	$activeTabLi.addClass("active");
	$("ul.tabs a").click(function(){
		activateTab(this);
		return false;
	});
	activateTab($("ul.tabs li.active a")[0]);
}
function activateTab(a) {
	$("div.tab").hide();
	$("ul.tabs li").removeClass("active");
	var tabId = $(a).attr("href");
	tabId = tabId.substr(tabId.indexOf("#") + 1);
	$(a).parents("li:first").addClass("active");
	$("div#" + tabId).show();
}

function activateDfn() {
	$("div#main-content dfn").click(function(){
		alert($(this).attr("title"));
	});
}

function scrollIntoViewFormError() {
	var firstErroneousRow = $("form div.row.warning:first");
	if (!firstErroneousRow.length) {
		return;
	}
	firstErroneousRow[0].scrollIntoView(true);
	alert(ValidatorData[""].invalid);
}

function scrollIntoViewFlood() {
	var warning = $("div#main-content p.warning");
	if (!warning.length) {
		return;
	}
	warning[0].scrollIntoView(true);
}

function initRandomQuotes() {
	var t = $("#random-quote");
	if (!t) {
		return;
	}
	setInterval(function(){
		t.load(BASE_HREF + "_system/modules/randomquote/getquote.php");
	}, 350000);
}

$(function(){
	prepareForms();
	enhanceContentTypography();
	dynamizeButtons();
	convertContactLinks();
	createPullQuotes();
	enhanceSearchForm();
	hidePoorComments();
	focusCommentTextfield();
	commentInteraction();
	enableTabbing();
	activateDfn();
	scrollIntoViewFormError();
	scrollIntoViewFlood();
	initRandomQuotes();
})

