$(function() {
	// index()-like function which matches elements according to uuid
	$.fn.indexOf = function(node) {
		var pos = -1;
		this.each(function(i) {
			if ( $.data(this) == $.data(node) )
				pos = i;
		});
		return pos;
	};

	// initialize fancybox if loaded
	if ("function" == typeof $.fn.fancybox)
		$("a[rel='gallery']").fancybox({
			zoomSpeedIn		: 150,
			zoomSpeedOut	: 150,
			overlayShow		: ($.browser.msie && $.browser.version < 7.0) ? false : true,
			overlayOpacity: 0.7,
			fancy					: true
		});

	// fix MSIE which doesn't do min-height
	if ($.browser.msie && $.browser.version < 7.0)
	{
		var bodyHeight		= $("body").height();
		var contentHeight = $("#content").height();

		if (contentHeight < (0.8 * bodyHeight))
			$("#content").height(0.8 * bodyHeight);
		else
		if (bodyHeight > contentHeight)
			$("#content").height(bodyHeight + 20);
	}

	// insert navigationlinks inline, if JS is supported
	$("#nav a").click(function() {
		var url = $(this).attr('href');

		// local testing
		if ('file:' == location.protocol)
			url += "/index.html";
		else
			return true;

		// load inline
		$("#text").hide().empty();
		$("#text").load(url + " #text", null, function() {
			$(this).fadeIn()
		});

		// prevent href from being followed
		return false;
	});

	// highlight current page in navigationlist
	$("#nav a").each(function() {
		if ($(this).attr('href') == location.pathname)
		{
			$(this).addClass('navcurrent');
			$(this).append("&nbsp;&raquo;");
		}
	});

	// random background image
	var images = [ "lamp.jpg", "boooh.jpg", "lock.jpg", "rings.jpg" ];
	$("body").css("background-image", 'url(/img/' + images[Math.floor(Math.random() * images.length)] + ')');

	// implement a faster slide for accordion
	$.extend($.ui.accordion.animations, {
		fastslide : function(options, additions) {
			// prevent unnecessary closing of always-be-open drawer
			if (options.toHide && options.toHide.hasClass("open"))
				options.toHide = $([]);
			this.slide(options, { duration : 150 });
		}
	});

	// determine drawer which is linked to current url
	var currentDrawer = -1;
	var m = location.pathname.match(/^\/([^\/]+)\//);
	if (m && m.length > 1)
	{
		currentDrawer = $("ul.drawers").children("li").index( $('#' + m[1])[0] );
		if (currentDrawer != -1)
		{
			$("ul.drawers > li:eq(" + currentDrawer + ")").
				addClass("activedrawer").
					find("ul").
					addClass("open");
		}
	}

	// use hoverIntent to switch accordion (apart from always-be-open drawer)
	$('ul.drawers > li:not(:eq(' + currentDrawer + ')) h2.drawer-handle').hoverIntent({
		sensitivity	: 2,
		interval		: 100,
		timeout			: 400,
		over				: function() {
			var idx = $( "h2.drawer-handle", $(this).parents("ul")[0] ).indexOf(this);
			if (idx != -1)
				$('ul.drawers').accordion("activate", idx);
		},
		out					: function() {}
	});

	// setup accordion
	$('ul.drawers').accordion({
			// start deactivated
			active				: false,

			// the accordion should always be opened
			alwaysOpen		: true,

			// use 'fastslide' animation (see above)
			animated			: 'fastslide',
			
			// type of event to trigger opening
			event					: '',

			// the drawer handle
			header				: 'h2.drawer-handle',

			// autoHeight gives problems
			autoHeight 		: false
		});
	
	// for testing purposes
	if (location.host.indexOf("localhost") != -1 || location.host.indexOf("192.") == 0)
	{
		$("img").each(function () {
			if ($(this).attr("src").indexOf("/img/photos/") != -1)
				$(this).attr("src", "http://klep.name/" + $(this).attr("src"));
		});
		$("a[rel='gallery']").each(function () {
			if ($(this).attr("href").indexOf("/img/photos/") != -1)
				$(this).attr("href", "http://klep.name/" + $(this).attr("href"));
		});
	}

});
