+$(document).ready(function(){
	
	
	
	/* Inline modal windows */
	$(".modal_lostpassword").colorbox({inline:true, href:"#lostpassword"});
	$(".modal_fconnect").colorbox({inline:true, href:"#fconnect"});
	
	$().bind('cbox_complete', function(){
  	$("input.lostpassword").focus();
  	if($("input.lostpassword").val() == "") {
    		$("label.lostpassword").removeClass("hide");
			$("label.lostpassword").removeClass("fade");
     	}
	});
	
	$("a.closemodal").click(function() {
		$.fn.colorbox.close();
		return false;
	});
	
	

	/* Basic form styling */
	$("input.username").focus(); 
	$('input:checkbox').checkbox();
	$("#lostpassword form button.button").addClass("disabled");
	$(".button").mousedown(function () {
		$(this).addClass("active");
	});
	$(".button").mouseup(function () {
		$(this).removeClass("active");
	});


	
	/* #login input - Username */
	$("input.username").ready(function() {
		if($("input.username").val() == "") {
			$("label.username").removeClass("hide");
			$("label.username").addClass("fade");
		 }
  	});
	$("input.username").focus(function() {
		$("label.username").addClass("fade");
	});
	$("input.username").mousedown(function() {
		$("label.username").addClass("fade");
	});
	$("input.username").keypress(function() {
		$("label.username").addClass("hide");
	});
	$("input.username").blur(function() {
  	if($("input.username").val() == "") {
    	$("label.username").removeClass("hide");
		$("label.username").removeClass("fade");
     }
  });
  
	/* #login input - Password */
	$("input.password").ready(function() {
		if($("input.password").val() == "") {
			$("label.password").removeClass("hide");
		 }
  	});
	$("input.password").focus(function() {
		$("label.password").addClass("fade");
	});
  $("input.password").mousedown(function() {
		$("label.password").addClass("fade");
	});
  $("input.password").keypress(function() {
		$("label.password").addClass("hide");
	});
  $("input.password").blur(function() {
  	if($("input.password").val() == "") {
    	$("label.password").removeClass("hide");
		$("label.password").removeClass("fade");
     }
  });
  
  /* #login input - Lost password */
	$("input.lostpassword").focus(function() {
		$("label.lostpassword").addClass("fade");
	});
	$("input.lostpassword").mousedown(function() {
		$("label.lostpassword").addClass("fade");
	});
	$("input.lostpassword").keypress(function() {
		$("label.lostpassword").addClass("hide");
	});
	$("input.lostpassword").blur(function() {
  	if($("input.lostpassword").val() == "") {
    	$("label.lostpassword").removeClass("hide");
		$("label.lostpassword").removeClass("fade");
     }
  });
  
  
  
  /* #login form - Validate form on keyup and submit */
	$("#login").validate({
		submitHandler: function(form) {
			$('span', '#login button.button').html("Laster...");
			$("#login button.button").addClass("complete");
	   	form.submit();
   	},
		errorElement: "label",
		rules: {
			username: "required",
			username: {
				required: true
			},
			password: "required",
			password: {
				required: true
			}
		}
	});
  
  
  
  /* #lostpassword form - Validate form on keyup and submit */
	$("#lostpassword form").validate({
		submitHandler: function(form) {
		
			/* 
				AJAX submit goes here 
			*/
 
			$.post("/ajax/lostpassword.php", { email: $("input.lostpassword").val() } , function(data){
			  $('div#lostpassword div.modal_content').html(data).hide().fadeIn(1200);
			  $('div#lostpassword div.modal_footer').html("<a href='' class='button closemodal'><span>Lukk</span></a>").fadeIn(1200);
			});

			//alert( returndata );
			
			/* User feedback */
			
			
			
			/* Button state - Mousedown */
			$(".button").mousedown(function () {
				$(this).addClass("active");
			});
			
			/* Button state - Mouseup */
			$(".button").mouseup(function () {
				$(this).removeClass("active");
			});
			
			$("a.closemodal").click(function() {
				$.fn.colorbox.close();
				return false;
			});
	    
   },
   
		errorElement: "label",
		rules: {
			lostpassword: "required",
			lostpassword: {
				required: true,
				email: true
			}
		}
	});
	
	
  
  /* #login button - Disables button (on site load) if any input fields are empty (to avoid bugging browsers autofill support) */
  $("#login").ready(function() {
		var username = $("input.username").val();
		var password = $("input.password").val();
		if(username && password && !this.value) {
   			$("#login button.button").removeClass("disabled");
		}
		else {
   			$("#login button.button").addClass("disabled");
		}
	});
  
  /* #login button - Enables button if all input fields has content */
  $("#login").keyup(function() {
		var username = $("input.username").val();
		var password = $("input.password").val();
		if(username && password && !this.value) {
   			$("#login button.button").removeClass("disabled");
		}
		else {
   			$("#login button.button").addClass("disabled");
		}
	});

	/* #lostpassword button - Enables button if all input fields has content */
  $("#lostpassword form").keyup(function() {
		
		var email = $("input.lostpassword").val();
		//var filter = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
		
		if(!validateEmail( email )){
   			$("#lostpassword form button.button").addClass("disabled");
		}
		else {
			$("#lostpassword form button.button").removeClass("disabled");
		}
	});

	

	/* #login checkbox - Outputs a message if the checkbox is checked */
	/* label on click */
	$("label span").mousedown(function() {
		if($('input.rememberuser').is(':checked')){
			$(".tooltip").animate({
				top: "18px",
				opacity: "0"
			},300, 'easeOutQuart');
			window.clearTimeout(myTimeout);
		}
		else {
			$(".tooltip").css({
				visibility: "visible"
			});
			$(".tooltip").animate({
				top: "25px",
				opacity: "1"
			},200, 'easeInSine');
			myTimeout = window.setTimeout(function() {  
				$(".tooltip").animate({
				opacity: "0"
			},2000, 'easeOutQuart'); }, 4200);
			}
	});
	/* checkbox on click */
	$(".jquery-checkbox img").mousedown(function() {
		if($('input.rememberuser').is(':checked')){
			$(".tooltip").animate({
				top: "18px",
				opacity: "0"
			},300, 'easeOutQuart');
			window.clearTimeout(myTimeout);
		}
		else {
			$(".tooltip").css({
				visibility: "visible"
			});
			$(".tooltip").animate({
				top: "25px",
				opacity: "1"
			},200, 'easeInSine');
			myTimeout = window.setTimeout(function() {  
				$(".tooltip").animate({
				opacity: "0"
			},2000, 'easeOutQuart'); }, 4200);
			}
	});
	
	
	
	/* Logo */
	$("#logo a").click(function(){
		return false;
	});
		


});