﻿
App = function () {

    /* Private */

    /* Properties */

    var cmp = {};




    /* Methods */

    var init = function () {

        cmp.newsletterForm = new TVI.Form({

            ID: 'newsletterForm',
            query: 'newsletter'

        });


        cmp.searchForm = new TVI.Form({

            ID: 'searchBoxContainer',
            validate: false,
            local: true,
            success: function () {
                window.location.href = 'search.aspx?keywords=' + this.field('keywords').val();
            }

        });

        /* Expand left navigation on click */
        $('.expandNav').click(function () {
            var mainLink = $(this);
            var subNav = $(this).parent().children('.subNav');

            if ($(mainLink).hasClass('expanded')) {

                $(subNav).slideUp(function () {
                    $(mainLink).removeClass('expanded');
                });

            }
            else {
                $(mainLink).addClass('expanded');
                $(subNav).slideDown();
            }

        });

        /* Show email to friend popup */
        $('#emailLink').click(function () {

            TVI.Popup.show({

                title: 'Email to a Friend',
                width: 450,
                template: 'emailPopup',
                success: function () {


                    cmp.emailFriendForm = new TVI.Form({

                        ID: 'emailFriendForm',
                        url: TVI.handlers + 'App.aspx/emailFriend',
                        submit: function () {

                            this.submit();

                            this.hiddenData.url = window.location.href;
                        }
                    });

                }

            });

            $('.TVI-popup-close').click(function () {

                TVI.Popup.close();

            });

        });

    };

    /* Highlight top tabs depending on the page being viewed */
    var menu = function (name) {
        TVI.ready(function () {

            switch (name) {
                case "0":
                    name = "about";
                    break;
            }

            var currentTab = $('#' + name + "Tab");

            $(currentTab).addClass('current');

        });
    };

    /* Highlight side nav depending on the page being viewed */
    var sideMenu = function (name, subTab) {
        TVI.ready(function () {

            var currentTab = $('#' + name + "Tab");
            var currentSubTab = $('#' + subTab + "SubTab");
            var subNav = $($(currentTab).parent().children('.subNav'));

            $(currentTab).addClass('expanded');
            $(subNav).show();
            $(currentSubTab).css('background-position', '200px');

        });
    };


    /* Public */

    TVI.apply(cmp, {

        /* Properties */

        menu: function (name) {

            menu(name);

        },

        sideMenu: function (name, subTab) {

            sideMenu(name, subTab);

        }

    });


    TVI.ready(init);


    return cmp;


} ();
