/**
 * Contains the main java script definitions.
 *
 * @author Jan Martin
 */

// preload images
jQuery.preloadImages = function()
{
	for(var i = 0; i < arguments.length; i++)
	{
		$("<img>").attr("src", arguments[i]);
	}
}
$.preloadImages
(
	"../graphics/icon_external_hover.gif",
	"../graphics/flag_uk_hover.png",
	"../graphics/flag_de_hover.png",
	"../graphics/logo_facebook_hover.png",
	"../graphics/logo_soundcloud_hover.png",
	"../graphics/logo_youtube_hover.png"
);

// clean whitespace
jQuery.fn.cleanWhitespace = function()
{
	textNodes = this.contents().filter
	(
		function()
		{
			return this.nodeType == 3 && !/\S/.test(this.nodeValue);
		}
	).remove();
}

/**
 * Definitions to load, when document is loaded.
 */
$(document).ready
(
	function()
	{
		// activate rounded border design
		$("#left, div.box").each
		(
			function()
			{
				var context = $(this);
				context.css("background-color", "transparent");
				context.css("margin", "0");
				context.css("padding", "0");
				if(context.hasClass("photo"))
				{
					context.html('<img src="../graphics/photobox.png" alt="" />');
					context.css("background-image", "none");
					context.css("width", "auto");
				}
				else
				{
					context.wrapInner('<div class="middle" />');
					context.prepend('<div class="top"></div>');
					context.append('<div class="bottom"></div>');
				}
			}
		);
		$("#left").css("margin-right", "2px");
		$("#left").css("width", "342px");
		
		// correct item margins
		$("article div.box:first").css("margin-top", "0");
		
		// span legends for styling
		$("legend").wrapInner(document.createElement("span"));
		
		// add classes to inputs for styling
		$(":text").addClass("text");
		$(":button, :submit, :reset").addClass("button");
		$(":checkbox").addClass("checkbox");
		$(":radio").addClass("radio");
		$(":password").addClass("password");
		$("input:not(:submit), textarea").focusin
		(
			function()
			{
				$(this).addClass("focus");
			}
		);
		$("input:not(:submit), textarea").focusout
		(
			function()
			{
				$(this).removeClass("focus");
			}
		);
		$(":button, :submit, :reset").hover
		(
			function()
			{
				$(this).addClass("hover");
			},
			function()
			{
				$(this).removeClass("hover");
			}
		);
		
		// clean white-space for aside in IE6
		if($.browser.msie && $.browser.version.substr(0,1) == "6")
		{
			$("aside").cleanWhitespace();
		}
		
		// external link icon image hover effects
		$("a.external").hover
		(
			function()
			{
				$(this).css("background-image",
					"url(../graphics/icon_external_hover.gif)");
			},
			function()
			{
				$(this).css("background-image",
					"url(../graphics/icon_external.gif)");
			}
		);
		$("aside ul.languages img[alt=en]").hover
		(
			function()
			{
				$(this).attr("src", "../graphics/flag_uk_hover.png");
			},
			function()
			{
				$(this).attr("src", "../graphics/flag_uk.png");
			}
		);
		$("aside ul.languages img[alt=de]").hover
		(
			function()
			{
				$(this).attr("src", "../graphics/flag_de_hover.png");
			},
			function()
			{
				$(this).attr("src", "../graphics/flag_de.png");
			}
		);
		$("aside ul.links img[alt=Facebook]").hover
		(
			function()
			{
				$(this).attr("src", "../graphics/logo_facebook_hover.png");
			},
			function()
			{
				$(this).attr("src", "../graphics/logo_facebook.png");
			}
		);
		$("aside ul.links img[alt=SoundCloud]").hover
		(
			function()
			{
				$(this).attr("src", "../graphics/logo_soundcloud_hover.png");
			},
			function()
			{
				$(this).attr("src", "../graphics/logo_soundcloud.png");
			}
		);
		$("aside ul.links img[alt=YouTube]").hover
		(
			function()
			{
				$(this).attr("src", "../graphics/logo_youtube_hover.png");
			},
			function()
			{
				$(this).attr("src", "../graphics/logo_youtube.png");
			}
		);
		
		// blockquote font replacement
		Cufon.replace("blockquote");
		
		// initialize shadowbox
		Shadowbox.init();
	}
)
