﻿
$.fn.delay = function(time, callback) {
    return this.animate({ opacity: '+=0' }, time, callback);
}

App = function() {

    /* Private */

    /* Properties */

    var cmp = {};





    /* Methods */

    var init = function() {

        /* Constructor */

        TVI.Forms.handlerURL = '/handlers/'; 
        TVI.Data.defaultURL = '/handlers/Data.aspx/query'; 

        // Focus/blur textboxes
        setFocusBlur($('INPUT'));


        // HOMEPAGE SLIDESHOW
        $('.homepageSlideshowContainer').delay(5000).fadeIn(function() {
            slideshow();
        });


        // 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;
        });

        // Watermarks
        $('.watermark').each(function() {
            $(this).val($(this).attr('title'));
        });
        $('.watermark').focus(function() {
            if ($(this).val() == $(this).attr('title')) {
                $(this).val('');
            }
        });
        $('.watermark').blur(function() {
            if ($(this).val() == '') {
                $(this).val($(this).attr('title'));
            }
        });


        // Login form
        App.loggedOutForm = new TVI.Form({
            ID: 'loggedOutForm',
            errorsEl: '.errors',
            handler: function() {

                // Validate the input
                App.loggedOutForm.validate({

                    success: logIn,

                    failure: function(d) {
                        App.loggedOutForm.error(d.errors);
                    }

                });

            }

        });

        // Create newsletter signup form
        App.newsletterForm = new TVI.Form({
            ID: 'newsletterForm',
            buttons: [{

                selector: '.newsletterGo',
                enter: true,
                handler: function() {
                    submitNewsletterEmail();
                }

            }]

        });



    };





    var logIn = function() {

        // Hide the form
        $('#accountLoggedOut').hide();

        // Submit the form to the handler
        App.loggedOutForm.submit({

            url: '/handlers/App.Account.aspx/logIn', 
            success: function(d) {
                $('.welcomeBack STRONG').text(d.name);
                $('.loggedOut').removeClass("loggedOut").addClass("loggedIn");
            },
            failure: function(d) {
                $('#accountLoggedOut').show();
                App.loggedOutForm.error(d.errors);
            }
        });

    };





    var submitNewsletterEmail = function() {
    
        if (App.newsletterForm.field('email').val() === '' || App.newsletterForm.field('email').val() === 'Please enter your email address'){ return; }
        

        // Submit the Form
        App.newsletterForm.submit({

            query: 'insertNewsletterEmail',
            success: function() {

                App.newsletterForm.el.find('.fields').hide();
                App.newsletterForm.el.find('.complete').show();

            }

        });

    };
    
    
    
    
    
    var writeMedia = function(id, file, width, height){
    
        /* Wimpy Media Player */
    
        writeWasp({
            r: 'NCUyOWU2ZTVIa1QlN0V4N1g0V3olNDB2aDZZNlklMjUlM0E5JTNCJTNGUldnLUxFJTdDWWZZ', //wdsassociates.co.uk
            instanceID: id,
            waspSwf: 'i/wasp.swf',
            pageColor: 'F2F2EA',
            f: file,
            s: '0',
            pw: width,
            ph: height || undefined,
            startPlaying: true,
            waspSkin: 'sr_1|1^st_1|1|16||000000^sg_1|1^sb_1|4|26|F8F8F8||B8B8B8^sp_1|11|24|3D3D8F|FFFFFF|69DAFF^sm_1|11|24|3D3D8F|FFFFFF|69DAFF^sa_1|1|5^sz_1|1|5'
        });
    
    };





    /* Public */

    TVI.apply(cmp, {

        /* Properties */

        /* Methods */

        writeMedia: function(id, file, width, height) {
        
            writeMedia(id, file, width, height);            

        }

    });


    TVI.ready(init);


    return cmp;


} ();


// Slideshow
function slideshow() {

    // Slide in/out container
    $('.homepageSlideshowContainer').fadeOut(1000, function() {

        // Move first image to end of stack
        $('.homepageSlideshow IMG:first').appendTo('.homepageSlideshowContainer');

        $('.homepageSlideshowContainer').fadeIn(1000, function() {

            slideshow();

        }).delay(3000);

    });

};


// Focus/blur textboxes
function setFocusBlur(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-image', 'none');
                }
            });
            $(this).blur(function() {
                if ($(this).val() === '') {
                    $(this).css('background-image', $(this).data('background'));
                }
            });
        }
    });
};
