// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

var freefall = {};
freefall.dialog = {};
freefall.ui = {};

freefall.dialog.show = function(url, width, height) {
    window.open(url, "_blank", "width=" + width + ",height=" + height);
}

/* override this on dialog scripts to change default cancel behaviour */
freefall.dialog.cancel = function() {
    window.close();
}

freefall.ui.showActivityIndicator = function() {
    $("activity_indicator").style.display = "block";
}

freefall.ui.hideActivityIndicator = function() {
    $("activity_indicator").style.display = "none";
}

Ajax.Responders.register({
    onCreate: function() {
        if (Ajax.activeRequestCount > 0)
            freefall.ui.showActivityIndicator();
    },
    onComplete: function() {
        if (Ajax.activeRequestCount == 0)
            freefall.ui.hideActivityIndicator();
    }
});

// (c) 2006 Jason Frame @ Magic Lamp

var menuState = {};
var expSize = 120;
var increment = 0.05;
var delay = 20;

function init() {
	var navRoot = $('main_nav');
	var i, a;
	for (i = 0; i < navRoot.childNodes.length; i++) {
		var child = navRoot.childNodes[i];
		if (child.nodeName) {
			var nodeName = child.nodeName.toLowerCase();
			if (nodeName == "a") {
				a = child;
			} else if (nodeName == "div" && a) {
			    if (child.getElementsByTagName("a").length > 0) {
    			    var subMenuId = "__submenu" + i;
    				child.id = subMenuId;
    				child.className += " " + a.className;
    				a.onmouseover = child.onmouseover = new Function("showSubMenu('" + subMenuId + "');");
    				a.onmouseout = child.onmouseout = new Function("hideSubMenu('" + subMenuId + "');");
    				menuState[subMenuId] = [0, null, null];
    			}
				a = null;
			}
		}
	}
}

function showSubMenu(id) {
	var s = menuState[id];
	if (s[1] == 'out') { window.clearInterval(s[2]); s[2] = null; }
	s[0] += increment;
	if (s[0] > 1) s[0] = 1;
	$(id).style.width = "" + Math.floor(Math.pow(s[0], 2) * expSize) + "px";
	s[1] = 'in';
	if (!s[2]) s[2] = window.setInterval("showSubMenu('" + id + "')", delay);
}

function hideSubMenu(id) {
	var s = menuState[id];
	if (s[1] == 'in') { window.clearInterval(s[2]); s[2] = null; }
	s[0] -= increment;
	if (s[0] < 0) s[0] = 0;
	$(id).style.width = "" + Math.floor(Math.pow(s[0], 2) * expSize) + "px";
	s[1] = 'out';
	if (!s[2]) s[2] = window.setInterval("hideSubMenu('" + id + "')", delay);
}