//==============================================================================
//------------------------------------------------------------------------------
function pel()
{
}//pel

//------------------------------------------------------------------------------
pel.prototype.init = function()
{
	var o = this;
	
	$("#pel_dialog").html( o.form_in() );
	initOverLabels();
	
	$(".pel_send").bind( 'click', function()
	{
		o.send();
		return false;
	});
	
	$("#pel_button").toggle(
		function()
		{
			$("#pel_button span").text( "Close" );
			$("#pel_dialog").show( 200 );
		}
		, function()
		{
			$("#pel_button span").text( "Sign In" );
			$("#pel_dialog").hide( 200 );
		}
	);

	$("#pel_logout").bind( 'click', function()
	{
		var p = { _: 'aces_ay', aces_op: 'logout' };
		$.post( '/index.php', p, function( r )
		{
			var d = o.ay_parse( r );
			if ( d['reload'] ) window.location.reload();
		});
		return false;
	});
	
};

//------------------------------------------------------------------------------
pel.prototype.send = function()
{
	var o = this;
	
	// ziskani parametru z formulare
	var a = $("#pel_form").serializeArray();
	var f = {};
	for ( var i in a ) f[a[i].name] = a[i].value;
	
	var p = {};
	
	$("#pel_log").text( "Signing In ..." );
	
	p["_"] = "aces_ay";
	p["aces_op"] = 'login';
	p["aces_name"] = f['pel_name'];
	p["aces_pass"] = f['pel_pass'];
	
	$.post( '/index.php', p, function( r )
	{
		var d = o.ay_parse( r );
		
		var ec = d['error'];
		var es = '';
		if ( ec == '2-1' ) es= 'Bad login name!';
		else if ( ec == '2-2' ) es= 'Bad password!';
		else if ( ec ) es = 'Error code: ' + ec;
		$("#pel_log").text( es );
		
		if ( d['reload'] ) window.location.reload();
	});
	
	return false;
};

//------------------------------------------------------------------------------
pel.prototype.ay_parse = function( raw )
{
	/// konvertuje raw data na asociativni pole
	var data = { "": "" };
	var i, m, s;
	var a = raw.split( '\n' );
	for ( i in a )
	{
		if ( typeof( a[i] ) != 'string' ) continue;
		m = a[i].match( '^:([-_a-z0-9]+):(.*)$' );
		if ( m ) data[m[1]] = m[2].replace( /\\n/g, "\n" ).replace( /\\\\/g , "\\" );
		else     data[''] += a[i] + "\n";
	}
	return data;
};

//------------------------------------------------------------------------------
pel.prototype.form_in = function()
{
	var s= '\
<div class="signinform2">\
<h3 id="pel_log">Sign In</h3>\
<form id="pel_form">\
<div class="form">\
\
<div><label for="pel_name" class="overlabel">Username or Email</label><div class="txt"><input id="pel_name" name="pel_name" type="text" /></div></div>\
<div><label for="pel_pass" class="overlabel">Password</label><div class="txt"><input id="pel_pass" name="pel_pass" type="password" /></div></div>\
<div class="hid"><input alt="Log In" name="send" type="submit" class="pel_send login hid" value="Log In"         src="http://sta.geg.cz/images/transp.gif" /></div>\
		 <input alt="Log In" name="send" type="image"  class="pel_send login"     onclick="return false" src="http://sta.geg.cz/images/transp.gif" />\
\
</div>\
</form>\
<div class="register tind imgr"><a href="/registration">New to this server? <strong>Register here</strong><span></span></a></div>\
<div class="signin-links"><a href="/registration/reset">Forgot your password?</a></div>\
</div>\
	';
	return s;
};

//==============================================================================
var PEL = new pel();

$(document).ready(function()
{
	PEL.init();
});
//==============================================================================
/*
Local Variables:
mode: blank
End:
*/
