var wave = function() {
	$('#waves').animate({
		backgroundPositionX : '+=200'
	}, 1000, "linear", function() {
		wave();
	});
}
$(window).load(function() {
	// wave();

	$('#waves').css("background-position", Math.floor(Math.random() * 1920) + "px 0px");
	$('.rollover').hover(function() {
		var img = $(this).children("img");
		img.attr("org-src", img.attr("src"));
		img.attr("src", img.attr("src").replace(img.attr("remove"), ""));
		$(this).children(".hint").animate({
			opacity : 1
		}, 300);
	}, function() {
		var img = $(this).children("img");
		img.attr("src", img.attr("org-src"));
		//$(this).children(".label").fadeOut("fast");
		$(this).children(".hint").animate({
			opacity : 0
		}, 300);
	});
	var boxshotAnims = new Array();

	var boxshotAnim = function(containerId, normal, images) {
		this.containerId = containerId;
		this.normal = normal;
		this.images = images;
		this.frame = 0;
		this.play = false;
		this.next = function() {
			if(this.images.length <= this.frame)
				return;
			this.setImage(this.images[++this.frame]);
			if(this.play)
				setTimeout(function() {
					boxshotAnims[containerId].next()
				}, 1000 / 50);
		}
		this.prev = function() {
			if(this.frame == 0)
				return;

			this.setImage(this.images[--this.frame]);
			setTimeout(function() {
				boxshotAnims[containerId].prev()
			}, 1000 / 50);
		}
		this.setImage = function(img) {
			if(img == undefined)
				return;
			var container = $("#" + containerId);
			container.children().attr("src", img.attr("src"));
		}
		this.stop = function() {
			if(!this.play)
				return;
			this.play = false;
			this.prev();
		}
		this.start = function() {
			if(this.play)
				return;
			this.play = true;
			this.next();
		}
	};
	$('.boxshot').each(function(index, element) {
		var normal = $(this).attr("normal");
		var hover = $(this).attr("hover");
		var id = "boxshot-" + index;
		$(this).attr("id", id);

		var tmp = hover.substr(hover.indexOf("[") + 1);
		tmp = tmp.substr(0, tmp.indexOf("]"));
		tmp = tmp.split("-");
		var min = tmp[0];
		var max = tmp[1];
		var images = new Array();
		for(var i = parseInt(min, 10); i <= parseInt(max, 10); i++)
		images[i] = $('<img />').attr('src', hover.replace(/\[.*\]/, $.sprintf("%0" + min.length + "d", i)));

		boxshotAnims[id] = new boxshotAnim($(this).attr("id"), normal, images);

		var label = $('<div>' + $(this).text() + '</div>').attr('class', 'label');
		$(this).text("");
		$(this).append(label);

		var img = $('<img />').attr('src', normal);
		$(this).append(img);
		$(this).hover(function() {
			$(this).children("img").css("clip", "auto");
			boxshotAnims[id].start();
		}, function() {
			$(this).children("img").css("clip", "");
			boxshotAnims[id].stop();
		});
	});
	$('.rollover').each(function() {
		$(this).append("<div class=\"hint\">" + $(this).attr("label") + "</div>");
		$(this).append("<div class=\"clear\"></div>");
	});
	$('.label').each(function() {
		$(this).css("backgroundImage", "url(gfx/rounded.php?shadeHeight=60&height=" + $(this).outerHeight() + "&width=" + $(this).outerWidth() + "&color1=f3d1b3&color2=f3d1b3)");
	});
	$('.intro').each(function() {
		$(this).css("backgroundImage", "url(gfx/rounded.php?shadeHeight=60&height=" + $(this).outerHeight() + "&width=" + $(this).outerWidth() + "&color1=EEE&color2=EEE)");
	});
	// slideboxes
	var slideboxesCount = $('.slidebox').size();
	var padding = 0;
	var margin = 0;
	var height = 0;
	$('.slidebox').each(function() {
		padding += parseInt($(this).css("padding-left")) + parseInt($(this).css("padding-right"));
		margin += parseInt($(this).css("margin-left")) + parseInt($(this).css("margin-right"));
	});
	$('.slidebox').each(function() {
		var spacing = (padding + margin) / slideboxesCount;
		var width = $(this).parent().innerWidth() / slideboxesCount - spacing;
		$(this).css("width", width + "px");

		if(height < $(this).height())
			height = $(this).height();
	});
	$('.slidebox').each(function() {
		var color1 = "EEE";
		var color2 = "FFF";
		if($(this).attr("color1"))
			color1 = $(this).attr("color1");
		if($(this).attr("color2"))
			color2 = $(this).attr("color2");

		$(this).css("overflow", "hidden");
		$(this).css("position", "relative");
		$(this).css("height", height);

		$(this).children("div").css("position", "absolute");
		$(this).children("div").css("left", "0px");
		$(this).children("div").css("right", "0px");
		$(this).children("div").css("height", height);
		$(this).children("div").css("backgroundImage", "url(gfx/gradient.php?shadeHeight=" + height + "&height=" + height + "&width=" + $(this).children("div").outerWidth() + "&color1=transparent&color2=000&flip=1)");
		$(this).children("div").css("backgroundRepeat", "repeat-x");

		var topPos = $(this).find("h2").next().position().top;
		var handleHeight = $(this).find("h2").outerHeight() + $(this).find("h2").css("margin-bottom");
		$(this).children("div").attr("orgPos", topPos);
		$(this).children("div").css("top", $(this).outerHeight() - topPos);
	});
	$('.slidebox').hover(function() {
		$(this).children("div").animate({
			top : $(this).children("div").attr("orgPos")
			//						top: 0
		}, {
			queue : false,
			duration : 500
		});
	}, function() {
		$(this).children("div").animate({
			top : $(this).outerHeight() - $(this).children("div").attr("orgPos") + 'px'
		}, {
			queue : false,
			duration : 500
		});
	});
	if($('body').height() < $(window).height()) {
		var diff = $(window).height() - $('body').height();
		$('#footer').css("marginTop", 70 + diff);
	}
	$('#cover').animate({
		opacity : 0
	}, 250, function() {
		$(this).css("display", "none");
	});
	$('a').click(function(event) {

		var url = $(this).attr("href");
		var target = $(this).attr("target");
		if(target == "_blank" || url.substr(0, 11) == "javascript:") {
			return;
		}

		if(url.substr(0, 1) == "#")
			return;
		event.preventDefault();
		$('#cover').css("display", "block");
		$('#cover').animate({
			opacity : 1
		}, 250, function() {
			window.location = url;
		});
	});
	$('h1').each(function() {

		var pos = $(this).position();
		var shadow = $(this).clone();

		shadow.css("position", "absolute");
		shadow.css("left", pos.left - 2);
		shadow.css("top", pos.top - 2);
		shadow.css("color", $(this).css("color"));

		$(this).css("color", "#FFF");
		$(this).before(shadow);

	});

	$('label.inside').each(function() {
		field = $("#" + $(this).attr("for"));
		field.focus(function() {
			$(this).siblings('label.inside').hide();
		});
		field.blur(function() {
			if($.trim($(this).val()) == "")
				$(this).siblings('label').show();
		});
	});
});

