/// <reference path="~/Scripts/Util.js"/>

var firstExtraTop = 0;
var firstExtraLeft = 0;
var belowExtraTop = 0;
var belowExtraLeft = 2;
var timeout = 350;
var openDelay = 260;
var openChildrenDelay = 150;
var maxAdjustment = 400;
var adjustmentPadding = 50;
var speed = 200;
var extraWidth = 110;

function isDescendantOf(a, b) {
    var o = b;
    while (o) {
        if (o === a) {
            return true;
        }
        o = o.parentNode;
    }
    return false;
}

function openDdl(ele) {
  
    var closure = ele;

    var level = $(ele).children("ul.expandable");
	//cancelTimer(this);
    $(ele).addClass("hover");
    $(ele).children("a").addClass("hover");
    if ($(ele).closest("ul.expandable").length == 0) {
        var top = $(ele).position().top + $(ele).height() + firstExtraTop;
        var left = $(ele).position().left + firstExtraLeft;
		level.css({ top: top, left: left });
		level.width($(ele).width() + extraWidth);
	} else {
	    var top = $(ele).position().top + belowExtraTop;
	    var absoluteTop = $(ele).offset().top + belowExtraTop;
		var i = 0;
		var windowHeight = $(window).height() - adjustmentPadding;
		var levelHeight = level.height();
		while (((absoluteTop + levelHeight) > windowHeight) && i < maxAdjustment) {
			top--;
			absoluteTop--;
			i++;
		}
		var left = $(ele).position().left + $(ele).width() + belowExtraLeft;
		level.css({ top: top, left: left });
		level.width($(ele).width());
	}
	level.fadeIn(speed);
}

function setCloseTimer() {
    var closure = $(this);
    cancelOpenTimer(this);
    this.closeTimer = window.setTimeout(function() { closeDdl(closure.children("ul.expandable")); }, timeout);
}

function setOpenTimer() {
    var closure = this;
    cancelCloseTimer(this);
    if ($(this).parent().parent("div#top_menu").length == 0) {
        this.openTimer = window.setTimeout(function() { openDdl(closure); }, openChildrenDelay);
        var expandables = $("ul.expandable");
        expandables.each(function() {
            if (!isDescendantOf(closure, this) && !isDescendantOf(this, closure)) {
                closeDdl($(this));
            }
        });
    } else {
        this.openTimer = window.setTimeout(function() { openDdl(closure); }, openDelay);
    }
}

function cancelOpenTimer(el) {
    if (el.openTimer) {
        window.clearTimeout(el.openTimer);
        el.openTimer = null;
    }
}

function cancelCloseTimer(el) {
	if (el.closeTimer) {
		window.clearTimeout(el.closeTimer);
		el.closeTimer = null;
	}
}

function closeDdl(el) {
    if (el != null && el.css("display") != "none") {
		el.parent().removeClass("hover");
		el.parent().children("a").removeClass("hover");
		el.fadeOut(speed);
		closeChildren(el);
	}
}

function closeChildren(el) {
	var openChildren = el.find("ul.expandable");
	openChildren.fadeOut(speed, function() { $(this).hide(); });
	openChildren.parent().removeClass("hover");
	openChildren.parent().children("a").removeClass("hover");
}

$(document).ready(function() {
    $("ul.tabs li.expandable_trigger").mouseover(setOpenTimer).mouseout(setCloseTimer);
});


