var adBanner =
{
    dissolveSpeed : 500,
    adBannerId : 'AdBig',
    imageArray : new Array(),
    nextImageIndex : 0,
    stopRotation : false,
    adBannerLinkId : 'adBannerLink',
    
    init : function(imageArray) 
    {
    
        parentObject        = this;
        if (imageArray)
            this.imageArray    = imageArray;
        if ($(this.adBannerId))
        {
            this.adBanner    = $(this.adBannerId);
            if (this.adBanner)
            {
                this.adBanner.style.backgroundImage = "url(images/progress_bar.gif)";
                this.adBanner.style.backgroundPosition  = "center";
                this.adBanner.style.backgroundColor = "#fff";
                this.adBannerList    = document.createElement('ul');
                
                for (i=0; i<this.imageArray.length;i++)
                {
                    newImageLink        = document.createElement('li');
                    newImageLinkA       = document.createElement('a');
                    newImageLinkA.id    = this.adBannerLinkId+i;
                    newImageLinkA.href    = '#';
                    newImageLinkA.innerHTML    = '&nbsp;';
                    newImageLink.appendChild(newImageLinkA);
                    newImageCache       = new Image();
                    newImageCache.src   = this.imageArray[i].src;
                    checkImageLoad      = false;
                    while(!checkImageLoad)
                    {
                        verifyCacheStatus   = true;
                        if (!newImageCache.complete)
                            checkImageLoad  = false;
                        if ((typeof newImageCache.naturalWidth != "undefined") && newImageCache.naturalWidth == 0)
                            checkImageLoad  = false;
                            
                        checkImageLoad  = verifyCacheStatus;
                    }
                    this.adBannerList.appendChild(newImageLink);
                }
                this.adBanner.appendChild(this.adBannerList);
                for (i=0; i<imageArray.length;i++)
                {
                    $(this.adBannerLinkId+i).addEvent('click', function(){
                        imageKey    = parseInt(this.id.replace(parentObject.adBannerLinkId, ""));
                        parentObject.rotateImages(imageKey);
                    });
                }
            }
            this.rotateImages();
        } else {
            alert("You must specify a proper Banner ID.");
        }
    },
    
    rotateImages : function (imageIndex) 
    {
        parentObject        = (!parentObject) ? this : parentObject;
        timedImageChange    = (imageIndex == null) ? true : false;
        if (!timedImageChange || !this.stopRotation)
        {
            if (imageArray.length > 0) 
            {
                if (imageIndex == null) 
                {
                    imageIndex                = this.nextImageIndex;
                    setTimeout(function(){
                        parentObject.rotateImages();
                    }, 6500);
                
                } else {
                    this.stopRotation    = true;
                }
                this.nextImageIndex                        = (imageIndex >= (imageArray.length-1)) ? 0 : imageIndex+1;
                this.bannerFx                            = new Fx.Style(this.adBannerId, 'opacity');
                this.bannerFx.start(1,0).chain(function(){
                    parentObject.adBanner.style.backgroundImage     = "url('"+parentObject.imageArray[imageIndex].src+"')";
                    parentObject.adBanner.style.backgroundColor     = "#000";
                    if ($('AdBigLink') && parentObject.imageArray[imageIndex].href)
                        $('AdBigLink').href = parentObject.imageArray[imageIndex].href;
                    parentObject.bannerFx.start(0,1);
                });
                this.highlightListItem(imageIndex);
            } else {
                alert("You must add images to the imageArray.");
            }
        }
    },
    
    highlightListItem : function(imageIndex)
    {
        listItems    = this.adBanner.getElementsByTagName("li");
        for (i=0;i<listItems.length;i++)
        {
            linkItems    = listItems[i].getElementsByTagName('a');
            if (linkItems[0].className!="ad-pause")
            {
                if (i==imageIndex)
                {
                    linkItems[0].setAttribute("class", "activeLink");
                    linkItems[0].setAttribute("className", "activeLink");
                } else {
                    linkItems[0].setAttribute("class", "");
                    linkItems[0].setAttribute("className", "");
                }
        
            }
        }
    }
}

