(function($) {

    /**
     * jQuery.tabs. A simple tabs plugin.
     * @param {Object} options An object with user defined options.
     * @return jQuery collection
     */
    
    
    $.fn.tabs = function(options) {
        var opts = $.extend({}, $.fn.tabs.defaults, options);
        return this.each(function() {
            new Tabs(this, opts);
        });
    };
    
    
    $.fn.tabs.defaults = {
        selected_class: 'marcado',
        trigger_on_click: 'visible'
    };
    
    
    function Tabs(el, opts) {
        this.el   = $(el);
        this.opts = opts;
        
        this.selected_tab = '';
        this.selected_li  = {};
        
        this.init();
    };
    
    Tabs.prototype.extend = $.extend;
    Tabs.prototype.extend({

        init: function() {
            var that = this;
            this.el.find('a').each(function() {
                var a = $(this);
                
                if (!a.attr('href').match(/^#.?/)) {
                    return;
                }
                
                a.click(function(e) {
                    e.preventDefault();
                    that.show_tab(a.attr('href'), a.parent());
                });
                
                if (a.parent().hasClass(that.opts.selected_class)) {
                    a.click();
                }
                
            });
        },
        

        show_tab: function(tab, li) {
            if (this.selected_tab) {
                $(this.selected_tab).hide();
                $(this.selected_li).removeClass(this.opts.selected_class);
            };
            
            var tab = $(tab);
            
            tab.show();
            if (this.opts.trigger_on_click) {
                tab.trigger(this.opts.trigger_on_click);
            }
            
            $(li).addClass(this.opts.selected_class);
            
            this.selected_tab = tab;
            this.selected_li  = li;
        }
    });
    
})(jQuery);


(function($) {
    $.fn.desplegables = function() {
        var document_width = document.width || document.documentElement.offsetWidth;

        return this.each(function() {

            var el = $(this);
            if (document_width < el.offset().left + 456) { // FIXME: calcular dinamicamente el offset necesario
                el.addClass('d');
            }

            el.bind('mouseenter', function() {
                el.data('over', true);

                setTimeout(function() {
                    if (el.data('over')) {
                        el.parent().children('.abierto').removeClass('abierto');
                        el.addClass('abierto');
                    }
                }, 100);
            });


            el.bind('mouseleave', function() {
                el.data('over', false);
                setTimeout(function() {
                    if ( !el.data('over') ) {
                        el.removeClass('abierto');
                    }
                }, 300);
            });

        });
    };
})(jQuery);


