﻿
ASI = function() {

    /* Private */

    /* Properties */

    var cmp = {};



    /* Methods */

    var init = function() {

        /* Constructor */

        TVI.Forms.handlerURL = '/Handlers/'; //DEVTEMP
        TVI.Data.defaultURL = '/Handlers/Data.aspx/query'; //DEVTEMP

        // Set up the map object
        ASI.map = {}

        // TOP NAV    
        $('#topNav .navItem').hover(
            function() {
            // Show dropdown and highlight menu item
            $(this).addClass('over').find('.dropdown').show();
        },

            function() {
            // Hide dropdown and remove highlight
            $(this).removeClass('over').find('.dropdown').hide();
        });

        $('#topNav .dropdown').hover(
            function() {
            // Show dropdown
            $(this).show();
            $(this).parents('.navItem').addClass('over');
        },

            function() {
            // Hide dropdown
            $(this).hide();
            $(this).parents('.navItem').removeClass('over');
        });


        // CONTACT FORM DROPDOWN
        $('#topNav .contactButton').click(function() {

            if (!$('#contactDropdown').hasClass('open')) {
                $('#contactDropdown').slideDown(function() {

                    if (GBrowserIsCompatible()) {

                        // Create and centre the map
                        ASI.map = new GMap2(document.getElementById("map"));
                        ASI.map.setCenter(new GLatLng(53.318111, -2.097356), 14);
                        ASI.map.addControl(new GSmallMapControl());

                        //Add marker
                        tvi_marker = new GMarker(new GLatLng(53.318111, -2.097356));
                        ASI.map.addOverlay(tvi_marker);

                    }
                
                }).addClass('open');
                $('#topNav .contactButton').addClass('selected');
            }
            return false;
        });

        $('#contactDropdown .close').click(function() {

            if ($('#contactDropdown').hasClass('open')) {
                $('#contactDropdown').slideUp().removeClass('open');
                $('#topNav .contactButton').removeClass('selected')
            }
            return false;
        });




        // LEFT MENU DROPDOWNS
        $('#leftMenu .category .categoryTitle A').click(function() {

            if ($(this).parents('.category').hasClass('selected')) {

                $(this).parents('.category').find('.subCategoryList').slideUp(function() {
                    $(this).parents('.category').removeClass('selected');
                });

            } else if ($(this).parents('.category').hasClass('open')) {

                $(this).parents('.category').find('.subCategoryList').slideUp(function() {
                    $(this).parents('.category').removeClass('open');
                });

            } else {

                $(this).parents('.category').addClass('open').find('.subCategoryList').slideDown();

            }

            return false;
        });


        // SUBSECTION DROPDOWN
        $('.subsection .subsectionTitle').click(function() {

            if ($(this).next('.subsectionContent').hasClass('open')) {
                $(this).css({ backgroundPosition: '0 0' }).next('.subsectionContent').slideUp().removeClass('open');

            } else {
                $(this).css({ backgroundPosition: '0 -30px' }).next('.subsectionContent').slideDown().addClass('open');
            }

            return false;
        });


        // Watermark for the newsletter signup
        $('#newsletterSignupForm-email-control').focus(function() {
            if ($(this).val() == 'Enter Email Address') {
                $(this).val('');
            }
        });
        $('#newsletterSignupForm-email-control').blur(function() {
            if ($(this).val() == '') {
                $(this).val('Enter Email Address');
            }
        });


        // Create the newsletter signup form
        ASI.newsletterSignupForm = new TVI.Form({

            ID: 'newsletterSignupForm',
            buttons: [{

                selector: '.newsletterSignup_button',
                enter: true,
                handler: function() {

                    // Check to see if email address is empty
                    if (ASI.newsletterSignupForm.field('email').val() === '') {
                        return;
                    }

                    // Add the email to the database
                    ASI.newsletterSignupForm.submit({

                        query: 'insertNewsletterEmail',
                        success: function() {

                            $('#newsletterSignupFormFields').hide();
                            $('#newsletterSignupFormThanks').show();
                        }

                    });

                }


            }]
        });
        
        
        // Create the Contact Form
        ASI.contactForm = new TVI.Form({
            ID: 'contactForm',
            errorsEl: '.errors',
            buttons: [{

                selector: '.sendMessage',
                enter: true,
                handler: function() {

                    // Validate the input
                    ASI.contactForm.validate({

                        success: contactFormSend,

                        failure: function(d) {
                            ASI.contactForm.error(d.errors);
                        }

                    });

                }
                

            }]
            
        });


    };


    var contactFormSend = function(){
    
        // Hide the form
        ASI.contactForm.el.find('.fields').hide();
        ASI.contactForm.el.find('.errors').hide();
        $('.sendMessage').hide();
        
        // Submit the form to the handler
        ASI.contactForm.submit({
            
            url: '/Handlers/ASI.Contact.aspx/sendContactForm',//DEVTEMP
            success: function(){
                ASI.contactForm.el.find('.thankYou').show();
            }

        });
    }



    /* Public */

    TVI.apply(cmp, {

        /* Properties */

        test: 'test',

        /* Methods */

        test: function() {

            /* Test function  */

        }

    });


    TVI.ready(init);


    return cmp;


} ();
