/**
 * ---
 * BackgroundLoader Class
 * ---
 */
function BackgroundLoader() {
    this.backgrounds = [
        {
            url: '/img/backgrounds/tahoe_1.jpg',
            bgcolor: '#7cb2db'
        }
    ];
    
    this.show_new_after = 3; //show a new background after 3 loads

    this.setBackground();
}

BackgroundLoader.prototype.loadBackground = function() {
    if(this.backgrounds.length <= 1) {
        if(!jQuery.cookie('background_loader_views')) {
            jQuery.cookie('background_loader_views', 1);
            jQuery.cookie('background_loader_index', 0);
            this.show_fade = true;
        }
        else {
            this.show_fade = false;
        }

        this.background = this.backgrounds[0];
    }
    else {
        var background = {};
        if(!jQuery.cookie('background_loader_views') || jQuery.cookie('background_loader_views') > this.show_new_after) {
            var new_index = Math.floor(Math.random() * this.backgrounds.length);
            var current_index = jQuery.cookie('background_loader_index');
            if(typeof current_index == 'string')
                current_index = parseInt(current_index, 10);

            while(new_index === current_index) //if the background is set, find an index besides the current one
                new_index = Math.floor(Math.random() * this.backgrounds.length);

            jQuery.cookie('background_loader_views', 1);
            jQuery.cookie('background_loader_index', new_index);

            this.background = this.backgrounds[new_index];
            this.show_fade = true;
        }
        else {
            jQuery.cookie('background_loader_views', parseInt(jQuery.cookie('background_loader_views'), 10) + 1);
            this.background = this.backgrounds[parseInt(jQuery.cookie('background_loader_index'), 10)];
            this.show_fade = false;
        }
    }
};

BackgroundLoader.prototype.setBackground = function() {
    this.loadBackground();
    var background = this.background;
    var img = new Image();

    /* The fade code shows the background fade in on page load. I've disabled it because their was an unresolvable
       issue with the background making the page height longer than it should be. If a solution can be found to this,
       that would be totally sweet. -JK
     */
//    if(this.show_fade) {
////        jQuery('#main_website_body').add(jQuery('#main_website_body').parent('html')).css({height: '100%'});
//        jQuery('#main_website_body').css({backgroundColor: background.bgcolor});
//
//        jQuery(img).load(function() {
//            var bg_holder = jQuery('<div></div>').css({
//                width: '100%',
////                height: 'auto !important',
////                minHeight: '100%',
//                height: img.height,
//                backgroundImage: 'url(' + background.url + ')',
//                backgroundPosition: 'top center',
//                backgroundRepeat: 'no-repeat',
//                position: 'absolute',
//                overflow: 'hidden'
//            });
//
//            jQuery('#main_website_body').prepend(bg_holder);
//            bg_holder.hide().fadeIn(2000);
//        }).attr('src', background.url);
//    }
//    else {
        jQuery('#main_website_body').css({
            backgroundColor: background.bgcolor,
            backgroundImage: 'url(' + background.url + ')',
            backgroundPosition: 'top center',
            backgroundRepeat: 'no-repeat'
        });
//    }
};

BackgroundLoader.prototype.hasSession = function() {
    if(window.name == 'background_loader_session')
        return true;

    window.name = 'background_loader_session';
    return false;
}
