(function($) {
    this.loadmap = {};
    this.options = {};
    this.tabpanels = undefined;


    $.fn.tabs = function(tabpanels, options) {
        this.loadmap = {};
        this.tabpanels = tabpanels;
        var defaults = {css:"now",params:{}};
        this.options = $.extend(defaults, options);
        var self = this;


        this.showTab = function(index) {
            var options = this.options
            var tabpanels = this.tabpanels;
            jQuery(this).removeClass(options.css)
            jQuery(this[index]).addClass(options.css)
            jQuery(tabpanels).css("display", "none")
            jQuery(tabpanels + ":eq(" + index + ")").css("display", "block")
        }
        this.loadTabView = function(index, url, params) {
            var tabpanels = this.tabpanels;
            var loadmap = this.loadmap;
            jQuery(tabpanels + ":eq(" + index + ")").load(url, params, function(responseText, textStatus, XMLHttpRequest) {
                loadmap[url] = true
                self.showTab(index)
                return false;
            })
        };
        
        return this.each(function(index) {
            jQuery(this).css("cursor", "pointer")
            jQuery(this).bind("click", function(e) {
                try {
                    var url = null;
                    if (this.children && this.children.length == 1) {
                        url = this.children[0].href;
                    }
                    if (url && !self.loadmap[url]) {
                        self.loadTabView(index, url, self.options.params)

                    } else {
                        self.showTab(index)
                    }
                } catch(e) {
                    //do nothing
                }
                return false;
            })
        });

    };
})(jQuery);
