﻿//numberBanners = 1;
//only set banner indexes if numberBanners is more than one
currentBannerIndex = ((numberBanners && numberBanners>1) ? (numberBanners-1) : 0);
nextBannerIndex = ((numberBanners && numberBanners>1) ? ((currentBannerIndex+1) % numberBanners) : 0);

if ((typeof $) == 'function')
{
    dynamicBannerOuterContainer = $('dynamicBannerOuterContainer');
}

function rotateBanners()
{
    if (((typeof $) != 'function') || (!BannerRotationPlay))
    {
        return;
    }
    
    //handle the rotation of banners if the js variables are set correctly, and there is banner content to rotate.
    if (rotateWaitTime && (rotateWaitTime > 0) && numberBanners && (numberBanners > 0) && (!isNaN(currentBannerIndex)) && (!isNaN(nextBannerIndex)) && dynamicBannerOuterContainer)
    {
        var currentBannerDiv = $(dynamicBannerContentIdBase+currentBannerIndex); //can be zero if there is only one banner availalble
        var nextBannerDiv = $(dynamicBannerContentIdBase+nextBannerIndex); //can be zero if there is only one banner availalble

        var currentBannerCaptionDiv = $(dynamicBannerCaptionIdBase+currentBannerIndex);
        var nextBannerCaptionDiv = $(dynamicBannerCaptionIdBase+nextBannerIndex);
        
        //start: do the fadeout and fadein
        if (currentBannerDiv && nextBannerDiv && nextBannerDiv.innerHTML && (nextBannerDiv.innerHTML != '') && (!nextBannerDiv.innerHTML.match(/^\s*$/)) && currentBannerCaptionDiv && nextBannerCaptionDiv)
        {
            //before fade effects, disable all &lt;a&gt; tags by bring this to the back
            currentBannerDiv.style.zIndex = "1";

            try
            {
                if (numberBanners>=1)
                {
                    ButtonActivate(nextBannerIndex);
                }

                 //animate opacity to rotate banner content and caption content
                //note: do not provide a from: argument for any of the Effect.Opacity calls (doing so would mean upon page load, the [numberBanners-1]th banner would appear and instantaneously start to fade out - ugly)
                
                //go to 1.0 opacity IE
                if (Prototype.Browser.IE)
                {
                    //uses Effects from scriptaculous
                    new Effect.Parallel([ 
                                            new Effect.Opacity(currentBannerDiv, { sync: true, to: 0.0 }), new Effect.Opacity(nextBannerDiv, { sync: true, to: 1.0 })
                                            , new Effect.Opacity(currentBannerCaptionDiv, { sync: true, to: 0.0 }), new Effect.Opacity(nextBannerCaptionDiv, { sync: true, to: 1.0 })
                                         ]
                                          , { duration: FadeTweenTime, delay: 0.0 }
                                        );
                }
                //go to 0.99999 opacity for non-IE
                else
                {
                    //uses Effects from scriptaculous
                    new Effect.Parallel([ 
                                            new Effect.Opacity(currentBannerDiv, { sync: true, to: 0.0 }), new Effect.Opacity(nextBannerDiv, { sync: true, to: 0.99999 })
                                            , new Effect.Opacity(currentBannerCaptionDiv, { sync: true, to: 0.0 }), new Effect.Opacity(nextBannerCaptionDiv, { sync: true, to: 0.99999 })
                                         ]
                                          , { duration: FadeTweenTime, delay: 0.0 }
                                        );
                }

            } catch (err)
            {
                //Banner - hide old Banner content without animation
                currentBannerDiv.style.opacity = "0";
                currentBannerDiv.style.filter = ""; //IE: opacity over a transparent PNG background doesn't look good in IE
                currentBannerDiv.style.display = "none"; //IE: opacity over a transparent PNG background doesn't look good in IE
                //Banner - show new Banner content without animation
                nextBannerDiv.style.opacity = "1";
                nextBannerDiv.style.filter = ""; //IE: opacity over a transparent PNG background doesn't look good in IE
                nextBannerDiv.style.display = "block"; //IE: opacity over a transparent PNG background doesn't look good in IE
                
                //Caption - hide old caption content without animation
                currentBannerCaptionDiv.style.opacity = "0";
                currentBannerCaptionDiv.style.filter = ""; //IE: opacity over a transparent PNG background doesn't look good in IE
                currentBannerCaptionDiv.style.display = "none"; //IE: opacity over a transparent PNG background doesn't look good in IE
                
                //Caption - show new caption content without animation
                nextBannerCaptionDiv.style.opacity = "1";
                nextBannerCaptionDiv.style.filter = ""; //IE: opacity over a transparent PNG background doesn't look good in IE
                nextBannerCaptionDiv.style.display = "block"; //IE: opacity over a transparent PNG background doesn't look good in IE

            }


            //after / during fade effects, enable all &lt;a&gt; tags by bring this to the front
            nextBannerDiv.style.zIndex = "2";

        }//end: do the fadeout and fadein
        
        //if there is more than one banner, loop through them, and set SetButtonPanelVisibility()
        if (numberBanners && (numberBanners > 1))
        {
            //modulus increment currentBannerIndex and nextBannerIndex
            nextBannerIndex = ((nextBannerIndex+1) % numberBanners);
            currentBannerIndex = ((nextBannerIndex-1)<0) ? (numberBanners-1) : (nextBannerIndex-1)
            
            SetButtonPanelVisibility();
            
            thread1 = setTimeout("rotateBanners();", rotateWaitTime);
        }
    }
}

