﻿$m.core.register('.searchPanel', function (hub) {
    var searchButton,
        searchField,
        self = {};

    self.construct = function () {
        searchButton = hub.find('.searchButton');
        searchField = hub.find('.searchField');

        $m.jQuery(searchButton).val('');
        $m.jQuery(searchField).val('search site');
    };

    self.destruct = function () {

    };

    return self;
});
//Calls the construct function of the above module.  We will be able to chain
//this to the end after the next train deploy.
//ex: $m.core.register('.searchPanel', function (hub) {}).construct();
if ($m.core.modules['.searchPanel']) {
    $m.core.modules['.searchPanel'].construct();
}


$m.core.register('.signupPanel', function (hub) {
    var signupButton,
        signupField,
        self = {};

    self.construct = function () {
        signupButton = hub.find('.signupButton');
        signupField = hub.find('.signupField');

        $m.jQuery(signupButton).val('Sign Up');
        $m.jQuery(signupField).val('enter email');
    };

    self.destruct = function () {

    };

    return self;
});
//Calls the construct function of the above module.  We will be able to chain
//this to the end after the next train deploy.
//ex: $m.core.register('.signupPanel', function (hub) {}).construct();
if ($m.core.modules['.signupPanel']) {
    $m.core.modules['.signupPanel'].construct();
}


$m.core.register('.main-nav', function (hub) {
    var isOffScreenOrClipped,
        mainNav,
        self = {};

    self.construct = function () {
        mainNav = hub.find('#mainnav');

        //Main nav flyout animation
        $m.jQuery('#mainnav li').hover(
            function () {
                var hoveredItem = this;
                var firstUl = $m.jQuery(hoveredItem).find('ul:first');
                var parentLi = $m.jQuery(firstUl).parents('li');

                //Check if we have sub nav
                if (firstUl.length > 0) {
                    //Check if sub nav is outside visible window
                    isOffScreenOrClipped = self.IsNodeClippedOrOffscreen(firstUl, parentLi);
                    if (isOffScreenOrClipped) {
                        firstUl.addClass('offScreen');
                    }

                    firstUl.filter(':not(:animated)').slideDown({ duration: 180, easing: 'easeInOutQuad' });
                }

            },
            function () {
                var hoveredItem = this;
                var firstUl = $m.jQuery(hoveredItem).find('ul:first');

                if (firstUl.length > 0) {
                    firstUl.fadeOut(150, 'easeInOutQuad');
                    if (firstUl.hasClass('offScreen')) {
                        firstUl.removeClass('offScreen');
                    }
                }

            }
        );

        hub.listen({
            'mainNavClone': this.mainNavClone
        });
    };

    self.destruct = function () {

    };

    self.mainNavClone = function (bottomNav) {
        $m.jQuery(mainNav).clone().prependTo(bottomNav);
    };

    self.IsNodeClippedOrOffscreen = function (node, parentLi) {
        var ulPos = node.parent().offset();
        var ulLeftPos = ulPos.left;
        var ulTopPos = ulPos.top;
        var ulWidth = node.outerWidth(true);
        var ulHeight = node.outerHeight(true);

        if (parentLi.length > 1) {
            posX = (ulWidth * 2) + ulLeftPos;
            var bOffScreen = self.CheckIfPointIsOffScreen(posX, ulTopPos);
            var bClipped = self.CheckIfPointIsOffScreen(posX, ulTopPos + ulHeight);
        } else {
            var bOffScreen = self.CheckIfPointIsOffScreen(ulLeftPos, ulTopPos);
            var bClipped = self.CheckIfPointIsOffScreen(ulLeftPos + ulWidth, ulTopPos + ulHeight);
        }

        return (bOffScreen || bClipped);
    };

    self.CheckIfPointIsOffScreen = function (iLeftPos, iTopPos) {
        var iBrowserWidth = $m.jQuery(window).width();
        var iBrowserHeight = $m.jQuery(window).height();
        var bOffScreen = false;

        if (iLeftPos < 0 || iLeftPos >= iBrowserWidth)
            bOffScreen = true;

        if (iTopPos < 0 || iTopPos >= iBrowserHeight)
            bOffScreen = true;

        return bOffScreen;
    };

    return self;
});
//Calls the construct function of the above module.  We will be able to chain
//this to the end after the next train deploy.
//ex: $m.core.register('.main-nav', function (hub) {}).construct();
if ($m.core.modules['.main-nav']) {
    $m.core.modules['.main-nav'].construct();
}


