if (!window.Varien)
    var Varien = new Object();

Varien.Dropdowns = Class.create();
Varien.Dropdowns.prototype = {
    initialize: function(selector) {
        var self=this;
        var globalDoc = document;
        this.ddselector = selector;
        $$('.'+selector).each(this.initDD.bind(this));
        Event.observe(globalDoc, 'click', function(event) {
            if (!$(event.target).hasClassName('fancy-inactive-item')) {
                this.hideDDall(0);
            }
        }.bind(this));
    },

    initDD: function(el) {
        this.ddcont = $(el.id+"-content");
        this.ddhead = $(el.id+"-header");
        this.ddlist = $(el.id+"-list");
        this.ddw = el.offsetWidth;
        this.ccw = this.ddlist.offsetWidth;
        //if(!el.hasClassName('select-dropdown')) this.fitContainers(this.ccw,this.ddw,this.ddcont,el);
        if(navigator.userAgent.indexOf("MSIE 6") > 0){
            el.firstChild.style.display = "block";
        }
        Event.observe(this.ddhead, 'click', this.showDD.bind(this, this.ddcont));
    },

    hideDDall: function(cid) {
        $$('.ddlist').each(function(item){
            item.style.display = 'none';
            item.removeClassName('ddon');
        });
    },

    hideDD: function(cid) {
        $$('.ddlist').each(function(item){
            if (item.hasClassName('ddon') && item.id != cid) {
                item.style.display = 'none';
                item.removeClassName('ddon');
            }
        });
    },

    showDD: function(c) {
        var hasLable = c.hasClassName('ddon');
        if (hasLable) {
            Effect.toggle(c.id, 'blind', {delay: 0.1, duration: .1});
            c.removeClassName('ddon');
        }else{
            c.addClassName('ddactive');
            c.style.display = 'none';
            Effect.toggle(c.id, 'blind', {delay: 0.1, duration: .1});
            c.addClassName('ddon');
        }
        this.hideDD(c.id);
    },

    fitContainers: function(cw,dw,c,dd){
        var pw = dd.parentNode.offsetWidth;
        this.fixTitle(dd);
        if(cw > dw){
            if(cw > pw){
                cw = pw;
            }
            dd.style.width = cw+'px';
            if(navigator.userAgent.indexOf("MSIE 6") > 0){
                dd.firstChild.style.width = (cw - 30)+"px";
            }
        }else{
            c.style.width = dw+'px';
            c.getElementsByTagName("div")[0].style.width = dw+'px';
        }
    },

    fixTitle: function(dd){
        setTimeout(function(){
            if(dd.getElementsByTagName("dt")[0] && dd.getElementsByTagName("dt")[0].getElementsByTagName("span")[0] ){
                var span = dd.getElementsByTagName("dt")[0].getElementsByTagName("span")[0];
                str = span.innerHTML;
                span.title = str;
                var padding = parseInt($(span.parentNode).getStyle('paddingLeft')) + parseInt($(span.parentNode).getStyle('paddingRight'));
                var oldSpanWidth = span.offsetWidth + padding;
                while (dd.parentNode.offsetWidth < span.offsetWidth + padding) {
                    oldSpanWidth = span.offsetWidth + padding;
                    str = str.substr(0, str.length - 1);
                    span.innerHTML = str + " ...";
                    if (span.offsetWidth == span.offsetWidth + padding) {
                        break;
                    }
                }
            }
        },0)
    }
}

Varien.DropdownsMu = Class.create();
Varien.DropdownsMu.prototype = Object.extend(new Varien.Dropdowns(), {
    fitContainers: function(cw,dw,c,dd){
        var pw = dd.parentNode.offsetWidth;
        this.fixTitle(dd);
        if (cw == 0 || dw == 0) {
            return;
        }
        if(cw > dw){
            dd.style.width = cw+'px';
            if(navigator.userAgent.indexOf("MSIE 6") > 0){
                dd.firstChild.style.width = (cw - 30)+"px";
            }
        }else{
            c.style.width = dw+'px';
        }
    }
});

Event.observe(window, 'load', function() {
    new Varien.Dropdowns('dropdown');
});

