var bannerRotationDelay = 7000;
var pictureRotationDelay = 10000;

function initObjectsRotation() {
    initBannerRotation("rotatingRightBanner");
    initBannerRotation("rotatingTop1Banner");
    initBannerRotation("rotatingTop2Banner");
    initBannerRotation("rotatingTop3Banner");
    initBannerRotation("rotatingLeftBanner");
    initBannerRotation("rotatingBottomBanner");

    rotatePictures("rotatingPictureLeft");
    rotatePictures("rotatingPictureRight");
    rotatePictures("rotatingPictureCenter");
}

function initBannerRotation(idPrefix) {
    window.setTimeout("rotateBanners('" + idPrefix + "');", bannerRotationDelay);
}

function rotateBanners(idPrefix) {
    var hasRotations = false;
    for (i = 0; i < 999; i++) {
        var obj = _id(idPrefix + i + "_1");
        var firstBanner = _id(idPrefix + i + "_0");
        if (firstBanner == null)
            break;
        if (obj == null)
            continue;

        var visibleBanner = null;
        var nextVisibleBanner = firstBanner;
        for (j = 0; j < 99; j++) {
            var current = _id(idPrefix + i + "_" + j);
            if (current == null)
                break;
            if (current.style.display == null || current.style.display != "none") {
                visibleBanner = current;
                continue;
            }
            if (visibleBanner != null) {
                nextVisibleBanner = current;
                break;
            }
        }
        if (visibleBanner != null) {
            visibleBanner.style.display = "none";
            nextVisibleBanner.style.display = "";
            hasRotations = true;
        }
    }
    if (hasRotations)
        window.setTimeout("rotateBanners('" + idPrefix + "');", bannerRotationDelay);
}

function rotatePictures(idPrefix) {
    var img = _id(idPrefix + "_img");
    if (img == null)
        return;
    jsrsExecute("fakeurl.rs", rotatePicturesCallback, "getNextRotatePicture", Array(window.location.href, idPrefix));
}

function rotatePicturesCallback(result) {
    if (result == null || result == "")
        return;
    var index = result.indexOf(";-;");
    if (index == -1)
        return;
    var idPrefix = result.substring(0, index);
    window.setTimeout("rotatePictures('" + idPrefix + "');", pictureRotationDelay);
    var img = _id(idPrefix + "_img");
    if (img == null)
        return;
    var index1 = result.indexOf(";-;", index + 3);
    if (index1 == -1)
        return;
    var fName = result.substring(index + 3, index1);
    index = index1;
    index1 = result.indexOf(";-;", index + 3);
    if (index1 == -1)
        return;
    var w = result.substring(index + 3, index1);
    index = index1;
    index1 = result.indexOf(";-;", index + 3);
    if (index1 == -1)
        return;
    var h = result.substring(index + 3, index1);
	img.style.display = "";
    img.src = fName;
    img.width = w;
    img.height = h;
}

function _id(idStr) {
    return document.getElementById(idStr);
}
