/*****************************************************************************
Bookmarking.js

Owner: MikeHung
Copyright (c) 2008 Microsoft Corporation

Javascript used for social bookmarking across the site.
******************************************************************************/

///////////////////////////// BOOKMARKING CONTROL /////////////////////////////

BookmarkingControl = 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(Bookmark) != 'undefined')
						window.setTimeout(Bookmark.Reposition, 1);
				}
			);
	} else {
		$(window).resize(function() {
			if (typeof(Bookmark) != 'undefined')
				Bookmark.Reposition();
		});
	}

	$(document).click(
			function() {
				if (typeof(Bookmark) != 'undefined') {
					if (!BookmarkingControl.hasFocus)
						Bookmark.TogglePopup("hide");
				}
			}
		);
	$(document).keypress(
			function(eventObject) {
				if (typeof(Bookmark) != 'undefined') {
					if (eventObject.keyCode == 27)	// 'Esc' key.
						Bookmark.TogglePopup("hide");
				}
			}
		);

	$("#cdBookmarkingPopup").hover(
			function() {
				BookmarkingControl.hasFocus = true;
			},
			function() {
				BookmarkingControl.hasFocus = false;
			}
		);


	$("#cdBookmarkingExpandLink").click(
			function() {
				$("#cdBookmarkingMinimizeButton").show();
				$("#cdBookmarkingExpandButton").hide();
				$("#cdBookmarkingExpandedSites").slideDown("normal");
			}
		);
	$("#cdBookmarkingMinimizeLink").click(
			function() {
				$("#cdBookmarkingExpandedSites").slideUp("normal", function() {
					$("#cdBookmarkingMinimizeButton").hide();
					$("#cdBookmarkingExpandButton").show();
				});
			}
		);

	$("#cdBookmarkingHelpLink").click(
			function() {
				$("#cdBookmarkingHelpText").slideToggle("normal");
			}
		);
	$("#cdBookmarkingHelpClose").click(
			function() {
				$("#cdBookmarkingHelpText").slideToggle("normal");
			}
		);

}
BookmarkingControl.prototype = {
    Asset: null,
    Types: null,
    WTInfo: null,

    // Initializes the bookmarking control.
    Initialize: function(parent, asset, assetTypes, wtInfo) {
        this.Asset = asset;
        this.Types = assetTypes;
        this.WTInfo = wtInfo;

        // Clone the bookmarking control, and place it into the parent element.
        BookmarkingControl.elem = $("#cdBookmarking").clone();
        BookmarkingControl.elem.appendTo(parent);

        // Initialize the bookmarking control events.
        BookmarkingControl.elem.click(
				function(eventObject) {
				    BookmarkingControl.elem = $(this);
				    Bookmark.TogglePopup();
				}
			);
        BookmarkingControl.elem.hover(
				function() {
				    BookmarkingControl.hasFocus = true;
				},
				function() {
				    BookmarkingControl.hasFocus = false;
				}
			);

        // Initialize the show/hide states of the control.
        $("#cdBookmarkingExpandedSites").hide();
        $("#cdBookmarkingMinimizeButton").hide();
        $("#cdBookmarkingExpandButton").show();
        $("#cdBookmarkingHelpText").hide();

        // Determine if we should show the bookmarking control based on asset type.
        var assetType = this.Asset.substr(0, 2);
        for (i in this.Types) {
            if (this.Types[i] == assetType) {
                BookmarkingControl.elem.css("display", "inline");
                return;
            }
        }
        BookmarkingControl.elem.css("display", "none");
    },

    // Toggles the popup.
    TogglePopup: function(state) {
        if (!BookmarkingControl.toggling) {
            BookmarkingControl.toggling = true;

            if (typeof (state) == 'undefined' || (state != "show" && state != "hide"))
                state = "toggle";

            var newLeft = BookmarkingControl.hideLeft;
            if ($("#cdBookmarkingPopup").css("display") == "none") {
                Bookmark.Reposition();
                newLeft = BookmarkingControl.showLeft;
                $("#cdBookmarkingPopup").css("left", BookmarkingControl.hideLeft);
            }

            $("#cdBookmarkingPopup").animate({
                left: newLeft,
                height: state,
                width: state,
                opacity: state
            }, "normal", function() { BookmarkingControl.toggling = false; });
        }
    },

    // Re-position the bookmarking popup to line-up with the bookmarking button.
    Reposition: function() {
        var bookmarking = BookmarkingControl.elem;
        if (typeof (bookmarking) != 'undefined') {
            var popup = $("#cdBookmarkingPopup");

            var offset = bookmarking.offset();
            var popupWidth = popup.width();

            // Check if we should align-left or align-right.
            if (offset.left + popupWidth >= $("body").width()) {
                BookmarkingControl.showLeft = offset.left + bookmarking.width() - popupWidth;
                BookmarkingControl.hideLeft = offset.left + bookmarking.width();
            } else {
                BookmarkingControl.showLeft = offset.left;
                BookmarkingControl.hideLeft = offset.left;
            }
            if (popup.css("display") != "none")
                popup.css("left", BookmarkingControl.showLeft);

            popup.css("top", offset.top + bookmarking.height());
        }
    },

    // Opens a bookmarking site in new window with the appropriate width and height.
    Open: function(siteId, width, height) {
        // Formulate the URL to open.
        var siteURL = "/_layouts/olsite/Bookmarking/Bookmarking.aspx";
        siteURL += "?BookmarkID=" + siteId;
        siteURL += "&BookmarkTitle=" + encodeURIComponent(document.title);
        siteURL += "&BookmarkURL=" + encodeURIComponent(document.URL);

        // Webtrends
        //wt_Bookmark(document.title, BookmarkSites[siteId]);
        
        // Opens the bookmarking window.
        var siteWindow = window.open(siteURL, "sbSiteWindow",
				"width=" + width + ",height=" + height +
				",resizable=1,scrollbars=1,status=1,toolbar=1,location=1,menubar=1");
    },

    // Move the control to the specified parent, and set a new bookmarking asset.
    Reset: function(parent, asset) {
        this.Initialize(parent, asset, this.Types, this.WTInfo);
    }
}

var Bookmark = new BookmarkingControl();
var BookmarkSites = new Array("Delicious", "Digg", "Facebook", "Live", "Stumbleupon", "MySpace", "Reddit", "Furl", "Yahoo", "Email");