﻿(function($) {

    $.fn.integrateLiveWorld = function() {
        
        // URL of the production server containing site files.
        var site = "http://www.goodnites.com/na";
            
        var initSeamlessExperience = function(site) {
            
            // Enhance the dropdown menus
            $(".MainMenu").superfish();

            // Generate the twinkling
            $("#Twinkle").flash({
                swf: site + "/Swf/StarryNight.swf",
                width: "100%",
                height: 625,
                hasVersion: 8,
                params: {
                    wmode: "transparent"
                }
            });

            // Add watermark capabilities to the search box
            $(".Search input[@type=text]").focus(function() {
                if ($(this).val() != "Search") {
                    $(this).select();
                } else {
                    $(this).val("");
                }
            }).blur(function() {
                if ($(this).val().length == 0) {
                    $(this).val("Search");
                }
            });
        }

        // Remove any page titles on pages that have announcements
        var removePageTitlesOnAnnouncements = function() {
            if ($(".lw-announcement").length > 0 && $(".lw-announcement").children().length > 0) {
                $(".jive-page-title").hide();
            }
        }

        var integrateSidebar = function(site) {

            // Grab the sidebar content and copy it to the 2nd column, then clear it
            var sidebar = $(".template-3col #Sidebar,.template-3col-wide #Sidebar");
            if (sidebar.length == 1) {
                $(".lw-portlet-col").append(sidebar.html());
                sidebar.remove();
            }
        }

        var integrateLiveWorldBreadCrumbs = function(site) {

            // There will be a breadcrumb array already in existence in the document with
            // placeholder elements that are somewhat incorrect. We can reassign the first
            // two, then iterate across the rest to rebuild the appropriate breadcrumbs.
            if (window.breadCrumbArray && window.breadCrumbArray.length >= 2) {

                breadCrumbArray[0] = {
                    name: "Home",
                    link: site
                };
                breadCrumbArray[1] = {
                    name: "Sharing &amp; Support",
                    link: site + "/Bedwetting-Support"
                };

                var createBreadCrumb = function(name, link, linkable) {
                    if (linkable) {
                        return "<span><a href=\"" + link + "\">" + name + "</a></span>";
                    } else {
                        return "<span>" + name + "</span>";
                    }
                };

                // Add a new breadcrumb for every entry in the breadcrumb array
                var trail = $("#LiveWorldContent .Breadcrumbs");
                var count = $(breadCrumbArray).length;
                $(breadCrumbArray).each(function(index, crumb) {
                    if (index < count - 1) {
                        trail.append(createBreadCrumb(crumb.name, crumb.link, true));
                        trail.append("<span>&nbsp;&gt;&nbsp;</span>");
                    } else {
                        trail.append(createBreadCrumb(crumb.name, crumb.link, false));
                    }
                });
            }
        };

        // Adjusts the main LiveWorld menu system in the upper right to work more to our liking 
        var integrateLiveWorldPageButtons = function(site) {

            // Remove any login/logout buttons
            /*
            $("ul.lw-page-buttons li").each(function(index, button) {
                var link = $("a", button);
                var href = link.attr("href");

                // Remove the login, logout, and register from the options using regular expressions to account for ! variants
                if ((/^login.*\.jspa.*$/).test(href) || (/^logout.*\.jspa.*$/).test(href) || (/^account.*\.jspa.*$/).test(href)) {
                    $(button).remove();
                }
            });
            */

            var createPageButton = function(href, name, addPipeForIE) {

                // Internet Explorer executes the addButtonPipes() method before we add our buttons
                // so for that browser only we need to add them in manually.
                if (addPipeForIE && $.browser.msie) {
                    return "<li class=\"button\"><span class=\"lw-pipe\">&nbsp;&#124;&nbsp;</span><a href=\"" + href + "\">" + name + "</a></li>";
                } else {
                    return "<li class=\"button\"><a href=\"" + href + "\">" + name + "</a></li>";
                }
            };

            var menu = $("ul.lw-page-buttons");
            
            // Add a button to access the profile after the login button
            $("li:eq(0)", menu).after(createPageButton("my.jspa?type=profile", "Profile", true));

            // Add a button to access the search after the preferences button
            $("li:eq(2)", menu).after(createPageButton("search!default.jspa", "Search", true));

            // Add a button to access the subscriptions after the search button
            $("li:eq(3)", menu).after(createPageButton("editwatches!default.jspa", "Subscriptions", true));

            // Show the menu now that we are complete. By default it is hidden with CSS to allow us to do our work first.
            menu.css("visibility", "visible").show();
        }

        var integrateLiveWorldContentButtons = function(site) {
            //$(".lw-portlet-col, .sidebar").prepend($(".lw-ddMenu-featured li a").clone());
        };

        // Resize the profile photo
        /*
        var photos = $("#profile .lw-photo");
        if (photos.length == 1) {
            
        // Profile photos are restricted to a size of 65 pixels by default. We can however adjust the
        // HTML and trigger the image scrtip again to resize our image.
        $("img", photos[0]).attr("width", 156);
        $("img", photos[0]).attr("src", $("img", photos[0]).attr("src").replace("mx=65", "mx=156"));
        }
        */

        initSeamlessExperience(site);
        integrateLiveWorldBreadCrumbs(site);
        integrateLiveWorldPageButtons(site);
        integrateLiveWorldContentButtons(site);
        removePageTitlesOnAnnouncements();
        integrateSidebar(site);
    };
})(jQuery);
