/************************************************************************
Author: Angus Lee (2003/01/02)
        WebServe Technology Group Limited

This object creates an advertisement banner. The banner swap image and
    target URL at given interval.

Constructor: DynamicAdBanner
    Desc: Create the DynamicAdBanner object
    Args:
        imgWidth:  Image Width
        imgHeight: Image Height
        imgBorder: Image Border
        interval:  Swap image interval in second

Methods: addItem
    Desc: Add an item into the banner object
    Args:
      image:  Image URL
      url:    Link URL

Methods: run
    Desc: Run the banner
    Args: null

Sample Usage:

    var myAdBanner = new DynamicAdBanner(150, 100, 0, 0.5);
    myAdBanner.addItem("http://www.google.com/intl/zh-TW/nav_first.gif", "http://www.webserve.com.hk/wstg/html/about.htm");
    myAdBanner.addItem("http://www.google.com/intl/zh-TW/nav_current.gif", "http://www.webserve.com.hk/wstg/html/solut.htm");
    myAdBanner.addItem("http://www.google.com/intl/zh-TW/nav_page.gif", "http://www.webserve.com.hk/wstg/html/part.htm");
    myAdBanner.addItem("http://www.google.com/intl/zh-TW/nav_next.gif", "http://www.webserve.com.hk/wstg/html/contact.htm");
    myAdBanner.run();

************************************************************************/

    var dynamicAdBannerObjHolder = new Array;

    // Constructor
    function DynamicAdBanner(imgWidth, imgHeight, imgBorder, interval) {

        // Declare properties
        this.currentIndex = -1;
        this.myImages = new Array;
        this.myImageHeight = imgHeight;
        this.myImageWidth = imgWidth;
        this.myImageBorder = imgBorder;
        this.myInterval = interval;
        this.myUrl = new Array;
        this.id = dynamicAdBannerObjHolder.length;
        dynamicAdBannerObjHolder[dynamicAdBannerObjHolder.length] = this;
        this.imgObjName = "myAdImage" + this.id;
        this.imgObj = null;
        this.isWritten = false;

        // Declare methods
        this.addItem = addAdBanner;
        this.run = writeAdBanner;

        return this;
	}

    // Object Method
    function addAdBanner(image, url) {

        var currentOpIndex = this.myImages.length;

        this.myImages[currentOpIndex] = new Image();
        this.myImages[currentOpIndex].src = image;
        this.myUrl[currentOpIndex] = url;
    }

    // Object Method
    function writeAdBanner() {
        if (!this.isWritten) {
            this.isWritten = true;
            document.write('<A HREF="#" onclick="adBannerUrl(' + this.id + ')"><IMG NAME="' + this.imgObjName + '" WIDTH="' + this.myImageWidth + '" HEIGHT="' + this.myImageHeight + '" BORDER="' + this.myImageBorder + '" SRC="' + this.myImages[0].src + '" ALT=""></A>');
            runAdBanner(this.id);
        }
    }

    // Global Helper Method
    function adBannerUrl(objId) {
        var selfObj = dynamicAdBannerObjHolder[objId];
        if (selfObj.myUrl[selfObj.currentIndex] != null)
            window.open(selfObj.myUrl[selfObj.currentIndex], "_blank");
    }

    // Global Helper Method
    function runAdBanner(objId) {

        var selfObj = dynamicAdBannerObjHolder[objId];

        if (selfObj.imgObj == null) {
            for (var i=0; i<document.images.length; i++) {
                if (document.images[i].name == selfObj.imgObjName)
                    selfObj.imgObj = document.images[i];
            }
        }

        if (selfObj.currentIndex > (selfObj.myImages.length-2))
            selfObj.currentIndex = 0;
        else
            selfObj.currentIndex = selfObj.currentIndex + 1;

        selfObj.imgObj.src = selfObj.myImages[selfObj.currentIndex].src;

        if (selfObj.myImages.length > 1) {
            var myCmd = "runAdBanner(" + objId + ")";
            window.setTimeout(myCmd, (selfObj.myInterval * 1000));
        }
    }

