var imgs = new Array();

var animType = "";
var animTime = 4; //Delay between animations default 3
var timer = animTime; //Global timer
var aTimer = 0; //Animation timer
var index = 0; //Index of image being animated
var previous = 0; //Index of i

function runBanner(bannerID, animationType) {
    animType = animationType;
    $("#" + bannerID).ready(function () {
        //Clear out the script that's inside the banner
        $("#" + bannerID).html("");
        //Now create the banner
        loadImages(bannerID);
        $("#" + bannerID + "_img0").css("opacity", 1);
        $("#" + bannerID + "_img0").css("z-index", 50);
        var t = setTimeout("runTimer()", 1000);
    });
}

function loadImages(bannerID) {
    var xmlDoc = loadXMLDoc("xml/" + bannerID + "_images.xml");
    for (var i = 0; i < xmlDoc.getElementsByTagName("img").length; i++) {
        //populate img attributes
        var img = xmlDoc.getElementsByTagName("img")[i];
        var imgID = bannerID + "_img" + i;
        var imgClass = "bannerImg";
        var imgSrc = img.getAttribute("src");
        var imgAlt = img.getAttribute("alt");
        var imgHref = "";
        if (img.getAttribute("href") != null)
            imgHref = "href='" + img.getAttribute("href") + "'";
        //create img tag
        var imgTag = "<a " + imgHref + "><img id='" + imgID + "' class='" + imgClass + "' src='" + imgSrc + "' alt='" + imgAlt + "' /></a>";
        //write the images to the banner
        $("#" + bannerID).html($("#" + bannerID).html() + imgTag);
        imgs[i] = imgID;
    }
    previous = imgs.length - 1;
}

function runTimer() {
    timer++;
    if (timer >= animTime) {
        $("#" + imgs[index]).css("z-index", "50");
        animate(animType);
    }
    else
        t = setTimeout("runTimer()", 1000);
}

function animate(animType) {
	if(index==0)
		$("#" + imgs[previous]).css("z-index",10);
    if (animType == "opacity")
        $("#" + imgs[index]).animate({ opacity: 1 }, "slow", function () { endAnimation(); });
    else if (animType == "maskRtoL") {
        aTimer = 0;
        var aT = setTimeout("fadeTimer()", 10);
    }
}

function fadeTimer() {
    aTimer += .01;
    var top = aTimer + .1;
    var mask = "-webkit-gradient(linear, 100% 0%, 0% 0%, color-stop(" + aTimer + ", rgba(0,0,0,1)), color-stop(" + top + ", rgba(0,0,0,0)),color-stop(1.20,rgba(0,0,0,0)))";
    $("#" + imgs[index]).css("webkitMaskImage", mask);
    if (aTimer == .01)
        $("#" + imgs[index]).css("opacity", 1);
    aT = setTimeout("fadeTimer()", 10);
    if (aTimer >= 1) {
        clearTimeout(aT);
        endAnimation();
    }
}

function endAnimation() {
    timer = 0;
    if (index < imgs.length - 1) {
        if (index == 0)
            $("#" + imgs[previous]).css("z-index", -5);
        previous = index;
        index++;
    }
    else {
        //put images in correct order except for last, which stays on top for transition
        for (var i = 0; i < imgs.length-1; i++)
            $("#" + imgs[i]).css("z-index", 0);
        $("#" + imgs[index]).css("z-index", 50);
        previous = index;
        index = 0;
    }
    $("#" + imgs[index]).css("z-index", 10);
    $("#" + imgs[index]).css("opacity", 0);
    t = setTimeout("runTimer()", 1000);
}

function loadXMLDoc(url) {
    if (window.XMLHttpRequest) // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    else // code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    xmlhttp.open("GET", url, false);
    xmlhttp.send();
    xmlDoc = xmlhttp.responseXML;
    return xmlDoc;
}
