﻿// JScript File

function RotatingAd(divId ) {

    this.rotEventArray = new Array();
    this.rotEventIndex = 0;
    this.rotEventCount = 0;
    this.onAuto = true;
    this.divId = divId;
    this.speed = 5000;
    this.id = RotatingAd.cnt;
    RotatingAd.objects[RotatingAd.cnt++] = this;
    createBlock(this.divId, this)
    
}

RotatingAd.cnt = 0;
RotatingAd.objects = new Array();
    RotatingAd.prototype.updateEvent = function(index) {

        var imgNode = document.getElementById(this.divId + "_rotEventImage");
        imgNode.src = this.rotEventArray[index].src;
        imgNode.title = this.rotEventArray[index].title;
        imgNode.alt = this.rotEventArray[index].title;
        document.getElementById(this.divId + "_rotEventLink").href = this.rotEventArray[index].eventLink;
        this.setCaption(this.rotEventArray[index].eventCaption, this.rotEventArray[index].eventDate);
    }

    //*************************
    // Update message.
    //*************************
   RotatingAd.prototype.setCaption = function(newCaption, newDate) {
        document.getElementById(this.divId + "_rotEventCaptionTitle").innerHTML = newCaption;
        document.getElementById(this.divId + "_rotEventCaptionDate").innerHTML = newDate;
    }

    //********************************
    // Store advertising info 
    // src = image url
    // title = used for alt text and title text
    // caption = text message displayed with image
    // date = event date / time
    //********************************
 RotatingAd.prototype.addRotEvent = function(src, title, caption, date, href) {

        this.rotEventArray[this.rotEventCount] = new Image();
        this.rotEventArray[this.rotEventCount].src = src;
        this.rotEventArray[this.rotEventCount].title = title;
        this.rotEventArray[this.rotEventCount].eventCaption = caption;
        this.rotEventArray[this.rotEventCount].eventDate = date;
        this.rotEventArray[this.rotEventCount].eventLink = href;
        this.rotEventIndex = this.rotEventCount;
        this.rotEventCount++;


    }
    RotatingAd.prototype.autoAdvance = function() {
        if (this.onAuto) {
            this.rotEventIndex = (this.rotEventIndex + 1) % this.rotEventCount;
            this.updateEvent(this.rotEventIndex);
        }
    }


    //*************************
    // Switch to next ad.
    //*************************
   RotatingAd.prototype.showNextEvent = function() {
        this.rotEventIndex = (this.rotEventIndex + 1) % this.rotEventCount;
        this.updateEvent(this.rotEventIndex);
    }

   RotatingAd.prototype.showPrevEvent = function() {
        this.rotEventIndex = (this.rotEventIndex - 1 + this.rotEventCount) % this.rotEventCount;
        this.updateEvent(this.rotEventIndex);
    }


   RotatingAd.prototype.playEvents = function() {
        this.onAuto = true;
    }

   RotatingAd.prototype.pauseEvents = function() {
        currentState = document.getElementById(this.divId + "_rotEventButton").value;
        if (currentState == "Pause") {
            this.onAuto = false;
            document.getElementById(this.divId + "_rotEventButton").value = "Play  ";
        } else {
            this.onAuto = true;
            document.getElementById(this.divId + "_rotEventButton").value = "Pause";
        }
    }

    RotatingAd.prototype.animate = function() {
        this.autoAdvance();
        setTimeout('RotatingAd.objects[' + this.id + '].animate()', this.speed);
    }

    RotatingAd.prototype.setSpeed = function(msec) {
        this.speed = msec;
    }

    function createBlock(container, adObj) {

        var el = document.getElementById(container);
        var rotEventBox = document.createElement('div');
        rotEventBox.setAttribute('id', container + '_rotEventBox');
        rotEventBox.className='rotEventBox';

        var rotEventImageBox = document.createElement('div', container + '_rotEventImageBox');
        rotEventImageBox.setAttribute('id', container + '_rotEventImageBox');
        rotEventImageBox.className = 'rotEventImageBox';

        var imgLink = document.createElement('a');
        imgLink.setAttribute('id', container + '_rotEventLink');
        imgLink.setAttribute('href', "/files/defaultad.jpg");

        var img = document.createElement('img');
        img.setAttribute('id', container + '_rotEventImage');
        img.setAttribute('src', '/files/defaultad.jpg');

        imgLink.appendChild(img);
        rotEventImageBox.appendChild(imgLink);

        rotEventBox.appendChild(rotEventImageBox);

        var rotEventCaptionBox = document.createElement('div');
        rotEventCaptionBox.setAttribute('id', container + '_rotEventCaptionBox');
        rotEventCaptionBox.className = 'rotEventCaptionBox';

        var title = document.createElement('h2');
        title.setAttribute('id', container + '_rotEventCaptionTitle');
        title.appendChild(document.createTextNode("Main Title"));

        var title2 = document.createElement('p');
        title2.setAttribute('id', container + '_rotEventCaptionDate');
        title2.appendChild(document.createTextNode('Secondary Title'));
        rotEventCaptionBox.appendChild(title);
        rotEventCaptionBox.appendChild(title2);

        rotEventBox.appendChild(rotEventCaptionBox);

        var form = document.createElement('form');
        var prevButton = document.createElement('input');
        prevButton.setAttribute('type', 'button');
        prevButton.setAttribute('value', 'Previous');
        prevButton.setAttribute('title', 'Previous');
        prevButton.onclick = function() { adObj.showPrevEvent(); };

        var nextButton = document.createElement('input');
        nextButton.setAttribute('type', 'button');
        nextButton.setAttribute('value', 'Next');
        nextButton.setAttribute('title', 'Next');
        nextButton.onclick = function() { adObj.showNextEvent(); };

        var playButton = document.createElement('input');
        playButton.setAttribute('type', 'button');
        playButton.setAttribute('value', 'Pause');
        playButton.setAttribute('title', 'Pause/Play');
        playButton.setAttribute('id', container + '_rotEventButton');
        playButton.onclick = function() { adObj.pauseEvents(); };

        form.appendChild(prevButton);
        form.appendChild(nextButton);
        form.appendChild(playButton);

        rotEventBox.appendChild(form);

        el.appendChild(rotEventBox);


    }

