$(document).ready(function(){

	$('#runlogin').click(function(){
		 // $('body').attr('id','login');
		$('.sitelogindisabled').attr('class', 'sitelogin').hide().fadeIn("slow");
		$('.sitelogin').fadeIn("slow");
		$('.sitelogin input#username').focus();
		$('.sitelogin input#username').select();
		$('.sitelogin #username').focus(function(){
			$(this).attr('value','');
		});
		
		$('.sitelogin #password').focus(function(){
			$(this).attr('value','');
		});
	})
	
	$('#runlogout').click(function(){
		 // $('body').attr('id','login');
		 $('.sitelogindisabled').attr('class', 'sitelogin').hide().fadeIn("slow");
		$('.sitelogin').fadeIn("slow");
	
	})
	
	$('#abortlogin').click(function(){
		 // $('body').attr('id','login');
		$('.sitelogin').fadeOut("slow");
	
	})

});

var login = function(obj)
{
	this.initialize(obj);
}

login.prototype = {

	initialize: function(obj)
	{
			var self = this;
			this.obj = obj;
			this.setObservers();
	},
	
	setObservers: function(){
		var self = this;
		
		$(this.obj.loginBtn).click(function(){
			self.login();
			return false;
		});
		
	},
	
	login: function(){
	
		var self = this;
	
		$.ajax({
			type: "POST",
			url: '/',
			data: $(self.obj.form).serialize(),
			dataType : 'html',
			success: function(response)
			{
				if(response == 'false'){
					$(self.obj.result).html('Feil brukernavn eller passord, prøv på nytt.');
					$(self.obj.result).show();
				}else{
					 window.location = '/mi-side/';
				}
			},
			complete: function()
			{

			},
			error: function()
			{
		
			}
		});	
	}

};

$(document).ready(function()
{
	if($('#sitelogin').size() > 0){
		new login(
		{
			form: '#sitelogin',
			loginBtn: '#loginBtn',
			result: '#sitelogin .error'
		});
	}
});


