
Loading = {
	StartLoading : function()
	{
		if(document.getElementById('Loading'))
			Loading.EndLoading();
		var d = document.createElement("div");
		d.setAttribute("id", "Loading");
		d.className = "Loading";

		d.style.left = document.body.scrollWidth / 2 - 70 + "px";
		d.style.top = (document.documentElement.scrollTop || document.body.scrollTop) + 200 + 'px';

		var text = document.createTextNode("Loading...");
		d.appendChild(text);

		document.body.appendChild(d);
		document.body.style.cursor = "wait";
	},
	EndLoading : function()
	{
		if(document.getElementById('Loading'))
		{
			var d = document.getElementById('Loading');
			document.body.removeChild(d);
			document.body.style.cursor = "default";
		}
	},
	errors : function(s)
	{
		alert(s);
	},
	ErrorView : function(t)
	{
		alert('Error ' + t.status + ' -- ' + t.statusText);
	},
	processRequest : function(url, param, callback)
	{
		if(url != '')
		{
			$.post(url, param, callback);
		}
	},
	processResponse : function(req)
	{
		var i;
		if($('innerblock'))
			i = $('innerblock');
		else if($('inner'))
			i = $('inner');
		if(req.responseText)
		{
			if(i)
			{
				i.innerHTML = '';
				i.innerHTML = req.responseText;
			}
			/*else
				alert(req.responseText);*/
		}
		Loading.EndLoading();
	},
	close: function(name)
	{
		if($(name))
		{
			//Effect.Shrink($(name));
			document.body.removeChild($(name));
		}

		if($("MaskBody"))
		{
			document.body.removeChild($("MaskBody"));
		}
	}
}



Comments = {
	addAnswer : function(id_a, idpar)
	{
		if(id_a)
		{
			Loading.StartLoading();

			// Delete old form
			if(jQuery("#answer_add", $(idpar)))
			{
				jQuery("#insansferform", $(idpar)).parent().parent().parent().remove();

				/*var inp = jQuery("#answer_add", $(idpar))['answer_id'];
				var inp_val = jQuery(inp).attr('value');
				if(jQuery('#insertaddform'+ inp_val, $(idpar))) jQuery('insertaddform'+ inp_val, $(idpar)).empty();*/
			}
			jQuery('#tableaddcomm', $(idpar)).hide("slow");
			if(jQuery('#insertaddform'+ id_a, $(idpar)))
			{
				jQuery('#insertaddform'+ id_a, $(idpar)).empty();
				var cntn = '<table cellpadding="0" cellspacing="0" width="100%" border="0"><tbody><tr><td id="insansferform" style="display: none;">';
				cntn += '</td></tr></tbody></table>';
				jQuery('#insertaddform'+ id_a, $(idpar)).html(cntn);
				var dans = jQuery('#insansferform', $(idpar));
				var fcontent = jQuery('#comment_add', $(idpar)).html();
				// Create new form
				var newform = "<form id='answer_add' action='"+jQuery('#comment_add', $(idpar)).attr("action")+"' method='"+jQuery('#comment_add', $(idpar)).attr("method")+"'>";
				newform += '<input type="hidden" name="answer_id" value="'+id_a+'"/>';
				newform += fcontent + '</form>';
				jQuery('#insansferform', $(idpar)).append(newform);
				jQuery('#insansferform', $(idpar)).show("slow");
			}

			Loading.EndLoading();
		}
	},
	ChangeView : function(elm, parel)
	{
		Loading.StartLoading();

		if(jQuery(elm + ":hidden", parel))
		{
			jQuery(elm, parel).show("slow");
			/*if(elm == '#tableaddcomm')
				document.location='#info';
			else
				document.location='#MailFriend';*/
		}
		else jQuery(elm, parel).hide("slow");

		Loading.EndLoading();
	},
	PostComm : function()
	{
		var idform = '';

		if($("#dialog").html().match(/answer_add/ig))
		{
			idform = "#answer_add";
		}
		else if($("#dialog").html().match(/comment_add/ig))
		{
			idform = "#comment_add";
		}

		if(idform != '')
		{
			Loading.StartLoading();
			jQuery.post($(idform,  $("#dialog")).attr('action'), $(idform,  $("#dialog")).serialize(),
						function (data) {
							jQuery("#dialog").empty().html(data);

							if($("#idimg", data))
							{
								var idph = $("#idimg", data).val();
								if(idph > 0)
								{
									if($("#CntCommCurr_" + idph, data) && $("#cntcomm_" + idph))
										$("#cntcomm_" + idph).empty().html($("#CntCommCurr_" + idph, data).html());
								}
							}
						}
			);
			Loading.EndLoading();
		}
	},
	Voting : function(id_comm, idstory, rate, type_comm)
	{
		Loading.StartLoading();
		jQuery.post("/comments/rate/", {idcomm: id_comm, idst: idstory, bal: rate, typec: type_comm},
					function (data) {
						jQuery.prompt(data, {prefix: "jqi", callback: Comments.UpdateRateVal, buttons: { OK: true } });
					},
					"html"
			);
		Loading.EndLoading();
	},
	UpdateRateVal : function(v,m,f)
	{
		if(m.children("#currrate").attr('commid') > 0)
		{
			$("#rate" + m.children("#currrate").attr('commid')).html(m.children("#currrate").html());
		}
		return true;
	}
}