function goToBannerNumber(bannNumber)
{
    if (numberBanners && (numberBanners > 1))
    {
        stopBannerRotation();

        nextBannerIndex = bannNumber;

        playBannerRotation();
    }
}

function SetButtonPanelVisibility()
{
    if ((typeof $) != 'function' || (!numberBanners) || (numberBanners < 2))
    {
        return;
    }
    
    for (var i=0;i<numberBanners;i++)
    {
        var tempButton = $('button'+i);
        if (tempButton)
        {
            tempButton.style.display = 'block';
        }
    }
    
    if (numberBanners && numberBanners > 1)
    {
        var tempPauseButton = $('buttonpause');
        if (tempPauseButton)
        {
            tempPauseButton.style.display = 'block';
        }

        //finally display dynamicBannerPagination if it exists
        var dynamicBannerPagination = $('dynamicBannerPagination');
        if (dynamicBannerPagination)
        {
            dynamicBannerPagination.style.display = "block";
        }

    }

}

function ButtonDeactivateAll()
{
    if ((typeof $) != 'function')
    {
        return;
    }
    
    for (var i=0;i<numberBanners;i++)
    {
        var tempButton = $('button'+i);
        if (tempButton)
        {
            tempButton.className = '';
        }
    }
    
    var tempPauseButton = $('buttonpause');
    if (tempPauseButton)
    {
        tempPauseButton.className = '';
    }
}

function ButtonActivate(buttonIdSuffix)
{
    if ((typeof $) != 'function')
    {
        return;
    }
    
    ButtonDeactivateAll();

    var buttonToActivate = $('button'+buttonIdSuffix);
    if (buttonToActivate)
    {
        buttonToActivate.className = 'active';
    }
    
}

function toggleBannerRotationPlay()
{
    if (BannerRotationPlay == null || (!BannerRotationPlay))
    {
        playBannerRotation();
    }
    else
    {
        stopBannerRotation();
    }
    
}

function playBannerRotation()
{
    BannerRotationPlay = true;
    //force a call to rotateBanners() to start play now
    rotateBanners();
}

function stopBannerRotation()
{
    BannerRotationPlay = false;
    clearTimeout(thread1);

    ButtonActivate('pause');
}

function BannerDivMouseOverHandler(e)
{
    if(!e) var e = window.event;
    if(!e) return;
    
    e.cancelBubble = true;
    if (e.stopPropagation) e.stopPropagation();
    
    if (e.preventDefault) e.preventDefault(); 
    else e.returnResult = false;
    
    stopBannerRotation();
}

function BannerDivMouseOutHandler(e)
{
    if(!e) var e = window.event;
    if(!e) return;
    
    e.cancelBubble = true;
    if (e.stopPropagation) e.stopPropagation();
    
    if (e.preventDefault) e.preventDefault(); 
    else e.returnResult = false;

    var dynamicBanner = $('dynamicBanner');

    var posX = e.clientX;
    var posY = e.clientY;
    var bannerLeft = findLeftEdge(dynamicBanner);
    var bannerRight = bannerLeft; //default to bannerLeft
    var bannerTop = findTopEdge(dynamicBanner);
    var bannerBottom = bannerTop; //default to bannerTop
    var dynamicBannerWidth = (dynamicBanner.style.width) ? dynamicBanner.style.width.replace('px','') : 0;
    var dynamicBannerHeight = (dynamicBanner.style.height) ? dynamicBanner.style.height.replace('px','') : 0;

    if (!isNaN(dynamicBannerWidth))
    {
        bannerRight = bannerLeft + parseInt(dynamicBannerWidth);
    }
    
    if (!isNaN(dynamicBannerHeight))
    {
        bannerBottom = bannerTop + parseInt(dynamicBannerHeight);
    }
    
    //alert('bannerLeft=['+bannerLeft+'], bannerRight=['+bannerRight+'], bannerTop=['+bannerTop+'], bannerBottom=['+bannerBottom+']');
    
    //dynamicBanner needs an inline style for width and height for this to work properly
    if (dynamicBanner && posX && posY && (bannerRight > 0) && (bannerBottom > 0) && OutsideOfBox(posX, posY, bannerLeft, bannerRight, bannerTop, bannerBottom))
    {
        thread1 = setTimeout("playBannerRotation();", rotateWaitTime);
        ButtonActivate(currentBannerIndex);
    }
}

