(function($){

    jQuery.extend( jQuery.easing,
    {
        customElastic: function (x, t, b, c, d) {
            var s=1.70158;
            var p=0;
            var a=c;
            if (t==0) return b;
            if ((t/=d)==1) return b+c;
            if (!p) p=d*.8;
            if (a < Math.abs(c)) { a=c; var s=p/4; }
            else var s = p/(2*Math.PI) * Math.asin (c/a);
            return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
        }
    });

    $(document).ready(function() {
        $('#site-search').click(function(){
            if($('#search-box').hasClass('expanded')){
                $('#search-box').animate({
                    top: '-200px'
                }).removeClass('expanded');
            }
            else {
                $('#search-box').animate({
                    top: '20px'
                }, 2500, 'customElastic').addClass('expanded');
                $('#searchTerm').focus();
            }
            return false;
        });

        $('#search-box .close').click(function(){
            $('#search-box').animate({
                top: '-200px'
            }).removeClass('expanded');
            return false;
        });

        var agencyNav = $('#nav-our-agency');
        var portfolioNav = $('#nav-portfolio');
        var servicesNav = $('#nav-services');

        var anim = 0;

        $('#our-agency').click(function(){
            $('#main-nav li').removeClass('dot');
            if(anim == 0){
                if(agencyNav.width() == 0){
                    $(this).parent().addClass('dot');
                    anim = 1;
                    if(portfolioNav.width() != 0){
                        portfolioNav.animate({
                            width: 0
                        }, function(){
                            $(this).hide();
                        });
                    }
                    if(servicesNav.width() != 0){
                        servicesNav.animate({
                            width: 0
                        }, function(){
                            $(this).hide();
                        });
                    }
                    agencyNav.animate({
                        width: 130
                    }, function(){
                        anim = 0;
                    });
                }
                else{
                    if($('body').hasClass('agency')){
                        $('#main-nav li').removeClass('dot');
                        $('#our-agency').parent().addClass('dot');
                        anim = 1;
                        agencyNav.animate({
                            width: 130
                        }, function(){
                            anim = 0;
                        });
                    }
                    else{
                        $('#main-nav li.current').addClass('dot');
                        anim = 1;
                        agencyNav.animate({
                            width: 0
                        }, function(){
                            $(this).hide();
                            anim = 0;
                        });

                        if($('body').hasClass('portfolio')){
                            $('#main-nav li').removeClass('dot');
                            $('#portfolio').parent().addClass('dot');
                            anim = 1;
                            portfolioNav.animate({
                                width: 130
                            }, function(){
                                anim = 0;
                            });
                        }
                        if($('body').hasClass('services')){
                            $('#main-nav li').removeClass('dot');
                            $('#services').parent().addClass('dot');
                            anim = 1;
                            servicesNav.animate({
                                width: 130
                            }, function(){
                                anim = 0;
                            });
                        }
                    }

                }
            }
            return false;
        });

        $('#services').click(function(){
            $('#main-nav li').removeClass('dot');
            if(anim == 0){
                if(servicesNav.width() == 0){
                    $(this).parent().addClass('dot');
                    anim = 1;
                    if(portfolioNav.width() != 0){
                        portfolioNav.animate({
                            width: 0
                        }, function(){
                            $(this).hide();
                        });
                    }
                    if(agencyNav.width() != 0){
                        agencyNav.animate({
                            width: 0
                        }, function(){
                            $(this).hide();
                        });
                    }
                    servicesNav.animate({
                        width: 130
                    }, function(){
                        anim = 0;
                    });
                }
                else{
                    if($('body').hasClass('services')){
                        $('#main-nav li').removeClass('dot');
                        $('#services').parent().addClass('dot');
                        anim = 1;
                        servicesNav.animate({
                            width: 130
                        }, function(){
                            anim = 0;
                        });
                    }
                    else{
                        $('#main-nav li.current').addClass('dot');
                        anim = 1;
                        servicesNav.animate({
                            width: 0
                        }, function(){
                            $(this).hide();
                            anim = 0;
                        });

                        if($('body').hasClass('portfolio')){
                            $('#main-nav li').removeClass('dot');
                            $('#portfolio').parent().addClass('dot');
                            anim = 1;
                            portfolioNav.animate({
                                width: 130
                            }, function(){
                                anim = 0;
                            });
                        }

                        if($('body').hasClass('agency')){
                            $('#main-nav li').removeClass('dot');
                            $('#our-agency').parent().addClass('dot');

                            anim = 1;
                            agencyNav.animate({
                                width: 130
                            }, function(){
                                anim = 0;
                            });
                        }
                    }
                }
            }
            return false;
        });

        $('#portfolio').click(function(){
            $('#main-nav li').removeClass('dot');
            if(anim == 0){
                if(portfolioNav.width() == 0){
                    $(this).parent().addClass('dot');
                    anim = 1;
                    if(agencyNav.width() != 0){
                        agencyNav.animate({
                            width: 0
                        }, function(){
                            $(this).hide();
                        });
                    }
                    if(servicesNav.width() != 0){
                        servicesNav.animate({
                            width: 0
                        }, function(){
                            $(this).hide();
                        });
                    }
                    portfolioNav.animate({
                        width: 130
                    }, function(){
                        anim = 0;
                    });
                }
                else{
                    if($('body').hasClass('portfolio')){
                        $('#main-nav li').removeClass('dot');
                        $('#portfolio').parent().addClass('dot');
                        anim = 1;
                        portfolioNav.animate({
                            width: 130
                        }, function(){
                            anim = 0;
                        });
                    }
                    else{
                        $('#main-nav li.current').addClass('dot');
                        anim = 1;
                        portfolioNav.animate({
                            width: 0
                        }, function(){
                            $(this).hide();
                            anim = 0;
                        });

                        if($('body').hasClass('services')){
                            $('#main-nav li').removeClass('dot');
                            $('#services').parent().addClass('dot');
                            anim = 1;
                            servicesNav.animate({
                                width: 130
                            }, function(){
                                anim = 0;
                            });
                        }
                        if($('body').hasClass('agency')){
                            $('#main-nav li').removeClass('dot');
                            $('#our-agency').parent().addClass('dot');
                            anim = 1;
                            agencyNav.animate({
                                width: 130
                            }, function(){
                                anim = 0;
                            });
                        }
                    }
                }
            }
            return false;
        });

        if($('body').hasClass('agency')){
            $('#nav-our-agency').show().width('130px');
        }

        if($('body').hasClass('services')){
            $('#nav-services').show().width('130px');
        }

        if($('body').hasClass('portfolio')){
			$('#nav-portfolio').show().width('130px');
        }


        if ($('#content').height() > $('#navigation').height()){
            $('#navigation').height($('#content').height());
            $('.sub-navigation').height($('#content').height());
        }
        else if ($('#content').height() < $('#navigation').height() && $('#content').height() > 735){
            $('#navigation').height($('#content').height());
            $('.sub-navigation').height($('#content').height());
        }
        else if ($('#content').height() <= 735){
            $('#navigation').height(735);
            $('.sub-navigation').height(735);
        }

        $('#quote-tab').click(function(){
            $('.quote-form form').show();
            $('.quote-form .thankyou').hide();
            if($('#quotepopup').hasClass('expanded')){
                $('#quotepopup').animate({
                    right: '26px'
                }).removeClass('expanded');
            }
            else {
                $('#quotepopup').animate({
                    right: '704px'
                }, 2500, 'customElastic').addClass('expanded');
            }
            return false;
        });

        $('.quote-form .close').click(function(){
            $('#quotepopup').animate({
                right: '26px'
            }).removeClass('expanded');
            return false;
        });

        $('.quote-form form').validate({
            submitHandler: function(form) {
                $('.quote-form form').ajaxSubmit();
                $('.quote-form form').hide();
                $('.quote-form .thankyou').show();
                $('.quote-form form').each(function(){
                    this.reset();
                });
                $('#quotepopup').animate({opacity: 1.0}, 3000).animate({
                    right: '26px'
                }).removeClass('expanded');
                return false;
            }
        });

        $('#contact-us').click(function(){
            if($('#contact-popup').hasClass('expanded')){
                $('#contact-popup').css('left', '-999em').removeClass('expanded');
            }
            else {
                $('#contact-popup').css('left', '215px').addClass('expanded');
            }
            return false;
        });

        $('#contact-popup .close').click(function(){
            $('#contact-popup').css('left', '-999em').removeClass('expanded');
            return false;
        });

        $('#messenger').click(function(){
            if($('#messenger-login-popup').hasClass('expanded')){
                $('#messenger-login-popup').animate({left: '-205px'}, 1000, null, function(){$(this).hide();}).removeClass('expanded');
            }
            else {
                $('#messenger-login-popup').show().animate({left: '175px'}, 2000, 'customElastic').addClass('expanded');
            }
            return false;
        });

        $('#client-login').click(function(){
            if($('#messenger-login-popup').hasClass('expanded')){
                $('#messenger-login-popup').animate({left: '-205px'}, 1000, null, function(){$(this).hide();}).removeClass('expanded');
            }
            else {
                $('#messenger-login-popup').show().animate({left: '175px'}, 2000, 'customElastic').addClass('expanded');
            }
            return false;
        });

        $('#messenger-login-popup .close').click(function(){
            $('#messenger-login-popup').animate({left: '-205px'}, 1000, null, function(){$(this).hide();}).removeClass('expanded');
            return false;
        });

        var navDist = $('#navigation').offset().left;

        $('#wrapper').after('<div id="messenger-hider" style="z-index: 9; position: absolute; left: 0; top: 410px; height: 220px; width: ' + navDist + 'px; background: #fff;"></div>');

        $(window).resize(function(){
            navDist = $('#navigation').offset().left;
            
            $('#messenger-hider').width(navDist);
        });

        Cufon.replace('h3');

    });
})(jQuery);