﻿
App = function () {

    /* Private */

    /* Properties */

    var cmp = {};





    /* Defaults */

    TVI.debug = true;





    /* Methods */

    var init = function () {

        //CUFON
        Cufon.replace('h1');

        /* EXPANDABLE MENU */
        // Open / close correct UL
        $('.menu LI UL').parent().find('A:first').click(function () {

            // If sub nav not visible slide down, else slide up
            if ($(this).parent().find('UL:first').is(':hidden')) {
                $(this).addClass('selected').parent().find('UL:first').slideDown();
            } else {
                // If already open, then slide the UL up
                $(this).parent().find('.selected').next('UL').slideUp(function () {
                    $(this).parent().find('.selected').removeClass('selected');
                });
            }

            return false;
        });

        //SIDE MENU

        $('#service-' + App.ServicePageID).addClass('selected').parents('UL:first').show().parents('.service').find('A:first').addClass('selected');




        // Image reflect
        $(".reflect").reflect({
            height: 0.16,
            opacity: 0.3
        });


        // Image cycle

        var current = 0;
        var next = 1;

        function changeImage(element, numImages, transition) {

            //console.log('BEFORE current: ' + current + ' next: ' + next + ' numImages: ' + numImages);

            element.find('.reflect:eq(' + current + ')').fadeOut(transition, function () {
                element.find('.reflect:eq(' + next + ')').fadeIn(transition, function () {
                    if (current < (numImages - 2)) {
                        current++;
                        next++;
                    } else if (next == 0) {
                        current = 0;
                        next = 1;
                    } else if (current == (numImages - 2)) {
                        current = (numImages - 1);
                        next = 0;
                    }

                    //console.log('AFTER current: ' + current + ' next: ' + next + ' numImages: ' + numImages);

                });
            });

        }

        function cycle(element) {

            // Variables
            var transition = 500;
            var delay = 4000;
            var numImages = element.find('.reflect').length;

            // Show just first image to start
            element.find('.reflect').each(function () {

                $(this).hide();

            });
            element.find('.reflect:first').show();

            // Fade between each image and loop
            setInterval(function () { changeImage(element, numImages, transition); }, delay);


        }

        $(window).load(function () {
            $('.cycle').each(function () {

                cycle($(this));

            });
        });





        /* TVI-FORM */
        cmp.contactForm = new TVI.Form({

            // ID is the id given in the HTML
            ID: 'contactForm',

            // Include location of relevant handler
            url: TVI.handlers + 'App.Contact.aspx/sendContactForm'

        });


        /* TVI-BUTTON */
        TVI.Components.Button.styles.defaultStyle = {
            leftEndWidth: 6,
            leftPadding: 6,
            rightEndWidth: 6,
            rightPadding: 6,
            hoverClass: 'buttonHover',
            activeClass: 'buttonActive'
        };

        /* Initialise Components */
        TVI.Components.initialize($('BODY'));

    };


    // Top Menu
    var menu = function (name) {
        TVI.ready(function () {

            $('#nav' + name).children('a').addClass('current');

        });
    };





    /* Public */

    TVI.apply(cmp, {


        ServicePageID: 0,

        /* Methods */

        menu: function (name) {

            menu(name);

        }


    });


    TVI.ready(init);


    return cmp;


} ();
