var advertImages = new Array();
var advertText = new Array();
var nextAdIndex = 0;
var timeBetweenSwaps = 2000;
var fadeSpeed = 5;

function startAdverts(root)
{
    var i = 0;
    advertImages[i++] = root + 'images/adverts/02.png';
    advertImages[i++] = root + 'images/adverts/03.jpg';
    advertImages[i++] = root + 'images/adverts/05.jpg';
    advertImages[i++] = root + 'images/adverts/06.png';
    advertImages[i++] = root + 'images/adverts/07.jpg';
    advertImages[i++] = root + 'images/adverts/08.jpg';
    advertImages[i++] = root + 'images/adverts/09.jpg';
    advertImages[i++] = root + 'images/adverts/10.jpg';
    advertImages[i++] = root + 'images/adverts/11.jpg';
    advertImages[i++] = root + 'images/adverts/12.jpg';

    i = 0;
    advertText[i++] = "AA Smartfuel Discounts";
    advertText[i++] = "Latest Diagnostic Equipment";
    advertText[i++] = "Bosch Car Service Centre";
    advertText[i++] = "European Vehicle Specialist";
    advertText[i++] = "AA Licenced Repairer";
    advertText[i++] = "MTA Assured Repairer";
    advertText[i++] = "Emission Testing Petrol and Diesel";
    advertText[i++] = "Your Electronic Specialist";
    advertText[i++] = "Approved Maintenance Provider";
    advertText[i++] = "Authorised Warranty Servicing and Repairer";

    // Set the first image
    document.getElementById('advertPhoto').src = advertImages[nextAdIndex];
    document.getElementById('advertText').innerHTML = advertText[nextAdIndex];

    // Do the backgrouond downloads
    for (index=0; index < advertImages.length; index++)
        document.getElementById('backgroundLoadingSlot').src = advertImages[index];

    setTimeout("doAdSwap()", timeBetweenSwaps);
}

function doAdSwap()
{
    // Move to next advert
    nextAdIndex++;
    if (nextAdIndex >= advertImages.length || nextAdIndex >= advertText.length)
        nextAdIndex = 0;

    // Set the photo and text against the back copy
    document.getElementById('advertPhoto').src = advertImages[nextAdIndex];
    document.getElementById('advertText').innerHTML = advertText[nextAdIndex];

    // Fade out the front picture...
    //fadeAdPicture(100 - fadeSpeed);
    setTimeout("doAdSwap()", timeBetweenSwaps);

}

function fadeAdPicture(opacity)
{
    updateAdOpacity(document.getElementById('advertPhoto'), opacity);
    updateAdOpacity(document.getElementById('advertText'), opacity);

    // Not see-through yet? Then do it again soon
    if (opacity > 0)
    {
        opacity -= fadeSpeed;
        setTimeout("fadeAdPicture("+opacity+")", 30);
    }

    // Once we get to zero, back image is showing.
    // Set front to be back, show front again, then set timer to do it all again soon!
    else
    {
        // Set front to be new info
        document.getElementById('advertPhoto').src = advertImages[nextAdIndex];
        document.getElementById('advertText').innerHTML = advertText[nextAdIndex];

        // Show front
        updateAdOpacity(document.getElementById('advertPhoto'), 100);
        updateAdOpacity(document.getElementById('advertText'), 100);

        // Do it all again in a mo...!
        setTimeout("doAdSwap()", timeBetweenSwaps);
    }
}

function updateAdOpacity(image, opacity)
{
    if (image.style) {
        if (image.style.MozOpacity!=null) {
            /* Mozilla's pre-CSS3 proprietary rule */
            image.style.MozOpacity = (opacity/100) - .001;
        } else if (image.style.opacity!=null) {
            /* CSS3 compatible */
            image.style.opacity = (opacity/100) - .001;
        } else if (image.style.filter!=null) {
            /* IE's proprietary filter */
            image.style.filter = "alpha(opacity="+opacity+")";
        }
    }
}

