﻿//Code to add "Choose Options" text to products variations section
$m.core.register('.product-variation', function (hub) {
    var self = {};

    self.construct = function () {        
        $m.jQuery(hub.container).prepend('<span>Choose Options</span>');
    };

    self.destruct = function () {
    };

    return self;
});
if ($m.core.modules['.product-variation']) {
    $m.core.modules['.product-variation'].construct();
}

//Code to change the text of the add to cart button to "next"
$m.core.register('.quantity-form-buttons', function (hub) {
    var cartButton,
        self = {};

    self.construct = function () {
        cartButton = hub.find('.cart-button');
        hub.setValue(cartButton, 'next');
    };

    self.destruct = function () {
    };

    return self;
});
if ($m.core.modules['.quantity-form-buttons']) {
    $m.core.modules['.quantity-form-buttons'].construct();
}

//Code to change the text for shipping to say "Included"
$m.core.register('.cart-review-wrapper', function (hub) {
    var manualCheckoutButton,
        self = {};

    self.construct = function () {
        var li = $m.jQuery('#Cart-Totals ul li:eq(1)');
        $m.jQuery('a', li).remove();
        $m.jQuery(li).append('<span class="shipping-Included">Included</span>');

        $m.jQuery('.manual-checkout').css('display', 'none');       
    };

    self.destruct = function () {
    };

    return self;
});
if ($m.core.modules['.cart-review-wrapper']) {
    $m.core.modules['.cart-review-wrapper'].construct();
}

//Code to change the text for shipping to say "Included"
$m.core.register('.cart-review', function (hub) {
    var self = {};

    self.construct = function () {
        var span = $m.jQuery('.shipping-value');
        var div = $m.jQuery('.cart-review-variations');

        $m.jQuery(span).text('Included');
        $m.jQuery('span', div).text('Options');               
    };

    self.destruct = function () {
    };

    return self;
});
if ($m.core.modules['.cart-review']) {
    $m.core.modules['.cart-review'].construct();
}

//same product heights
$m.core.register('.store-category-display', function (hub) {
    var self = {};

    self.construct = function () {
        var items = $m.jQuery('.store-category-display > ul > li');
        var maxHeight = 0;

        $m.jQuery.each(items, function (index, value) {
            var itemHeight = $m.jQuery(value).height();

            if (itemHeight > maxHeight) {
                maxHeight = itemHeight;
            }
        });

        $m.jQuery.each(items, function (index, value) {
            $m.jQuery(value).height(maxHeight);
        });
    };

    self.destruct = function () {
    };

    return self;
});
if ($m.core.modules['.store-category-display']) {
    $m.core.modules['.store-category-display'].construct();
}

//var $j = jQuery.noConflict();

//Runs when document is ready to be manipulated
//$j(document).ready(function () {
    //code to set last link width in main nav
    //    if ($j('.main-nav').length) {
    //        var li = $j('#mainnav').children();
    //        for (var i = 0; i < li.length; i++) {
    //            var a = $j(li[i]).children().filter('a');
    //            if ($j(a).hasClass('last')) {
    //                var ulWidth = $j('#mainnav').width();
    //                var siteWidth = $j('#Wrapper').width();
    //                var difference = (siteWidth - ulWidth) - 1;
    //                var aWidth = $j(a).width();
    //                $j(a).css("width", aWidth + difference + "px");
    //                $j(a).css("text-align", "center");
    //            }
    //        }
    //    }

 
    //Code to add "Choose Options" text to products variations section
//    if ($j('.product-variation').length) {
//        $j('.product-variation').prepend('<span>Choose Options</span>');
//    }

    //Code to change the text of the add to cart button to "next"
//    if ($j('.quantity-form-buttons').length) {
//        $j('.cart-button').val('next');
//    }

//    //Code to change the text for shipping to say "Included"
//    if ($j('.cart-review-wrapper').length) {
//        var li = $j('#Cart-Totals ul li:eq(1)');

//        $j('a', li).remove();
//        $j(li).append('<span class="shipping-Included">Included</span>');
//        $j('.manual-checkout').css('display', 'none');
//    }

  

//    if ($j('.store-category-display').length) {
//        var items = $j('.store-category-display > ul > li');
//        var maxHeight = 0;

//        $j.each(items, function () {
//            var itemHeight = $j(this).height();

//            if (itemHeight > maxHeight) {
//                maxHeight = itemHeight;
//            }
//        });

//        $j.each(items, function () {
//            $j(this).height(maxHeight);
//        });
//    }
//});


//Runs when document is fully loaded including images.
//$j(window).load(function() {
//    //Set cart items to same height
//    if ($j('#Interior-2-Column').length) {
//        var items = $j('.product-list-item');
//        var maxHeight = 0;

//        $j.each(items, function() {
//            var itemHeight = $j(this).height();

//            if (itemHeight > maxHeight) {
//                maxHeight = itemHeight;
//            }
//        });

//        $j.each(items, function() {
//            $j(this).height(maxHeight);
//        });
//    }

//    //Checks if side nav is there if not hide it and slide content under it up
//    if (!$j('#subnav').length) {
//        $j('.side-nav').fadeOut(300);
//        $j('#Column2').css('backgroundImage', 'none');
//        $j('.column2-content').stop().animate({
//            marginTop: '0px'
//        }, 400);
//    }

//});


$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('Enroll');
        $m.jQuery(signupField).val('sign up now...');
    };

    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', 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 (index, item) {
                var itemHeight = $m.jQuery(item).height();

                if (itemHeight > maxHeight) {
                    maxHeight = itemHeight;
                }
            });

            $m.jQuery.each(storeItems, function (index, item) {
                $m.jQuery(item).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']) {
    $m.core.modules['#Store'].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();
}
