$(document).ready(function(){
    $('#videoThumbContainer ul li a').click(function(){
        var link = $(this);


        $('.the_video').fadeOut(function(){
            $(this).load(link.attr('href')+ ' #videoContent',function(){
                $('.the_video').fadeIn('fast',function(){
                    centerVideo();
                });
            });
				
        });
        $('.info').animate({
            'opacity':0
        },function(){
            $(this).load(link.attr('href')+ ' .info',function(){
                $('.info').animate({
                    'opacity':1
                });
            });
        });

        //add active class
        $('#videoThumbContainer ul li').removeClass('active');
        $(this).parent('li').addClass('active');

        return false;
    });

    $('.work_page .video_nav ul li a').click(function() {
        var item_class = $(this).attr('id');
        $('.video_nav ul li a').removeClass('active');
        $(this).addClass('active');

        if( item_class != '' ) {
            var the_items = $('.videos.' + item_class + '');
            var not_items = $('.videos:not(.' + item_class + ')');

            $('.videos').removeClass('visible');
            the_items.addClass('visible');
            not_items.hide();
            the_items.fadeIn('slow');
            $('#videoThumbContainer').hScrollResize();
			
        } else {
            $('.videos').fadeIn('slow',function(){});
            $('#videoThumbContainer').hScrollResize();
        }
    });

    //custom thumb slider
    $('#videoThumbContainer').hscroll({
        visibleItems:4,
        afterRender:function(){
            $('.video_nav ul li a:first').click();
        }
    });

    //client page margin fix
    $('.clientBox').each(function(){
        var index = $(this).prevAll('.clientBox').length+1;
        if(index%3==0)
            $(this).css({
                'marginRight':0
            });
    });
});


var fadeInElements = '.work li:visible, .the_video';

$(window).load(function() {
    if( typeof(fadeInElements) != 'undefined' ) {
        $(fadeInElements).each(function(n) {
            var $this = $(this);
            setTimeout(function() {
                $this.fadeIn("slow",centerVideo());
            }, n*100);
        });
    }
});
function centerVideo()
{
    $('.the_video').each(function(){
        var vObject = $(this).find('object');
        var oWidth = vObject.width();
        
        if(oWidth > 0)
            $(this).width(oWidth);
    });
}
