(function($) {
    $(function() {
        $('.haps-page .news:last').addClass('last');

        $('.field, textarea').focus(function() {
            if(this.title==this.value) {
                this.value = '';
            }
        }).blur(function(){
            if(this.value=='') {
                this.value = this.title;
            }
        });


        $('.side-navigation ul li').hover(function  () {
            $(this).find('ul.dd').stop(true,true).slideDown();
            $(this).find('a:eq(0)').addClass('hover');
        }, function  () {
            $(this).find('ul.dd').slideUp();
            $(this).find('a:eq(0)').removeClass('hover');
        });

        // Vertical centering crew member text
        $('.main.crew-page .right').each(function  () {
            var textHeight = $(this).find('p').height() ; 
            var cntHeight = 190;
            var paddingTop = parseInt( (cntHeight - textHeight)/2 );
            $(this).find('p').animate({'paddingTop' : paddingTop});
        });
     
        $('.video-box:nth-child(2n)').addClass('second');

        $('.video-box').each(function() {
            $(this).attr('data-idx', $(this).index());
        })

        // Gallery
        $('.video-box').click(function() {
        
            if ($(this).hasClass('current') == false) {
             
                var idx = $(this).index()/2;
                idx = idx.toString().length;
                if(idx>0) {
                    $(this).prev().after($(this).next());
                } else {

                }

                $('.video-cnt').hide();
                $('.video-box').css({
                    width: '343',
                    height: '190'
                });
            
                $('.image-thumb').fadeIn('fast');
                $('.video-box').removeClass('current');
              
                $(this).addClass('current');
             
                $(this).find('.image-thumb').fadeOut('fast');
                $(this).css({ width : '699'})
                $(this).animate({
                    height: '388'
                }, 500, function() {

                var idx = $(this).index()/2;
                idx = idx.toString().length;
               
                    
                    $('#container').masonry({
                        itemSelector: '.video-box',
                        columnWidth: 357,
                        isAnimated: true,
                        isFitWidth: true
                    });

                    if(idx>1) {
                        $(this).prev().after($(this).next());
                        $('#container').masonry( 'reload' );
                    } else {
                        $('#container').masonry( 'reload' );
                    }
                    setTimeout(function() {
                        $('.video-box.current').find('.video-cnt').fadeIn();
                        // Slide to current Video
                        var position_top = $('.video-box.current').offset().top;
                        $('html, body').animate({
                            scrollTop: position_top - 12 + 'px'
                        }, 400); 

                    }, 800);
                }); 
            };
        });

        // Crew Video
        $('.crew-member .left').click(function() {
            
            if ( !$(this).hasClass('current')) {
                $('.crew-video .video-holder').hide();
                $('.crew-member .left').removeClass('current');
                $(this).addClass('current');

                $(this).addClass('current');
                $('.crew-video').animate({
                    height: '0'
                }); 

                $(this).parents('.crew-member:eq(0)').next().css({ width : '699'})
                $(this).parents('.crew-member:eq(0)').next().stop(true,true).animate({
                    height: '388'
                }, 500, function() {
                    $('.crew-video .video-holder').hide();
                    $(this).parents('.crew-holder:eq(0)').find('.video-holder').show();

                    // Slide to current Video
                    var position_top = $(this).offset().top;
                    $('html, body').stop(true,true).animate({
                        scrollTop: position_top - 190 + 'px'
                    }, 400);
                });    
            }
        });

    });

  $(window).load(function(){
      resizeBg();
  });

  // Resize Background
  function resizeBg() {
      var bgImg = $('img.body-bg');
      var imgwidth = bgImg.width();
      var imgheight = bgImg.height();

      var winwidth = $(window).width();
      var winheight = $(window).height();

      var widthratio = winwidth / imgwidth;
      var heightratio = winheight / imgheight;

      var widthdiff = heightratio * imgwidth;
      var heightdiff = widthratio * imgheight;

      if(heightdiff>winheight) {
          bgImg.css({
              width: winwidth + 'px',
              height: heightdiff + 'px'
          });
      } else {
          bgImg.css({
              width: widthdiff + 'px',
              height: winheight + 'px'
          });     
      }
  } 
      

  $(window).resize(function() {
      resizeBg();
  });

})(jQuery)
