//create the namespace
var rad = function()
{
    var currAdIndex = 0;
    var intervalTimeout = 1500;
    var intervalID;
    var adElements;
    var contentDivName = "adplaceholder";
    var contentListName = "adlist";
    
    return {
        //delegates include (implement in a script tag of RotatingAdControl.ascx or 
        //    anywhere on the page where the ad control lives):
        //
        //beforeAdSwap(currentIndex) - runs before any elements of a list item are swapped, 
        //    passes in the index of the ad element to be displayed
        //
        //afterAdSwap(currentIndex) - runs after all elements of a list item have been swapped, 
        //    passes in the index of the ad element to be displayed
        //
        //beforeAdElementSwap(newAdElement) - runs before each root element of a list item is swapped, 
        //    passes in the new ad element that will be swapped in
        //
        //afterAdElementSwap(newAdElement) - runs after each root element of a list item is swapped, 
        //    passes in the new ad element that was be swapped in
        
        rotateAd : function()
        {
            var placeholderDiv = document.getElementById(contentDivName);

            //debugger;
            //remove all children in the placeholder div
            while(placeholderDiv.childNodes.length > 0)
            {
                //move the element back to the list
                var currChild = placeholderDiv.childNodes[0];
                var prevIndex = (currAdIndex - 1 >= 0) ? (currAdIndex - 1 % adElements.length) : (adElements.length - 1);
                adElements[prevIndex].appendChild(currChild);
                //remove this element from the placeholder div
                //placeholderDiv.removeChild(placeholderDiv.childNodes[0]);
            }
            
            if(typeof beforeAdSwap == 'function')
            {
                beforeAdSwap(currAdIndex);
            }
            
            //add the new content to the placeholder div
            while(adElements[currAdIndex].childNodes.length > 0)
            {
                var adElement = adElements[currAdIndex].childNodes[0];//adElements[currAdIndex].childNodes[i].cloneNode(true);
                //debugger;
                if(typeof beforeAdElementSwap == 'function')
                {
                    beforeAdElementSwap(adElement);
                }
                placeholderDiv.appendChild(adElement);
                if(typeof afterAdElementSwap == 'function')
                {
                    afterAdElementSwap(adElement);
                }
            }
            if(typeof afterAdSwap == 'function')
            {
                afterAdSwap(currAdIndex);
            }
            currAdIndex++;
            currAdIndex = currAdIndex % adElements.length;
        },
        
        setIntervalTimeout : function(ms)
        {
            intervalTimeout = ms;
        },
        
        setContentDivName : function(divName)
        {
            contentDivName = divName;
        },
        
        setContentListName : function(listName)
        {
            contentListName = listName;
        },
        
        init : function()
        {
            var adList = document.getElementById(contentListName);
            adElements = new Array();
            
            //gather all the content for each ad and add them
            //to the adElements array
            var liCollection = adList.getElementsByTagName("LI");
            if(liCollection)
            {
                for(i = 0; i < liCollection.length; i++)
                {
                    adElements.push(liCollection[i]);
                }
            }
        },
        
        start : function()
        {
            rad.init();
            //var currAdIndex = 0;
            //display the first one immediately, or else nothing will be displayed
            //until the value specified by intervalTimeout elapses
            rad.rotateAd();
            //start the rotation
            intervalID = setInterval("rad.rotateAd();", intervalTimeout);
        },
        
        stop : function()
        {
            clearInterval(intervalID);
        }
    };
}();