$m.core.register('#Bottom-Nav', function (hub) {
    var bottomNav,
        ul,
        self = {};

    self.construct = function () {
        bottomNav = hub.container;

        hub.notify({
            type: 'mainNavClone',
            data: bottomNav
        });

        ul = hub.find('#mainnav');
        $m.jQuery(ul).removeAttr('id');
        $m.jQuery('li', ul).each(function () {
            $m.jQuery(this).removeAttr('id');
        });
    };

    self.destruct = function () {

    };

    return self;
});
//Calls the construct function of the above module.  We will be able to chain
//this to the end after the next train deploy.
//ex: $m.core.register('#Bottom-Nav', function (hub) {}).construct();
if ($m.core.modules['#Bottom-Nav']) {
    $m.core.modules['#Bottom-Nav'].construct();
}


$m.core.register('.store-category-display', function (hub) {
    var self = {};

    self.construct = function () {
        //Set cart items to same height
        $m.jQuery(window).load(function () {
            var storeItems = hub.find('.product-list-item');
            var maxHeight = 0;

            $m.jQuery.each(storeItems, function () {
                var itemHeight = $(this).height();

                if (itemHeight > maxHeight) {
                    maxHeight = itemHeight;
                }
            });

            $m.jQuery.each(storeItems, function () {
                $(this).height(maxHeight);
            });
        });
    };

    self.destruct = function () {

    };

    return self;
});
//Calls the construct function of the above module.  We will be able to chain
//this to the end after the next train deploy.
//ex: $m.core.register('#Store', function (hub) {}).construct();
if ($m.core.modules['.store-category-display']) {
    $m.core.modules['.store-category-display'].construct();
}

$m.core.register('#category-products', function (hub) {
    var self = {};

    self.construct = function () {
        //Set cart items to same height
        $m.jQuery(window).load(function () {
            var storeItems = hub.find('.product-list-item');
            var maxHeight = 0;

            $m.jQuery.each(storeItems, function () {
                var itemHeight = $(this).height();

                if (itemHeight > maxHeight) {
                    maxHeight = itemHeight;
                }
            });

            $m.jQuery.each(storeItems, function () {
                $(this).height(maxHeight);
            });
        });
    };

    self.destruct = function () {

    };

    return self;
});
//Calls the construct function of the above module.  We will be able to chain
//this to the end after the next train deploy.
//ex: $m.core.register('#Store', function (hub) {}).construct();
if ($m.core.modules['#category-products']) {
    $m.core.modules['#category-products'].construct();
}


$m.core.register('body', function (hub) {
    var $window,
        self = {};

    self.construct = function () {
        $m.jQuery(hub.container).append("<div id='IE6'><a href='#' class='ie6close'></a><span class='warning'></span> <span>Wow, you're using IE6!</span><br /><p>The browser you're using is very out of date. We beg you to consider upgrading. This website will work for you, but some things may not work as intended because, well, your browser is really old. Upgrade now and be a happier web surfer.</p><a href='http://www.microsoft.com/nz/windows/internet-explorer/default.aspx' class='ie6'></a><a href='http://www.google.com/chrome' class='chrome'></a><a href='http://www.mozilla.com/en-US/firefox/upgrade.html' class='firefox'></a></div>");

        //IE 6 browser detection
        if ($m.jQuery.browser.msie) {
            var version = $m.jQuery.browser.version;

            if (version == '6.0') {
                var cookie = $m.jQuery.cookie('IE6');

                if (cookie != 'set') {
                    $m.jQuery.cookie('IE6', 'set');
                    $m.jQuery('#IE6').show().stop().animate({
                        top: 0
                    }, 1000);
                }

                $m.jQuery('.ie6close').click(function () {
                    $m.jQuery('#IE6').fadeOut(500);
                });
            }
        }
    };

    self.destruct = function () {

    };

    return self;
});
//Calls the construct function of the above module.  We will be able to chain
//this to the end after the next train deploy.
//ex: $m.core.register('#Store', function (hub) {}).construct();
if ($m.core.modules['body']) {
    $m.core.modules['body'].construct();
}

