﻿// JScript File
// Copyright ABC123 IT Inc. All Rights Reserved.

var currentAdIndex = 0;
var AdIDs = new Array();
var AdBtnIDs = new Array();
var adTimer = null;

function $(id) {
    return document.getElementById(id);
}


function switchAds() {
//    if (AdIDs.length > currentAdIndex) {
//        var currentAd = $(AdIDs[currentAdIndex]);
//        if (currentAd != null) {
//            currentAd.style.display = 'none';
//        }
    //    }
    hideAd();
    deselectBtn();
    currentAdIndex++;
    if (currentAdIndex >= AdIDs.length)
        currentAdIndex = 0;
//    if (AdIDs.length > currentAdIndex){
//        var currentAd = $(AdIDs[currentAdIndex]);
//        if (currentAd != null) {
//            currentAd.style.display = 'block';
//        }
    //    }
    showAd();
    selectBtn();
    startTimer(11000);
}

function manualSwitchAd(adIndex) {
    pauseTimer();
    hideAd();
    deselectBtn();
    currentAdIndex = adIndex;
    if (currentAdIndex >= AdIDs.length)
        currentAdIndex = 0;
    showAd();
    selectBtn();
    startTimer(11000);
}

function showAd() {
    if (AdIDs.length > currentAdIndex) {
        var currentAd = $(AdIDs[currentAdIndex]);
        if (currentAd != null) {
            currentAd.style.display = 'block';
        }
    }
}

function selectBtn() {
    if (AdBtnIDs.length > currentAdIndex) {
        var currentAd = $(AdBtnIDs[currentAdIndex]);
        if (currentAd != null) {
            currentAd.style.backgroundColor = 'white';
        }
    }
}

function hideAd() {
    if (AdIDs.length > currentAdIndex) {
        var currentAd = $(AdIDs[currentAdIndex]);
        if (currentAd != null) {
            currentAd.style.display = 'none';
        }
    }
}

function deselectBtn(index) {
    if (AdBtnIDs.length > currentAdIndex) {
        var currentAd = $(AdBtnIDs[currentAdIndex]);
        if (currentAd != null) {
            currentAd.style.backgroundColor = 'transparent';
        }
    }
}

function pauseTimer() {
//    if (adTimer != null)
       clearTimeout(adTimer);
}

function startTimer(delayTime) {
    adTimer = setTimeout(switchAds, delayTime);
}