
// Resize background image to fill entire screen
function backgroundResize(){ 
    var $bg = $('#background');
    if( ! $bg.is('.resize') ) return;
    var wh = $(window).height();
    var ww = $(window).width();
    var bh = $bg.height();
    var bw = $bg.width();
    if( wh > bh ) {
        $bg.css({'width':'auto', 'height':'100%'});
    }
    else if( ww > bw) {
        $bg.css({'width':'100%', 'height':'auto'});
    }
}

// Crop background image to screen dimensions;
function cropBackground() {    
    var bgimg = $('#background')[0];
    if( bgimg && $(bgimg).is('.resize') ) {
        var src = bgimg.src.replace('http://'+window.location.hostname,'');
        if( src.search('thumbnail.php') != -1 ) {
            src = src.split('&');
            query = src[0].replace('/img/thumbnail.php?img=','') + '&w='+screen.width+'&h='+screen.height+'&crop=0';
        } else {
            query = src.replace('/img/','') + '&w='+screen.width+'&h='+screen.height+'&crop=0';
        }
        src = '/img/thumbnail.php?img='+query;
        bgimg.src = src;
    }        
}

var imageRotator = function(images){ this.init(images); }
imageRotator.prototype = {
    bgindex : 0,
    bgtimer : null,
    bginterval : 6000,
    doSlideshow: true,
    nextID: 'nextImage',
    prevID: 'prevImage',
    pauseID: 'pauseImage',
    playing : false,

    init: function(images) {
        if( images.length < 2 ) return;
        var self = this;
        self.images = images;
        
        self.start();

        var $controls = $('<div id="image-controls">' +
            '<img src="/img/image_controls/previous.png" id="prev" /> '+
            '<img src="/img/image_controls/stop.png" id="stop" /> '+
            '<img src="/img/image_controls/next.png" id="next" /> '+
            '</div>').appendTo('#page');

        $('#stop').click( function(){ self.toggle() });
        $('#next').click( function(){ self.next() });
        $('#prev').click( function(){ self.prev() });

        $('img', $controls).mouseover(rollOverEvent).mouseout(rollOutEvent).iepngfix();
    },

    start: function() {
        var self = this;
        self.playing = true;
        self.bgtimer = setTimeout( function(){self.rotate()}, self.bginterval);
        $('#stop').attr( 'src', '/img/image_controls/stop.png');
    },

    next: function() {
        var self = this;
        self.stop();
        self.rotate(1);
    },

    prev: function() {
        var self = this;
        self.stop();
        self.rotate(-1);
    },

    stop: function() {
        var self = this;
        self.playing = false;
        clearTimeout(self.bgtimer);
        $('#stop').attr('src', '/img/image_controls/play.png');
    },

    toggle: function() {
        var self = this;
        if( self.playing )
            self.stop();
        else 
            self.start();
    },
    
    rotate: function(c) {
        var self = this;
        var img = $('#background')[0];
        var src = img.src;
        var c = (c ? c : 1);
        
        // Next image source
        self.bgindex = ((self.bgindex+c) + self.images.length)% self.images.length;
        var newsrc = 'gallery/' + self.images[self.bgindex];
        var uri = src.split('?');
        var qstr = uri[1];
        qstr = qstr.replace(/img=[^&]+/, 'img='+newsrc);
        
        img.onload = function() {
            backgroundResize();
            if( self.playing ) self.bgtimer = setTimeout( function(){self.rotate(1)}, self.bginterval );
        };
        img.src = uri[0] + '?' + qstr;

        // Preload next one
        nextindex = ((self.bgindex+1) % self.images.length);
        var nextsrc = 'gallery/' + self.images[nextindex];
        prel = new Image();
        var nexturi = src.split('?');
        var nextqstr = uri[1];
        nextqstr = nextqstr.replace(/img=[^&]+/, 'img='+nextsrc);
        prel.src = nexturi[0] + '?' + nextqstr;
    }
}


// EVENTS 
$(document).ready( function () {
    cropBackground();    
    $(window).bind('resize', backgroundResize);
});

$(window).bind('load', function() {
    // Resizing background
    backgroundResize();    
    // Start background image rotation
    if( typeof(backgrounds) != 'undefined' && backgrounds.length > 1 ) 
        new imageRotator(backgrounds); 
});
