if (typeof(WV) == 'undefined') WV = {};

WV.Tabs = function () {
    var addTab = function (tab_id, tab_content_id, default_display, callbacks) {
        if (typeof default_display == "undefined") {
            default_display = false;
        }            
        this.tabs[tab_id] = new Tab(tab_id, tab_content_id, callbacks);
        this.bindClick(tab_id);
        if (default_display) {
            this.default_display     = tab_id;
            this.active_tab            = tab_id;
        }
    };
    var addTabByName = function(tab_name, selected) {
        var callbacks = [];
        // XXX MRS drop?
        callbacks.push(function(tab_id) {
            var active_tab = 'activeTab_' + this.instance_id;
            $.cookie(active_tab, tab_id, {path: '/'});
        });
        if (tab_name === 'map') {
            callbacks.push(function() {
                $(document).trigger('#map_event', [{ 'event_name' : 'MapView'}]);
                if (!map_is_loaded) {
                    $("#map_loading_layer").removeClass('hidden');
                    $("#map_loading_layer").addClass('active');                 
                    document.getElementById("map").style.border = 'none';                           
                    setTimeout("load()", 1000);             
                }
            });
            if (selected) {
                load();
            }
        } else if (tab_name === 'video') {
            callbacks.push(function() {
                $(document).trigger('#video_view_event', [{ 'event_name' : 'VideoView'}]);
                $(document).unbind('#video_view_event');
            });
        }
        var tab_id = 'tab_' + tab_name;
        var content_id = 'content_' + tab_name;
        this.addTab(tab_id, content_id, selected, callbacks);
    };
        
    var bindClick = function (tab_id) {
        var ref = this;
        $("#"+tab_id).click(function () {    
            ref.show(tab_id);
                
            return false;
        });
    };
        
    var show = function (tab_id) {
        if ($("#"+tab_id).length) {
            target_tab = this.tabs[tab_id];
            activeTab = this.tabs[this.active_tab];
            
            this.hideContent(activeTab.tab_content_id);
            this.hideTab(activeTab.tab_id);
          
            this.showContent(target_tab.tab_content_id);
            this.showTab(target_tab.tab_id);
                
            this.active_tab = tab_id;
                
            //fire callback events attached to this tab
            for (var i = 0; i < target_tab.callbacks.length; i++) {
                target_tab.callbacks[i].call(this, tab_id);
            }
        }                
    };
        
    var showContent = function (tab_content_id) {
        $("#"+tab_content_id).show();            
    };
        
    var hideContent = function (tab_content_id) {
        $("#"+tab_content_id).hide();
    };
        
    var showTab = function (tab_id) {
       $("#"+tab_id).removeClass(this.inactive_class);
       $("#"+tab_id).addClass(this.active_class);
    };
        
    var hideTab = function (tab_id) {
       $("#"+tab_id).removeClass(this.active_class);
       $("#"+tab_id).addClass(this.inactive_class);
    };
        
    //Tab object
    var Tab = function (tab_id, tab_content_id, callbacks) {
        this.tab_id         = tab_id;
        this.tab_content_id = tab_content_id;
        this.callbacks        = callbacks;
    };
        
    var TabSet = function (properties) {
        this.instance_id = properties.instance_id;
        
        this.active_class = properties.active || ''; 
        this.inactive_class = properties.inactive || '';
        if (window.console && window.console.log) {
          console.log(this.active_class);
          console.log(this.inactive_class); 
        }
        //Members
        this.tabs = new Object();
        this.active_tab         = '';
        this.default_display     = '';
                        
        //Methods
        this.addTab         = addTab;
        this.addTabByName   = addTabByName;
        this.bindClick      = bindClick;
        this.show           = show;
        this.showContent    = showContent;
        this.hideContent    = hideContent;
        this.showTab        = showTab;
        this.hideTab        = hideTab;
    };
        
    return {
        TabSet: TabSet,
        show:show
    };
}();


