
var KRATT = KRATT || {};

var swapImages = function() {
	var $cur = $("#krotator img.current");
	$cur.css({"zIndex": 1000});

	if ($("#krotator img.current").is(":last-child")) {
		$("#krotator img:first-child")
			.css({"zIndex": 999, "opacity": 1, "display": "block"})
			.addClass("current");
	} else {
		$("#krotator img.current").next()
			.css({"zIndex": 999, "opacity": 1, "display": "block"})
			.addClass("current");
	}

	$cur.fadeOut().removeClass("current");

};

$(function() {
	if ($("#krotator").length) {
		swapImages();
		setInterval(function() {
			swapImages();
		}, 3000);
	}
});


KRATT.sendEmail = function() {
	$(function() {
		var $form = $("form.sendEmail");
		if (!$form.length) {
			return;
		}

		$form.find("input").bind("keyup", function() {
			if ($(this).val() != "") {
				$form.find("button").removeClass("disabled").attr("disabled", false);
			} else {
				$form.find("button").addClass("disabled").attr("disabled", true);
			}
		});

		$form.attr("autocomplete", "off").bind("submit", function(ev) {
			ev.preventDefault();

			$.ajax({
				url: $(this).attr("action"),
				data: $(this).serialize(),
				type: "POST",
				dataType: "json",
				success: function(response, textStatus) {
					if (response && response.status && response.status == 200 && response.message) {
						$("form").fadeOut(function() {
							$("#registerResponse")
								.html(response.message)
								.fadeIn(function() {
									$("#socialStuff").fadeIn();
								});
						});
					} else {
						$("#registerResponse")
							.addClass("error")
							.html("Paraku läks midagi näpusti :(")
							.fadeIn();
						$("#socialStuff").fadeIn();
						if (console && console.log) {
							console.log(response);
						}
					}
				},
				error: function(request, textStatus) {
					$("#registerResponse")
						.addClass("error")
						.html("Paraku läks midagi näpusti :(")
						.fadeIn();
					$("#socialStuff").fadeIn();
				}
			});
		});
	});
}();


KRATT.loginBox = function() {
	$(function() {
		var $login = $("#login:not(.loggedIn)");
		if (!$login.length) {
			return;
		}

		$login.bind("mouseover", function(e) {
			$(this).addClass("open");
			$(this).find(".loginArea").fadeIn(200);
			$("#username").focus();
		});

		$login.find(".loginTitle").bind("click", function(e) {
			if ($login.hasClass("open")) {
				$login.removeClass("open");
				$login.find(".loginArea").fadeOut(250);
			} else {
				$login.addClass("open");
				$login.find(".loginArea").fadeIn(200);
				$("#email").focus();
			}
		});
	});
}();


KRATT.AnimateClouds = function() {
	var minLength = 25,
	    maxLength = 50;

	var animate = function($cloud, duration) {

		$cloud.animate({
			backgroundPosition:"180% 0"
		}, duration * 1000
		, "linear"
		, function() {
			$(this).css({ "backgroundPosition": "-80% 0" });
			animate($(this), duration);
		});
	};

	$(function() {
		if (!$("#clouds div.animate").length) {
			return;
		}

		$("#clouds div").each(function() {
			var duration = Math.floor(Math.random() * (maxLength - minLength + 1)) + minLength;
			animate($(this), duration);
		});
	});
}();

