(function() {
    'use strict';

    // Navigation
    var navigation = (function() {
        var init = function() {
            var markUp = ['<select id="nav-select">'],
                $li,
                $a;

                markUp.push('<option value="" selected="selected">Go to...</option>');
                $('#navigation > ol > li').each(function() {
                    $li = $(this);
                    $a = $li.find('a:first');
                    markUp.push('<option value="' + $a.attr('href') + '">' + $a.text() + '</option>');
                });
                markUp.push('</select>');
                $('#navigation > ol').after(markUp.join(''));
            
                $('#nav-select').change(function() { window.location = $(this).val(); });
            };

        return {
            init: init
        };
    }()),


    // Tabs
    tabs = (function() {
        var current  = 0,
            tabs     = [],
            sections = [],

            display = function() {
                var i,
                    max = sections.length;

                for(i = 0; i < max; i += 1) {
                    if(i === current) {
                        tabs[i].addClass('active');
                        sections[i].show();
                    }
                    else {
                        tabs[i].removeClass('active');
                        sections[i].hide();
                    }
                }
            },

            init = function() {
                var $tabs = $('#tabs'),
                    $index;

                if($tabs.length > 0) {
                    $index = $tabs.find('#index');
                    $index.addClass('tabs');

                    $index.find('li a').each(function(index) {
                        var section = $(this).attr('href');

                        tabs.push($(this));
                        sections.push($(section));

                        $(this).click(function(event) {
                            event.preventDefault();
                            current = index;
                            display();
                        });
                    });

                    $tabs.find('h2').hide();
                    display();
                }
            };

        return {
            init: init
        };
    }()),

    // Interest page
    interest = (function() {
        var init = function() {
                var $interest = $('#interest');

                if($interest.length > 0) {
                    $interest.submit(function() {
                        var isValid = true,

                            $fire         = $interest.find('#fire:checkbox:checked'),
                            $oshupdate    = $interest.find('#oshu:checkbox:checked'),
                            $irish        = $interest.find('#irish:checkbox:checked'),

                            $name         = $interest.find('#name'),
                            $organisation = $interest.find('#organisation'),
                            $email        = $interest.find('#email'),
                            $telephone    = $interest.find('#telephone');

                        $interest.find('.error').removeClass('show');

                        if(!$fire.val() && !$oshupdate.val() && !$irish.val()) {
                            $interest.find('#products .error').addClass('show');
                            isValid = false;
                        }

                        if($.trim($telephone.val()).length === 0) {
                            $telephone.focus();
                            $telephone.siblings('.error').addClass('show');
                            isValid = false;
                        }

                        if($.trim($email.val()).length === 0) {
                            $email.focus();
                            $email.siblings('.error').addClass('show');
                            isValid = false;
                        }

                        if($.trim($organisation.val()).length === 0) {
                            $organisation.focus();
                            $organisation.siblings('.error').addClass('show');
                            isValid = false;
                        }

                        if($.trim($name.val()).length === 0) {
                            $name.focus();
                            $name.siblings('.error').addClass('show');
                            isValid = false;
                        }

                        if(isValid) {
                            $interest.find('#submit').attr('disabled', true);
                            return true;
                        }
                        else {
                            $interest.find('#error').addClass('show');
                            return false;
                        }
                    });
                }
            };

        return {
            init: init
        };
    }());

    $(document).ready(function() {
        navigation.init();
        tabs.init();
        interest.init();
    });

}());

