/**	Create by olmiha@gmail.com	**/
/**
*	$Id$
*/

var PosWinY;
var PosWinX;
var actionGl = '';


Loading = {
	Init : function(e)
	{
		PosWinY = Event.pointerY(e);
		PosWinX = Event.pointerX(e);
	},
	StartLoading : function()
	{
		if($('Loading'))
			Loading.EndLoading();
		var d = document.createElement("div");
		d.setAttribute("id", "Loading");
		d.className = "Loading";
		 if(PosWinX)
			d.style.left = (PosWinX + 20) + "px";
		else
			d.style.left = document.body.clientWidth / 2 - 100 + "px";
		if(PosWinY)
			d.style.top = PosWinY + "px";
		else
			d.style.top = document.body.scrollTop + 100 + "px";

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

		document.body.appendChild(d);
		document.body.style.cursor = "wait";
	},
	EndLoading : function()
	{
		if($('Loading'))
		{
			var d = $("Loading");
			document.body.removeChild(d);
			document.body.style.cursor = "default";
		}
	},
	errors : function(s)
	{
		alert(s);
	},
	ErrorView : function(t)
	{
		alert('Error ' + t.status + ' -- ' + t.statusText);
	},
	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();
	},
	showDialogDiv: function(txt)
	{
		Loading.StartLoading();
		if($('DialogText'))
			Loading.close('DialogText');

		var d = document.createElement("div");
		d.setAttribute("id", "DialogText");
		d.className = "LoginBlock";
		d.style.display = "none";

		if(PosWinX)
			d.style.left = (PosWinX + 20) + "px";
		else
			d.style.left = document.body.scrollWidth / 2 - 70 + "px";

		if(PosWinY)
			d.style.top = PosWinY + "px";
		else
			d.style.top = 30 + "px";

		var text = '<table cellpadding="0" cellspacing="0" border="0" width="100%">';
		text += '<tr><td style="height: 26px; padding: 20px 30px 20px 20px;" align="right" valign="middle"><a style="color: #fff;" href="javascript: Loading.close(\'DialogText\');"">X</a>';
		text += '</td></tr><tr>';
		text += '<td align="center">';
		text += txt;
		text += '</td></tr></table>';

		//var t = document.createTextNode(text);
		//d.appendChild(t);
		d.innerHTML = text;
		document.body.appendChild(d);

		Effect.Grow(d);
		new Draggable('DialogText', {} );

		Loading.EndLoading();
	},
	close: function(name)
	{
		if($(name))
		{
			Effect.Shrink($(name));
			//document.body.removeChild($(name));
		}

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


Schetchik = {
	Up : function (el)
	{
		var d = document.getElementById(el);
		var dv = d.value;
		if(dv.match(/[\d+]/gi))
		{
			d.value = parseInt(dv) + 1;
		}
		else { alert("Error! Not number."); }
	},
	Down : function (el)
	{
		var d = document.getElementById(el);
		var dv = d.value;
		if(dv.match(/[\d+]/gi))
		{
			dv = parseInt(dv);
			if(dv > 0)
				d.value =dv - 1;
		}
		else { alert("Error! Not number."); }
	}
}


//	Login
Login = {
	Init : function(action, type, cat, idbl)
	{
		Loading.StartLoading();
		this.NoLogin();
		new Ajax.Request('/index.php', {
							method: 'post',
							parameters: 'mode=Ajax&actionblock=' + action + '&sex=' + type + '&C=' + cat + '&B=' + idbl,
							onSuccess:Login.ParseLogin,
							onFailure:Loading.ErrorView
						});
		actionGl = action;
		//Loading.EndLoading();
	},
	PostLogin : function(f)
	{
		Loading.StartLoading();
		this.NoLogin();
		var param = Form.serialize($(f));
		new Ajax.Request('/index.php', {
									method: 'post',
									parameters:param + '&mode=Ajax',
									onSuccess:Login.ParseLogin,
									onFailure:Loading.ErrorView
								});
		//Loading.EndLoading();
	},
	ParseLogin : function(req)
	{
		if(req.responseText.match(/Welcome to your account!/gi))
			document.location.href = document.location;
		if($('ContLogin'))
			this.NoLogin();
		var text = req.responseText;
		//alert(window.event);
		//alert(document.body.position + ' ' + document.body.scrollWidth + ' ' + document.body.offsetHeight + ' ' + document.body.scrollTop);
		var d = document.createElement("div");
		d.setAttribute("id", "MaskBody");
		d.setAttribute("align", "center");
		d.className = "MaskBody";
		if(actionGl == '')
		{
			if(document.body.scrollHeight < 1000)
				d.style.height = '1050px';
			else
				d.style.height = document.body.scrollHeight + "px";
		}
		else
			d.style.height = document.body.scrollHeight + "px";
		d.style.width = document.body.scrollWidth + "px";

		document.body.appendChild(d);

		var c = document.createElement("div");
		c.setAttribute("id", "ContLogin");
		if(actionGl == 'choose' || actionGl == 'login' || actionGl == 'text')
			c.className = "LoginBlock";
		else
			c.className = "RegisterBlock";
		c.style.display = "none";
		//c.style.height = "200px";
		c.style.left = document.body.clientWidth / 2 - 300 + "px";
		//c.style.top = document.body.clientHeight / 2 - 100 + "px";
		if(PosWinY)
		{
			var h = PosWinY - 300;
			c.style.top = h + "px";
			PosWinY = 0;
		}
		else
			c.style.top = document.body.scrollTop + 50 + "px";
			//c.style.top = document.body.scrollTop + (document.body.clientHeight / 2) - 100 + "px";
		document.body.appendChild(c);

		Effect.Grow(c);
		//Effect.Appear(c);
		//Effect.BlindDown(c);

		var con = document.getElementById("ContLogin");
		con.innerHTML = text;

		// Change input file view
		SI.Files.stylizeAll();

		new Draggable('ContLogin', {} );
		Loading.EndLoading();
	},
	NoLogin : function()
	{
		if(document.getElementById("ContLogin"))
		{
			var c = document.getElementById("ContLogin");
			document.body.removeChild(c);
		}
		if(document.getElementById("MaskBody"))
		{
			var d = document.getElementById("MaskBody");
			document.body.removeChild(d);
		}
	}
}


InvateFriend = {
	Show : function()
	{
		if($('invatefriend'))
		{
			if($('invatefriend').style.display == 'none')
				$('invatefriend').style.display = 'block';
			else
				$('invatefriend').style.display = 'none';
		}
	}
}


Amneziy = {
	Show : function()
	{
		if($('amneziyform'))
		{
			var d = $('amneziyform').innerHTML;
			Loading.showDialogDiv(d);
			$('amneziyform').style.display = 'none';
		}
	}
}
