﻿
DropDownMenu.prototype.OPEN = false;
DropDownMenu.prototype.OPENING = false;
DropDownMenu.prototype.CLOSING = false;
DropDownMenu.prototype.Object = null;

function DropDownMenu(object, TopContainer) {
    this.Object = object;
    this.OPEN = false;
    this.OPENING = false;
    this.CLOSING = false;

    var own = this;
    //object.hover(function() { OpeningDropDown(own) }, function() { ClosingDropDown(own) });
    //object.hover(function() { OpenDropDown(own) }, function() { ClosingDropDown(own) });

//    object.children("ul:first li").each(
//    function(i) {
//        this.mouseover(function() { OpenDropDown(own); });
//    });
    
    
    var left = 0;
    if (object.children("ul:first").position().left < 0)
        left = -1 * object.children("ul:first").position().left;
    else
        left = object.children("ul:first").position().left;
    
    if ((left + object.children("ul:first").outerWidth()) > (TopContainer.offset().left + TopContainer.outerWidth())) {
        var newLeft = ((left + object.children("ul:first").width()) - (TopContainer.offset().left + TopContainer.width()))
        newLeft = -1 * newLeft;        
        object.children("ul:first").css("left", newLeft+"px");
    }
}


function OpeningDropDown(DropDown) {

    if (DropDown.CLOSING) {
        DropDown.CLOSING = false;
        DropDown.Object.stopTime("closeMenuDropDrown", function() { CloseDropDown(DropDown) });
    }

    if (!DropDown.OPEN || !DropDown.OPENING) {
        DropDown.OPENING = true;
        DropDown.Object.oneTime(500, "openMenuDropDrown", function() { OpenDropDown(DropDown) }, 1);
    }
}

function ClosingDropDown(DropDown) {
    //    object.stopTime("openMenuDropDrown", function() { OpenDropDown(object) });
    //    object.oneTime(500, "closeMenuDropDrown", function() { CloseDropDown(object) }, 1);
    //
    if (DropDown.OPENING) {
        DropDown.OPENING = false;
        DropDown.Object.stopTime("openMenuDropDrown", function() { OpenDropDown(DropDown) });
    }

    if (DropDown.OPEN || !DropDown.CLOSING) {
        DropDown.CLOSING = true;
        DropDown.Object.oneTime(800, "closeMenuDropDrown", function() { CloseDropDown(DropDown) }, 1);
    }
}

function OpenDropDown(DropDown) {

    if (DropDown.CLOSING) {
        DropDown.CLOSING = false;
        DropDown.Object.stopTime("closeMenuDropDrown", function() { CloseDropDown(DropDown) });
    }
    
    DropDown.OPENING = false;
    DropDown.Object.children("ul:first").show();
    DropDown.OPEN = true;
}

function CloseDropDown(DropDown) {
    DropDown.CLOSING = false;
    DropDown.Object.children("ul:first").hide();
    DropDown.OPEN = false;
}