/**
 * Manages the Nikon SRO homepage.
 * @author Adam J. McIntyre
 */
SRO_HP = function() {
    var cookie = 'sro_homepage_img';
    var backgroundImageId = 'hero_homepage';
    var images = [];

    return{
        /***
         * Sets all swappable background images.
         * @param {Array} arr Array of images
         */
        setImages : function(arr) {
            images = arr;
            return this;
        },
        _getImages : function() { return images; },
        /***
         * Getter for returning ID of background image container
         */
        getBackgroundImage : function() {
            return backgroundImageId;
        },
        /***
         * Adds a background image to managed array.
         * @param {String} img URL of image to push
         */
        addBgImage : function(img) { images.push(img); },
        /***
         * Sets a cookie showing which background image a user just saw.
         * @param {String} name Name of cookie to set
         * @param {String} value Value to set
         * @param {Duration} duration Duration to set cookie for
         */
        setCookie : function(name, value, duration) {
            var d = new Date();
            d.setTime(d.getTime() + (duration * 24 * 60 * 60 * 1000));
            document.cookie = name + '=' + value + ';Expires=' + d.toGMTString();
        },
        /***
         * Reads a cookie of a given name
         * @param {String} name Name of cookie to read
         */
        readCookie : function(name) {
            var nameEq = name + '=';
            var cookies = document.cookie.split(';');
            for(var i = 0; i < cookies.length; i++) {
                var c = cookies[i];
                while(c.charAt(0) == ' ') {
                    c = c.substr(1, c.length);
                }
                if(c.indexOf(nameEq) == 0) {
                    return c.substr(nameEq.length);
                }
            }
            return false;
        },
        /***
         * Removes a cookie of a given name
         * @param {String} name Name of cookie to remove
         */
        removeCookie : function(name) {
            this.setCookie(name, -1);
            return true;
        },
        /***
         * Binds set up code to YUI's onDOMReady.
         */
        init : function() {
            var that = this;
            YAHOO.util.Event.onDOMReady(function() {
                var idx = 0;

                if(images.length > 1) {
                    var idx = Math.round(Math.random() * (images.length - 1));
                    var lastImg = parseInt(that.readCookie(cookie), 10) || 0;
                    while(lastImg == idx) {
                        idx = Math.round(Math.random() * (that._getImages().length - 1));
                    }
                }

                that.setCookie(cookie, idx, 1);
                document.getElementById(that.getBackgroundImage()).style.backgroundImage = 'url(' + that._getImages()[idx] + ')';
            });
        }
    }
}();

