﻿// created by fredrik@yard.se. copyright (c) by digitalyard sverige ab

// animates a series of div:s as slides, delay can be "slow", "normal", "fast" or a number
// type can be 0 for random animation type, 1 for sliding up-to-down, 2 sliding-down-up
function AnimateSlides(args, type, delay) {
	// check for defaults
	switch (delay) {
		case "slow":
			delay = 20000;
			break;
		case "fast":
			delay = 2000;
			break;
		default:
			delay = 5000;
			break;
	}

	// chain items with the .nextanim attribute
	var x;
	for (x = 0; x < args.length; x++) {
		$(args[x]).attr("nextanim", args[(x + 1) % args.length].id);
	}

	// start chain of animation!
	var e = 'easeOutCubic';
	if ((!type) || (type == 0) || (type > 5)) {
		type = Math.floor(Math.random() * 5 + 1);
		switch (Math.floor(Math.random() * 2 + 1)) {
			case 1:
				e = 'easeOutCubic';
				break;
			case 2:
				e = 'easeOutBounce';
				break;
		}
	}
	var o3 = { duration: 3000, easing: e };
	setTimeout(AnimateSlidesWorker($(args[0]), type, o3, delay), delay);
}
function AnimateSlidesWorker(o, type, options, delay) {
	return function() {
		var o1 = { duration: options.duration, easing: options.easing };
		var o2 = { duration: options.duration, easing: options.easing };
		var o3 = { duration: options.duration, easing: options.easing };
		var n = $("#" + o.attr("nextanim"));
		if (type == 1) {
			// left
			n.css("left", o.outerWidth());
			n.show();
			o.animate({ left: -o.outerWidth() }, o1);
			n.animate({ left: 0 }, o2);
		} else if (type == 2) {
			// top
			n.css("top", -o.outerHeight());
			n.show();
			o.animate({ top: o.outerHeight() }, o1);
			n.animate({ top: 0 }, o2);
		} else if (type == 3) {
			// right
			n.css("left", -o.outerWidth());
			n.show();
			o.animate({ left: o.outerWidth() }, o1);
			n.animate({ left: 0 }, o2);
		} else if (type == 4) {
			// bottom
			n.css("top", o.outerHeight());
			n.show();
			o.animate({ top: -o.outerHeight() }, o1);
			n.animate({ top: 0 }, o2);
		} else if (type == 5) {
			// fade
			n.fadeIn();
			o.fadeOut();
		}
		setTimeout(AnimateSlidesWorker(n, type, o3, delay), delay);
	};
}

$(document).ready(function(event) {
	// init spotlight
	var i = $("img[id^=splash]");
	i.not("#splash1").css("display", "none");
	AnimateSlides(i, 0, "slow");

	// init contact
	$("input[type=text],textarea").not('.special').focus(function () {
		var t = $(this);
		if (!t.data("defaultValue")) {
			t.data("defaultValue", t.val());
		}
		if (t.val() == t.data("defaultValue")) {
			t.val("");
			t.css("color", "#222");
		}
	}).blur(function() {
		var t = $(this);
		if ((t.val().length == 0) && (t.data("defaultValue"))) {
			t.val(t.data("defaultValue"));
			t.css("color", "#666");
		}
	});
});
