﻿if (typeof(KIM) == 'undefined') { KIM ={};} 
KIM.bgswitch = {

    __version: 1.0, // class version
    __class: 'KIM.bgswitch', // class name

    // set default options
    _oDefaults: {
        bgTrigger: "#toolbar .style_switch li a",
        arrColor: ["silver", "blue", "turquoise", "pink", "violet", "green"],
        arrColorValue: ["#bebfbf", "#54a0d9", "#107e8b", "#d7548d", "#713799", "#4f7c21"],
        cookieIdentifier: "KIMBgColor"
    },

    /**
    * Setup
    * @return {Void}
    */
    _setUp: function(oOptions) {
        var _scope = KIM.bgswitch;

        // merge options and defaults
        _scope._oOpt = jQuery.extend({}, _scope._oDefaults, oOptions);
        _scope._oDefaults = null;

        jQuery(_scope._oOpt.bgTrigger).bind("click.bgswitch", this._changeBGColour);
        jQuery(_scope._oOpt.bgTrigger).bind("click.bgswitch", this._setCookie);
        jQuery(_scope._oOpt.bgTrigger).bind("focus.bgswitch", this._blurElement);
    },

    _changeBGColour: function() {
        var _scope = KIM.bgswitch;
        var image = "/images/backgrounds/body-" + this.className + ".jpg";
        var bgColor = _scope._oOpt.arrColorValue[jQuery.inArray(this.className, _scope._oOpt.arrColor)];

        /* Browser switch for IE6 */
        jQuery.each(jQuery.browser, function(i, val) {
            if (i == "msie" && jQuery.browser.version.substr(0, 1) == "6") {
                window.setTimeout(function setBGImage() {
                    jQuery("body").css("background", bgColor + " url(" + image + ") repeat-y left top");

                }, 0);
            }
            else {
                jQuery("body").css("background", bgColor + " url(" + image + ") repeat-y left top");
            }
        });

        return false;
    },

    _blurElement: function() {
        this.blur();
    },

    _setCookie: function() {
        var _scope = KIM.bgswitch;
        var expires = new Date();
        var oneMonth = expires.getTime() + (1000 * 60 * 60 * 24 * 30); //30 days
        expires.setTime(oneMonth);
        document.cookie = _scope._oOpt.cookieIdentifier + "=" + this.className + "; expires=" + expires.toGMTString() + "; path=/";
    },


    /**
    * Get singleton instance
    * @param {Object} [oOptions]
    * @return {Instance}
    */
    getInstance: function(oOptions) {
        return KIM.bgswitch.initialize(oOptions);
    },

    /**
    * Constructor
    * @param {Object} [oOptions]
    * @constructor
    */
    initialize: function(oOptions) {
        // fake singleton
        if (this._oOpt) {
            return this;
        }
        // setup 
        this._setUp(oOptions);
        // fake singleton
        return this;
    }

};