﻿
LES = function(){

    var main = function(){
    
        var cmp = null;
        
        
        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').parent('LI').addClass('highlight');
                },
                
                function(){
                    // Hide dropdown and remove highlight
                    $(this).parent('LI').find('.dropdown').hide();
                    $(this).parent('LI').find('.dropdown').parent('LI').removeClass('highlight');
            });
            
            $('#mainNav .dropdown').hover(
                function(){
                    // Show dropdown
                    $(this).show();
                    $(this).parent('LI').addClass('highlight');
                },
                
                function(){
                    // Hide dropdown
                    $(this).hide();
                    $(this).parent('LI').removeClass('highlight');
            });
            
            
            // BIG TARGET 
            
            // Latest News
            $(".latestNews .newsItem A").bigTarget({
	            clickZone : '.newsItem' // jQuery parent selector
  	        });
  	        $('.newsItem').css({'cursor': 'pointer'});
          	
  	        // Home products
            $(".homeProduct A").bigTarget({
	            clickZone : '.homeProduct' // jQuery parent selector
  	        });
  	        $('.homeProduct').css({'cursor': 'pointer'});  	
          	
          	
  	        // LIGHTBOX
            $('A.lightbox').lightBox({
            
                imageLoading:'/i/lightbox-ico-loading.gif',
                imageBtnPrev:'/i/lightbox-btn-prev.gif',
                imageBtnNext:'/i/lightbox-btn-next.gif',
                imageBtnClose:'/i/lightbox-btn-close.gif',
                imageBlank:'/i/lightbox-blank.gif'
                
            });
            
            initContact();
            
            initMailingList();
            
            initMenu();
            
            initCategory();
        
        };
        
        
        var initContact = function(){
        
            /* Setup contact form */
        
            if ($('.contactForm').length === 0){ return; }
            
        
            $('.contactForm INPUT').keypress(function(e){
                        
                if (e.which === 13){
                
                    e.preventDefault();
                    
                    contactUs(); 
                
                }
            
            });
            $('.contactForm .sendMessage A').click(function(e){
            
                e.preventDefault();
                
                contactUs();
            
            });
        
        };
        
        
        var initMailingList = function(){
        
            /* Setup mailing list form */
        
            $('#txt_mailingList').focus(function(){
            
                $(this).val('');
            
            });
            $('#txt_mailingList').blur(function(){
            
                if ($(this).val() === ''){
                
                    $(this).val('Enter your email address here');
                
                }
            
            });
            $('#txt_mailingList').keypress(function(e){
                        
                if (e.which === 13){ 
                
                    e.preventDefault();
                    
                    addToMailingList(); 
                
                }
            
            });
            $('#btn_mailingList').click(function(e){
            
                e.preventDefault();
                
                addToMailingList();
            
            });
        
        };
        
        
        var initMenu = function(){
        
            /* Setup menu */
        
            if (!cmp.menu){ return; }
            
        
            $('#mainNav LI').removeClass('selected');
        
            $('#mainNav .' + cmp.menu).parent().addClass('selected');
        
        };
        
        
        var initCategory = function(){
        
            /* Setup service category menu */
        
            if (!cmp.categoryId){ return; }
            
        
            $('#cat' + cmp.categoryId).addClass('selected');
        
        };
        
        
        var addToMailingList = function(){
                
            var email = $('#txt_mailingList').val();
            
            if (email === '' || email === 'Enter your email address here'){ return; }
            
            
            TVI.ajax({
            
                data: '{ \"email\": \"' + email + '\" }',
                
                url: '/Handlers/LES.aspx/addToMailingList',
                    
                success: function(){
            
                    //set thank you
                    $('.mailingList LABEL').html('Thank you');
                    $('#txt_mailingList').val('Enter your email address here');
                                    
                },
                failure: function(){
                
                    $('.mailingList LABEL').html('Please enter a valid email');
                
                }
            
            });
        
        };
        
        
        var contactUs = function(){
                
            var name = $('#txt_name').val();
            var company = $('#txt_company').val();
            var email = $('#txt_email').val();
            var subject = $('#ddl_subject OPTION:selected').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){ $('.contactForm .status').html(errors); return; }
            
            
            $('.contactForm .status').html('');            
            
            
            TVI.ajax({
            
                data: '{ \"d\": {' +
                '\"name\": \"' + name + '\",' +
                '\"company\": \"' + company + '\",' +
                '\"email\": \"' + email + '\",' +
                '\"subject\": \"' + subject + '\",' +
                '\"message\": \"' + message + '\"' +
                '} }',
                
                url: '/Handlers/LES.aspx/contactUs',
                    
                success: function(){
            
                    $('.contactForm .status').html('Thank you, your message has been sent.');      
                    
                    $('#txt_name').val('');
                    $('#txt_company').val('');
                    $('#txt_email').val('');
                    $('#ddl_subject OPTION:selected').val('');
                    $('#txt_message').val('');      
                                    
                },
                failure: function(){
                
                    
                
                }
            
            });
        
        };
        
        
        return {
        
            menu: null,
            categoryId: null,
        
            init: function(){
            
                cmp = this;
            
                $(init);
            
            }
        
        }
    
    };
    
    
    var m = new main();
    m.init();
    
    return m;

}();