$m.core.register('.product-template', function (hub) {
    var self = {};

    self.construct = function () {
        $m.jQuery('#Admin-Custom-Content').remove();
    };

    self.destruct = function () {

    };

    return self;
});
//Calls the construct function of the above module.  We will be able to chain
//this to the end after the next train deploy.
//ex: $m.core.register('.searchPanel', function (hub) {}).construct();
if ($m.core.modules['.product-template']) {
    $m.core.modules['.product-template'].construct();
}

$m.core.register('.the-story-locator', function (hub) {
    var self = {};

    self.construct = function () {
        $m.jQuery('li:contains("Featured")').css('background', '#fff8ed');
    };

    self.destruct = function () {

    };

    return self;
});
//Calls the construct function of the above module.  We will be able to chain
//this to the end after the next train deploy.
//ex: $m.core.register('#Bottom-Nav', function (hub) {}).construct();
if ($m.core.modules['.the-story-locator']) {
    $m.core.modules['.the-story-locator'].construct();
}


$m.core.register('#mainnav', function (hub) {
    var self = {};

    self.construct = function () {
        var li = $m.jQuery('.main-nav > ul').find('li:last');

        $m.jQuery(li).css('border', '0px');
    };

    self.destruct = function () {

    };

    return self;
});
//Calls the construct function of the above module.  We will be able to chain
//this to the end after the next train deploy.
//ex: $m.core.register('.searchPanel', function (hub) {}).construct();
if ($m.core.modules['#mainnav']) {
    $m.core.modules['#mainnav'].construct();
}


$m.core.register('.header-1', function (hub) {
    var self = {};

    self.construct = function () {
        var links = $m.jQuery('#mainnav').children().find('a.current');

        if ($m.jQuery(links).length > 0) {
            $m.jQuery('.header-1').text($(links[0]).text());
        }

    };

    self.destruct = function () {

    };

    return self;
});
//Calls the construct function of the above module.  We will be able to chain
//this to the end after the next train deploy.
//ex: $m.core.register('.searchPanel', function (hub) {}).construct();
if ($m.core.modules['.header-1']) {
    $m.core.modules['.header-1'].construct();
}

$m.core.register('#Default', function (hub) {
    var self = {};

    self.construct = function () {
        $m.jQuery(".video a[rel^='prettyPhoto']").prettyPhoto();
    };

    self.destruct = function () {

    };

    return self;
});
//Calls the construct function of the above module.  We will be able to chain
//this to the end after the next train deploy.
//ex: $m.core.register('.searchPanel', function (hub) {}).construct();
if ($m.core.modules['#Default']) {
    $m.core.modules['#Default'].construct();
}

$m.core.register('#Store', function (hub) {
    var self = {};

    self.construct = function () {
        $m.jQuery(".video a[rel^='prettyPhoto']").prettyPhoto();
    };

    self.destruct = function () {

    };

    return self;
});
//Calls the construct function of the above module.  We will be able to chain
//this to the end after the next train deploy.
//ex: $m.core.register('.searchPanel', function (hub) {}).construct();
if ($m.core.modules['#Store']) {
    $m.core.modules['#Store'].construct();
}

