﻿var visibleBrands = new Array(0, 1, 2, 3, 4, 5);
var numImages = 43;
var fadedOutImages = 0;
var speed = 40;
var brandUrls = new Array('http://www.propertywide.co.uk/',
    'http://www.rabennett.co.uk/',
    'http://www.abbotts.co.uk/',
    'http://www.rentonscountrywide.co.uk/',
    'http://www.buckellandballard.co.uk/',
    'http://www.alandemaid.co.uk/',
    'http://www.chappellandmatthews.co.uk/',
    'http://www.austinwyatt.co.uk/',
    'http://www.bairstoweves.co.uk/',
    'http://www.beresfordadams.co.uk/',
    'http://www.bridgfords.co.uk/',
    'http://www.carsons.co.uk/',
    'http://www.dixonsestateagents.co.uk/',
    'http://www.entwistlegreen.co.uk/',
    'http://www.faronsutaria.co.uk/',
    'http://www.frankinnes.co.uk/',
    'http://www.freemanforman.co.uk/',
    'http://www.fulfords.co.uk/',
    'http://www.gpees.co.uk/',
    'http://www.johndwood.co.uk/',
    'http://www.manncountrywide.co.uk/',
    'http://www.millercountrywide.co.uk/',
    'http://www.morrisdibben.co.uk/',
    'http://www.palmersnell.co.uk/',
    'http://www.countrywidescotland.co.uk/',
    'http://www.geeringandcolyer.co.uk/',
    'http://www.hetheringtons.co.uk/',
    'http://www.slaterhogg.co.uk/',
    'http://www.spencers.co.uk/',
    'http://www.strattoncreber.co.uk/',
    'http://www.taylorsestateagents.co.uk/',
    'http://www.watsonbullporter.co.uk/',
    'http://www.wilsonpeacock.co.uk/',
    'http://www.kingandchasemore.co.uk/',
    'http://www.lockeandengland.co.uk/',
    'http://www.hamptons.co.uk/',
    'http://www.sothebysrealty.com/',
    'http://www.rentonscountrywide.co.uk/',
    'http://www.andrewreeves.co.uk/',
    'http://www.accordproperty.co.uk/',
    'http://www.aboutletting.co.uk/',
    'http://www.crldirect.co.uk/',
    'http://www.speclets.com/',
    'http://www.andrewbutler.co.uk/');

if (navigator.appName === "Microsoft Internet Explorer") {
    speed = 5;
}

function StartFade() {
    
    // Get the max image number
    var maxImgNum = 1;
    for (var i = 0; i < visibleBrands.length; i++) {
        if (visibleBrands[i] > maxImgNum) {
            maxImgNum = visibleBrands[i];
        }
    }
    maxImgNum++;

    // Create the next set array
    var newBrands = new Array();
    for (var i = 0; i < 5; i++) {
        if ((maxImgNum + i) > numImages) maxImgNum = 1;
        newBrands[i] = maxImgNum + i;
    }

    newBrands[5] = 0;
    visibleBrands = newBrands;

    // Mix up the ordering
    visibleBrands.sort(RandomOrder);

    // Fade the current images out
    setTimeout(function() {
        FadeOut("brand0", 1);
        FadeOut("brand1", 1);
        FadeOut("brand2", 1);
        FadeOut("brand3", 1);
        FadeOut("brand4", 1);
        FadeOut("brand5", 1);
    }, 2000);
}

function RandomOrder() {
    return (Math.round(Math.random()) - 0.5);
}

function FadedOut(imgID) {
    fadedOutImages++;

    // Change the Image
    var img = document.getElementById(imgID);
    var imgNum = parseFloat(imgID.replace("brand", ""));       //parseFloat(img.src.substring(img.src.length - 6).replace("/", ""));
    
    img.src = "/brands/" + visibleBrands[imgNum] + ".jpg";
    img.onmouseover = function() { this.src = this.src.replace(".jpg", "-clr.jpg"); };
    img.onmouseout = function() { this.src = this.src.replace("-clr.jpg", ".jpg"); };

    // Change the url
    var link = document.getElementById(imgID.replace("brand", "brandlink"));
    link.href = brandUrls[visibleBrands[imgNum]];

    // When all finished
    if (fadedOutImages == 6) {
        // Fade them back in
        FadeIn("brand0", 0);
        FadeIn("brand1", 0);
        FadeIn("brand2", 0);
        FadeIn("brand3", 0);
        FadeIn("brand4", 0);
        FadeIn("brand5", 0);
    }
}

function FadedIn(imgID) {
    fadedOutImages--;

    // When all faded in
    if (fadedOutImages == 0) {
        // Set the cycle going again
        StartFade();
    }
}

function FadeOut(imgID, opacity) {
    if ((opacity - 0.02) < 0) {
        opacity = 0;
    } else {
        opacity -= 0.02;
    }

    DoFade(imgID, opacity);

    if (opacity > 0) {
        setTimeout(function() { FadeOut(imgID, opacity); }, speed);
    } else {
        FadedOut(imgID);
    }
}

function FadeIn(imgID, opacity) {
    if ((opacity + 0.02) > 1) {
        opacity = 1;
    } else {
        opacity += 0.02;
    }

    DoFade(imgID, opacity);

    if (opacity < 1) {
        setTimeout(function() { FadeIn(imgID, opacity); }, speed);
    } else {
        FadedIn(imgID);
    }
}

function DoFade(imgID, opacity) {
    var img = document.getElementById(imgID);

    if (typeof img.filters == "object") {
        if (opacity > 0 || opacity < 100) {
            img.style.filter = "alpha(opacity:" + opacity + ")";
            img.filters.alpha.opacity = opacity * 100;
        }
    } else if (typeof img.style.KhtmlOpacity != "undefined") {
        if (opacity > 0 || opacity < 1) {
            img.style.KhtmlOpacity = opacity;
        }
    } else {
        if (opacity > 0 || opacity < 1) {
            img.style.MozOpacity = opacity;
        }
    }
}

window.onload = function() {
    StartFade()
}