var thread1;
var dynamicBannerOuterContainer;
var dynamicBannerContent_background_while_loading;
var BannerRotationPlay = true;
var currentBannerIndex;
var nextBannerIndex;
var rotateWaitTime;
var numberBanners;
var FadeTweenTime;

rotateWaitTime = 6000;
numberBanners = 5;
FadeTweenTime = 1;


//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');
    dynamicBannerContent_background_while_loading = $('dynamicBannerContent_background_while_loading');
}

function rotateBannersInitial()
{

    //if javascript works up till this point, then use javascript to hide display of element "dynamicBannerContent_background_while_loading"
    if (dynamicBannerContent_background_while_loading)
    {
        dynamicBannerContent_background_while_loading.style.display="none";
    }

    rotateBanners();


}

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 = $('dynamicBannerContent'+currentBannerIndex); //can be zero if there is only one banner availalble
        var nextBannerDiv = $('dynamicBannerContent'+nextBannerIndex); //can be zero if there is only one banner availalble

        //MOD: do not include dynamic captions
        //var currentBannerCaptionDiv = $('dynamicBannerCaption'+currentBannerIndex);
        //var nextBannerCaptionDiv = $('dynamicBannerCaption'+nextBannerIndex);
        
        //MOD: do not include dynamic captions (start: do the fadeout and fadein)
        // /*if (currentBannerDiv && nextBannerDiv && nextBannerDiv.innerHTML && (nextBannerDiv.innerHTML != '') && (!nextBannerDiv.innerHTML.match(/^\s*$/)) && currentBannerCaptionDiv && nextBannerCaptionDiv)*/
        //start: do the fadeout and fadein
        if (currentBannerDiv && nextBannerDiv && nextBannerDiv.innerHTML && (nextBannerDiv.innerHTML != '') && (!nextBannerDiv.innerHTML.match(/^\s*$/)))
        {
            //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 })
                                            //MOD: do not include dynamic captions
                                            //, 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 })
                                            //MOD: do not include dynamic captions
                                            //, 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
                
                ////MOD: do not include dynamic captions
                ////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

                ////MOD: do not include dynamic captions
                ////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)
    {
        //MOD: do not include the pause button
        //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 = '';
        }
    }
    
    //MOD: do not include the pause button
    //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);

    //Don't need to activate because there is no pause button in this case.
    //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)
{
    //if javascript works up till this point, then use javascript to set display of element "dynamicBannerContent_background_while_loading"
    if (dynamicBannerContent_background_while_loading)
    {
        dynamicBannerContent_background_while_loading.style.display="block";
    }

    dynamicBannerOuterContainer.style.display = "block";

//                        for (var i=0;i<numberBanners;i++)
//                        {
//                            var tempBannerDiv = $('dynamicBannerContent'+i);
//                            if (tempBannerDiv) { InitialSetMouseOver(tempBannerDiv, BannerDivMouseOverHandler); InitialSetMouseOut(tempBannerDiv, BannerDivMouseOutHandler); }
//                        }
    InitialSetMouseOver($('dynamicBanner'), BannerDivMouseOverHandler); InitialSetMouseOut($('dynamicBanner'), BannerDivMouseOutHandler);

    //try to use jQuery's $j function instead of prototype's $ function
    if ($j && $j != null)
    {
        $j(document).ready(
            function() { rotateBannersInitial(); }
        );
    }
    //if no jQuery for $j, then just rely on plain old window.onload
    else
    {
        window.onload = rotateBannersInitial;
    }
}


