var agt = navigator.userAgent.toLowerCase();
var is_ie = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));

function hover(e)
{
    if ((e.className != null))
    {
        e.classNameSaved = e.className;
        e.className = "snew";
    }
}

function hoverout(e)
{
    if (e.classNameSaved != null)
    {
        e.className = e.classNameSaved;
    }
}

function gotobb(e, url)
{
    window.location = url;
}

var timerId;

function initContentControls()
{
    //number of frames we have
    var itemCount = isNaN(bestBetItemCount) ? 0 : bestBetItemCount;
    if (itemCount <= 0)
    {
        return;
    }

    var anchorCtrl = getBBContentFrame(0);
    anchorCtrl.style.backgroundColor = "white";
    if (anchorCtrl != null)
    {
        var maxHeight = anchorCtrl.clientHeight;
        for (var i = 1; i < itemCount; i++)
        {
            var targetCtrl = getBBContentFrame(i);
            targetCtrl.style.backgroundColor = "white";
            if (targetCtrl != null)
            {
                //1st: force the targetCtrl's width to be same as anchorCtrl's width
                targetCtrl.style.width = anchorCtrl.clientWidth;

// TODO: Figure out how to find the max height without showing the other controls for a split second.
				/*
				//target control must not be display = none when calculating it's height.
				targetCtrl.style.display = 'block';
                //2nd: after forcing the width of the target Ctrl, get the height
                maxHeight = Math.max(maxHeight, targetCtrl.clientHeight);
				targetCtrl.style.display = 'none';
				*/
            }
        }
        //NOTE: sometimes the contents of the non-anchor frames are
        //bigger than the anchor frame. Because of this we need to
        //make sure the anchor is at least the maximum height of all frames.
        anchorCtrl.style.height = maxHeight;
    }

    //browser bug. shows downloading status continuously... need to clear status for this
    window.status = " ";
}

var filterString = "DXImageTransform.Microsoft.Fade";
var enableFilter = is_ie;

function isBBFilterDone(e)
{
    if (e == null)
    {
        return true;
    }
    if (enableFilter)
    {
        var filter = getBBFilter(e);
        if (filter != null && filter.status == 2)
        {
            return false;
        }
    }
    return true;
}

function getBBFilter(e)
{
	try
	{
		if (!hasBBFilter(e))
		{
			e.style.filter = "progid:" + filterString + "(duration=.5,overlap=1.0)";
		}
	    return e.filters.item(filterString);
	}
	catch (error)
	{
		return null;
	}
}

function hasBBFilter(e)
{
	if ((e.filters == null) || (e.filters.length <= 0))
	{
		return false;
	}
	return (e.filters.item(filterString) != null);

}

function fadeOutBB(e) 
{
    if (e == null)
    {
        return;
    }

    if (enableFilter)
    {
        // Make sure the filter is not playing.
        var filter = getBBFilter(e);
		if (filter != null)
		{
			if (filter.status != 2)
			{
				filter.Apply();
				e.style.display = 'none';
				filter.Play();
			}
		}
		else
		{
			e.style.display = 'none';
		}
    }
    else
    {
        e.style.display = 'none';
    }
}

function fadeInBB(e) 
{
    if (e == null)
    {
        return;
    }

    if (enableFilter)
    {
        // Make sure the filter is not playing.
        var filter = getBBFilter(e);
        if (filter != null)
        {
			if (filter.status != 2)
			{
				filter.Apply();
				e.visibility = 'visible';
				e.style.display = 'block';
				filter.Play();
			}
		}
		else
		{
			e.visibility = 'visible';
			e.style.display = 'block';
		}
    }
    else
    {
        e.visibility = 'visible';
        e.style.display = 'block';
    }
}

function getBBContentFrame(index)
{    
    var id = bbContainerIdPrefix + index;
    if (bbContainerIdPrefix == null)
    {
        id = index + '';
    }
        
    return document.getElementById(id);
}

function swapContent(index, bAutoRotate)
{
    //----------------------------------------------------------
    //initialize global variables
    //----------------------------------------------------------
    //total number of best bet items
    var itemCount = isNaN(bestBetItemCount) ? 0 : bestBetItemCount;
    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 (timerId != null)
        {
            clearTimeout(timerId);
        }
    }  

    //----------------------------------------------------------
    //show the chosen content panel
    //----------------------------------------------------------
    var fadeOutCtrl = null;
    var fadeInCtrl = getBBContentFrame(index);

    for (var i = 0; i < itemCount; i++)
    {		
        var contentFrame = getBBContentFrame(i);
        if (contentFrame.style.display != 'none')
        {
            //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 (!isBBFilterDone(fadeOutCtrl) || !isBBFilterDone(fadeInCtrl))
    {
		// need to set a new timer for when the filter is done
		timerId = setTimeout("swapContent(" + nextIndex + ", true)", intervalSeconds * 1000);
        return false;
	}	
    if (fadeInCtrl == fadeOutCtrl)
    {
        //do nothing... its already shown
    }
    else
    {
		fadeOutBB(fadeOutCtrl);
        fadeInBB(fadeInCtrl);
    }

    //----------------------------------------------------------
    //highlight the chosen button
    //----------------------------------------------------------
    var targetButtonId = 'bbButton' + index;
    for (var i = 0; i < itemCount; i++)
    {
        var sId = 'bbButton' + i;
        var cButton = document.getElementById(sId);
        if (cButton != null)
        {
            if (cButton.id == targetButtonId)
            {
                cButton.className = 'bbButtonHL';
                if (bAutoRotate && (intervalSeconds > 0))
                {
                    var nextIndex = (index + 1) % itemCount;
                    timerId = setTimeout("swapContent(" + nextIndex + ", true)", intervalSeconds * 1000);
                }
            }
            else
            {
                cButton.className = 'bbButton';
            }
        }
    }
}

function genreSelected(ddl)
{
    if ((ddl == null) || (ddl.options.length <= 0) || (ddl.selectedIndex < 0))
    {
        return;
    }   
    
    var genreId = ddl.options[ddl.selectedIndex].value;
    if (!isNaN(genreId))
    {
        window.location = "../tv/search/?cat=" + genreId;
        ddl.disabled = true;
    }
}

//browser bug. shows downloading status continuously... need to clear status for this
window.status = " ";