/* 
    Showtimes Quickpick Popup Menu
    Copyright (C) 2008 Fandango, Inc.
*/

$(document).ready(function() {

    // Configure buy buttons to display popup menu
    setUpBuyButtons();
    
    // move data to correct DOM location for positioning
    $(".showtimes_quickpick").appendTo("#primary");
    
    // Remove the "More Info" link for the amenity showtimes header
    $(".showtimes_quickpick h5 a").remove();
});

function showBuyButtonMenu(windowId, button)
{
    // Set up visual positioning parameters
    var arrowWidth = 16;
    var popWinVerticalPosAboveButton = 0;
    var arrowVerticalPosOffset = 0;
    var dropShadowOffset = 6;
    
    // Get our window objects
    var win = document.getElementById(windowId);
    var drop = document.getElementById("quickpickDropShadow");
    
    // Get additional positioning variables
    var pos = $(button).offset();         
    var alignwinwidth = $(button).width();
    var posDivMain = $("#main").offset();    
    
    // check for TLP Page
    if ($("#showtimes").length > 0) { 
    	posDivMain = $("#main").offset(); 
    }
	var leftCorrection = -1 * (posDivMain.left);
    var topCorrection = -1 * (posDivMain.top);   	
    
    // Position the quickpick popup menu.  Show now to mitigate "outerHeight()" bug.
    $(win).css("left", pos.left + leftCorrection + alignwinwidth + arrowWidth);
	$(win).css("top", pos.top + topCorrection - popWinVerticalPosAboveButton);
    win.style.visibility = "visible";

    // Position and size the drop shadow    
	    // double check due to difference reference points
	    if ($("#showtimes").length > 0){ 
	    	posDivMain = $("#showtimes").offset(); 
	    	leftCorrection = -1 * (posDivMain.left);
	    	topCorrection = -1 * (posDivMain.top); 
	    }
    
    $(drop).css("left", pos.left + leftCorrection + alignwinwidth + arrowWidth + dropShadowOffset);
    $(drop).css("top", pos.top + topCorrection - popWinVerticalPosAboveButton + dropShadowOffset);
    $(drop).css("width", $(win).outerWidth());
    $(drop).css("height", $(win).outerHeight());
    
    // Position the arrow pointer
    $("#white_arrow_pointer").css("left", pos.left + leftCorrection + alignwinwidth + 1);
    $("#white_arrow_pointer").css("top", pos.top + topCorrection + arrowVerticalPosOffset);
    
    // Make it so
    win.style.visibility = "visible";
    drop.style.visibility = "visible";
    $("#white_arrow_pointer").addClass("show");
}

function hideBuyButtonMenu(windowId)
{
    var win = document.getElementById(windowId);
    win.style.visibility = "hidden";
    
    $("#white_arrow_pointer").removeClass("show");
    
    var drop = document.getElementById("quickpickDropShadow");
    drop.style.visibility = "hidden";
}

function setUpBuyButtons()
{
    // Configure quickpick menu display and auto-hide behavior
    
    $("a").filter(".btn_buy").hover(function() {
        // (optional: hover actions here)
    }, function() {
        // (optional: un-hover actions here)
    });
    
    $("a").filter(".btn_buy").click(function() {
    
        // Hide any visible menus
        $("div").filter(".showtimes_quickpick").each(function() {
            hideBuyButtonMenu($(this).attr("id"));
        });
        
        showBuyButtonMenu($(this).attr("id") + "_quickpick_menu", this);
        return false;
    });
    
    $(".showtimes_quickpick h4").click(function() {
        hideBuyButtonMenu($(this).parent().attr("id"));
    });
}

