TryItLinkControl = function() {
	// CONSTRUCTOR: Sets event handlers.
	if ($.browser.safari) {
		// Safari bug fix: Delay resize event to give the page a chance to update.
		$(window).resize(
				function() {
		            if (typeof (TryIt) != 'undefined')
						window.setTimeout(TryIt.Reposition, 1);
				}
			);
	} else {
		$(window).resize(function() {
			if (typeof(TryIt) != 'undefined')
				TryIt.Reposition();
		});
	}

	$(document).click(
			function() {
				if (typeof(TryIt) != 'undefined') {
				    if (!TryItLinkControl.hasFocus)
						TryIt.TogglePopup("hide");
				}
			}
		);
	$(document).keypress(
			function(eventObject) {
				if (typeof(TryIt) != 'undefined') {
					if (eventObject.keyCode == 27)	// 'Esc' key.
						TryIt.TogglePopup("hide");
				}
			}
		);

	$("#cdTryItPopup").hover(
			function() {
	            TryItLinkControl.hasFocus = true;
			},
			function() {
			    TryItLinkControl.hasFocus = false;
			}
		);


	$("#cdTryItExpandLink").click(
			function() {
				$("#cdTryItMinimizeButton").show();
				$("#cdTryItExpandButton").hide();
				$("#cdTryItExpandedSites").slideDown("normal");
			}
		);
	$("#cdTryItMinimizeLink").click(
			function() {
				$("#cdTryItExpandedSites").slideUp("normal", function() {
					$("#cdTryItMinimizeButton").hide();
					$("#cdTryItExpandButton").show();
				});
			}
		);
}
TryItLinkControl.prototype = {

    // Initializes the bookmarking control.
    Initialize: function(parent) {

        // Clone the bookmarking control, and place it into the parent element.
        TryItLinkControl.elem = $("#cdTryIt").clone();
        TryItLinkControl.elem.appendTo(parent);

        // Initialize the bookmarking control events.
        TryItLinkControl.elem.click(
				function(eventObject) {
				    TryItLinkControl.elem = $(this);
				    TryIt.TogglePopup();
				}
			);
        TryItLinkControl.elem.hover(
				function() {
				    TryItLinkControl.hasFocus = true;
				},
				function() {
				    TryItLinkControl.hasFocus = false;
				}
			);

        // Initialize the show/hide states of the control.
        $("#cdTryItExpandedSites").hide();
        $("#cdTryItMinimizeButton").hide();
        $("#cdTryItExpandButton").show();

        TryItLinkControl.elem.css("display", "inline");
    },

    // Toggles the popup.
    TogglePopup: function(state) {
        if (!TryItLinkControl.toggling) {
            TryItLinkControl.toggling = true;

            if (typeof (state) == 'undefined' || (state != "show" && state != "hide"))
                state = "toggle";

            var newLeft = TryItLinkControl.hideLeft;
            if ($("#cdTryItPopup").css("display") == "none") {
                TryIt.Reposition();
                newLeft = TryItLinkControl.showLeft;
                $("#cdTryItPopup").css("left", TryItLinkControl.hideLeft);
            }

            $("#cdTryItPopup").animate({
                left: newLeft,
                height: state,
                width: state,
                opacity: state
            }, "normal", function() { TryItLinkControl.toggling = false; });
        }
    },

    // Re-position the bookmarking popup to line-up with the bookmarking button.
    Reposition: function() {
        var links = TryItLinkControl.elem;
        if (typeof (links) != 'undefined') {
            var popup = $("#cdTryItPopup");

            // make sure popup was found on page
            if (null == popup || typeof (popup) == 'undefined') {
                return;
            }

            popup.css("position", "absolute");

            var offset = popup.offset();
            var popupWidth = popup.width();

            // Check if we should align-left or align-right.
            if (offset.left + popupWidth >= $("body").width()) {
                TryItLinkControl.showLeft = offset.left + popup.width();
                TryItLinkControl.hideLeft = offset.left + popup.width();
            } else {
                TryItLinkControl.showLeft = offset.left;
                TryItLinkControl.hideLeft = offset.left;
            }

            if (navigator.userAgent.match(/firefox/i)) {
                TryItLinkControl.showLeft = $("#cdBookmarkingControl").offset();
                TryItLinkControl.hideLeft = $("#cdBookmarkingControl").offset();
                popup.css("left", $("#cdBookmarkingControl").offset());
                popup.css("top", $("#cdBookmarkingControl").offset().top);
            } else {
                if (popup.css("display") != "none")
                    popup.css("left", TryItLinkControl.showLeft);

                popup.css("top", offset.top + 45);
            }
        }
    },

    // Move the control to the specified parent, and set a new bookmarking asset.
    Reset: function(parent) {
        this.Initialize(parent);
    }
}

var TryIt = new TryItLinkControl();