$m.core.register('.product-name', function (hub) {
    var self = {};

    self.construct = function () {
        var salePrice = $m.jQuery('.our-price').text().split(":");
        var retailPrice = $m.jQuery('.msrp-price').text().split(":");
        var productName = $m.jQuery('.product-name > h1').text();

        retailPrice[0] = 'Retail';
        salePrice[0] = 'Ministry Price';

        $m.jQuery('.our-price').text(salePrice[0] + ':' + salePrice[1]);
        $m.jQuery('.msrp-price').text(retailPrice[0] + ':' + retailPrice[1]);

        $m.jQuery('#Section-Title').text(productName);
        //console.log(retailPrice);
        //console.log(salePrice);
        //console.log(productName);
    };

    self.destruct = function () {

    };

    return self;
});
//Calls the construct function of the above module.  We will be able to chain
//this to the end after the next train deploy.
//ex: $m.core.register('.searchPanel', function (hub) {}).construct();
if ($m.core.modules['.product-name']) {
    $m.core.modules['.product-name'].construct();
}

$m.core.register('.member-register', function (hub) {
    var self = {};

    self.construct = function () {
        $m.jQuery('.member-register').css('display', 'none');
    };

    self.destruct = function () {

    };

    return self;
});
//Calls the construct function of the above module.  We will be able to chain
//this to the end after the next train deploy.
//ex: $m.core.register('.searchPanel', function (hub) {}).construct();
if ($m.core.modules['.member-register']) {
    $m.core.modules['.member-register'].construct();
}

$m.core.register('#Cart-Totals', function (hub) {
    var self = {};

    self.construct = function () {
        var ul = $m.jQuery('#Cart-Totals ul');

        $m.jQuery('li:first', ul).nextAll().css('display', 'none');
    };

    self.destruct = function () {

    };

    return self;
});
//Calls the construct function of the above module.  We will be able to chain
//this to the end after the next train deploy.
//ex: $m.core.register('.searchPanel', function (hub) {}).construct();
if ($m.core.modules['#Cart-Totals']) {
    $m.core.modules['#Cart-Totals'].construct();
}

$m.core.register('.faq-template', function (hub) {
    var allQuestions = hub.container,
         count = $m.jQuery(allQuestions).length - 1,
         lastQuestion = $m.jQuery(allQuestions[count]).find('.faq-next-question a'),
         self = {};

    self.construct = function () {
        $m.jQuery(lastQuestion).addClass('last-question');

        $m.jQuery('.question a').click(function (e) {
            e.preventDefault();

            var answer = $m.jQuery(this).closest('.faq-template');

            $m.jQuery('.faq-template').fadeOut(500, function () {
                $m.jQuery(answer[0]).fadeIn(300, function () {
                    $m.jQuery('.faq-answer', answer[0]).slideDown(600);
                });
            });

            $m.jQuery('.faq-question').fadeOut(300);

            return false;

        });

        $m.jQuery('.faq-answer-back-to-list a').click(function (e) {
            e.preventDefault();

            var currentAnswer = $m.jQuery(this).closest('.faq-answer');

            $m.jQuery(currentAnswer).slideUp(500, function () {
                $m.jQuery('.faq-template').fadeIn(300, function () {
                    $m.jQuery('.faq-question').fadeIn(300);
                    $m.jQuery('.template-content').fadeIn(300);
                });
            });

            return false;
        });

        $m.jQuery('.faq-next-question a').click(function (e) {
            e.preventDefault();

            var currentQuestion = $m.jQuery(this).closest('.template-content');
            var currentAnswer = $m.jQuery(this).closest('.faq-answer');
            var nextQuestion = $m.jQuery(currentQuestion).next().children().filter('.faq-template');

            if (!$m.jQuery(this).hasClass('last-question')) {
                $m.jQuery(currentAnswer).slideUp(500, function () {
                    $m.jQuery(currentQuestion).fadeOut(500, function () {
                        $m.jQuery(nextQuestion).fadeIn(300, function () {
                            $m.jQuery('.faq-answer', nextQuestion).slideDown(600);
                        });
                    });
                });
            }
            return false;
        });
    };

    self.destruct = function () {

    };

    return self;
});
//Calls the construct function of the above module.  We will be able to chain
//this to the end after the next train deploy.
//ex: $m.core.register('.searchPanel', function (hub) {}).construct();
if ($m.core.modules['.faq-template']) {
    $m.core.modules['.faq-template'].construct();
}


