﻿var Dropdown = new Class({
    initialize: function() {



        this.Dropdowns = $$('.nav a');
        var y = this.OpenMenu.bind(this);
        var x = this.StartClose.bind(this);
        this.Dropdowns.each(function(e) {
            e.addEvent('mouseover', y.pass(e));
            e.addEvent('mouseout', x.pass(e));
        })

      
    },
    OpenMenu: function(e) {
        $clear(this.timeout);

        var parent = e.getParent().getParent();
        parent.getElements('a').each(function(k) {

            if (k.hasClass('open') && k != e) {
                k.removeClass('open');
                k.getParent().getElements('ul').each(function(i) {
                    i.setStyle('display', 'none');
                });
            }
        });


        if (!e.hasClass('open')) {
            e.addClass('open');

            if (e.getParent().getChildren().length > 1) {

                var children = e.getParent().getChildren()[1];
                children.setOpacity(0);
                children.setStyles({ 'display': 'block' });
                children.fade(1);


                children.setStyles({ 'left': e.getPosition(e.getParent()).x + 'px' });

            }
        }
    },
    StartClose: function(e) {
        this.timeout = this.CloseMenu.delay(500);
    },
    CloseMenu: function() {
        $clear(this.timeout);

        $$('.nav ul').each(function(i) {
            i.fade(0);
            i.getParent().getElements('a').removeClass('open');
        });

    }
});
