$(document).ready(function() {	
	var frm	= $("form[@name='login']");				
	$("input[@name='username']",frm).blur(function() { cfInput(this,2); }).focus(function() { cfInput(this,1); });
	$("input[@name='password']",frm).blur(function() { ctpInput(this,2); });
	$("input[@name='fake_password']",frm).focus(function() { ctpInput(this,1); });
	$("input[@name='login']",frm).click(function() { login(); return false; });
	$("a.btnSignOff").click(function() { logout(); return false; });
	
	$("#clsWnd").click(function () {
		parent.location.reload(); 
   });
   
   $("#clsWnd2").click(function () {
		parent.location.reload(); 
   });
});

function cfInput(obj,bool) 
{
	if ((!bool || bool == 1) && $(obj).val() == $(obj).attr('default')) $(obj).val(''); 
	else if ((!bool || bool == 2) && $(obj).val() == '') $(obj).val($(obj).attr('default'));	
}

function ctpInput(obj,bool) 
{
	if ((!bool || bool == 1) && $(obj).val() == $(obj).attr('default')) { $(obj).hide();	$(obj).parent().find("input[@type='password']").show().focus(); }
	else if ((!bool || bool == 2) && $(obj).val() == '') { $(obj).hide();	$(obj).parent().find("input[@type='text']").show(); }
}

function login() 
{
	var frm	= $("form[@name='login']");
	var usr = $("input[@name='username']",frm);
	var pwd = $("input[@name='password']",frm);

	if (usr.val() == '' || usr.val() == usr.attr('default') || pwd.val() == '') 
	{
		$("div.btnLoginError span").html("Vnesite uporabniško ime in geslo").parent().fadeIn();
		setTimeout("$('div.btnLoginError').fadeOut();",3000);		
	}
	else
	{				
		$("div.btnLoginError").hide();
		
		$.getJSON("/script/action/login.php", "un="+usr.val()+"&pw="+pwd.val(), function(data) 
		{			
			if (data.code == 0) {				
				$("div.btnLoginError span").html(data.message).parent().fadeIn();
				setTimeout("$('div.btnLoginError').fadeOut();",3000);				
			}
			else if (data.code == 1) {
				window.location = window.location;
			}	
		});
	}
	return;		
}

function logout() 
{	
	$.getJSON("/script/action/logout.php", "", function(data) 
	{			
		if (data.code == 1) {
			window.location = window.location;
		}	
	});
}

function VF_clearErrors() 
{
	$('#VF_statusMsg').html('');
}

function VF_formResponse(data) 
{	
	var status = data.status;
	var message = data.message;	
	
	if (status == 1) {
		$('#rateAndCommentOn').hide();
		$('#rateAndCommentOff').show();
		$('#VF_disableMsg').html(data.message);
		$('SELECT[name=commentSort]').change();
	}
	else {								
		$('#VF_statusMsg').html(data.message);		
	}
	
	refreshCaptcha();	
}	

function refreshCaptcha() 
{
	var rndCode = Math.random();
	$('#captchaImage').attr('src',$('#captchaImage').attr('src')+'&r='+rndCode);
}

