/**********************************************************************************************************
* Author: Joshua Carmody
* Last Modified: 2009-04-08
**********************************************************************************************************
**********************************************************************************************************/


function cyclingImagesGetImageWidth(imgElement) {
    var imageWidth = 0;
    if (imgElement.clientWidth > 0) {
        imageWidth = imgElement.clientWidth;
    }
    else if (imgElement.width) {
        imageWidth = imgElement.width;
    }
    else if (imgElement.style.width) {
        imageWidth = parseInt(imgElement.style.width.replace("px", "").replace("pt", "").replace("em", ""));
    }
    return imageWidth;
}


function cyclingImagesGetImageHeight(imgElement) {
    var imageHeight = 0;
    if (imgElement.clientHeight > 0) {
        imageHeight = imgElement.clientHeight;
    }
    else if (imgElement.height) {
        imageHeight = imgElement.height;
    }
    else if (imgElement.style.height) {
        imageHeight = parseInt(imgElement.style.width.replace("px", "").replace("pt", "").replace("em", ""));
    }
    return imageHeight;
}


function cycleImagesInDiv(divId)
{
    var divElement = document.getElementById(divId)
    if (divElement) {
        var imgElements = divElement.getElementsByTagName("img");
        imgElements[divElement.cyclingImagesCurrentImage].style.display = "none";
        divElement.cyclingImagesCurrentImage = (divElement.cyclingImagesCurrentImage + 1) % imgElements.length;
        imgElements[divElement.cyclingImagesCurrentImage].style.display = "block";
        setTimeout("cycleImagesInDiv(\"" + divId + "\");", 2000);
    }
}


function cyclingImagesSetup() {
    var divElements = document.getElementsByTagName("div");

    for (var divElmCounter = 0; divElmCounter < divElements.length; divElmCounter++) {
        if (divElements[divElmCounter].className) {
            if (divElements[divElmCounter].className.indexOf("cyclingimages") > -1) {
                var imgElements = divElements[divElmCounter].getElementsByTagName("img");
                var maxImageWidth = 0;
                var maxImageHeight = 0;
                for (var imgElmCounter = 0; imgElmCounter < imgElements.length; imgElmCounter++) {
                    var imgWidth = cyclingImagesGetImageWidth(imgElements[imgElmCounter]);
                    var imgHeight = cyclingImagesGetImageHeight(imgElements[imgElmCounter]);
                    maxImageWidth = imgWidth > maxImageWidth ? imgWidth : maxImageWidth;
                    maxImageHeight = imgHeight > maxImageHeight ? imgHeight : maxImageHeight;
                    if (imgElmCounter == 0) {
                        imgElements[imgElmCounter].style.display = "block";
                    } else {
                        imgElements[imgElmCounter].style.display = "none";
                    }
                    imgElements[imgElmCounter].style.visibility = "visible";
                }
                if (maxImageWidth > 0) {
                    divElements[divElmCounter].style.width = maxImageWidth + "px";
                }
                if (maxImageHeight > 0) {
                    divElements[divElmCounter].style.height = maxImageHeight + "px";
                }
                divElements[divElmCounter].cyclingImagesCurrentImage = 0;
                if (!(divElements[divElmCounter].id)) {
                    divElements[divElmCounter].id = "cyclingimagesdiv" + divElmCounter;
                }
                setTimeout("cycleImagesInDiv(\"" + divElements[divElmCounter].id + "\");", 2000);
            }
        }
    }
}


try {
    window.addEventListener("load", cyclingImagesSetup, false);
}
catch (err) {
    try {
        window.attachEvent("onload", cyclingImagesSetup);
    }
    catch (err2) {
        // Seems our images just won't cycle.
    }
}

