﻿
App = function() {

    /* Private */

    /* Properties */

    var cmp = {};





    /* Methods */

    var init = function() {

        /* Constructor */

        TVI.debug = true;

        // Rollovers for top menu
        $('#topMenu UL LI').hover(function() {
            $(this).addClass('active');
        }, function() {
            $(this).removeClass('active');
        });

        // Products Menu
        $('#topMenu .products').mouseenter(function() {
            $('#productsMenu').show();
        });
        $('#topMenu .products').mouseleave(function() {
            $('#productsMenu').hide();
        });
        $('#topMenu .products > A').click(function() {
            return false;
        });

        // News Menu
        $('#topMenu .newsRoom').mouseenter(function() {
            $('#newsMenu').show();
        });
        $('#topMenu .newsRoom').mouseleave(function() {
            $('#newsMenu').hide();
        });
        $('#topMenu .newsRoom > A').click(function() {
            return false;
        });



        // Initialise Popups
        TVI.event('.lightBox', 'click', function() {
        
            //reference button
            var btn = $(this);

            // Get the popup from the repeater
            var popup = $('<div class="popup">' + btn.parents('.item').find('.lightBoxTemplate').html() + '</div>');
            
            
            //check if button opens a video
            var video = btn.hasClass('lightBoxVideo');
            
            
            //get meta data if video
            var metaData = null;            
            
            if (video){
            
                metaData = TVI.meta(btn);
                            
                //set video player id
                popup.find('.player').attr('id', 'waspTarget' + metaData.ID);
                                                        
            }
            

            // Create the overlay and fade it in
            var overlay = $('<div class="overlay"></div>');
            
            $('body').prepend(overlay);
            
            overlay.fadeTo(0, 0.9).fadeIn(200, function() {

                // Add the popup to the DOM
                overlay.after(popup);
                
                
                //render video player if necessary
                if (video){
                                
                    mediaPlayer(metaData.ID, metaData.file, metaData.width, metaData.height);
                
                }

                // position the popup
                var offset = $(window).scrollTop() + 100;
                
                popup.css({ 'top': offset + 'px' });

                // Fade the popup in
                popup.fadeIn(200);

            });
            
        });
        
        TVI.event('.popup A', 'click', function() {
            $('.popup').fadeOut(200, function() {
                $('.popup').remove();
            });
            $('.overlay').fadeOut(200, function() {
                $('.overlay').remove();
            });
        });


        // Watermark for the newsletter signup
        $('#newsletterForm-email-control').focus(function() {
            if ($(this).val() == 'Enter Email Address...') {
                $(this).val('');
            }
        });
        $('#newsletterForm-email-control').blur(function() {
            if ($(this).val() == '') {
                $(this).val('Enter Email Address...');
            }
        });


        // Create newsletter signup form
        App.newsletterForm = new TVI.Form({
            ID: 'newsletterForm',
            buttons: [{

                selector: '.submit A',
                errorsEl: '.errors',
                enter: true,
                handler: function() {
                    
                    // Validate the input
                    App.newsletterForm.validate({

                        success: function() {
                            submitNewsletterEmail();
                        },

                        failure: function(d) {
                            var errorsEl = App.newsletterForm.el.find('.errors');
                            errorsEl.html(d.errors[0].message);
                            errorsEl.show();
                        }
                    });

                }

            }]

        });
        
        
        //cycle through banners
        cycle('#banner IMG');

    };
    
    
    
    
    
    var cycle = function(selector, speed){
    
        /* Cycles through elements */
        
        //get elements to cycle through
        var elements = $(selector);
        
        //get number of elements
        var length = elements.length;
        
        if (length <= 1){ return; }
        
        
        speed = speed || 5000;        
        
        //hide all elements
        elements.hide();
        
        //show the first element
        elements.eq(0).show();
        
        var i = 0;
        
        //fade out current element and fade in next one at the given speed
        setInterval(function(){
        
            elements.eq(i).fadeOut('fast', function(){
            
                i++;
                
                if (i >= length){ i = 0; }
                
                elements.eq(i).fadeIn('fast');
            
            });
        
        }, speed);
    
    };





    var submitNewsletterEmail = function() {

        // Submit the Form
        App.newsletterForm.submit({

            query: 'insertNewsletterEmail',
            success: function() {

                App.newsletterForm.el.find('.fields').hide();
                App.newsletterForm.el.find('.errors').hide();
                App.newsletterForm.el.find('.complete').show();

            }

        });

    }
    
    
    
    
    
    var mediaPlayer = function(id, file, width, height){
    
        /* Wimpy Media Player */
    
        writeWasp({
            r: 'MklnV0olM0FOMiUzQiU3RHA2OW1oOEpXbCUzQyUyRm1NMVJYbCUzRUlrWCU3QnZ2Q0pIOTQlMkZ3', //handyvideorecorder.com
            instanceID: id,
            waspSwf: 'i/wasp.swf',
            pageColor: '090C0F',
            f: file,
            s: '1',
            pw: width,
            ph: height || undefined,
            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 */
        
        cycle: function(selector, speed){
        
            cycle(selector, speed);
        
        },

        mediaPlayer: function(id, file, width, height) {

            mediaPlayer(id, file, width, height);

        }

    });


    TVI.ready(init);


    return cmp;


} ();