﻿var Menu = Class.create({
    initialize: function(options) {
        this.Options = options;
        this.Holder = $(options.holder);
        this.DropDowns = new Array();
        this.Options.tag = this.Options.tag == null ? "a" : this.Options.tag;
        this.Options.align = this.Options.align == null ? "right" : this.Options.align;
        this.BindLinks(this);
    },
    ShowItem: function(index) {
        var dd = this.DropDowns.find(function(p) {
            return p.Id == 'dd_' + index;
        });
        console.log(dd);
        dd.Show();
    },
    BindLinks: function(g) {
        var i = 0;
        this.Holder.select('ul')[0].select(g.Options.tag).each(function(a) {
            if (a.select('span').length == 0)
                return;
            var sb = new StringBuilder();
            sb.Append('menu_');
            sb.Append(a.getAttribute('rel'));            
            var dd = new DropDown({
                trigger: a.select('span')[0],
                baseElement: 'parent',
                displayElement: sb.ToString(),
                closeOnClick: false,
                align: g.Options.align,
                shadow: { opacity: .2, offset: 4 },
                effects: g.Options.effects,
                closeOnMouseout: true,
                padding: g.Options.padding,
                showOnMouseover: g.Options.showOnMouseover,
                onShowBegin: function() {
                    $(this.DropDowns).each(function(d) {
                        if (dd.Id != d.Id) {
                            d.HideDropDown();
                            d.BaseElement.removeClassName('menuBarSelected');
                        }
                    });
                    dd.BaseElement.addClassName('menuBarSelected');
                } .bind(this),
                onHideFinish: function() {
                    dd.BaseElement.removeClassName('menuBarSelected');
                }
            });
            dd.Id = 'dd_' + i;
            i++;
            this.DropDowns.push(dd);
        } .bind(this));
    }
});

// String Builder
var StringBuilder = Class.create({
    initialize:     function() { this.array = new Array(); },
    Append:         function(str) { this.array.push(str); },
    AppendStrings:  function(arr) { this.array.push(arr.join("")); },
    AppendFormat:   function(str, obj) { this.array.push(str.interpolate(obj)); },
    AppendLine:     function() { this.array.push("\n"); },
    Length:         function() { return this.array.join("").length; },
    ToString:       function(t) { return this.array.join(t == null || t.blank() ? "" : t); }    
});