﻿
PDX = function(){

    var main = function(){
    
        /* Private */
        
        /* Properties */
        
        var cmp = null;
        
        
        /* Methods */
        
        var init = function(){
        
            TVI.debug = true;

            TVI.Forms.handlerURL = '/handlers/';
    
            TVI.Data.defaultURL = '/handlers/data.aspx/query';
                       
            // FAQ slide Up/Down
            $('.faq .question A').click( function() {
            
                // Open if not open already
                if( ! $(this).parents('.faq').hasClass('open') ){
                    
                    // Show FAQ answer
                    $(this).parents('.faq').addClass('open').find('.answer').slideDown();

                } else {
                    
                    // Hide answer
                    $(this).parents('.faq').removeClass('open').find('.answer').slideUp();
                
                }

                return false;
            });
            
            // DATE PICKER
            var currentTime = new Date();
            if (currentTime.getHours() < 15) {
                $('.datePick INPUT').datepick({beforeShowDay: $.datepick.noWeekends, showOn: 'focus', yearRange: '-0:+1', minDate:'0', dateFormat: 'dd/mm/yy'});
            }
            else {
                $('.datePick INPUT').datepick({beforeShowDay: $.datepick.noWeekends, showOn: 'focus', yearRange: '-0:+1', minDate:'+1', dateFormat: 'dd/mm/yy'});
            }
                       
            initLogin();

        
        };
        
        
        var initLogin = function() {


            PDX.setFocusBlur($('#loginForm INPUT'));


            // Submit login form
            var submitLogin = function() {
                if (cmp.loginForm.field('email').val() === '' || cmp.loginForm.field('email').val() === 'Email Address') {
                    return;
                }

                cmp.loginForm.submit({

                    url: '/handlers/PDX.aspx/login',

                    success: function(d) {
                    
                        if (d.type == 'Warehousing') {
                            window.location = '/warehousinghome.aspx';
                        } 
                        else {
                           window.location = '/collectionhome.aspx';
                        }

//                        $('.loggedOut').hide();
//                        $('.welcomeBack').html('Welcome back ' + d.name);
//                        $('.loggedIn').show();
//                        $('.loggedIn').addClass("user" + d.type);

                    },
                    failure: function(d) {

                        $('#loginError').html(d.errors[0].message);
                    }

                });

            };


            cmp.loginForm = new TVI.Form({

                ID: 'loginForm',
                buttons: [{

                    selector: '.login',
                    enter: true,
                    handler: submitLogin

                }]

            });

                TVI.event('.logOut', 'click', function() {
                    TVI.ajax({
                        url: '/handlers/pdx.aspx/logout',
                        data: {},
                        success: function(d) {
                            $('.loggedOut').show();
                            $('.welcomeBack').html('');
                            $('.loggedIn').hide();
                            if ($('.secure').length > 0) {
                                window.location = '/';
                            }
                        }
                    });
                });


            };
        
        

        
        
        /* Public */
        
        return {
        
            /* Properties */
        
            test: 'test',
            
        
            /* Methods */
        
            init: function(){
            
                /* Constructor */
                
                cmp = this;
            
                //wait for jQuery to load and initialise object
                TVI.ready(init);
            
            },
            
             setFocusBlur: function(inputs) {

                    // Function to set focus-blur on textboxes
                    inputs.each(function() {

                        // For password boxes remove the background on focus and restore it on blur if its empty
                        if ($(this).attr('type') === 'password') {

                            $(this).focus(function() {
                                if ($(this).val() === '') {
                                    $(this).parent().data('background', $(this).parent().css('background-image'));
                                    $(this).parent().css('background-image', $(this).parent().data('background').replace("-password",""));
                                }
                            });
                            $(this).blur(function() {
                                if ($(this).val() === '') {
                                    $(this).parent().css('background-image', $(this).parent().data('background'));
                                }
                            });
                        }
                        else {
                            // For normal
                            $(this).data('original', $(this).val());

                            $(this).focus(function() {
                                if ($(this).val() === $(this).data('original')) {
                                    $(this).val('');
                                }
                            });

                            $(this).blur(function() {
                                if ($(this).val() === '') {
                                    $(this).val($(this).data('original'));
                                }
                            });
                        }
                    });

                }
        
        }
    
    };
    
    var m = new main();
    m.init();
    
    return m;

}();
