$(document).ready(setup);
function setup()
{
    $('#top-nav:first').children().hover(
    function(){
        var link = $('a:first', this);
        var over = $.Event('mouseover');
        over.stopPropagation();
        var out = $.Event('mouseout');
        out.stopPropagation();
        $('ul', this).css('left', '-10px').hover(
            function(){
                link.trigger(over);
            },
            function(){
                link.trigger(out);
            }
        );
        
        if($(this).hasClass('first')){
            $('ul', this).css('left', '15px');
        }
    },
    function(){
        $('ul', this).css('left', '-999em');
    }
    );

    $('#search').hide();

    $('#search-link').click(function(){
        if($('#search').is(':hidden')){
            $('#search').show();
            $('#searchTerm').focus();
        }
        else{
            $('#search').hide();
        }

        return false;
    });

    $('#appointment').hide();

    $('#appointment .close').click(function(){
        $('#appointment').animate({
            height: 0
        }, function(){
            $(this).hide();
            newsanim = 0;
        });
        return false;
    });

    var offset = $('#appointment-link img').offset();
    $('#appointment').css('bottom', 720 - offset.top);

    var appanim = 0;

    $('#appointment-link').click(function(){
        if($('#newsletter').height() != 0){
            $('#newsletter').animate({
                height: 0
            }, function(){
                $(this).hide();
                newsanim = 0;
            });
        }
        if(appanim == 0){
            if($('#appointment').is(':hidden')){
                $('#appointment .content').show();
                $('#appointment .thankyou').hide();
                appanim = 1;
                $('#appointment').animate({
                    height: 300
                }, function(){
                    appanim = 0;
                });
            }
            else{
                appanim = 1;
                $('#appointment').animate({
                    height: 0
                }, function(){
                    $(this).hide();
                    appanim = 0;
                });
            }
        }
        return false;
    });

    $('#appointment .thankyou').hide();

    $('#appointment-form').validate({
        rules: {
            name: 'required',
            dob: 'required',
            phone: 'required',
            insurance: 'required',
            email: 'required'
        },
        messages: {
            name: ' *',
            dob: ' *',
            phone: ' *',
            insurance: ' *',
            email: ' *'
        },
        submitHandler: function(form) {
            $('#appointment-form').ajaxSubmit();
            $('#appointment .content').hide();
            $('#appointment .thankyou').show();
            $('#appointment-form').each(function(){
                this.reset();
            });
            $('#appointment').animate({opacity: 1.0}, 3000).animate({
                height: 0
            }, function(){
                $(this).hide();
                appanim = 0;
            });
            return false;
        }
    });

    $('#newsletter').hide();

    $('#newsletter .close').click(function(){
        $('#newsletter').animate({
            height: 0
        }, function(){
            $(this).hide();
            newsanim = 0;
        });
        return false;
    });

    var offset = $('#newsletter-link img').offset();

    $('#newsletter').css('bottom', 720 - offset.top);

    var newsanim = 0;

    $('#newsletter-link').click(function(){
        if($('#appointment').height() != 0){
            $('#appointment').animate({
                height: 0
            }, function(){
                $(this).hide();
                appanim = 0;
            });
        }
        if(newsanim == 0){
            if($('#newsletter').is(':hidden')){
                $('#newsletter .content').show();
                $('#newsletter .thankyou').hide();
                newsanim = 1;
                $('#newsletter').animate({
                    height: 300
                }, function(){
                    newsanim = 0;
                });
            }
            else{
                newsanim = 1;
                $('#newsletter').animate({
                    height: 0
                }, function(){
                    $(this).hide();
                    newsanim = 0;
                });
            }
        }
        return false;
    });

    $('#newsletter .thankyou').hide();

    $('#newsletter-form').validate({
        rules: {
            name: 'required',
            address: 'required',
            city: 'required',
            email: 'required',
            'news[]': 'required'
        },
        messages: {
            name: ' *',
            address: ' *',
            city: ' *',
            email: ' *',
            'news[]': ' *'
        },
        submitHandler: function(form) {
            $('#newsletter-form').ajaxSubmit();
            $('#newsletter .content').hide();
            $('#newsletter .thankyou').show();
            $('#newsletter-form').each(function(){
                this.reset();
            });
            $('#newsletter').animate({opacity: 1.0}, 3000).animate({
                height: 0
            }, function(){
                $(this).hide();
                newsanim = 0;
            });
            return false;
        }
    });
};