var SwitcherBar = $.inherit(
{
    __constructor : function(jContainer)
    {
        this.jContainer = jContainer;
        this.jjItem = this.jContainer.find('li');
        this.jSelector = this.jjItem.filter('.selector:first');
        this.jActiveItem = this.getActiveItem();
        this.jInitialItem = this.jActiveItem;
        this.jDecoration = this.jSelector.find('ins');
        this.oListenerList = {};

        this.jSelector.css(this.__self.oSelectorInitialCss);
        if (this.jActiveItem.length)
        {
            this.jSelector.css(this.getGeometry());
        }
        
        this.bRestoreMode = false;
        
        this.activateItem();
        this.bindItemHandler();
    },
    
    getActiveItem : function()
    {
        return this.jjItem.filter('.selected:first');
    },
    
    getGeometry : function()
    {
        return {
            'left': this.jActiveItem.position().left,
            'width': this.jActiveItem.outerWidth()
        };
    },
    
    activateItem : function()
    {
        this.jSelector.hide();
        this.jActiveItem.addClass('active selector');
        this.jDecoration.appendTo(this.jActiveItem);
    },
    
    deselectActiveItem : function()
    {
        this.jDecoration.hide();
        this.jActiveItem.removeClass('active selector selected');
    },
    
    bindItemHandler : function()
    {
        this.jjItem.click( function(that) { return function(event)
        {
            if ( ! $(this).hasClass('active') )
            {
                that.jSelector.css('display', 'inline-block');
                if (that.jActiveItem.length)
                {
                    that.jActiveItem.removeClass('active selector selected');
                    that.jDecoration.show().appendTo(that.jSelector);
                }
                that.jActiveItem = $(this);
                that.toggle();
                
                event.preventDefault();
                event.stopPropagation();
            }
        }}(this));
    },
    
    toggle : function()
    {
        if (this.jActiveItem.find('a').length)
        {
            window.location = this.jActiveItem.find('a').attr('href');
        }
        this.triggerEvent(this.__self.EVT_BEFORE_TOGGLE);
        this.jSelector.animate(
            this.getGeometry(),
            'fast',
            'linear',
            function(that) { return function()
            {
                that.activateItem();
                that.triggerEvent(that.__self.EVT_ON_TOGGLE);
                if (that.bRestoreMode)
                {
                    that.bRestoreMode = false;
                    that.triggerEvent(that.__self.EVT_ON_RESTORE);
                }
            }}(this)
        );
    },
    
    restoreInitial : function()
    {
        this.bRestoreMode = true;
        this.jInitialItem.click();
    },
    
    addEventListener : function(eventId, callback)
    {
        if(! this.oListenerList[eventId])
        {
            this.oListenerList[eventId] = [];
        }
        this.oListenerList[eventId].push(callback);
    },
    
    triggerEvent : function(eventId)
    {
        if (this.oListenerList[eventId])
        {
            jQuery.each(this.oListenerList[eventId], function(){this()});
        }
    }
},{
    EVT_BEFORE_TOGGLE : 1,
    EVT_ON_TOGGLE : 2,
    EVT_ON_RESTORE : 3,

    oSelectorInitialCss : {
        'padding': 0,
        'position': 'absolute'
    }
});

$(function()
{
    $("ul.switcher_panel").each(function()
    {
        new SwitcherBar($(this));
    }); 
});