Login = {
	ClickLogin : function(url, param)
	{
		if(url != '')
		{
			Loading.StartLoading();
			jQuery.post(url, param,
					function (data) {
						jQuery.prompt(data, {prefix: "jqi", submit: Login.ActionLogin, buttons: { Вход: true, Отмена: false } }); // .corner()
					},
					"html"
			);
			Loading.EndLoading();
		}
	},
	ActionLogin : function(v,m,f)
	{
		if(v)
		{
			var frm = m.children('#loginform');
			var url = frm.attr('action');
			var param = frm.serialize();

			if(f.username == '')
			{
				jQuery('#username').css("border","1px solid #ff0000");
				return false;
			}
			else
				jQuery('#username').css("border","solid #bfbfbf 1px");

			if(f.passwd == '')
			{
				jQuery('#passwd').css("border","solid #ff0000 1px");
				return false;
			}
			else
				jQuery('#passwd').css("border","solid #bfbfbf 1px");

			if(url != '')
			{
				Loading.StartLoading();
				jQuery.post(url, param,
						function (data) {
							if(data.match(/form/ig))
								jQuery.prompt(data, {prefix: "jqi", submit: Login.ActionLogin, buttons: { Вход: true, Отмена: false } }); // .corner()
							else if(data.match(/loginoklink/ig))
							{
								jQuery.prompt(data, {prefix: "jqi", callback: Login.AffterLogin, buttons: { OK: true } }); // .corner()
							}
							else
								document.location=data;
						},
						"html"
				);
				Loading.EndLoading();
			}
		}
		return true;
	},
	AffterLogin : function(v,m,f)
	{
		Loading.StartLoading();
		if(v)
		{
			document.location = m.children('a').attr('href');
		}
		Loading.EndLoading();
	}
}



Subscribe = {
	ClickSubscribe : function(url, param)
	{
		if(url != '')
		{
			Loading.StartLoading();
			jQuery.post(url, param,
					function (data) {
						jQuery.prompt(data, {prefix: "jqi", submit: Subscribe.ActionSubscribe, buttons: { Подписаться: true, Отмена: false } }); // .corner()
					},
					"html"
			);
			Loading.EndLoading();
		}
		else
			return false;
	},
	ActionSubscribe : function(v,m,f)
	{
		if(v)
		{
			var frm = m.children('#Subscribe');
			var url = frm.attr('action');
			var param = frm.serialize();

			if(f.email == '')
			{
				frm.children('#email').css("border","solid #ff0000 1px");
				return false;
			}
			else
				frm.children('#email').css("border","solid #bfbfbf 1px");

			if(f.code_img == '')
			{
				frm.children('#code_img').css("border","solid #ff0000 1px");
				return false;
			}
			else
				frm.children('#code_img').css("border","solid #bfbfbf 1px");

			if(url != '')
			{
				Loading.StartLoading();
				jQuery.post(url, param,
						function (data) {
							if(data.match(/form/ig))
								jQuery.prompt(data, {prefix: "jqi", submit: Subscribe.ActionSubscribe, buttons: { Подписаться: true, Отмена: false } }); // .corner()
							else
								jQuery.prompt(data, {prefix: "jqi", buttons: { OK: true } }); // .corner()
						},
						"html"
				);
				Loading.EndLoading();
			}
		}
		return true;
	}
}


