var bgTotalImages = 8;     // Images to cycle through
var bgNewImage = 0;         // Next image to show
var bgCurrentImage =  0   // Currently displayed image
var imageURLBase = "/cms_data/assets/backgrounds/front"
var currentQuote = 0;

var quotes = [
    '<em>"We believe what truly sets us apart from other companies is our passion for what we do, the flexibility or our products and the relationship we build with our clients."</em> Rusada CEO',
    '<em>"The collaboration and the sharing of data between OEMs, operators and MROs will become increasingly more important and is the key to driving significant cost savings and efficiency gains for the future."</em> Rusada CEO',
    '<em>"We selected Rusada since they were the only solution provider who could offer true, single database integration covering so many business functions, combined with the scalability to grow with the expansion of our business."</em> V Australia',
    '<em>"We considered a number of fleet propositions, but only Rusada was able to accommodate our exacting requirements and those of the military for both fleet management and maintenance."</em> Eurocopter',
    '<em>"When we took the decision to expand our business into the provision of Global CAMO services, our new offering had to be built on the strongest IT platform possible, not least to meet the tough regulatory approval requirements. We knew the Rusada solution was the best on the market."</em> Eurocopter'
];

/*
** Array made up of:
**  x value = top left of quote box
**  y value = top left of quote box
**  background image for the quote box
**  Lowest position in array to choose quotes from
**  Number of quotes to choose from 
*/

var quotePosition = [
    [630, 180, 'none', 0, 2],
    [630, 20, 'url(/images/front_quote_bg.png)', 3, 2],
    [20, 20, 'url(/images/front_quote_bg.png)', 0, 2],
    [598, 30, 'none', 0, 2],
    [60, 50, 'url(/images/front_quote_bg.png)', 0, 2],
    [60, 50, 'url(/images/front_quote_bg.png)', 2, 1],
    [15, 15, 'url(/images/front_quote_bg.png)', 3, 2],
    [60, 50, 'url(/images/front_quote_bg.png)', 0, 2]
]

$(document).ready(function() {                    

       
    // Set tht current image
    bgCurrentImage = Math.floor(Math.random()*bgTotalImages) + 1;
    $('.slideshow').css("background-image", "url(" + imageURLBase + bgCurrentImage + ".jpg)"); 

    // Set a random quote
    currentQuote = Math.floor(Math.random() * quotePosition[bgCurrentImage - 1][4]) + quotePosition[bgCurrentImage - 1][3];
    $("#quote").html(quotes[currentQuote]);
    $("#quote").css("background-image", quotePosition[bgCurrentImage - 1][2]); 
    $("#quote").css("top", quotePosition[bgCurrentImage - 1][1]); 
    $("#quote").css("left", quotePosition[bgCurrentImage - 1][0]); 
    
    // Start the rotation funciton (every 12 seconds)
    setTimeout( function() { switchBackground(); }, 12000 );

    // Fade in quote after 5 seconds
    setTimeout( function() { fadeInQuote(); }, 4000 );
    
    
    // Handle default text for login email box
    $(".loginform input#username").focus(function() {
        if (this.value == 'E-mail') {
            this.value = '';
            this.style.color = '#000000';
        }
    });
    $(".loginform input#username").blur(function() {
      if (this.value == '') {
        this.style.color = '#ababab';
        this.value = 'E-mail';
      }
    });
    
    // Handle default text for login password box
    $('.loginform input#passwordclear').focus(function() {
        $('.loginform input#passwordclear').hide();
        $('.loginform input#password').show();
        $('.loginform input#password').focus();
    });
    $('.loginform input#password').blur(function() {
        if($('.loginform input#password').val() == '') {
            $('.loginform input#passwordclear').show();
            $('.loginform input#password').hide();
        }
    });
});

function fadeInQuote()
{
    $('#quote').fadeIn(500);
}

// Function to choose a new image and fade out the current image
function switchBackground()
{
    // Select new image
    bgNewImage = Math.floor(Math.random()*bgTotalImages) + 1;
        
    // Ensure it's not the same as the current one 
    while(bgNewImage == bgCurrentImage)
    {
        bgNewImage = Math.floor(Math.random()*bgTotalImages) + 1;
    }
    
    // Cache the new image
    var cache = [];
    var cacheImage = document.createElement('img');
    cacheImage.src = imageURLBase + bgNewImage + ".jpg";
    cache.push(cacheImage);
    
    bgCurrentImage = bgNewImage;

    // Begin the fade out
    $("div.group1").fadeIn(500);
    setTimeout( function() { FadeIn(2, null); }, 200 );
    setTimeout( function() { FadeIn(3, null); }, 400 );
    setTimeout( function() { FadeIn(4, null); }, 600 );
    setTimeout( function() { FadeIn(5, null); }, 800 );
    setTimeout( function() { FadeIn(6, null); }, 1000 );
    setTimeout( function() { FadeIn(7, null); }, 1200 );
    setTimeout( function() { FadeIn(8, null); }, 1400 );
    setTimeout( function() { FadeIn(9, null); }, 1600 );
    setTimeout( function() { FadeIn(10, null); }, 1800 );
    setTimeout( function() { FadeIn(11, null); }, 2000 );
    setTimeout( function() { FadeIn(12, null); }, 2200 );
    setTimeout( function() { FadeIn(13, null); }, 2400 );
    setTimeout( function() { FadeIn(14, performBackgroundSwitch); }, 2600 );
}   

// Function to fade out the background div (By fading in the covering DIV)
function FadeIn(val, callback)
{
    $("div.group" + val).fadeIn(500, callback);
}


// Function to fade in the background div (By fading out the covering DIV)
function FadeOut(val, callback)
{
    $("div.group" + val).fadeOut(500, callback);
}

// Function to perform the background image change and fade back in the image
function performBackgroundSwitch()
{
    
    // Set a random quote
    var newQuote = Math.floor(Math.random() * quotePosition[bgNewImage - 1][4]) + quotePosition[bgNewImage - 1][3];

    $("#quote").html(quotes[newQuote]);
    currentQuote = newQuote;
    
    // Set BG colour and position and hide
    $("#quote").hide();
    $("#quote").css("background-image", quotePosition[bgNewImage - 1][2]); 
    $("#quote").css("top", quotePosition[bgNewImage - 1][1]); 
    $("#quote").css("left", quotePosition[bgNewImage - 1][0]); 

    $('.slideshow').css("background-image", "url(" + imageURLBase + bgNewImage + ".jpg)"); 

    $("div.group1").fadeOut(500);
    setTimeout( function() { FadeOut(2, null); }, 400 );
    setTimeout( function() { FadeOut(3, null); }, 600 );
    setTimeout( function() { FadeOut(4, null); }, 800 );
    setTimeout( function() { FadeOut(5, null); }, 1000 );
    setTimeout( function() { FadeOut(6, null); }, 1200 );
    setTimeout( function() { FadeOut(7, null); }, 1400 );
    setTimeout( function() { FadeOut(8, null); }, 1600 );
    setTimeout( function() { FadeOut(9, null); }, 1800 );
    setTimeout( function() { FadeOut(10, null); }, 2000 );
    setTimeout( function() { FadeOut(11, null); }, 2200 );
    setTimeout( function() { FadeOut(12, null); }, 2400 );
    setTimeout( function() { FadeOut(13, null); }, 2600 );
    setTimeout( function() { FadeOut(14, null); }, 2800 );
    
    setTimeout( function() { switchBackground(); }, 12000 );    // Start the cycle again
    setTimeout( function() { fadeInQuote(); }, 4000 );  
}