/* 
    TLP Features
    Copyright (C) 2009 Fandango, Inc.
*/
    
var numSprites = 5;
var durationMs = 400;

function rotate(arrow, upOrDown, animationIndex) {
    // Rotate the arrow graphic by moving the background around.  There are "numSprites" sprites in the image, so animate to the other "numSprites - 1" of them.
    var doit = false;
    var animImgHeight = parseInt($(arrow).css("height"));
    
    if (animationIndex == null) {
        animationIndex = upOrDown == "up" ? numSprites - 1 : 0;
    }

    if ((upOrDown == "up") && (animationIndex > 0)) {
        animationIndex--;
        doit = true;
    } else if ((upOrDown == "down") && (animationIndex < numSprites - 1)) {
        animationIndex++;
        doit = true;
    }
    
    if (doit) {
        $(arrow).css("background-position", "100% -" + (animationIndex * animImgHeight) + "px");
        var cmd = "rotate('#" + $(arrow).attr("id") + "', '" + upOrDown + "', " + animationIndex + ")";
        setTimeout(cmd, durationMs / numSprites);
    }
}

/**
 * This function tests whether the parameter s is whitespace, and treats 
 * null as whitespace too.
 *
 * @return Boolean value
 */
function isThisWhiteSpace(s)
{
    return (s === null) || s.match(/^(\s)*$/);
}

/**
 * This function determines if the <div> with id value matching the value of the divId
 * parameter is empty (contains only spaces, tabs or carriage returns)
 *
 * @return Boolean value
 */
function isDivEmpty(divId)
{
    var isEmpty = false;
    var div = document.getElementById(divId);
    
    if (div)
    {
        if (div.childNodes.length == 0)
        {
            isEmpty =  true;
        }
        else
        {
            
            for(var i=0; i < div.childNodes.length; i++)
            {
                if (!div.childNodes[i])
                {
                    continue;
                }
                else if (!isThisWhiteSpace(div.childNodes[i].nodeValue))
                {
                    isEmpty = false;
                    break;
                }
            }
            
        }
    }
    
    return isEmpty;
}

$(document).ready(function() {
    $.getScript(scriptPath + siteFAJAX); // pre-load Fandango AJAX

    var pageDone = false;
    $(".movie_detail_revealer").click(function() {
        // Get the integer index of this object, that gives us the detail index to show.  Then show it.
        var index = $(this).attr("id").substring(22);
        var target = "#movie_details_" + index;
        var visible = $(target).css("display") != "none";
        
        var titleClosed = "Click to expand";
        var titleOpen = "Click to close";
        
        if (visible) {
            rotate(this, "up");
            $(target).slideUp();
            $(this).attr("title", titleClosed); 
        } else {
            rotate(this, "down");
            $(target).slideDown();
            $(this).attr("title", titleOpen); 
        }
        
        if (pageDone) {
            ReportToWss($(target));
        }
    });
    
    if ($(".glowbox").length) {
        $.getScript(scriptPath + jQglowbox, function() {
	$.getScript(scriptPath + siteUIFloat, function() {
              $(".glowbox").glowbox().show();
              // trigger class, layerId, topOffset, leftOffset, popLocation (middle, auto)
              addRolloverLayer("nonwired_message_popup", "nonwired_message_popup_container", 30, 0, "middle");
          });   
        });
    }
    
    $(".movie_detail_revealer:eq(0)").click();
    
    // if the TlpMessageContainer <div> is present and it is empty, then its css style is set to
    // "display":"none" to prevent unnecessary extra blank line(s) from being rendered to the page.
		if (isDivEmpty("TlpMessageContainer"))
		{
			$("#TlpMessageContainer").css({"display":"none"});
		}

    pageDone = true;
}); 


