// JavaScript Document
//Rotating feature code.
var agt = navigator.userAgent.toLowerCase();
var is_ie = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
var filterString = "DXImageTransform.Microsoft.Fade";
var enableFilter = is_ie;

function isFilterDone(e)
{
    if (e == null)
    {
        return true;
    }
    if (enableFilter)
    {
        var filter = getFilter(e);
        if (filter.status == 3)
        {
            return false;
        }
    }
    return true;
}

function getFilter(e)
{
    if (!hasFilter(e))
    {
        e.style.filter = "progid:" + filterString + "(duration=.5,overlap=1.0)";
    }
    return e.filters.item(filterString);
}

function hasFilter(e)
{
    if ((e.filters == null) || (e.filters.length <= 0))
    {
        return false;
    }
    return (e.filters.item(filterString) != null);
}

function fadeOut(e) 
{
    if (e == null)
    {
        return;
    }

    if (enableFilter)
    {
        // Make sure the filter is not playing.
        var filter = getFilter(e);
        if (filter.status != 3)
        {
            filter.Apply();
            e.style.visibility = 'hidden';
            filter.Play();
        }
    }
    else
    {
        e.style.visibility = 'hidden';
    }
}

function fadeIn(e) 
{
    if (e == null)
    {
        return;
    }

    if (enableFilter)
    {
        // Make sure the filter is not playing.
        var filter = getFilter(e);
        if (filter.status != 3)
        {
            filter.Apply();
            e.style.visibility = 'visible';
            filter.Play();
        }
    }
    else
    {
        e.style.visibility = 'visible';
    }
}

function getContentFrame(idPrefix, index)
{
    var id = idPrefix + index;
    return document.getElementById(id);
}

function rotateFeature(idPrefix, index, bAutoRotate)
{
    var rotationInterval = eval(idPrefix + "_Interval");
    var rfItemCount = eval(idPrefix + "_ItemCount");
    var timerId = eval(idPrefix + "_TimerId");
    //----------------------------------------------------------
    //initialize global variables
    //----------------------------------------------------------
    //total number of best bet items
    var itemCount = isNaN(rfItemCount) ? 0 : rfItemCount;
    if (itemCount <= 0)
    {
        return;
    }

    //auto-rotation interval in seconds
    var intervalSeconds = isNaN(rotationInterval) ? 5 : rotationInterval / 1000.0;

    //----------------------------------------------------------
    //normalize the input variables
    //----------------------------------------------------------
    if (index == null) index = 0;
    if (bAutoRotate == null) bAutoRotate = false;
    if (!bAutoRotate) enableFilter = false;

    //----------------------------------------------------------
    //cancel pending rotation if we were called with bAutoRotate = false
    //----------------------------------------------------------
    if (!bAutoRotate)
    {
        //when user clicks a button, this is set to false indicating
        //we need the timer to stop. Because of this we need to cancel
        //any existing timer that has already been set to go off.
        if (typeof(timerId) != "undefined")
        {
            clearTimeout(timerId);
        }
    }  

    //----------------------------------------------------------
    //show the chosen content panel
    //----------------------------------------------------------
    var fadeOutCtrl = null;
    var fadeInCtrl = getContentFrame(idPrefix, index);
    for (var i = 0; i < itemCount; i++)
    {
        var contentFrame = getContentFrame(idPrefix, i);
        if (contentFrame.style.visibility != 'hidden')
        {
            //found the previous chosen one
            fadeOutCtrl = contentFrame;
            break;
        }
    }

    //if we're in the middle of an animation... don't try to mess it up!!
    if (!isFilterDone(fadeOutCtrl) || !isFilterDone(fadeInCtrl))
        return false;

    if (fadeInCtrl == fadeOutCtrl)
    {
        //do nothing... its already shown
    }
    else
    {
        fadeOut(fadeOutCtrl);
        fadeIn(fadeInCtrl);
    }

    //----------------------------------------------------------
    //highlight the chosen button
    //----------------------------------------------------------
    if (bAutoRotate && (intervalSeconds > 0))
    {
        var nextIndex = (index + 1) % itemCount;
        eval(idPrefix + "_TimerId = setTimeout(\"rotateFeature('" + idPrefix + "', "+ nextIndex + ", true)\", "+ intervalSeconds * 1000 + ");");
    }
    
    var targetButtonId = idPrefix + 'Button' + index;
    for (var i = 0; i < itemCount; i++)
    {
        var sId = idPrefix + 'Button' + i;
        var cButton = document.getElementById(sId);
        if (cButton != null)
        {
            if (cButton.id == targetButtonId)
            {
                cButton.className = 'bbButtonHL';
            }
            else
            {
                cButton.className = 'bbButton';
            }
        }
    }
}

