/*
$(document).ready(function() {
  var playerIframe = document.getElementById("vimeo_player");
  playerIframe.api("api_play");  
});
*/

var number_of_thumb_slides = 3;
var number_of_banners = 0; //gets updated to actual amount below.
var current_slide = 0;

function nextSlide(){
  next_slide = current_slide+1;
  
  if(next_slide == number_of_banners){
    next_slide = 0;
  } 
  
  $('#main-header img:visible').fadeOut(1000);
  $($('#main-header img')[next_slide]).fadeIn(1000);
  
  current_slide = next_slide;
}

$(function(){
  // Smooth scrolling to sections via the nav menu.
  $('#navigation a').bind('click', function(){
    $('html, body').animate({scrollTop: $($(this).attr('rel')).offset().top}, 1500, 'easeInOutCubic');
    return false;
  });
  
  // Smooth scroll back to the top on click of the logo.
  $('#logo').bind('click', function(){
    $('html, body').animate({scrollTop: 0}, 1500, 'easeInOutCubic');
    return false;
  });
  
  // Smooth scroll back to the top on click of the back-to-top links.
  $('#casestudies .top').bind('click', function(){
    $('html, body').animate({scrollTop: $('#casestudies').offset().top}, 1500, 'easeInOutCubic');
    return false;
  });
  
  // Set up every news overlay div for display.
  $('#news .popup[rel]').overlay({
    mask: {
      color: '#fff',
      loadSpeed: 200,
      opacity: 0.8
    }
  });
    
  // Video div replacement
  $('.video_placeholder').bind('click', function(){
    iframe = $('<iframe src="'+$(this).attr('rel')+'" width="773" height="435" frameborder="0"></iframe>');
    $(this).replaceWith(iframe);
  });


  
  //Header image rotation
  number_of_banners = $('#main-header img').length;
  $($('#main-header img')[0]).show();
  window.setInterval(nextSlide, 4000);
  
  // Enables the scrollable sections for each ID given.
  $.each(['#news', '#videos', '#about'], function(index, value){
    $(value+' .scrollable').scrollable({
      next: value+' .next', 
      prev: value+' .prev',
      circular: true
    }).navigator({
      navi: value+'-nav'
    });
  });
  
  $('#portfolio .scrollable').scrollable({
    next: '#portfolio .next',
    prev: '#portfolio .prev',
    circular: true,
    onSeek: function(e, index){
      slides = $('#portfolio .slide:not(.cloned)');
      slide = $('#portfolio .slide:not(.cloned)')[index];
      port_img = $(slide).find('.portfolio-img');
      
      if(port_img.length > 0){
        src = port_img.attr('rel');
        placeholder = port_img.find('.portfolio-placeholder');
        placeholder.replaceWith('<img src="'+src+'" />');
      }
    }
  }).navigator({
    navi: '#portfolio-nav'
  });  
  
  // This makes the portfolio images clickable, causes the scroller to advance by one.
  $('.portfolio-img').live('click', function(){
    $('#portfolio .scrollable').data('scrollable').next();
  });
  
  // Case study opener/closer. Checks the state and acts accordingly.
  $('.casestudy').bind('click', function(){
    el = $(this);
    full = $('#'+el.attr('id')+'-full');
    
    $('.casestudy').removeClass('active');
    $('.casestudy-full').slideUp();
    
    if(!full.is(':visible')){
      el.toggleClass('active');
      full.slideDown();
    }
  });
  
  // Need slightly different behaviour when the X is clicked.
  $('.casestudy-full .close').bind('click', function(e){
    el = $($(this).parents('.casestudy-full')[0]);
    id = '#'+el.attr('id').split('-')[0];
    
    $('.casestudy-full').slideUp(400, function(){
      $(id).removeClass('active');
    });
  });
  
});
