/**
 * Wallpaper ads helper functions that can be used in the response code
 * from the ads server.to alter some content in the swoodoo page to match
 * the ads
 */
function wallpaper() {

    this.colorWhite = '#FFFFFF';
    this.colorGray = '#ECEEF1'

    /**
     * Default backgroud color to use
     */
    this.backgroundColor = this.colorWhite;



    /**
     * By default right skyscraper is NOT to be skipped (set to FALSE)
     */
    this.skipRightSkyscraper = false;

    /**
     * Change the background color to the given color
     * Color must be given in HEX. Hash char is optional
     *
     * @param string color Color code in 6 character HEX with [optional] hash (#) character
     */
    this.setBackground = function(color) {
        if (typeof color == 'undefined' || !color.match(/#?[0-9ABCDEF]{6}/i)) {
            color = this.backgroundColor;
        }

        color = (color.indexOf('#') == 0 ? '' : '#') + color;

        var body = dojo.query('body')[0];

        /**
         * If we set new design of gray background, and from ads we receve white background,
         * white background is overrided with gray.
         * This is done because language of selector.
         * It is in white background, and corners will not be seen on white background
         */
        if (dojo.hasClass(body, 'bodyGray') && color.toLowerCase() == this.colorWhite.toLowerCase()) {
            dojo.style(body, 'backgroundColor', this.colorGray);
        } else {
            dojo.style(body, 'backgroundColor', color);
        }


        dojo.removeClass(body, 'bodyNoWallpaper');
        dojo.style(dojo.byId('wallpaper-top'), 'display', 'block');

        this.setSkipRightSkyscraper(true);
    };

    /**
     * Toggle the right skyscraper ads call. If set to true
     * right skyscraper ads calls will not execute.
     */
    this.setSkipRightSkyscraper = function(skip) {
        if (typeof skip == 'undefined') {
            skip = true;
        }

        this.skipRightSkyscraper = skip;
    }

    /**
     * Fixes firefox middle rounting issue when window width is an on number by adding/removing
     * additional 1px margin-left for the body to even the window width.
     */
    this.fixFirefoxBody = function() {
        var body = dojo.query('body')[0];
        dojo.style(body, 'marginLeft', '0px');
        dojo.style(body, 'marginLeft', (body.offsetWidth % 2) + 'px');
    };
};

/**
 * Init wallpaper ads handler
 */
wallpaper = new wallpaper();

/**
 * There is a FireFox3 bug with rounding center points that affects the positioned elements
 * by not aligning correctly (an additional 1px apears) when window width is an odd number.
 *
 * So this script adds a onresize event for the firefox browser that adds/removes 1px margin
 * for the body to even the center point for the browser.
 */
dojo.addOnLoad(function() {
    if (dojo.isFF) {
        wallpaper.fixFirefoxBody();
        window.onresize = function() {
            wallpaper.fixFirefoxBody();
        };

        // Additional timed out call to fix body dimensions for Firefox
        // in case body dimensions changed in the any other processes.
        window.setTimeout(function() {
            wallpaper.fixFirefoxBody();
        }, 1000);
    }
});
