(function($) {

var galleryItems = $('#home-gallery-contents .home-gallery-item'),
    imgCount = galleryItems.size(),
    imgIndex = 0,
    nextImgIndex = imgIndex + 1,
    
    /************************************************
                MAIN OBJECT
     ***********************************************/
    teachHere = {

        ready: function() {

            teachHere.navFAQ();
            teachHere.placeholders();
            teachHere.contactForm();
            teachHere.newsletterForm();
            teachHere.buildGalleryNav();
            teachHere.startShow();       
        },
        
        // Bind nav and FAQ functionality
        navFAQ: function() {
            $('<a id="nav-clone"/>').appendTo('body');
            $('#nav ul li a, .secondary-nav ul li a').click(function(e) {
                var href = $(this).attr('href');
                $.scrollTo(href, 700);
                $('#nav-clone').attr('href', href);
                $('#nav-clone').click();
                e.preventDefault();
            });
            
            // Start all answers hidden
            $('#faq ul li span.answer').hide();
            
            // Toggle showing answers
            $('#faq ul li').toggle(
                function(e) {
                    var li = $(this);
                    li.find('span.answer').show(300);
                    li.addClass('expanded');
                },
                function(e) {
                    var li = $(this);
                    li.find('span.answer').hide(300);
                    li.removeClass('expanded');
                }
            );
            
            // Share link and share links div hover
            $('div.share-links').hide();
            $('div.share a').each(function(){
                var sh = $(this);
                sh.hover(
                    function(){
                        sh.parent('div.share').prev('div.share-links').show();
                    },
                    function(){
                        sh.parent('div.share').prev('div.share-links').hide();
                    }
               );
            });
            $('div.share-links').each(function(){
                var shl = $(this);
                shl.hover(
                    function() {
                        shl.show();
                    },
                    function() {
                        setTimeout(function(){shl.hide();}, 1500);
                    }
                );
            });
            
        },
        
        // Automagic placeholders for text inputs
        placeholders: function() {
            var inputs = $('input:text');
            inputs.each(function() {
                var input = $(this);
                var defaultText = input.val();
                input.focus(function() {
                    if(input.val() == defaultText) { input.val(''); }
                });
                input.blur(function() {
                    if($.trim(input.val()) == '') { input.val(defaultText); }
                });
            });
        },
        
        // Send contact form with AJAX
        contactForm: function() {
            $("form#contact-form").submit(function(ev) {
                var form = this;
                ev.preventDefault();
                $.ajax({  
                    type: form.method,  
                    url: form.action,  
                    data: $(form).serialize(),  
                    dataType: "json",
                    success: function(data) {  
                        if (data.status == "error") {
                            $(form).replaceWith($(data.html).find("form"))
                            contactForm();
                        } 
                        else {
                            $(form).replaceWith('');
                            $('.rt-confirmation_message').show();
                            $('.c-confirmation').show();
                        }
                    }  
                });
            });
        },
        
        // Send newsletter form with AJAX
        newsletterForm: function() {
            $("form#newsletter-form").submit(function(ev) {
                var form = this;
                ev.preventDefault();
                $.ajax({  
                    type: form.method,  
                    url: form.action,  
                    data: $(form).serialize(),  
                    dataType: "json",
                    success: function(data) {  
                        if (data.status == "error") {
                            $(form).find("input").not(':hidden').css({"border":"#FF0000 1px solid"});
                        } else {
                            $(form).replaceWith('<div id="newsletter-confirm">Your information has been submitted.</div>');
                        }
                    },
                    error: function(data) {
                      alert('Communication error: ' + data.status + '\nPlease try again later.');
                    }
                });
            });
            $("form#newsletter-form input").focus(function(ev) {
                $(this).attr("value", "");
            });
        },
        
        // Build the Gallery Hero Nav
        buildGalleryNav: function() {
            var htmlString = '';
            $('#home-gallery-contents div.home-gallery-item').each(function(index) {
                var i = index+1;
                htmlString += '<a class="gallery-nav-link" href="#'+i+'">'+i+'</a>';
            });
            $('#home-gallery-nav span').replaceWith(htmlString);
            $('#home-gallery-nav a:eq(0)').addClass('active');
        },
        
        // Blog ticker
        moveTicker: function() {
            $('#blog-ticker').scrollable().next();
        },
        
        // Splash Hero
        nextSlide: function() {

            $('div.home-gallery-item').
                eq(nextImgIndex).fadeIn(500).end().
                eq(imgIndex).fadeOut(500);
            
            $('#home-gallery-nav a').removeClass('active').
                eq(nextImgIndex).addClass('active');
            
            nextImgIndex = nextImgIndex + 1;
            imgIndex = imgIndex + 1;
            if(nextImgIndex == imgCount){
                nextImgIndex = 0;
            }
            if(imgIndex == imgCount){
                imgIndex = 0;
            }
        },
        
        // Initialize the blog ticker and Splash Hero
        startShow: function() {

            $("#blog-ticker").scrollable({
                size: 1,
                items: '.bs-snippet',
                loop: true,
                clickable: false,
                keyboard: false
            });
            
            var ticker_timer = setInterval( teachHere.moveTicker, 6000 );
            
            // Image Gallery Splash
            $('div.home-gallery-item').hide().eq(0).show();
            if(imgCount > 1)
                var gallery_timer = setInterval( teachHere.nextSlide, 8000 );
            
            // Click Handlers for ticker
            $('span.bs-next a').click(function(ev) {
                ev.preventDefault();
                $('#blog-ticker').scrollable().next();
                window.clearInterval(ticker_timer);
                ticker_timer = setInterval( teachHere.moveTicker, 6000 );
            });
            $('span.bs-prev a').click(function(ev) {
                ev.preventDefault();
                $('#blog-ticker').scrollable().prev();
                window.clearInterval(ticker_timer);
                ticker_timer = setInterval( teachHere.moveTicker, 6000 );
            });
            
            // Click handlers for gallery
            $('a.gallery-nav-link').click(function(e) {
                var nl = $(this);
                var i = nl.index();
                $('div.home-gallery-item').
                    eq(i).fadeIn(500).end().
                    eq(imgIndex).fadeOut(500);
                
                $('#home-gallery-nav a').removeClass('active').
                    eq(i).addClass('active');
                
                imgIndex = i;
                nextImgIndex = i + 1;
                if(nextImgIndex == imgCount) {
                    nextImgIndex = 0;
                }
                if(imgIndex == imgCount) {
                    imgIndex = 0;
                }
                window.clearInterval(gallery_timer);
                gallery_timer = setInterval( teachHere.nextSlide, 8000 );
            });
        }
        
    }
    
    // Start it up
    $(document).ready(teachHere.ready);

})(jQuery)