﻿//lets jQuery play nice with DNN
jQuery.noConflict();

/*
 * fadeBannerOut
 *
 * Fades the banner out and then calls switch text
 */  
function fadeBannerOut()
{
   //asyncronous call to switchText
   jQuery("#rotatingBanner").fadeOut('slow', switchText);  
}

/*
 * switchText
 *
 * Switches the text, calls the database to get the next text and then fades out the text
 */
function switchText()
{
   //switch banners (show the next one)
   document.getElementById('rotatingBanner').innerHTML = document.getElementById('nextRotatingBanner').value;

   var id = document.getElementById('moduleId').value;
   var index = document.getElementById('currentIndex').value;
   var defId = document.getElementById('moduleDefId').value;
   var webserviceURL = "/DesktopModules/RotatingBanner/RotatingBanner.ashx?mid=" + id +"&defId=" + defId + "&ci=" + index;
   
   //update the next banner (asynchronous call to updateNextBanner)
   jQuery.getJSON(webserviceURL, updateNextBanner);

   //fade the text back in and reset the timer (asynchronous call to setTimer)
   jQuery("#rotatingBanner").fadeIn('slow', setTimer);
}

/*
 * updateCounter
 *
 * updates the counter variable
 */
function updateCounter()
{
   var index = document.getElementById('currentIndex').value;
   var max = document.getElementById('upperBounds').value;
       
   //update the current index
   index = parseInt(index) + 1;
   
   if(index > max)
       index = 0;
   
   document.getElementById('currentIndex').value = index;
}

/*
 * updateNextBanner
 *
 * callback method that sets the next value of the next banner
 */
function updateNextBanner(result)
{
    document.getElementById('nextRotatingBanner').value = result.value;
    updateCounter();
}

/*
 * setTimer
 *
 * sets the timer in seconds and calls the fadeBannerOut
 */
function setTimer()
{
   var rate = document.getElementById('refreshRate').value;
   setTimeout('fadeBannerOut();', parseInt(rate * 1000));
}

/*
 * sets the timer on load
 */
jQuery(document).ready(function() 
{ 
    setTimer();
});