/*
 * Fade header images in and out
 */

dojo.addOnLoad(function(){
    //count the images
    var count = dojo.query('.header_image').length;

    //remove the static header image and show the first image
    dojo.style(dojo.byId("fading_header"), "background", "none");
    dojo.style(dojo.byId("header_image1"), "display", "block");
    
    //set the opacity to 0 on all but the first image and show them
    for(var i = 2; i <= count; i++){
        dojo.style(dojo.byId("header_image" + i), "opacity", "0");
        dojo.style(dojo.byId("header_image" + i), "display", "block");
    }

    //fade the images in succession
    var current = 1;
    var fadeHeader = function(){
        var fadeOut = {
            node: dojo.byId("header_image" + current),
            duration: 5000
        };

        var next;
        if(current == count){
            current = 1;
            next = 1;
        }
        else{
            next = current + 1;
            current ++;
        }

        var fadeIn = {
            node: dojo.byId("header_image" + next),
            duration: 5000
        }

        dojo.fadeOut(fadeOut).play();
        dojo.fadeIn(fadeIn).play();

        window.setTimeout(fadeHeader, 8000);
    };

    fadeHeader();
});