$m.core.register('.member-login', function (hub) {
    var memberLogin,
        memberLoginSpan,
        memberLoginText,
        self = {};

    self.construct = function () {
        memberLogin = hub.container;
        memberLoginSpan = hub.find('> span', memberLogin);

        $m.jQuery('> p:eq(0)', memberLogin).css('display', 'none');
        $m.jQuery(memberLoginSpan).css('display', 'none');
        $m.jQuery('#ctl00_cphPageBody_lblMsg').css('display', 'block');
        $m.jQuery(memberLogin).prepend('<p>Welcome to the Online Resource Library</p>If you have accessed the library before, please enter your username and password below.  If you are new here, welcome! <a href="resource-library-signup">Please sign up to access the resource library.</a><br/><br/><p style="font-size: 11px; font-style: italic">Note: If you have already accessed the library before, don&#8217;t miss NEW features. You can now add your church to our site "Church Locator" and get a customized badge for your churchs&#8217; website. Both will market The Story at your church and help new visitors find you! <a href="add-your-church">Add Your Church Now</a></p><br/><br/>');
    };

    self.destruct = function () {

    };

    return self;
});
//Calls the construct function of the above module.  We will be able to chain
//this to the end after the next train deploy.
//ex: $m.core.register('#Store', function (hub) {}).construct();
if ($m.core.modules['.member-login']) {
    $m.core.modules['.member-login'].construct();
}

$m.core.register('#subnav', function (hub) {
    var self = {};

    self.construct = function () {
        $('.side-nav').css('display', 'block');
        $('#subnav').css('display', 'block');
    };

    self.destruct = function () {

    };

    return self;
});
//Calls the construct function of the above module.  We will be able to chain
//this to the end after the next train deploy.
//ex: $m.core.register('#Store', function (hub) {}).construct();
if ($m.core.modules['#subnav']) {
    $m.core.modules['#subnav'].construct();
}


$m.core.register('.member-login', function (hub) {
    var self = {};

    self.construct = function () {
        $m.jQuery('.header-1').append('Church Login');
    };

    self.destruct = function () {

    };

    return self;
});
//Calls the construct function of the above module.  We will be able to chain
//this to the end after the next train deploy.
//ex: $m.core.register('#Store', function (hub) {}).construct();
if ($m.core.modules['.member-login']) {
    $m.core.modules['.member-login'].construct();
}

//$m.core.register('.side-nav', function (hub) {
//    var mainNav = hub.find('p'),
//        self = {};

//    self.construct = function () {
//        $m.jQuery("#ctl00_cphSideMenu_swSiblingMenu_hlLogin", mainNav).clone().appendTo("#Church-Login");
//    };

//    self.destruct = function () {

//    };

//    return self;
//});
////Calls the construct function of the above module.  We will be able to chain
////this to the end after the next train deploy.
////ex: $m.core.register('#Bottom-Nav', function (hub) {}).construct();
//if ($m.core.modules['.side-nav']) {
//    $m.core.modules['.side-nav'].construct();
//}

//$m.core.register('#Church-Login', function (hub) {
//    var viewProfileLink,
//        logInLink,
//        logOutLink,
//        self = {};

//    self.construct = function () {
//        logInLink = hub.find('#ctl00_cphSideMenu_swSiblingMenu_hlLogin');
//        viewProfileLink = hub.find('a.view-profile');

//        if (logInLink) {
//            $m.jQuery(viewProfileLink).css('display', 'none');
//        }
//    };

//    self.destruct = function () {

//    };

//    return self;
//});
////Calls the construct function of the above module.  We will be able to chain
////this to the end after the next train deploy.
////ex: $m.core.register('#Bottom-Nav', function (hub) {}).construct();
//if ($m.core.modules['#Church-Login']) {
//    $m.core.modules['#Church-Login'].construct();
//}
