$(document).ready(function() {
    colors = new Array();
    colors['green'] = '#98d916';
    colors['orange-red'] = '#fa4c0c';
    colors['yellow'] = '#ffd000';
    colors['turquoise'] = '#62d5d9';
    colors['purple'] = '#993d8a';    

    //navigation lavalamp
    $('.navigation li').not('.navigation li.current').hover(
        function() {
            var target = $(this);
            currentNavItem.find('a').css('color', '#666');
            $('.navigation .bubble').animate({
                width: target.outerWidth(),
                left: target.position().left,
                backgroundColor: colors[$(this).attr('data-color')]
            }, {
                duration: 250,
                easing: 'easeOutQuad',
                queue: false
            });
        },
        function() {
            currentNavItem.find('a').css('color', '#fff');            
            $('.navigation .bubble').animate({
                width: currentNavItem.outerWidth(),
                left: currentNavItem.position().left,
                backgroundColor: colors[currentNavItem.attr('data-color')]
            }, {
                duration: 250,
                easing: 'easeOutQuad',
                queue: false
            });

        }
    );

    $('.intro a').hover(
        function() {
            $(this).find('.clone').remove();
            $(this).find('h4').children().css('top', '0px');
            var h4Clone = $(this).find('h4').children().clone().addClass('clone');
            $(this).find('h4').append(h4Clone);
            $(this).find('h4').children().animate({
                top: '-30px'
            }, 'fast', 'easeOutQuad');
        },
        function() {            
        }       
    )

    //show school picker when links with href=#school-picker are clicked
    $('a[href=#school-picker]').click(function() {
        $('#school-picker').fadeIn('fast');
        return false;
    })

    //hide the school picker when the close button or the overlay background is clicked
    $('.overlay .pop-up .close, .overlay .background').click(function() {
        $(this).closest('.overlay').fadeOut('fast');
        return false;
    })

    //on pages with a sign up form
    if ($('form.sign-up').length) {
        var slideHeight = $('form.sign-up > ul > li:nth-child(3)').outerHeight();
        
        $('form.sign-up > ul').cycle({
            fx: 'scrollHorz',            
            timeout: 0,
            prev: 'form.sign-up .prev-next-panel a.prev',
            next: 'form.sign-up .prev-next-panel a.next',
            speed: 1000,
            easing: 'easeOutCubic',
            height: slideHeight + 'px'
        });


        $('form.sign-up .prev-next-panel a.prev, form.sign-up .prev-next-panel a.next').click(function() {
            return false;
        })
        
        $('form.sign-up .teachers.grid + .add-more').click(function() {
            var teacherCount = $(this).prev().children('li').length - 1;            
            for (var i = 0; i < 2; i++) {
                $(this).prev().append('<li><input type="text" name="teacher-name-'+ (i + teacherCount) +'" id="teacher-name-'+ (i + teacherCount) +'" /><input type="text" name="teacher-email-'+ (i + teacherCount) +'" id="teacher-email-'+ (i + teacherCount) +'" /></li>');
            }
            $(this).prev().prev().val($(this).prev().children('li').length - 1);
            
            $(this).parent().parent().css('height', 'auto');
            var newHeight = $('form.sign-up > ul > li:nth-child(3)').outerHeight();
            $(this).parent().parent().css('height', newHeight + 'px');
            $('form.sign-up > ul').css('height',  newHeight + 'px');

            return false;
        });

        $('form.sign-up .students.grid + .add-more').click(function() {
            var studentCount = $(this).prev().children('li').length - 1;            
            for (var i = 1; i <= 5; i++) {
                $(this).prev().append('<li><input type="text" name="student-name-'+ (i + studentCount) +'" id="student-name-'+ (i + studentCount) +'" /><input type="text" name="parent-1-email-'+ (i + studentCount) +'" id="parent-1-email-'+ (i + studentCount) +'" /><input type="text" name="parent-2-email-'+ (i + studentCount) +'" id="parent-2-email-'+ (i + studentCount) +'" /></li>');
            }
            $(this).prev().prev().val($(this).prev().children('li').length - 1);

            $(this).parent().parent().css('height', 'auto');
            var newHeight = $('form.sign-up > ul > li:nth-child(3)').outerHeight();
            $(this).parent().parent().css('height', newHeight + 'px');
            $('form.sign-up > ul').css('height',  newHeight + 'px');

            return false;
        });
    }
})

$(window).load(function() {
    currentNavItem = $('.navigation').find('li.current');    
    $('.navigation .bubble').css({
        width: currentNavItem.outerWidth(),
        left: currentNavItem.position().left,
        backgroundColor: colors[currentNavItem.attr('data-color')]
    });
})
