﻿var ESC_CODE_KEY = 27;
var ENTER_CODE_KEY = 13;
var focusedInputSelector = null;
var formAnt;
var webMethodsAddress = "WebMethods.aspx/";
var emoEnum = { "lol": 1, "nice": 2, "fury": 3, "blue": 4 };
var imgAddress = "i/";
var articleIconsAddress = "i/articleicons/";
var categoryIconsAddress = "i/categoryicons/";
var fullArticleHeight = 400;

//		Валидаторы

var formValidators = [];
formValidators["addArticle"] =
[
{
	field: ".articleForm .linkControl",
	validators:
	[
	{
		type: "length",
		min: 4,
		max: 255
	},
	{
		type: "url",
		message: "Пожалуйста, проверьте написание адреса.",
		regex: new RegExp("^((([hH][tT][tT][pP][sS]?|[fF][tT][pP])\\:\\/\\/)?([\\w\\.\\-]+(\\:[\\w\\.\\&%\\$\\-]+)*@)?((([^\\s\\(\\)\\<\\>\\\\\\\"\\.\\[\\]\\,@;:]+)(\\.[^\\s\\(\\)\\<\\>\\\\\\\"\\.\\[\\]\\,@;:]+)*(\\.[a-zA-Z]{2,4}))|((([01]?\\d{1,2}|2[0-4]\\d|25[0-5])\\.){3}([01]?\\d{1,2}|2[0-4]\\d|25[0-5])))(\\b\\:(6553[0-5]|655[0-2]\\d|65[0-4]\\d{2}|6[0-4]\\d{3}|[1-5]\\d{4}|[1-9]\\d{0,3}|0)\\b)?((\\/[^\\/][\\w\\.\\,\\?\\'\\\\\\/\\+&%\\$#\\=~_\\@-]*)*[^\\.\\,\\?\\\"\\'\\(\\)\\[\\]!;<>{}\\s\\x7F-\\xFF])?)$") //источник - http://regexlib.com/REDetails.aspx?regexp_id=1121
	}
	]
},
{
	field: ".articleForm .previewControl",
	validators:
	[
	{
		type: "length",
		min: 5,
		max: 255
	}
	]
}
];

$(document).ready(function() {
	formAnt = new FormAnt({ validate: formValidators });

	var locationHash = location.hash.replace(/^#/, "");
	var query = ParseHash(locationHash);
	if (query["link"] && query["aid"]) {
		ShowArticle(query["aid"], query["link"]);
	}

	$(document).keypress(function(e) {
		if (e.keyCode == ENTER_CODE_KEY) {
			if (focusedInputSelector != null) {
				$(focusedInputSelector).trigger("enterPressed");
			}
		}
		if (e.keyCode == ESC_CODE_KEY) {
			CloseAllControlPanels();
		}
	});
	
	$(".nav1,.nav2").click(function(){
		location.href=$(this).find("a").attr("href");
		//$(this).find("a").trigger("click");
	});

	$(".link").live("click", function() {
		ShowArticle($(this));
		return false;
	});
	
	$(".addlink").click(function() {
		$(".emoIdControl").val(GetEmoIdOfArticle($(this)));
		AddArticle();
		return false;
	});

	$(".articleIcon").click(function() {
		$(".iconIdControl").val(GetIdFromClass($(this), "id_"));
		$(".selectedIcon").removeClass("selectedIcon").addClass("articleIcon");
		$(this).removeClass("articleIcon").addClass("selectedIcon");
	});

	$(".closearticle").live("click", function() {
		CloseArticle($(this));
		return false;
	});

	$(".article").live("click", function() {
		ShowControlPanel($(this));
		return false;
	});

	$(".closeemo").live("click", function() {
		CloseAllControlPanels();
		return false;
	});


});

function GetLink(link, width, height) {
	return "innerIFrame.htm" +
		   "#link=" + link +
		   "&width=" + (width - 8) +
		   "&height=" + (height - 23);
}

function ShowArticle(jEl) {

	var jBlock = jEl.parent().parent();
	jBlock.addClass("openarticle");
	var jFullArticle = $(".template").filter(".fullarticle").clone(true);
	jFullArticle.removeClass("template");
	jBlock.after(jFullArticle);
	jFullArticle.show();

	var clearedLink = ClearHashLink(jEl.attr("href"));

	var jFullArticleContent = jFullArticle.find(".fullarticlecontent");

	jFullArticleContent.empty().append(
		$("<iframe/>").attr({
			src: GetLink(clearedLink, jFullArticleContent.width(), fullArticleHeight),
			frameborder: "0",
			width: "100%",
			height: "100%"
		})
	);

	jEl.removeClass("link").addClass("closearticle");
}

function CloseArticle(jEl) {
	var jBlock = jEl.parent().parent();
	jBlock.removeClass("openarticle");
	jBlock.next().remove();
	jEl.removeClass("closearticle").addClass("link");
}

function ShowControlPanel(jEl) {
	CloseAllControlPanels();

	var controlPanel = $(".controlpanel").filter(".template").clone(true);
	controlPanel.removeClass("template").find(".template").removeClass("template");
	
	controlPanel.find(".categoryIdControl").val(GetIdFromClass(jEl, "category_"));
	controlPanel.find(".linkControl").val(ClearHash(jEl.find(".articlename a").attr("href")));
	controlPanel.find(".previewControl").val(jEl.find(".articlename a").text());
	
	jEl.before(controlPanel);
}

function CloseAllControlPanels() {
	$(".controlpanel").not(".template").remove();
}

function AddArticle() {
	if (!formAnt.Validate("addArticle")) return false;

	var article = formAnt.GetObject("article");

	article.challenge = "";
	article.response = "";

	sendJson(webMethodsAddress + "AddArticle", article, OnAddArticleSuccess, OnAddArticleFailure, true, { debugMode: false });
}
function OnAddArticleSuccess(d, o) {
}
function OnAddArticleFailure(d, e, o) {
}

function GetEmoIdOfArticle(jEl) {
	var classNames = jEl.getClassNames();

	for (var i in classNames) {
		if (classNames[i].indexOf("article") >= 0) {
			return GetFromEnum(classNames[i].replace("article", ""), emoEnum);
		}
	}
}