function findLeftEdge(thisElement)
{
    if ((!thisElement) || (thisElement == null))
        return 0;
        
    if ((!thisElement.offsetParent) || (thisElement.offsetParent == null)) //this is the document object
    {
        if (Prototype.Browser.IE)
        {
            return (document.body.scrollLeft) ? (-(document.body.scrollLeft)) : 0;
        }
        return (window.pageXOffset) ? (-(window.pageXOffset)) : 0;
    }
    else
    {
        return findLeftEdge(thisElement.offsetParent) + ((thisElement.offsetLeft) ? thisElement.offsetLeft : 0);
    }
    
}

function findTopEdge(thisElement)
{
    if ((!thisElement) || (thisElement == null))
        return 0;
        
    if ((!thisElement.offsetParent) || (thisElement.offsetParent == null)) //this is the document object
    {
        if (Prototype.Browser.IE)
        {
            return (document.body.scrollTop) ? (-(document.body.scrollTop)) : 0;
        }
        return (window.pageYOffset) ? (-(window.pageYOffset)) : 0;
    }
    else
    {
        return findTopEdge(thisElement.offsetParent) + ((thisElement.offsetTop) ? thisElement.offsetTop : 0);
    }
    
}

function OutsideOfBox(posX, posY, bannerLeft, bannerRight, bannerTop, bannerBottom)
{
    var dynamicBannerFooter = $('dynamicBannerFooter');
    var footerHeight = (dynamicBannerFooter.style.width) ? dynamicBannerFooter.style.width.replace('px','') : 0;

    return ( (posX <= bannerLeft) || (posX >= bannerRight) || (posY <= bannerTop) || (posY >= (bannerBottom-footerHeight)) );
}

function RemoveDescendentEventListeners(eventName,functionPtr,isCapture, rootElement)
{
    for(var i=0; ((rootElement.childNodes) && i<rootElement.childNodes.length); i++)
    {
        if (rootElement.childNodes[i].removeEventListener)
        {
            rootElement.childNodes[i].removeEventListener(eventName,functionPtr,isCapture);
        }
        else if (rootElement.childNodes[i].detachEvent)
        {
            //rootElement.detachEvent("on"+"eventName,functionPtr);
        }
        
        RemoveDescendentEventListeners(eventName,functionPtr,isCapture, rootElement.childNodes[i]);
    }
}

function InitialSetMouseOver(rootElement, functionPtr)
{
    var isCapture = true; //true to use the capture phase to fire event.
    if (rootElement.addEventListener)
    {
        rootElement.addEventListener("mouseover",functionPtr,isCapture);
    }
    else if (rootElement.attachEvent)
    {
        rootElement.attachEvent("on"+"mouseover",functionPtr);
    }
    
    RemoveDescendentEventListeners("mouseover",functionPtr,isCapture, rootElement);

}

function InitialSetMouseOut(rootElement, functionPtr)
{
    var isCapture = true; //true to use the capture phase to fire event.
    if (rootElement.addEventListener)
    {
        rootElement.addEventListener("mouseout",functionPtr,isCapture);
    }
    else if (rootElement.attachEvent)
    {
        rootElement.attachEvent("on"+"mouseout",functionPtr);
    }
    
    RemoveDescendentEventListeners("mouseout",functionPtr,isCapture, rootElement);

}

//show the banners outer container and start the rotation of banners if the js variables are set correctly, and there is banner content to rotate.
if (rotateWaitTime && (rotateWaitTime > 0) && numberBanners && (numberBanners > 0) && (!isNaN(currentBannerIndex)) && (!isNaN(nextBannerIndex)) && dynamicBannerOuterContainer)
{
    dynamicBannerOuterContainer.style.display = "block";
    
//                        for (var i=0;i<numberBanners;i++)
//                        {
//                            var tempBannerDiv = $(dynamicBannerContentIdBase+i);
//                            if (tempBannerDiv) { InitialSetMouseOver(tempBannerDiv, BannerDivMouseOverHandler); InitialSetMouseOut(tempBannerDiv, BannerDivMouseOutHandler); }
//                        }
    InitialSetMouseOver($('dynamicBanner'), BannerDivMouseOverHandler); InitialSetMouseOut($('dynamicBanner'), BannerDivMouseOutHandler);

    window.onload = rotateBanners;
}


