﻿
$(document).ready(function(e){

    var img = new Image();
    img.src = backgroundImages[0];
rotateImages(0, 3000, true);

    
	    scaleRotator();
    
	    $(window).resize(function(){
	        scaleRotator();
	    });
});

function scaleRotator()
{
    var height = $.viewportHeight();
    var wrapperHeight = $('#wrapper').height();

    if(wrapperHeight > height)
    {
        height = wrapperHeight;
    }

    $('.backgroundImages').height(height);
}

$.viewportHeight = function() {
 return self.innerHeight ||
    jQuery.boxModel && document.documentElement.clientHeight ||
    document.body.clientHeight;

};

var running = true;
function rotateImages(index, speed, cache)
{
    if(running)
    {
        var count = backgroundImages.length;
        
        if($('#imageFadeIn').is(":hidden"))
        {
            $('#imageFadeIn').attr('src', backgroundImages[index]);
        }
        else
        {
            $('#imageFadeOut').attr('src', backgroundImages[index]);
        }
        
        $('#imageFadeOut').fadeToggle(speed);
        $('#imageFadeIn').fadeToggle(speed, function()
        {
            index = index + 1;
            
            if(index != (count - 1))
            {
	    	if(cache)
		{
                	var img = new Image();
                	img.src = backgroundImages[index];
		}
            }
            else
            {
		cache = false;
		index = 0;
            }
            
            setTimeout('rotateImages(' + index + ', ' + speed + ', ' + cache + ');', 10000);
            
        });
    }
    else
    {
        setTimeout('rotateImages(' + index + ', ' + speed + ', ' + cache + ');', 10000);
    }
}

function pause()
{
    running = false;
}

function resume()
{
    running = true;
}

jQuery.fn.fadeToggle = function(speed, easing, callback) {
    return this.animate({opacity: 'toggle'}, speed, easing, callback);
}; 