$(document).ready(function() {
	
	$("#uname").focus( function(){
		$("#uname").val( "" );
	});
	
	$("#upass").focus( function() {
		$("#upass").val( "" );
	});
	
	$("#b_login").click( function(){
		user = $("#uname").val();
		pass = Whirlpool.hash( $("#upass").val() );
		$("#upass").val( "" );
		$.ajax({
			dataType: "json",
			type: "POST",
			url: "http://localhost/shiftedbits/ajax.php?login",
			data: {username: user, password: pass, rand: Math.random()},
			success: function(data, status){
				if( data["success"] == true )
				{
					$.cookie( "sb_id", data["id"], {expires: 1} );
					$.cookie( "sb_hash", data["hash"], {expires: 1} );
					$("#s_username").text( data['username'] );
					$("#d_login_fail").hide( "fast" );
					$("#d_login").hide( "fast" );
					$("#d_userbar").show("fast");
				}
				else
				{
					$("#d_login_fail").show( "fast" );
					$("#uname").val( "" );
					$("#upass").val( "" );
				}
			},
			error: ajaxFailure
		});
	});
	
	$("#b_logout").click( function(){
		$.cookie( "sb_id", null );
		$.cookie( "sb_hash", null );
		$("#d_userbar").hide( "fast" );
		$("#d_login").show( "fast" );
	});
	
	$(".configure").hover(
	function(){
		this.src = "http://localhost/shiftedbits/icons/configure_hot.png";
	},
	function(){
		this.src = "http://localhost/shiftedbits/icons/configure.png";		
	});

	$(".add").hover(
	function(){
		this.src = "http://localhost/shiftedbits/icons/list-add_hot.png";
	},
	function(){
		this.src = "http://localhost/shiftedbits/icons/list-add.png";		
	});
	
	$(".blue-folder").hover(
	function(){
		this.src = "http://localhost/shiftedbits/icons/folder-blue_hot.png";
	},
	function(){
		this.src = "http://localhost/shiftedbits/icons/folder-blue.png";		
	});
	
	$(".cyan-folder").hover(
	function(){
		this.src = "http://localhost/shiftedbits/icons/folder-cyan_hot.png";
	},
	function(){
		this.src = "http://localhost/shiftedbits/icons/folder-cyan.png";		
	});

	$(".green-folder").hover(
	function(){
		this.src = "http://localhost/shiftedbits/icons/folder-green_hot.png";
	},
	function(){
		this.src = "http://localhost/shiftedbits/icons/folder-green.png";		
	});

	$(".violet-folder").hover(
	function(){
		this.src = "http://localhost/shiftedbits/icons/folder-violet_hot.png";
	},
	function(){
		this.src = "http://localhost/shiftedbits/icons/folder-violet.png";		
	});

	$(".edit").hover(
	function(){
		this.src = "http://localhost/shiftedbits/icons/edit-rename_hot.png";
	},
	function(){
		this.src = "http://localhost/shiftedbits/icons/edit-rename.png";		
	});

	$(".favourite").hover(
	function(){
		this.src = "http://localhost/shiftedbits/icons/emblem-favorite_hot.png";
	},
	function(){
		this.src = "http://localhost/shiftedbits/icons/emblem-favorite.png";		
	});
});

function ajaxFailure( xhr, status, error){
	alert( "Error: "
			+ xhr.status + "\n"
			+ xhr.getAllResponseHeaders() + "\n"
			+ xhr.responseText + "\n"
			+ xhr.responseXML + "\n"
			+ xhr.statusText + "\n"
			+ status + "\n"
			+ error
	);
}

