'use strict';

var self;

var Bind = require('github/jillix/bind');
var Events = require('github/jillix/events');

module.exports = function (config) {

    self = this;

    Events.call(self, config);
    config = processConfig(config);

    var container = $(config.container, self.dom);

    if (config.options.reuse) {
        self.tabs = {};
    }

    // On tab click
    $(config.tabs, self.dom).on('click', function() {

        // Change the hash in URL
        if (config.options.hash) {
            if (!$(this).attr('data-hash')) {
                return;
            }

            window.location.hash = $(this).attr('data-hash');
        } else {
            activateTab(config, $(this));
        }

        return false;
    });

    // hashchange handler
    $(window).on('hashchange', function () {
        var tab = $('[data-hash=' + window.location.hash.substring(1) +  ']', self.dom);
        activateTab(config, tab);
    })

    // Load first content
    if ((config.options.first) && (!window.location.hash)) {
        M(config.container, config.options.first, function () {
            self.emit('tabActivated', config.options.first);

            if (config.options.reuse) {
                self.tabs[config.options.first] = true;
            }
        });
        return;
    }

    // Load the right content for hash
    if ((window.location.hash) && (config.options.hash)) {
        var tab = $('[data-hash=' + window.location.hash.substring(1) +  ']', self.dom);
        activateTab(config, tab);
        return;
    }

    if (typeof window[config.onInitEnd] === 'function') {
        window[config.onInitEnd].apply(self);
    }
}

function activateTab(config, tab) {

    if (!tab.length) {
        return;
    }

    var active = $(config.tabs, self.dom).parent().find('.' + config.options.classes.selected);
    // Removes the active class
    $(config.tabs, self.dom).removeClass(config.options.classes.selected);
    // Adds active class
    tab.addClass(config.options.classes.selected);

    // TODO we cannot rely that the selected class exists and based on this
    // to test the current tab. The current miid should be buffered
    if (tab.attr('data-miid') === active.attr('data-miid')) {
        return;
    }


    // Removes the content of container if reuse is false or undefined
    if (!config.options.reuse) {
        $(config.container, self.dom).html('');
    }


    var miid = tab.attr('data-miid');

    // Sets the content in container
    // reuse is true
    if (config.options.reuse) {

        // miid was NOT loaded
        if (!self.tabs[miid]) {
            M(config.container, miid, function () {
                self.emit('tabActivated', miid);

                self.tabs[miid] = true;
                $(config.container).children().hide();
                $("#" + miid).show();
            });
        }
        // miid was already loaded, show it!
        else {
            $(config.container).children().hide();
            $("#" + miid).show();
        }
    }
    // reuse is false
    else {
        M(config.container, miid, function () {
            self.emit('tabActivated', miid);
        });
    }
}

function processConfig (config) {

    config.options = config.options || {};
    config.options.classes = config.options.classes || {};

    return config;
}

@@ -1478,32 +1478,119 @@
 '%5D', self.dom);%0A
+%0A        var options = %7B%0A            tab: tab,%0A            firstLoad: true%0A        %7D;%0A%0A
         activate
@@ -1597,27 +1597,31 @@
 Tab(config, 
-tab
+options
 );%0A        r
@@ -1780,14 +1780,57 @@
 ig, 
-tab) %7B
+options) %7B%0A%0A    var tab = options.tab %7C%7C options;
 %0A%0A  
@@ -1869,24 +1869,51 @@
 urn;%0A    %7D%0A%0A
+    // gets the active tab%0A
     var acti
@@ -1992,24 +1992,25 @@
 .selected);%0A
+%0A
     // Remov
@@ -2100,24 +2100,25 @@
 .selected);%0A
+%0A
     // Adds 
@@ -2384,16 +2384,38 @@
 a-miid')
+ && !options.firstLoad
 ) %7B%0A    
@@ -2429,25 +2429,24 @@
 urn;%0A    %7D%0A%0A
-%0A
     // Remov
@@ -2589,16 +2589,15 @@
 ');%0A
+
     %7D%0A%0A
-%0A
     

Fix hash load.