/*  Title: Category header randomizer
    Author: Tim Dunham (based largely on BKK home page code written by Isaac Chellin)
    Company: Bose Corporation
    Requires: scriptaculous and Prototype
    Last updated: 09/24/2008
    Description: Rotates header image based on percentage.
*/
function createRandomizer(objAssets,intRange) {
    this.collection = objAssets;
    this.range = intRange;
    /*  a precentage has been given so we add some info.
        we also sort the object by weight high [0] to low [5]
        to support the randomization logic
    */
    if(this.range) {
        this.ranNum = Math.ceil(Math.random() * this.range);
        for(var i=0;i<=this.collection.length;i++) {
            for(var j=0;j<=this.collection.length-2;j++){
                var thisIndex = this.collection[j];
                var nextIndex = this.collection[j+1];
                if(this.collection[j].weight < this.collection[j+1].weight) {
                    this.collection[j] = nextIndex;
                    this.collection[j+1] = thisIndex;
                }
            }
        }
    }
    /* methods */
    this.getDefaultDisplayPercentage =  function() {
                                            var percent = (this.range / (this.collection.length));
                                            return percent;
                                        };
    /* properties */
    this.defaultDisplayPercentage = this.getDefaultDisplayPercentage();
    this.renderAsset =  function() {
                            var count = 0;
                            var num = this.ranNum;// scope issue ?
                            this.collection.each(function(obj) {
                                obj.range = {low: count+1, high: obj.weight + count};
                                if (num >= obj.range.low && num <= obj.range.high){
                                    var imageBlock = $('page_header');
                                    imageBlock.setStyle({
                                        background: "url('" + obj.src + "') no-repeat",
                                        backgroundColor: "#5E719B"
                                    });
                                }
                                count += obj.weight;
                            });
                        };
};

/*  Example expected data structure
    var objAssets = [
                    {src:'image_1.gif',weight:25},
                    {src:'image_2.gif',weight:30},
                    {src:'image_3.gif',weight:40},
                    {src:'image_4.gif',weight:5}
                ]; */

new PeriodicalExecuter( function(randomHeaderPE) {
    if ($('page_header')){
        randomHeaderPE.stop();
        if(objAssets.length >= 1) {
            var randomizer = new createRandomizer(objAssets,100);
            randomizer.renderAsset();
        }
    }
},.1);

new PeriodicalExecuter( function(navextend_pe) {
    if ($('footer')){
        navextend_pe.stop();
        navExtend('content_inner');
    }
},.1);