﻿
FLR = function() {

    /* Private */

    /* Properties */

    var cmp = {};






    /* Methods */

    var init = function() {

        TVI.debug = false;

        TVI.Forms.handlerURL = '/handlers/';

        TVI.Data.defaultURL = '/handlers/data.aspx/query';

        if ($('.rotatingImage').length > 0) {
            // Homepage Slideshow
                $('.rotatingImage').cycle({
                    fx: 'fade',
                    speed: 2500
                });
        }




        $('#registerLoginNav .login').click(function() {

            // Show correct content
            $('#login').show();
            $('#register').hide();

            // Correct tab colouring        
            $(this).css({ backgroundPosition: '-245px -32px' });
            $('#registerLoginNav .register').css({ backgroundPosition: '0 -32px' });

            return false;

        });


        // Sectors
        $('.sector .heading A').click(function() {

            if (!$(this).parents('.sector').hasClass('open')) {

                $(this).parents('.sector').find('.content').slideDown(function() {
                    $(this).parents('.sector').addClass('open');
                });

            } else {

                $(this).parents('.sector').find('.content').slideUp(function() {
                    $(this).parents('.sector').removeClass('open');
                });

            }

            return false;

        });
        
        // Select all on multiple boxes
        $('.selectAll').click(function() {
            $(this).parents('.field').find('option').attr('selected','selected');
            $(this).parents('.field').find('select').change();
            return false;
        });

        initLogin();
        initMailinglistForm();

        initWishlist();
        initFirsts();

    };

    var initWishlist = function() {

        TVI.event('.jobsList .jobItem .title', 'click', function() {

            var that = $(this);

            TVI.ajax({

                url: '/handlers/candidates.aspx/addToWishList',
                data: {
                    'jobid': that.parent().attr('id').substring(5)
                },
                success: function(d) {
                    if (d.added) {
                        that.parent().addClass('featured');
                    }
                    else {
                        that.parent().removeClass('featured');
                    }
                },
                failure: function(d) {

                    alert("You must be logged in to add jobs to your wishlist. Please register or login above");

                }
            });

        });

    }
    
    var initFirsts = function() {
        $('.first').append('<div class="firstButton" title="Be the first to apply"></div>');
    }

    var initMailinglistForm = function() {

        // Mailing list form

        // Set focus/blur
        FLR.setFocusBlur($('#mailinglistForm INPUT'));

        cmp.mailinglistForm = new TVI.Form({

            ID: 'mailinglistForm',
            buttons: [{

                selector: '.submit',
                enter: true,
                handler: function() {

                    if (cmp.mailinglistForm.field('email').val() === '' || cmp.mailinglistForm.field('email').val() === 'Email Address...' || cmp.mailinglistForm.field('forename').val() === '' || cmp.mailinglistForm.field('forename').val() === 'Name...') {
                        $('#mailinglistForm .status').html('Please fill in your name and email address');
                        return;
                    }

                    cmp.mailinglistForm.submit({

                        url: '/handlers/candidates.aspx/addToMailinglist',

                        success: function(d) {

                            $('#mailinglistForm .status').html('Thank you, your address has been added.');

                            $('#txt_name').val('');
                            $('#txt_email').val('');

                        },
                        failure: function(d) {

                            $('#mailinglistForm .status').html(d.errors[0].message);
                        }

                    });

                }

}]

            });

        };



        var initLogin = function() {


            FLR.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/candidates.aspx/login',

                    success: function(d) {

                        $('.notLoggedIn').hide();
                        $('#candidateName').html(d.name);
                        $('.loggedIn').show();

                    },
                    failure: function(d) {

                        $('#loginError').html(d.errors[0].message);
                    }

                });

            };


            // Submit forgot password form
            var submitForgotten = function() {

                if (cmp.loginForm.field('email').val() === '' || cmp.loginForm.field('email').val() === 'Email Address') {
                    return;
                }


                cmp.loginForm.submit({

                    url: '/handlers/candidates.aspx/forgot',
                    success: function(d) {

                        $('#loginError').html("Password reminder sent");
                    },
                    failure: function(d) {

                        $('#loginError').html(d.errors[0].message);
                    }
                });

            };


            cmp.loginForm = new TVI.Form({

                ID: 'loginForm',
                buttons: [{

                    selector: '.login',
                    enter: true,
                    handler: submitLogin

                }, {

                    selector: '.forgotPassword',
                    handler: submitForgotten

}]

                });

                TVI.event('.logout', 'click', function() {
                    TVI.ajax({
                        url: '/handlers/candidates.aspx/logout',
                        data: {},
                        success: function(d) {
                            $('.notLoggedIn').show();
                            $('#candidateName').html('');
                            $('.loggedIn').hide();
                            if ($('.secure').length > 0) {
                                window.location = '/';
                            }
                        }
                    });
                });


            };



            /* Public */

            TVI.apply(cmp, {

                /* Properties */

                /* Methods */

                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).data('background', $(this).css('background-image'));
                                    $(this).css('background', '#ffffff');
                                }
                            });
                            $(this).blur(function() {
                                if ($(this).val() === '') {
                                    $(this).css('background-image', $(this).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'));
                                }
                            });
                        }
                    });

                }

            });


            TVI.ready(init);

            return cmp;

        } ();