//очищает поля кода
var demo = $(".text");
var text = "Здесь появится код настроенного информера";
demo.attr('ntext', text);
demo.attr('value', text);
					

$("td[code]").click(selInfCell);
var currInfCell = null;
var infError = null;
function getDemo(text)
{
	var caption = $("input[name=title]").attr("value");
	var title = encodeURI(caption);	
	if (!title)
	{
		//title = $("input[name=title]").attr("def");
		if (text.indexOf("<title>") > 0 || text.indexOf("<caption>") > 0)
			infError = "не указан <b>" + $("[title_name]").html() + "</b>";
		return text;
	}
	caption = caption.replace('"', '&quot;');
	return text.replace("<title>", title).replace("<caption>", caption);
}
function getCode(text)
{
	var url = encodeURI($("input[name=domain]").attr("value"));
	if (!url)
	{
		url = $("input[name=domain]").attr("def");
		if (text.indexOf("<url>") > 0)
			infError = "не указан <b>" + $("[domain_name]").html() + "</b>";
	}
	return getDemo(text.replace(/(^&nbsp;+)|(^\s+)|(&nbsp;){2,}|( {2,})|(&nbsp;+$)|(\s+$)/g, '')).replace("<url>", url);
}
function selInfCell(e)
{
	if (currInfCell)
	{
		if (currInfCell.get(0) == this) return;
		
		currInfCell.removeClass("inf_cell_sel");
		if ($(this).attr('code') != currInfCell.attr('code'))
		{
			$("#" + currInfCell.attr('code') + "_error").css("visibility", "hidden");		
			var demo = $("#" + currInfCell.attr('code'));
			demo.attr('value', demo.attr('ntext'));
			demo.addClass("none");
		}
	}
	
	currInfCell = $(this);
	showInfCode();
}
function showInfCode()
{
	currInfCell.addClass("inf_cell_sel");
	infError = null;
	var code = $(currInfCell);
	var text = getCode(code.find('textarea').attr("value"));
	if (infError)
	{
		$("#" + code.attr('code') + "_error").css("visibility", "visible").html(infError);
		var demo = $("#" + code.attr('code'));
		demo.attr('value', demo.attr('ntext'));
		demo.addClass("none");
	}
	else 
	{
		$("#" + code.attr('code') + "_error").css("visibility", "hidden");
		$("#" + code.attr('code')).attr('value', text).removeClass("none");		
	}
}
function updateDemoInf(e)
{
	var title = $("input[name=title]").attr("value");
	if (title) $("td[code] [t]").html(title);
	
	if (currInfCell) showInfCode();
}
function keyUpdateDenoInf(event)
{
	if(event.keyCode==13) updateDemoInf(event);
};

