﻿var rotateInterval = 5000; // delay in rotating to the next ad
var pauseSelection = 10000; // delay in rotating to the next ad upon selection of an ad
var transitionInterval = 10; // rotation speed (do not set to lower than 10)
var nextAd = 0; // starting ad

var rotateAd = true; // environment var - do not adjust
var timerArrayIds = new Array();
var TimerIDCount = 0;

function startAds() {
    jQuery("#adFirst").load("/ads/ad1.html");
    jQuery("#adSecond").load("/ads/ad2.html");
    jQuery("#adThird").load("/ads/ad3.html");
    jQuery("#adFourth").load("/ads/ad4.html");

    adRotate();
}

function boxSelected(ad) {

    rotateAd = false;
    adReset();

    for (i = 0; i < TimerIDCount; i++) {
        window.clearTimeout(timerArrayIds[i]);
    }

    timerArrayIds = new Array();
    TimeIDCount = 0;

    switch (ad) {
        case 1:
            document.getElementById('adFirstBox').className = "adBoxSelected";
            adMove(parseInt(document.getElementById('adContainer').style.left) * -1, 0);
            nextAd = 2;
            break;
        case 2:
            document.getElementById('adSecondBox').className = "adBoxSelected";
            adMove(parseInt(document.getElementById('adContainer').style.left) * -1, 645);
            nextAd = 3;
            break;
        case 3:
            document.getElementById('adThirdBox').className = "adBoxSelected";
            adMove(parseInt(document.getElementById('adContainer').style.left) * -1, 1290);
            nextAd = 4;
            break;
        case 4:
            document.getElementById('adFourthBox').className = "adBoxSelected";
            adMove(parseInt(document.getElementById('adContainer').style.left) * -1, 1935);
            nextAd = 1;
            break;
    }

    timerArrayIds[TimerIDCount++] = window.setTimeout(function () {
        rotateAd = true;
        adRotate();
    }, pauseSelection);
}

function adReset() {
    document.getElementById('adFirstBox').className = "adBox";
    document.getElementById('adSecondBox').className = "adBox";
    document.getElementById('adThirdBox').className = "adBox";
    document.getElementById('adFourthBox').className = "adBox";
}

function adRotate() {

    if (rotateAd) {
        adReset();

        switch (nextAd) {
            case 0:
                document.getElementById('adFirstBox').className = "adBoxSelected";
                nextAd = 2;
                adMove(0, 0);
                break;
            case 1:
                document.getElementById('adFirstBox').className = "adBoxSelected";
                nextAd = 2;
                adMove(1935, 0);
                break;
            case 2:
                document.getElementById('adSecondBox').className = "adBoxSelected";
                adMove(0, 645);
                nextAd = 3;
                break;
            case 3:
                document.getElementById('adThirdBox').className = "adBoxSelected";
                adMove(645, 1290);
                nextAd = 4;
                break;
            case 4:
                document.getElementById('adFourthBox').className = "adBoxSelected";
                adMove(1290, 1935);
                nextAd = 1;
                break;
        }

        timerArrayIds[TimerIDCount++] = window.setTimeout("adRotate()", rotateInterval);
    }
}

function adMove(start, target) {

    if (target == 0 && start >= 0) {
        document.getElementById('adContainer').style.left = (start * -1) + "px";
    
        if (start != 0) {
            timerArrayIds[TimerIDCount++] = window.setTimeout(function () {
                adMove(start - 129, target);
            }, transitionInterval);
        }
    } else if (target > 0 && target >= start) {
        document.getElementById('adContainer').style.left = (start * -1) + "px";

        if (target != start) {
            timerArrayIds[TimerIDCount++] = window.setTimeout(function () {
                adMove(start + 129, target);
            }, transitionInterval);
        }
    } else if (start > target) {
        document.getElementById('adContainer').style.left = (start * -1) + "px";

        timerArrayIds[TimerIDCount++] = window.setTimeout(function () {
            adMove(start - 129, target);
        }, transitionInterval);
    }
}
