/**
 * LIDTmere 2009
 *
 * I like to moo.
 */

// Do the thing with the search field
$(function () {
	$("div.search input.query").each(function () {
		$(this).removeClass("blur");
		if (this.value == "") {
			this.value = this.title;
		}
		if (this.value == this.title) {
			$(this).addClass("blur");
		}
		$(this).focus(function () {
			if (this.value == this.title) {
				this.value = "";
				$(this).removeClass("blur");
			}
		});
		$(this).blur(function () {
			if (this.value == "") {
				this.value = this.title;
				$(this).addClass("blur");
			}
		});
	});
});

// Do the stupid scrolling thing with the ticker
$(function () {
	$("div.ticker p").each(function () {
		this.scrollLeft = this.offsetWidth;
	}).mouseover(function () {
		$(this).addClass("stopped");
		window.clearTimeout(this.ticker);
	}).mouseout(function () {
		$(this).removeClass("stopped");
		var ticker = this;
		this.ticker = window.setInterval(function () {
			ticker.scrollLeft += 1;
			if (ticker.scrollLeft >= ticker.scrollWidth - ticker.offsetWidth) ticker.scrollLeft = 0;
		}, 30);
	}).mouseout();
});

// Do that other thing with the help menu
$(function () {
	$("div.helpmenu li").each(function () {
		$(this).attr("description", $(this).children("a").attr("title")).mouseover(function () {
			if ($(this).find("span.description").length == 0) $(document.createElement("span")).addClass("description").append(document.createTextNode($(this).attr("description"))).css("z-index", "2").appendTo(this).fadeIn(200);
		}).mouseout(function () {
			$(this).children("span.description").css("z-index", "1").fadeOut(400, function () {
				$(this).remove();
			});
		}).children("a").removeAttr("title").click(function () {
			return false;
		});
	});
});

// Do those things that have to do with the images
$(function () {
	$("div.gallery").each(function () {
		if ($(this).find("ul.images li").length > 0) {
			$(this).append($(document.createElement("ul")).addClass("controls"));
			if ($(this).find("ul.images li").length > 1) {
				$(this).children("ul.controls").append($(document.createElement("li")).addClass("previous").append($(document.createElement("a")).attr("href", "").attr("title", "Forrige billede").append(document.createTextNode("Forrige")).click(function () {
					$(this).parents("div.gallery").find("ul.images li").hide().each(function (i, item) {
						if ($(item).is(".active")) {
							$(item).removeClass("active").show().css("z-index", "1");
							if ($(item).is(":first-child")) $(this).siblings(":last-child").addClass("active").css("z-index", "2").fadeIn(400);
							else $(item).prev().addClass("active").css("z-index", "2").fadeIn(400);
							return false;
						}
					});
					return false;
				}))).append($(document.createElement("li")).addClass("next").append($(document.createElement("a")).attr("href", "").attr("title", "Næste billede").append(document.createTextNode("Næste")).click(function () {
					$(this).parents("div.gallery").find("ul.images li").hide().each(function (i, item) {
						if ($(item).is(".active")) {
							$(item).removeClass("active").show().css("z-index", "1");
							if ($(item).is(":last-child")) $(this).siblings(":first-child").addClass("active").css("z-index", "2").fadeIn(400);
							else $(item).next().addClass("active").css("z-index", "2").fadeIn(400);
							return false;
						}
					});
					return false;
				})));
			}
		}
	});
});

