﻿
App = function(){

    var main = function(){
    
        /* Private */
        
        /* Properties */
        
        var cmp = null;
        
        
        /* Methods */
        
        var init = function(){
        
            /* Constructor */
            
            // MAIN NAV DROPDOWN  
            $('#mainNav LI A').hover(
                function(){
                    // Show dropdown and highlight menu item
                    $(this).parent('LI').find('.dropdown').show();
                    $(this).parent('LI').find('.dropdown').prev().addClass('highlight');
                },
                
                function(){
                    // Hide dropdown and remove highlight
                    $(this).parent('LI').find('.dropdown').hide();
                    $(this).parent('LI').find('.dropdown').prev().removeClass('highlight');
            });
            
            $('#mainNav .dropdown').hover(
                function(){
                    // Show dropdown
                    $(this).show();
                    $(this).prev().addClass('highlight');
                },
                
                function(){
                    // Hide dropdown
                    $(this).hide();
                    $(this).prev().removeClass('highlight');
            });
            
           
            
            // FAQ
            $('.faq .question').toggle(
                
                function() {
                    
                    // Show answer and change arrow
                    $(this).next('.answer').slideDown(300);
                    $(this).css({ backgroundPosition: '10px -18px' });
                    
                },
                
                function() {
                
                    // Hide answer and change arrow
                    $(this).next('.answer').slideUp(300);
                    $(this).css({ backgroundPosition: '10px 0' });
                    
                }
                    
            );
            
            // Image rotators 
            
            $('.rotator').cycle({
                timeout: 5000
            });
            
            // Lightbox links
            $('.lightbox').lightBox();
            
            initContact();
            
            initVideo();
        
        };
        
        var initVideo = function() {
            $('.openVideo').click(function() {
                var overlay = $('<div class="overlay"></div>');
                $('body').prepend(overlay);
                $('.overlay').fadeTo(0,0.90).fadeIn(200);
                $('.overlay').css('height',300);
                $('.overlay').after('<div class="videoWrapper"><div class="videoContainer"><div class="closeVideo"><a href="#">x</a></div><iframe src="video.aspx" height="405" width="720" scrolling="no" frameborder="0"></iframe></div></div>')
                return false;
            });
            $('.closeVideo a').live('click', function() {
                $('.videoWrapper').remove();
                $('.overlay').fadeOut(200,function() {
                    $('.overlay').remove();
                });
                return false;
            });
        }
        
        
        var initContact = function(){
        
            /* Setup contact form */
        
            if ($('.contactPage .TVI-form').length === 0){ return; }
            
        
            $('.contactPage .TVI-form INPUT').keypress(function(e){
                        
                if (e.which === 13){
                
                    e.preventDefault();
                    
                    contactUs(); 
                
                }
            
            });
            $('.contactPage .TVI-form A.sendMessage').click(function(e){
            
                e.preventDefault();
                
                contactUs();
            
            });
        
        };
        
        
        var contactUs = function(){
                
            var name = $('#txt_name').val();
            var email = $('#txt_email').val();
            var subject = $('#txt_subject').val();
            var message = $('#txt_message').val();
            
            var errors = '';
            
            if (name === ''){ errors += '* Please enter your name.<br />'; }
            if (email === ''){ errors += '* Please enter your email address.<br />'; }
            if (message === ''){ errors += '* Please enter your message.<br />'; }
            
            if (errors.length > 0){ $('.contactPage .TVI-form .status').html(errors); return; }
            
            
            $('.contactPage .TVI-form .status').html('');            
            
            
            TVI.ajax({
            
                data: '{ \"d\": {' +
                '\"name\": \"' + name + '\",' +
                '\"email\": \"' + email + '\",' +
                '\"subject\": \"' + subject + '\",' +
                '\"message\": \"' + message + '\"' +
                '} }',
                
                url: '/Handlers/App.aspx/contactUs',
                    
                success: function(){
            
                    $('.contactPage .TVI-form .status').html('Thank you, your message has been sent.');      
                    
                    $('#txt_name').val('');
                    $('#txt_email').val('');
                    $('#txt_subject').val('');
                    $('#txt_message').val('');      
                                    
                },
                failure: function(){
                
                    
                
                }
            
            });
        
        };
        
        
        /* Public */
        
        return {
        
            /* Properties */
        
            test: 'test',
            
        
            /* Methods */
        
            init: function(){
            
                /* Constructor */
                
                cmp = this;
            
                //wait for jQuery to load and initialise object
                TVI.ready(init);
            
            },
            
            test: function(){
            
                /* Test function  */
            
                // Code goes here
            
            }
        
        }
    
    };
    
    var m = new main();
    m.init();
    
    return m;

}();
