/*
 * site.js
 * Copyright (C) 2009 Adrian Perez <aperez@igalia.com>
 *
 * Distributed under terms of the MIT license.
 */


// Check whether our namespace is properly defined.
if (typeof jln == "undefined") jln = {};



if (typeof Object.clone != "function") {
	Object.clone = function () {
		var F = function () {};
		F.prototype = this;
		return $.extend.apply(new F(), arguments);
	}
}


jln._current = null;


jln._randstr = function (nchars)
{
	var result = '';

	while (nchars--) {
		result = result + Math.floor(Math.random() * 10).toString();
	}

	return result;
}


jln.spinner = {};

jln.spinner.init = function ()
{
	jln.spinner._elt = $("#load-spinner");
	jln.spinner._num = 0;
}

jln.spinner.show = function ()
{
	if (jln.spinner._num++ == 0) {
		jln.spinner._elt.fadeIn("slow");
	}
}

jln.spinner.hide = function ()
{
	if (--jln.spinner._num == 0) {
		jln.spinner._elt.fadeOut("slow");
	}
}


jln.gallery = {};

jln.gallery.init = function (enclosure, items)
{
	if (enclosure == null) {
		enclosure = "#gallery";
	}
	if (items == null) {
		items = "li";
	}

	jln.gallery._images = $(enclosure + " " + items + " img");

	var all_li = $(enclosure + " " + items);

	all_li.each(function () {
		// Initially hide description
		var dsc = $(".description:first", this);
		dsc.hide();

		// Change source to that of the thumbnail
		var img = $("img:first", this);
		var src = img.attr("src");

		var real_src = src;
		var idx = src.indexOf("_thumb.jpg");
		if (idx + 10 == src.length) {
			real_src = src.substr(0, idx) + ".jpg";
		}

		// Add "alt" attribute if not present
		if (!img.attr("alt")) {
			img.attr("alt", img.attr("title"));
		}

		// Add the same "rel" to all images
		img.attr("rel", "jln-gallery-img");

		// Create and place outer <a> element in the DOM, so fancybox works
		var link = $("<a/>");
		var parent = img.parent();
		parent.empty().append(
				link.attr("title", img.attr("title"))
						.attr("href", real_src)
						.attr("rel", "jln-gallery-a")
						.append(img)
				);

		// Wire events in the image
		img.hover(jln.gallery._img_over_cb, jln.gallery._img_out_cb);
	});

	all_li.children("a").fancybox({
			padding       : 0,
			overlayShow   : true,
			overlayOpacity: 0.8
	});
}

jln.gallery._img_over_cb = function ()
{
	var item = $(this);
	item.addClass("hover");
	jln.gallery._images.not(item).stop().fadeTo("normal", 0.4);
}

jln.gallery._img_out_cb = function ()
{
	jln.gallery._images.stop().fadeTo("normal", 1.0);
	$(this).removeClass("hover");
}



jln.on_window_resize = function (event)
{
	var wheight = $(window).height();
	var element = $("#load-progress");
	var margin = (wheight - element.height()) / 2;
	element.css("margin-top", margin + "px");
	$("#content").height(wheight - 60);

	// Now for elements inside #content
	var i = 0;
	var elements = $(".center-vertical");
	wheight = $("#content");
	if (wheight) {
		wheight = wheight.height();
		while (i < elements.length) {
			element = $(elements[i++]);
			margin = (wheight - element.height()) / 2 - 20;
			element.css("margin-top", margin + "px");
		}
	}
}


jln._is_child = function (toplevel, item)
{
	if (item === null)
		return false;

	toplevel = toplevel.context.parentNode;

	var p = item.parents();
	var i;

	for (i = 0; i < p.length; i++) {
		if (p[i] == toplevel)
			return true;
	}
	return false;
}


jln.on_sidebar_load = function ()
{
	var sidebar = $("#sidebar > ul").addClass("toplevel");

	// Collapse non first-level sublists
	$("ul", sidebar).hide();

	// Add extra CSS styling to top-level menu elements
	sidebar.children("li").children("a").addClass("toplevel");

	// Create menu events
	$("a", sidebar).click(function (event) {
		event.stopPropagation();
		event.preventDefault();

		var obj = $(this);

		// If we are hitting the active object, do nothing.
		if (obj.hasClass("active") || jln._is_child(obj, jln._current))
			return;

		jln._current = obj;

		var uri = $.trim(obj.attr("href"));
		var up  = obj.parent();

		// Unfold chosen item and fold up the other ones
		$("li ul", up.parent()).slideUp();
		$("> ul:hidden", up).slideDown();

		// Mark only clicked element as active, and remove focus to avoid
		// awful dotted box around the element (e.g. in Firefox)
		$("a", sidebar).removeClass("active");
		obj.addClass("active").blur();

		if (uri && uri[0] != "#") {
			// If the element has an href, load the pointed document.
			$("#content").fadeOut("slow", function () {
				// Randomize URI, so we workaround caching.
				uri = uri + "?token=" + jln._randstr(16);
				$(this).load(uri, function () {
					jln.gallery.init();
					jln.on_window_resize();
					$(this).fadeIn("slow");
				});
			});
		}
		else if (uri && uri == "#main") {
			$("#content").fadeOut("slow");
		}
		else {
			/*
			 * Element does not have an href per-se, so try to find child element
			 * which *does* have it, and trigger events to get ourselves called in
			 * a recursive fashion. Do this only if it is inside a list which has
			 * been marked with class="open-first"
			 */
			var first = $("> ul.open-first > li > a:first", obj.parent());
			if (first) {
				first.trigger("click");
			}
		}
	});

	// Show top-level stuff
	sidebar.show();
}


jln.site_start = function ()
{
	jln.spinner.init();
	jln.spinner.show();

	$(window)
		.load(jln.spinner.hide)
		.resize(jln.on_window_resize)
		.ajaxStart(jln.spinner.show)
		.ajaxStop(jln.spinner.hide);

	$("#iexploder-warning").click(function () {
		$("#iexploder-warning").slideUp("slow");
	});

	jln.on_window_resize();
	if ($.browser.msie) {
		$("#iexploder-warning").slideDown("slow");
	}

	jln.on_sidebar_load();
}


$(document).ready(jln.site_start);


