
jQuery(document).ready(function() {
	jQuery('a.load-local').cluetip({ local:true,
								cluetipClass: 'jtip',
								arrows: true,
								cursor: 'pointer',
								sticky: true,
								closePosition: 'title',
								closeText: 'Закрыть <img src="/img/admin/red_close.gif" alt="close" />'
							});
	jQuery('a.leter-load').cluetip({ cluetipClass: 'jtip',
								activation: 'click',
								arrows: true,
								cursor: 'pointer',
								sticky: true,
								closePosition: 'title',
								closeText: 'Закрыть <img src="/img/admin/red_close.gif" alt="close" />'
							});
});

// ############## Objects ###############################

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 != '')
		{
			jQuery.post(url, param, callback);
		}
	},
	processResponse : function(req)
	{
		var i;
		if(jQuery('innerblock'))
			i = jQuery('innerblock');
		else if(jQuery('inner'))
			i = jQuery('inner');
		if(req.responseText)
		{
			if(i)
			{
				i.innerHTML = '';
				i.innerHTML = req.responseText;
			}
			/*else
				alert(req.responseText);*/
		}
		Loading.EndLoading();
	},
	close: function(name)
	{
		if(jQuery(name))
		{
			//Effect.Shrink(jQuery(name));
			document.body.removeChild(jQuery(name));
		}

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

Comments = {
	addAnswer : function(id_a)
	{
		if(id_a)
		{
			// Delete old form
			if(jQuery("#answer_add"))
			{
				var inp = jQuery("answer_add")['answer_id'];
				var inp_val = jQuery(inp).attr('value');

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

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



Cart = {
	AddToCart : function(tovarid)
	{
		Loading.StartLoading();

		if(!tovarid)
		{
			Loading.EndLoading();
			jQuery.prompt('Ошибка! Товар не добавлен в корзину!', { prefix: "jqi", promptspeed: "fast" });
		}
		else
		{
			jQuery.post('/tools/cart/',
						{type: "json", field: "cart", val:  tovarid},
						function (data) {
								 if(data.result)
								 	jQuery.prompt('Товар добавлен в корзину!', { prefix: "jqi", promptspeed: "fast" }); // .corner()
								 else
								 	jQuery.prompt('Ошибка! Товар не добавлен в корзину!', { prefix: "jqi", promptspeed: "fast" });

								 if(data.count && data.allsum)
								 {
									 jQuery('#carttovars').html(data.count);
									 jQuery('#cartallsum').html(data.allsum);
							 	}
						},
						"json"
				);
		}
		Loading.EndLoading();
	},
	updateFieldsCart : function(c_cnt, c_sum)
	{
		if(!c_cnt) c_cnt = 0;
		if(!c_sum) c_sum = 0.00;

		jQuery('#carttovars').html(c_cnt);
		jQuery('#cartallsum').html(c_sum);
	}
}


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;
	}
}


AnonsTabsCat = {
	LoadTab : function(url, param, conteyner)
	{
		Loading.StartLoading();
		jQuery.post(url, param, function (data) { jQuery(conteyner).html(data); })
		Loading.EndLoading();
	}
}


Menu = {
	changeview : function(id)
	{
		if(jQuery('#podli' + id) && jQuery('#podul' + id))
		{
			var tmp_ul = jQuery('#podul' + id);
			jQuery('#podul' + id).remove();
			jQuery('#podli' + id).append(tmp_ul);
		}
	}
}


CompareTovar = {
	clickCompare : function(elm, catid, tovarid)
	{
		Loading.StartLoading();
		if(!tovarid || !catid)
		{
			Loading.EndLoading();
			return false;
		}

		var addcompare = 0;
		if(jQuery(elm).attr("checked"))
			addcompare = 1;

		jQuery.post('/tools/compare/',
					{type: "json", cmpfield: addcompare, cmpcatid:  catid, cmptovarid: tovarid},
					function (data) {
							 if(data.result) {
							 	//jQuery.prompt('Товар добавлен в корзину!', { prefix: "jqi", promptspeed: "fast" }); // .corner()
							 }
							 else
							 	jQuery.prompt('Ошибка при сравнении товара!', { prefix: "jqi", promptspeed: "fast" });
					},
					"json"
			);
		Loading.EndLoading();
	}
}

