/**
 * Katalogo elemento kontaktų blokelis
 */
var WeatherHolidaysControls = new Class({

    Implements: Options,

    options: {
        'cookieName' : 'weathersmall',
        'tabs': 'weathersmall-tabs',
        'activeTabId': 'activate_block_weather'
    },

    tabs: [],
    cookieOptions: {
        'path': '/'
    },

    initialize: function(options) {
        this.setOptions(options);
        this.addTabs();
    },


    activateTab: function() {
        var active = Cookie.read(this.options.cookieName);
        if (active != null) {
            if (active != 0 && $(active)) {
                this.setActiveTab($(active), true);
            }
        }
        else {
            //this.setActiveTab($(this.options.activeTabId), true);
        }
    },


    addTabs: function() {
        if ($(this.options.tabs)) {
            $$('#' + this.options.tabs + ' a').each(function(item) {
                this.tabs.push(item);
                this.observeTab(item);
            }.bind(this));
        }
        
    },

    observeTab: function(item) {
        item.addEvent('click', function(event) {
            event.stop();
            this.setActiveTab(event.target, false);
        }.bind(this));
        var target = this.getTargetContainer(item.get('href'));
        target.setStyle('display', 'none');
    },


    setActiveTab: function(item, force) {

        var targetContainer = this.getTargetContainer(item.get('href'));
        var displayState = targetContainer.getStyle('display');

        this.tabs.each(function(el) {
            el.removeClass('active');
            var container = this.getTargetContainer(el.get('href'));
            if (container !== null) {
                container.setStyle('display', 'none');
            }
        }.bind(this));
        
        if (displayState == 'block' && force == false) {
            targetContainer.setStyle('display', 'none');
            item.removeClass('active');
            Cookie.write(this.options.cookieName, 0, this.cookieOptions);
        }
        else {
            targetContainer.setStyle('display', 'block');
            item.addClass('active');
            Cookie.write(this.options.cookieName, item.get('id'), this.cookieOptions);
        }
    },


    getTargetContainer: function(href) {
        var name = href.split('#')[1];
        if ($(name)) {
            return $(name);
        }
        return null;
    }
});


window.addEvent('load', function() {
    var wh_controls = new WeatherHolidaysControls();
    if ($('weather')) {
        $('weather').inject('weather-container').setStyle('display', 'block');
    }
    wh_controls.activateTab();
});
