	
	$(document).ready(function() {
		$.ajaxSetup( { type: "POST" } );
		changeLogoColor();

		var l = $.cookie('c_login');
		var p = $.cookie('c_password');
		
		if(l && l.length > 0 && p && p.length > 0) {
			$("#i_login").val(l);
			$("#i_password").val(p);
			if($("#page").attr("rel") == "0") setTimeout(function() { makeLogIn(); }, 2000);
			if($("#page").attr("rel") == "2") {
				$.cookie('c_password', null, { expires: 31, path: '/', domain: window.location.host });
				$("#i_password").val("");				
			}
		}
	});
		
	function changeLogoColor()
	{
		var c = new Array('840', 'b00', '080', '0b0', '088', '08b', '004', 'b0b', '808', '888');
		var i = Math.round(Math.random() * c.length);
		$('#logoimg').css('background-color', '#' + c[i]);
	}

	
	/*
	 * 	Registration
	 */
	
	function showBox(id)
	{
		$("ul.lc > li").removeClass("ac").toggleClass("c", true);
		$("div.fm").each(function(){
			if($(this).css("display") != "none") {
				$(this).fadeOut(function() {
					$("#" + id).toggleClass("c", false).addClass("ac");
					$("div#" + id + "box").fadeIn();
					
					if(id == "reg") reloadCaptcha('rcaptcha');
					if(id == "reset") reloadCaptcha('scaptcha');
					
					$("#result").fadeOut();
				});
			}
		});		
	}
	
	function reloadCaptcha(i)
	{
		var now = new Date();
		id = "" + now.getTime() + Math.floor(Math.random() * 99999);		
		$('#' + i + "-img").attr("src", "/testimg.php?" + id);
		$("#i_" + i).val("");
	}
	
	function makeLogIn() 
	{
		if($.FormCheck.validateForm($("#loginform")[0])) {
			$.cookie('c_login', $("#i_login").val(), { expires: 31, path: '/', domain: window.location.host });
			$.cookie('c_password', $("#i_password").val(), { expires: 31, path: '/', domain: window.location.host });
			$("#loginform").submit();
		}
	}

	function forget()
	{
		$.cookie('c_login', null, { expires: 31, path: '/', domain: window.location.host });
		$.cookie('c_password', null, { expires: 31, path: '/', domain: window.location.host });
		$("#i_login").val("");
		$("#i_password").val("");
	}	
	
	function register()
	{
		if($.FormCheck.validateForm($("#regform")[0])) {
			$('#result').fadeIn().html("<img src='/i/loading.gif' alt='Loading...' title='Loading...'/>").load(
				"/?usecase=Register", 
				$("#regform").serializeArray()
			);
		}	
	}
	
	function resetPassword()
	{
		if($.FormCheck.validateForm($("#resetform")[0])) {
			$('#result').fadeIn().html("<img src='/i/loading.gif' alt='Loading...' title='Loading...'/>").load(
				"/?usecase=ResetPassword", 
				$("#resetform").serializeArray()
			);			
		}			
	}
	
	function reset()
	{
		$("#regform").clearForm();
		$("#resetform").clearForm();
		setTimeout("showBox('login')", 5000);
	}
		
