﻿
App = function() {

    /* Private */

    /* Properties */

    var cmp = {};





    /* Defaults */

    TVI.debug = true;

    TVI.Mapping.key = 'ABQIAAAAy7Od-n3EG1T00QnIoDJUbBTEO8kSll7zNy02SLRVsRk17rGT-RQYF_GtDy1N7XPjuD9yCdkXfzpxDA';
    
    TVI.Popup.modal = true;
    TVI.Popup.overlay = 80;
    TVI.Popup.width = 700;
//    TVI.Popup.top = 100;
    TVI.Popup.setTemplate(

        '<div class="box">' +
            '<div class="boxHeader TVI-popup-header">' +
                '<h5>${title}</h5>' +
                '<div class="alignRight boxHeaderToolbar TVI-popup-close">' +
                    '<a class="TVI-button TVI-button-iconRight TVI-button-iconStyle-icon16RightArrow align" href="#">close</a>' +
                '</div>' +
            '</div>' +
            '<div class="boxMain">' +
                '<div class="boxMiddle">' +
                    '<div class="boxSection boxBottomSection boxInner TVI-popup-main clearfix">' +

                        '${template}' +

                    '</div>' +
                '</div>' +
                '<div class="boxFatFooter clearfix TVI-popup-footer">' +
                    '<div class="alignRight clearfix">' +

                        '{for b in buttons}' +
                            '<a class="TVI-button TVI-button-style-button44 TVI-button-iconRight TVI-button-iconStyle-icon24RightArrow TVI-fixedWidth align ${b.cls}" style="width:320px;" href="#">${b.text}</a>' +
                        '{/for}' +

                    '</div>' +
                '</div>' +
            '</div>' +
        '</div>'

    );


    //COMPONENTS

    // Set up the default style for buttons and DDLs
    TVI.Components.Button.styles.defaultStyle = {
        leftEndWidth: 5,
        leftIconWrapperWidth: 24,
        leftPadding: 7,
        leftPaddingWithIcon: 2,
        rightEndWidth: 5,
        rightIconWrapperWidth: 24,
        rightPadding: 7,
        rightPaddingWithIcon: 2,
        iconClass: 'icon16',
        hoverClass: 'buttonHover',
        activeClass: 'buttonActive'
    };

    TVI.Components.Button.styles.button44 = {
        leftEndWidth: 6,
        leftIconWrapperWidth: 31,
        leftPadding: 7,
        leftPaddingWithIcon: 2,
        rightEndWidth: 6,
        rightIconWrapperWidth: 31,
        rightPadding: 7,
        rightPaddingWithIcon: 2,
        iconClass: 'icon24',
        hoverClass: 'buttonHover44',
        activeClass: 'buttonActive44'
    };


    TVI.Components.CustomDDL.styles.defaultStyle = {
        buttonLeftEndWidth: 1,
        buttonLeftPadding: 0,
        buttonRightEndWidth: 24,
        buttonRightPadding: 0,
        panelBorderWidth: 1,
        hoverClass: 'TVI-customDDL-hover'
    };


    TVI.Components.DatePickers.styles.defaultStyle = {
        inputWrapperLeftSize: 1,
        inputWrapperRightSize: 1,
        inputLeftSize: 6,
        inputRightSize: 6,
        buttonWidth: 26
    };





    /* Methods */

    var init = function() {

        /* Constructor */

        //set login form
        cmp.accountBoxlogInForm = new TVI.Form({

            ID: 'accountBoxlogInForm',
            url: TVI.handlers + 'App.Account.aspx/logIn',
            validate: false,
            success: function(d) {
            
                TVI.fireEvent('accountChanged', d);

            },
            failure: function(){ window.location.href = '/login'; }

        });


        //handle account changed event
        TVI.addListener('accountChanged', function(d) {

            if (d !== undefined) {

                cmp.user = d;

                $('#accountBox .accountState').removeClass('loggedOut').addClass('loggedIn');
                $('#accountBox .firstname').text(d.firstname);

                return;

            }

            cmp.user = {
                loggedIn: false
            };

            $('#accountBox .accountState').removeClass('loggedIn').addClass('loggedOut');

        });


        TVI.event('.logout', 'click', function() {

            TVI.ajax({

                url: TVI.handlers + 'App.Account.aspx/logout',
                success: function() {

                    TVI.fireEvent('accountChanged');

                }

            });

        });


        //HELP POPUPS
        TVI.event('.helpPopup', 'click', function(){
        
            TVI.Popup.show({
            
                title: 'Help',
                template: this.id.replace('hp-', '')
            
            });
        
        });


//        //TEMP SETTINGS    
//        TVI.files = 'http://www.tvi5.co.uk/files/';

        // Initialize all the components on the page
        TVI.Components.initialize($('BODY'));


        // This code needs to be moved into a fileupload control
        TVI.event('.fileUploadControl', 'mouseover', function() {
            $(this).parents('.fileUploadControlWrapper')
                .find('.fileUploadControlButton')
                .addClass('buttonHover');
        });
        TVI.event('.fileUploadControl', 'mouseout', function() {
            $(this).parents('.fileUploadControlWrapper')
                .find('.fileUploadControlButton')
                .removeClass('buttonHover');
        });


        //handle popup event to initialise popup components
        TVI.addListener('TVI.Popup.onShow', function() {

            TVI.Components.initialize($('#TVI-popup'));

        });

    };
    
    
    
    
    
    var menu = function(name){
    
        /* sets the active menu item */
    
        TVI.ready(function(){
        
            $('#header UL A').removeClass('active');
            
            if (name){ $('#header UL .' + name+ ' A').addClass('active'); }            
        
        });
    
    };





    /* Public */

    TVI.apply(cmp, {

        /* Properties */

        user: {
            loggedIn: false
        },

        /* Methods */
        
        formatPrice: function(price){
        
            return price.toFixed(2).replace('.00', '');
        
        },
        
        menu: function(name){
        
            menu(name);
        
        }

    });


    TVI.ready(init);


    return cmp;


} ();