$m.core.register('#Page-Edit', function (hub) {
    var ModalBG,
        tutorialVideo,
        backToClassicEditPopup,
        closeClassicEditingPopup,
        returnToClassicEditing,
        win = $m.jQuery(window),
        iframes,
        iframe,
        iwindow,
        divPageEditHover,
        mindscape,
        ddlJump,
        self = {};

    self.construct = function () {
        iframes = $m.jQuery(hub.find("iframe[id$=_ifPageEdit]")[0]);
        returnToClassicEditing = hub.find('.classic');
        closeClassicEditingPopup = hub.find('.cancel-classic');
        backToClassicEditPopup = hub.find('#Back-To-Classic');
        tutorialVideo = document.getElementById('Tutorial-Video');
        ModalBG = $m.jQuery('#Modal-BG');
        ddlJump = hub.select('#ddlJump');

        iframes.load(function () {
            self.loadFrame();
        });

        //Browser check for IE 7 & 8.  if true no fancy for you....
        if (!$m.jQuery.support.opacity) {
            $m.jQuery(hub.find('#Old-Browser-Message')).slideDown(200, 'easeInOutQuad');
        } else {

            $m.jQuery(hub.find('#Help - Banner')).slideDown(200, 'easeInOutQuad');
        }

        //Back to classic editing - show feedback form.
        $m.jQuery(returnToClassicEditing).click(function (e) {
            e.preventDefault();
            e.stopPropagation();

            $m.jQuery(backToClassicEditPopup).fadeIn(175, 'easeInOutQuad');

            return false;
        });

        //cancel return to classic
        $m.jQuery(closeClassicEditingPopup).each(function (index, item) {
            $m.jQuery(item).click(function (e) {
                e.preventDefault();
                e.stopPropagation();

                $m.jQuery(backToClassicEditPopup).fadeOut(175, 'easeInOutQuad');

                return false;
            });
        });


        //show video
        $m.jQuery('#Help-Video-Overview').click(function (e) {
            e.preventDefault();
            e.stopPropagation();

            $m.jQuery(ModalBG).fadeIn(175, 'easeInOutQuad');
            $m.jQuery('#Help-Video').fadeIn(175, 'easeInOutQuad');
            tutorialVideo.play();

            return false;
        });

        //Jump to the correct page
       ddlJump.change(function () {
           
            window.location.href = ddlJump.val();
        });

        hub.listen({
            'showVideo': this.showVideo
        });

    };

    self.destruct = function () {
    };

    self.showVideo = function () {
        if ($m.jQuery.queryString['new'] === 'true') {
            if ($m.jQuery.support.opacity) {
                //only show for good browsers, not bad ID browsers
                $m.jQuery(ModalBG).fadeIn(175, 'easeInOutQuad');
                $m.jQuery('#Help-Video').fadeIn(175, 'easeInOutQuad');
                tutorialVideo.play();
            }

        } else {

            $m.jQuery(ModalBG).fadeOut(175, 'easeInOutQuad');
            $m.jQuery('#Help-Video').fadeOut(175, 'easeInOutQuad');
            tutorialVideo.pause();
        }
    };

    /// Function to call when we hover over a mindscape editable region
    self.pageEditHover = function (editable, mousePosY) {
        //set variables from mindscape:editable tag
        var $editable = $m.jQuery(editable);
        var sa = editable.getAttribute('sa');
        var pa = editable.getAttribute('pa');
        var tabid = editable.getAttribute('tabid');
        var unique = editable.getAttribute('unique');
        var tag = editable.getAttribute('tag');
        var master = editable.getAttribute('master');

        //We've actually changed what element we are hovering over, so do some stuff :)
        if (window.pageEditUnique !== unique) {
            window.pageEditUnique = unique;

            //Variable set up on hover of editable section
            var divSiteWide = divPageEditHover.find("#Page-Edit-Hover-Site-Wide");
            var divPageSpecific = divPageEditHover.find("#Page-Edit-Hover-Page-Specific");
            var siteWideHelp = divSiteWide.find("a:eq(0)");
            var pageSpecificHelp = divPageSpecific.find("a:eq(0)");
            var siteWideEdit = divSiteWide.find("a:eq(1)");
            var pageSpecificEdit = divPageSpecific.find("a:eq(1)");
            var titleText = divPageEditHover.find("#Page-Edit-Header");
            var siteWidth = 900;
            var position = $editable.parents('div:eq(0)').offset();
            var cssObjReset = { 'left': '0px', 'top': '0px' };
            var iframHeight = $m.jQuery(iframe).outerHeight();
            var siteWideHelpPopup = divPageEditHover.find('#Site-Wide-Help');
            var pageSpecificHelpPopup = divPageEditHover.find('#Page-Specific-Help');

            //Unbind click event on the "Site Wide" and "Page Specific"
            //help links.  Cuases the help sections to flicker if we don't
            //do this.
            $m.jQuery(siteWideHelp).unbind('click');
            $m.jQuery(pageSpecificHelp).unbind('click');


            //If any of the help sections are visible hide them
            if ($m.jQuery(siteWideHelpPopup).is(':visible')) {
                $m.jQuery(siteWideHelpPopup).css('display', 'none');
            }

            if ($m.jQuery(pageSpecificHelpPopup).is(':visible')) {
                $m.jQuery(pageSpecificHelpPopup).css('display', 'none');
            }


            //Check if any Admin-Edit control divs are visible, if so hide them
            //then show the one that the user has hovered over.  If none are 
            //showing show the one that the user has hovered over.
            if ($m.jQuery(divPageEditHover).is(':visible')) {

                $m.jQuery(divPageEditHover).fadeOut(275, function () {

                    $m.jQuery(divPageEditHover).css(cssObjReset);

                    if ((position.left + 330) > siteWidth) {
                        var posX = (position.left + 330) - siteWidth;
                        var cssObj = {
                            'left': (position.left - posX) + 'px',
                            'top': mousePosY + 'px'
                        };

                        $m.jQuery(divPageEditHover).css(cssObj).slideDown(150, 'easeInOutQuad');
                    } else {
                        var cssObj = {
                            'left': position.left + 'px',
                            'top': mousePosY + 'px'
                        };

                        $m.jQuery(divPageEditHover).css(cssObj).slideDown(150, 'easeInOutQuad');
                    }

                });
            } else {
                if ((position.left + 330) > siteWidth) {
                    var posX = (position.left + 330) - siteWidth;
                    var cssObj = {
                        'left': (position.left - posX) + 'px',
                        'top': mousePosY + 'px'
                    };

                    $m.jQuery(divPageEditHover).css(cssObj).slideDown(150, 'easeInOutQuad');
                } else {
                    var cssObj = {
                        'left': position.left + 'px',
                        'top': mousePosY + 'px'
                    };

                    $m.jQuery(divPageEditHover).css(cssObj).slideDown(150, 'easeInOutQuad');
                }
            }


            //Site wide and pagespecific help icons hover events
            siteWideHelp.mouseenter(function () {
                hub.select('> img', this).stop().animate({
                    top: '-3px',
                    left: '-3px',
                    width: '27px',
                    height: '27px'
                }, 175, 'easeInOutCirc');
            });

            pageSpecificHelp.mouseenter(function () {
                hub.select('> img', this).stop().animate({
                    top: '-3px',
                    left: '-3px',
                    width: '27px',
                    height: '27px'
                }, 175, 'easeInOutCirc');
            });

            siteWideHelp.mouseleave(function () {
                hub.select('> img', this).stop().animate({
                    top: '0px',
                    left: '0px',
                    width: '21px',
                    height: '21px'
                }, 175, 'easeInOutCirc');
            });

            pageSpecificHelp.mouseleave(function () {
                hub.select('> img', this).stop().animate({
                    top: '0px',
                    left: '0px',
                    width: '21px',
                    height: '21px'
                }, 175, 'easeInOutCirc');
            });


            //Site wide and pages pecific edit icons hover events
            $m.jQuery(siteWideEdit).mouseenter(function () {
                hub.select('> img', this).stop().animate({
                    top: '-3px',
                    left: '-3px',
                    width: '27px',
                    height: '27px'
                }, 175, 'easeInOutCirc');
            });

            $m.jQuery(pageSpecificEdit).mouseenter(function () {
                hub.select('> img', this).stop().animate({
                    top: '-3px',
                    left: '-3px',
                    width: '27px',
                    height: '27px'
                }, 175, 'easeInOutCirc');
            });

            $m.jQuery(siteWideEdit).mouseleave(function () {
                hub.select('> img', this).stop().animate({
                    top: '0px',
                    left: '0px',
                    width: '21px',
                    height: '21px'
                }, 175, 'easeInOutCirc');
            });

            $m.jQuery(pageSpecificEdit).mouseleave(function () {
                hub.select('> img', this).stop().animate({
                    top: '0px',
                    left: '0px',
                    width: '21px',
                    height: '21px'
                }, 175, 'easeInOutCirc');
            });


            //Click event for the "Site Wide Help" help icon
            $m.jQuery(siteWideHelp).click(function (e) {
                e.preventDefault();
                e.stopPropagation();

                var linkPosition = $m.jQuery(this).position();
                var helpPosX = 330 + position.left;
                var helpCssObj = {
                    'left': '104%',
                    'top': (linkPosition.top - 40) + 'px'
                };
                var helpCssObj2 = {
                    'left': '-83%',
                    'top': (linkPosition.top - 40) + 'px'
                };

                if ($m.jQuery(siteWideHelpPopup).is(':visible')) {
                    $m.jQuery(siteWideHelpPopup).fadeOut(150, 'easeInOutQuad');
                }

                if ($m.jQuery(pageSpecificHelpPopup).is(':visible')) {
                    $m.jQuery(pageSpecificHelpPopup).fadeOut(150, 'easeInOutQuad');
                }

                if (helpPosX >= siteWidth) {
                    $m.jQuery('div:eq(0)', siteWideHelpPopup).css('display', 'none');
                    $m.jQuery('div:eq(1)', siteWideHelpPopup).css('display', 'block');
                    $m.jQuery(siteWideHelpPopup).css(helpCssObj2);
                    $m.jQuery(siteWideHelpPopup).fadeIn(150, 'easeInOutQuad');
                } else {
                    $m.jQuery('div:eq(1)', siteWideHelpPopup).css('display', 'none');
                    $m.jQuery('div:eq(0)', siteWideHelpPopup).css('display', 'block');
                    $m.jQuery(siteWideHelpPopup).css(helpCssObj);
                    $m.jQuery(siteWideHelpPopup).fadeIn(150, 'easeInOutQuad');
                }

                return false;
            });


            //Click event for the "Page Specific Help" help icon
            $m.jQuery(pageSpecificHelp).click(function (e) {
                e.preventDefault();
                e.stopPropagation();

                var linkPosition = $m.jQuery(this).position();
                var helpPosX = 330 + position.left;
                var helpCssObj = {
                    'left': '104%',
                    'top': (linkPosition.top - 40) + 'px'
                };
                var helpCssObj2 = {
                    'left': '-83%',
                    'top': (linkPosition.top - 40) + 'px'
                };

                if ($m.jQuery(siteWideHelpPopup).is(':visible')) {
                    $m.jQuery(siteWideHelpPopup).fadeOut(150, 'easeInOutQuad');
                }

                if ($m.jQuery(pageSpecificHelpPopup).is(':visible')) {
                    $m.jQuery(pageSpecificHelpPopup).fadeOut(150, 'easeInOutQuad');
                }

                if (helpPosX >= siteWidth) {
                    $m.jQuery('div:eq(0)', pageSpecificHelpPopup).css('display', 'none');
                    $m.jQuery('div:eq(1)', pageSpecificHelpPopup).css('display', 'block');
                    $m.jQuery(pageSpecificHelpPopup).css(helpCssObj2);
                    $m.jQuery(pageSpecificHelpPopup).fadeIn(150, 'easeInOutQuad');
                } else {
                    $m.jQuery('div:eq(1)', pageSpecificHelpPopup).css('display', 'none');
                    $m.jQuery('div:eq(0)', pageSpecificHelpPopup).css('display', 'block');
                    $m.jQuery(pageSpecificHelpPopup).css(helpCssObj);
                    $m.jQuery(pageSpecificHelpPopup).fadeIn(150, 'easeInOutQuad');
                }

                return false;
            });


            /*******************************************************
            So, we can tell when we hover over something now. What do we do?
            1) Construct proper links
            2) display proper pop up
            3) adjust pop up to proper location

            If tabid = "no", then it is an appearance tag.
            If sa="no", then it the value has been changed on the page, and it is a page specific value.
            If sa="yes", then the value has been changed on the site wide level, and both sitewide and page specific should be shown
            tabid correpsonds to main, top, left, right, bottom content areas. the tabid needs to be in the link to go to the correct one.
            *******************************************************/
            if (tabid == "no") {
                // Appearance Tag
                if (sa == "no") {
                    //Page Specific 
                    divSiteWide.toggle(false);

                    divPageSpecific.find("a:eq(1)").attr('href', 'admin/PageAppearance.aspx?pageid=' + pa + '&tagname=' + tag);
                    titleText.html("Appearance tag: " + tag);
                } else {
                    //Site Wide
                    divSiteWide.toggle(true);

                    divPageSpecific.find("a:eq(1)").attr('href', 'admin/PageAppearance.aspx?pageid=' + pa + '&tagname=' + tag);
                    divSiteWide.find("a:eq(1)").attr('href', 'admin/SiteAppearance.aspx?pageid=' + pa + '&tagname=' + tag + '&mp=' + master);
                    titleText.text("Appearance tag: " + tag);
                }


            } else {
                var area = '';
                switch (tabid) {
                    case '1': area = '<b>Main</b>   page parts'; break;
                    case '2': area = '<b>Area 1</b> page parts'; break; //top
                    case '3': area = '<b>Area 2</b> page parts'; break; //bottom
                    case '4': area = '<b>Area 3</b> page parts'; break; //left
                    case '5': area = '<b>Area 4</b> page parts'; break; //right
                }

                //One of the 5 content areas
                if (sa == "no") {
                    //Page Specific
                    divSiteWide.toggle(false);
                    divPageSpecific.find("a:eq(1)").attr('href', 'admin/PageMgmt.aspx?pageid=' + pa + '&panelid=0&tabid=' + tabid);

                    titleText.html(area);
                } else {
                    //Site Wide
                    divSiteWide.toggle(true);

                    divPageSpecific.find("a:eq(1)").attr('href', 'admin/PageMgmt.aspx?pageid=' + pa + '&panelid=0&tabid=' + tabid);
                    divSiteWide.find("a:eq(1)").attr('href', 'admin/SiteWideContentMgmt.aspx?pageid=' + pa + '&tabid=' + tabid);

                    titleText.html(area);
                }
            }


        }
    };

    self.loadFrame = function () {

        if (!iframes) { return false; }

        if (!iframes.length) { return false; }

        iframe = iframes[0];

        if (!iframe) { return false; }

        if (!iframe.contentWindow) { return false; }

        iwindow = iframe.contentWindow;

        if (!iwindow) { return false; }

        iwindow.pageEditHover = self.pageEditHover;

        var html = iwindow.document;

        if (!html) { return false; }

        html.documentElement.setAttribute("xmlns:mindscape", "http://www.mindscape-hm.com/2011/editable");

        var root = html.body;

        // Set iframe height
        var htmlHeight = $m.jQuery(root).outerHeight();

        if (htmlHeight <= 500) {
            htmlHeight = 1000;
        }

        $m.jQuery(iframe).height(htmlHeight + 250);


        // Disable all links
        var aTags = $m.jQuery('a', html);

        $m.jQuery(aTags).each(function (index, item) {

            if ($m.jQuery(item).hasClass("do-not-kill")) {
                //site wide help link
            } else {
                $m.jQuery(item).attr('href', '#');
            }


            if ($m.jQuery(item).parent('div').hasClass('admin-controls')) {
                if ($m.jQuery(item).hasClass('admin-controls-edit')) {
                    $m.jQuery(item).click(function (e) {
                        e.preventDefault();
                        e.stopPropagation();

                        window.location = '/' + $m.jQuery(item).attr('href') //AB on 3.24 - the "/" was needed in IE in order to make it go to the right page instead of Admin/admin

                        return false;
                    });
                }
            } else {
                if ($m.jQuery(item).hasClass("do-not-kill")) {
                    //site wide help link
                } else {
                    $m.jQuery(item).click(function (e) {
                        e.preventDefault();
                        e.stopPropagation();

                        return false;
                    });
                }
            }

        });


        $m.jQuery('input[type=submit]', html).attr('disabled', 'disabled');


        // Show page load progress then show page
        $m.jQuery('#Page-Loading').fadeOut(450, function () {
            $m.jQuery('#Page-Edit-Site-Container').animate({ height: htmlHeight + 250 }, 450, 'easeInOutQuad', function () {
                hub.notify({
                    type: 'showVideo'
                });
            });
        });

        //Checks if there is an iframe then grabs all editable regions on page sets some styles
        //and adds hover event to each.
        if (iframe) {
            mindscape = $m.jQuery(iframe).contents().find('.mindscape-editable');

            $m.jQuery.each(mindscape, function (index, item) {
                var cssObj = {
                    'display': 'block',
                    'box-shadow': 'rgba(51,255,51,1.0) 0px 0px 20px',
                    '-moz-box-shadow': 'rgba(51,255,51,1.0) 0px 0px 20px',
                    '-webkit-box-shadow': 'rgba(51,255,51,1.0) 0px 0px 20px',
                    'float': 'left',
                    'width': '100%',
                    'min-height': '50px'
                };

                $m.jQuery(item).css(cssObj);

                $m.jQuery(item).mouseenter(function (e) {
                    self.pageEditHover(this, e.pageY);
                });

                //when we leave an editable area, let's reset unique so that entering the same spot will cause popup to appear in new place. 
                //AB on 3.24.2011
                $m.jQuery(item).mouseleave(function (e) {
                    window.lastPageEditUnique = window.pageEditUnique;
                    window.pageEditUnique = 0;
                    //divPageEditHover.toggle(false);  //AB on 3.30.11 - tried turning off the div when leaving it.  However, we'd need make sure it left divPageEditHover and the Mindscape tag before turning it off. Otherwise, confusion.
                });

            });
        }


        divPageEditHover = $m.jQuery(iframes).contents().find("#Page-Edit-Hover");
        divPageEditHover.mouseleave(function (e) {
            //If we are leaving our popup, we want the Mindscape region to think that we never actually left it. This should solve the flicker.
            window.pageEditUnique = window.lastPageEditUnique;
            window.lastPageEditUnique = 0; //if we leave to the page, we're reset. If we leave to the mindscape element, this value will be set leaving there.
        });
    };

    return self;
});


$m.core.register('#Help-Video', function (hub) {
    var ModalBG, 
        tutorialVideo,
        closeVideo,
        self = {};

    self.construct = function () {
        closeVideo = hub.find('.video-close');
        tutorialVideo = document.getElementById('Tutorial-Video');
        ModalBG = $m.jQuery('#Modal-BG');

        $m.jQuery(closeVideo).click(function (e) {
            e.preventDefault();
            e.stopPropagation();

            tutorialVideo.pause();
            $m.jQuery(ModalBG).fadeOut(175, 'easeInOutQuad');
            $m.jQuery(hub.container).fadeOut(150, 'easeInOutQuad');

            return false;
        });
    };

    self.destruct = function () {
    };


    return self;
});
