/* 
    TLP Features
    Copyright (C) 2009 Fandango, Inc.
*/
    
var numSprites = 5;
var durationMs = 400;
var debug = false;

function log(msg) {
    if (typeof(console) != "undefined" && debug) {
        console.log(msg);
    }
}

// Perform various actions when theater dropdown list is changed.
function HandleDropDownChange(dropdown) {
    var selectedValue = dropdown.value;
    var delayForTracking = false;
    
    if (typeof(trackDropdownChange) == "function") {
        trackDropdownChange();
        delayForTracking = true;
    }
    
    if (selectedValue != '') {
        if (delayForTracking) {
            triggeredDropDownList = dropdown;
            setTimeout("DropDownNav(triggeredDropDownList);", 500);
        } else {
            DropDownNav(dropdown);
        }
    }
}

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"));
    log('animImgHeight: ' + animImgHeight);
    
    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 + ")";
        log(cmd);
        setTimeout(cmd, durationMs / numSprites);
    }
}

$(document).ready(function() {
    $(".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";
        
        log("index: " + index + ", target: " + target + ", visible: " + visible);
        
        if (visible) {
            rotate(this, "up");
            $(target).slideUp();
            $(this).attr("title", titleClosed); 
        } else {
            rotate(this, "down");
            $(target).slideDown();
            $(this).attr("title", titleOpen); 
        }
    });
    
    $(".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();
}); 
