﻿var SendToFriend = {
    Init: function(jContainer, closeButtonID, direction) {
        var initButton = jContainer.find(".SendToFriendButton"),
            popup = jContainer.find(".SendToFriendPopup"),
            popupHeight = 222,
            popupDelay = 100, //milliseconds
            initKey = "Initialized",
            popupTimeout;

        if ((direction != undefined || direction != null) && typeof String) {            
            direction = direction.toLowerCase();
            if (direction == "down") {
                popupHeight = -1 * (initButton.height());
            }
        }

        if (!jContainer.data(initKey)) //if this hasn't been initialized
        {
            // Bind Events
            initButton.bind("mouseover", DelayPopup);
            initButton.bind("mouseout", CancelPopup);
            popup.find(".Close").live("click", ClosePopup);
            popup.find(".CloseButton").live("click", ResetPopup);
            // Mark as initialized
            jContainer.data(initKey, true);
        }
        
        function DelayPopup(e)
        {
            var callback = function (e) {
                    OpenPopup(e);
                };
            
            popupTimeout = setTimeout(callback, popupDelay);
        }
        
        function CancelPopup(e)
        {
            clearTimeout(popupTimeout);
        }

        function OpenPopup(e) {
            // Apply offset to position popup above the 'open' button
            var offset = initButton.offset();
            popup.css({
                top: offset.top - popupHeight,
                left: offset.left
            });
            // Show the popup
            popup.show();
        }

        function ClosePopup(e) {
            // Hide the popup
            popup.hide();
        }

        function ResetPopup(e) {
            // Show a "Resetting" message as the postback to reset the form occurs
            popup.find(".ConfirmationView").hide();
            popup.find(".ResettingView").show();
            // Trigger postback to reset the form
            popup.find("#" + closeButtonID).click();
        }
    }
};