"use strict";

/*jslint white: true, onevar: true, undef: true, nomen: true, eqeqeq: true, plusplus: false,
bitwise: true, regexp: true, strict: true, newcap: true, immed: true */

/*global window, dojo, SWFObject */

/**
 * create Tag Cloud switcher for WI-WE Bank 
 * @constructor
 * @author ladibryc
 * @author milodorn
 * @version 2.0
 */
function WWBTagCloudSwitcher() {
    
    var THIS = this;

    /**
     * tag cloud height
     * @memberOf WWBTagCloudSwitcher
     * @type Number
     * @return {Number}
     */
    this.boxHeight = 200;
    
    /**
     * tag cloud container element id
     * @memberOf WWBTagCloudSwitcher
     * @type String
     * @return {String}
     */
    this.boxId = 'tag-cloud-switcher';
    
    /**
     * tag cloud width
     * @memberOf WWBTagCloudSwitcher
     * @type Number
     * @return {Number}
     */
    this.boxWidth = 200;
    
    /**
     * default tag group
     * @memberOf WWBTagCloudSwitcher
     * @type String
     * @return {String}
     */
    this.defaultTagGroup = '';
    
    /**
     * tag cloud container element
     * @memberOf WWBTagCloudSwitcher
     * @type HTMLElement
     * @return {HTMLElement}
     */
    this.elContainer = null;

    /**
     * tag cloud flash element
     * @memberOf WWBTagCloudSwitcher
     * @type HTMLElement
     * @return {HTMLElement}
     */
    this.elFlash = null;

    /**
     * tag cloud group switcher element
     * @memberOf WWBTagCloudSwitcher
     * @type HTMLElement
     * @return {HTMLElement}
     */
    this.elSwitcher = null;

    /**
     * tag cloud SFW file name 
     * @memberOf WWBTagCloudSwitcher
     * @type String
     * @return {String}
     */
    this.swfObjectFile = 'tagcloud.swf';
    
    /**
     * tag cloud SFW id 
     * @memberOf WWBTagCloudSwitcher
     * @type String
     * @return {String}
     */
    this.swfObjectId = 'tagcloud';
    
    /**
     * path to tag cloud SWF file
     * @memberOf WWBTagCloudSwitcher
     * @type String
     * @return {String}
     */
    this.swfObjectPath = '../swf/';
    
    /**
     * object with groups of tags
     * @memberOf WWBTagCloudSwitcher
     * @type Object
     * @return {Object}
     */
    this.tagGroups = {};
    
    /**
     * add Tag Cloud group
     * @memberOf WWBTagCloudSwitcher
     * @type Function
     * @return {undefined}
     */
    this.addGroup = function (groupId, data) {
        THIS.tagGroups[groupId] = data;
        if (THIS.defaultTagGroup === '') {
            THIS.setDefaultGroup(groupId);
        }
    };

    /**
     * add item to tag cloud switcher
     * @memberOf WWBTagCloudSwitcher
     * @type Function
     * @return {undefined}
     */
    this.addSwitcherItem = function (groupId) {
        var el; // new item element
        
        el = dojo.create('a', { href: '#', innerHTML: groupId, className: 'button button-gray button_over-green'},
            THIS.elSwitcher);
        el.onclick = THIS.switcherClick;
        if (groupId === THIS.defaultTagGroup) {
            THIS.switcherClick.call(el);
        }
    };

    /**
     * generate Flash for given group
     * @memberOf WWBTagCloudSwitcher
     * @type Function
     * @return {undefined}
     */
    this.generateFlash = function (groupId) {
        var so; // SWF object
        
        THIS.elFlash.innerHTML = '';
        so = new SWFObject(THIS.swfObjectPath + THIS.swfObjectFile, THIS.swfObjectId,
            THIS.boxWidth, THIS.boxHeight, "7", "#fff");
        so.addParam("wmode", "transparent");
        so.addVariable("hicolor", "0xff0000");
        so.addVariable("tcolor", "0x006db9");
        so.addVariable("tcolor2", "0x0085c9");
        so.addVariable("mode", "tags");
        so.addVariable("distr", "true");
        so.addVariable("tspeed", "200");
        so.addVariable("tagcloud", "<tags>" + THIS.tagGroups[groupId] + "</tags>");
        so.write(THIS.elFlash);
    };
    
    /**
     * initialize Tag Cloud
     */
    this.init = function () {
        var i; // iterator
        
        THIS.elContainer = dojo.byId(THIS.boxId);
        THIS.elFlash = dojo.create('div', null, THIS.elContainer);
        THIS.elSwitcher = dojo.create('div', null, THIS.elContainer);
        for (i in THIS.tagGroups) {
            if (!THIS.tagGroups.hasOwnProperty || THIS.tagGroups.hasOwnProperty(i)) {
                THIS.addSwitcherItem(i);
            }
        }
        THIS.refreshButtons();
    };
    
    /**
     * refresh graphical buttons
     */
    this.refreshButtons = function () {
        if (window.cfGraphicalButtons) {
            window.cfGraphicalButtons.refreshButtons(dojo.query('a', THIS.elSwitcher));
        }
    };
    
    /**
     * set default Tag Cloud group
     * @memberOf WWBTagCloudSwitcher
     * @type Function
     * @return {undefined}
     */
    this.setDefaultGroup = function (groupId) {
        if (typeof THIS.tagGroups[groupId] !== undefined) {
            THIS.defaultTagGroup = groupId;
        }
    };
    
    /**
     * handle click on switcher element
     * @memberOf WWBTagCloudSwitcher
     * @type Function
     * @return {Boolean} FALSE
     */
    this.switcherClick = function () {
        dojo.query('a', THIS.elSwitcher).removeClass('button-blue');
        dojo.query('a', THIS.elSwitcher).addClass('button-gray');
        dojo.removeClass(this, 'button-gray');
        dojo.addClass(this, 'button-blue');
        THIS.generateFlash(this.innerHTML);
        try {
            this.blur();
            THIS.refreshButtons();
        } catch (e) {}
        return false;
    };

}