$(document).ready(function() {

    //Testimonials ticker
    //grab all testimonials
    $testimonials = $("#testimonials li");

    //remove any that are showing already, let JS do this
    $testimonials.removeClass("show");

    interval = setInterval(tickTestimonials, 20000);
    tickTestimonials();
    
    $links = null;

    $('#navigation').each(function() {

        //navigation links
        var $links = $('a', this),
        contentItemIds = $links.map(function() {
            return this.hash;
        }).get().join(','),
        $contentItems = $('.panel');
        $contentItemsWrapper = $contentItems.filter(':first').parent(),
        delay = 500;

        //hide the panels at the start
        $contentItems.hide();
        $contentItemsWrapper.css('height', 0);



        $links.click(function() {

            var $link = $(this);
            var $footer = $("#ft");
            link = this;

            if ($link.is('.active')) {
              //allow default behaviour
              return true;
            };

            //hide footer during the transition
            $footer.hide();

            $links.animate({
                paddingTop: "0"
            },
            delay,
            function() {
                $(this).removeClass('active');
                $link.addClass('active');
            }
            );

            $(this).animate({
                paddingTop: "52px"
            },
            delay);

            $contentItemsWrapper.stop().animate({
                height: "0"
            },
            delay,
            //chain aimations
            function() {
                var height = $contentItems.hide().filter(link.hash).show().outerHeight();
                $contentItemsWrapper.animate({
                    height: height
                },
                delay * 2,
                function() {
                    $footer.fadeIn();
                }
                );
            }

            );
        });
        
        //Any link classed as a navigation link should trigger the navigation animation
        $('a.navigation-link').click(function(){
          //strip off the hash
          $links.filter('[hash='+this.hash+']').click();
        });

        var showtab = window.location.hash ? '[hash=' + window.location.hash + ']': ':first';
        $links.filter(showtab).click();
        
        



        //Logo hover effect
        $('#logo')
        .removeClass('csshover')
        .find('a')
        .append('<span class="jshover"></span>')
        .each(function() {
            //cache the new span
            var $span = $('> span.jshover', this).css('opacity', 0);
            $(this).hover(function() {
                //on hover
                $span.stop().fadeTo("slow", 1);
            },
            function() {
                //off hover
                $span.stop().fadeTo("slow", 0);
            })
        });
    });

    //vcard hover effect
    $('a#vcard')
    .removeClass('csshover')
    .append('<span class="jshover"></span>')
    .each(function() {
        //cache the new span
        var $span2 = $('> span.jshover', this).css('opacity', 0);
        $(this).hover(function() {
            //on hover
            $span2.stop().fadeTo("fast", 1);
        },
        function() {
            //off hover
            $span2.stop().fadeTo("normal", 0);
        })
    });


    function tickTestimonials() {
        //find current showing
        current = $testimonials.filter('.show');

        //if none are showing, pick a random one
        if (!current.length) {
            r = Math.ceil(Math.random() * $testimonials.length);
            current = $testimonials.parent().children(':nth-child(' + r + ')');
        };

        //find the next
        next = current.next()

        if (!next.length) {
            next = $testimonials.filter(":first");
        }

        //fade out the current one
        current.stop().hide().removeClass('show');

        //fade in the next
        next.fadeIn("slow").addClass("show");

    }

});
