$(function() {
	Bola.initialize();
});

var Bola = {
	initialize: function() {
		Bola.detectResolution();
		Bola.prepareLoginForm();
		Bola.addBreakLineBeforeImages();
	},
	
	img_max_width: 770,
	
	image_loaded: function(img) {
		var width = img.width;
		var height = img.height;
		
		if (width > Bola.img_max_width) {
			img.width = Bola.img_max_width;
			img.height = height * (770/width);
		}
	},
	
	detectResolution: function() {
		if (window.screen.width > 800) {
			$("body").addClass("large");
		}
	},
	
	addBreakLineBeforeImages: function() {
		$("img").before("<br/>");
	},
	
	prepareLoginForm: function() {
		$("#fake-password").focus(function(){
			$(this).hide();
			$("#password").each(function(){
				$(this).show();
				this.focus();
			});
		});
		
		$("#password").blur(function(){
			if (this.value == "") {
				$(this).hide();
				$("#fake-password").show();
			}
		});
		
		$("#username")
			.focus(function(){
				if (this.value == "Username") {
					this.value = "";
				}
			})
			.blur(function(){
				if (this.value == "") {
					this.value = "Username";
				}
			});
			
		$("a.login").click(function(e){
			e.stopPropagation();
			e.preventDefault();
			
			if ($(this).is(".opened")) {
				$(this).removeClass("opened");
				$("#login-form").width("0px");
			} else {
				
				$(this).addClass("opened");
				$("#login-form").width("250px");
/*  				$("#login-form").animate({width:"250px"},1500); */
			}
			
			return false;
		});
		
		$("#submit").click(function(){
			var data = {
				username: $('#username').val(),
				password: $('#password').val()
			};
			
			$.ajax({
				dataType: 'json',
				type: 'post',
				url: BASE + '/wp-login-ajax.php',
				data: data,
				complete: function(success, data) {
					if (!success) {
						alert('Unable to login. Try again.');
					}
				},
				success: function(authenticated) {
					if (authenticated) {
						$('.login').hide();
						$('.logout').show();
						$('#login-form').hide();
					} else {
						alert('Invalid login data.');
					}
				}
			});
		});
	}
}