$('document').ready(
  
  function(){
  
    var h264support = false;
    if (Modernizr.video.h264){ h264support = true;}  
        
    $('ul#slip').css(
      {
        'width':$('ul#slip').children('li').length * 926    
      }    
    );
    
    $('body#home ul#slip').reorder();

    $('img.center').click(
      function(){
      
        
        $(this).siblings('.vimeo').fadeIn();
        $(this).fadeOut();
        $('div.viewport').animate({height: '430px'});
        
        
        if ($(this).siblings('object').length == 1){
          $(this).siblings('object').show();
      
        }else {

          var vid = $(this).siblings('.vimeo').attr('id');
          vimeoPlayer.setup(vid);
        
        }

        
      }
    );    
        
    $('a.left,a.right').one('click',function(){ slide($(this)); });
    
    if ($('body.inside').length > 0){


      $('body.inside div.reels ul li a').not('p.meta a').click(
      
        function(){
          var indexClicked = $('div.reels ul li a').not('p.meta a').index($(this)) * -1;
  
  
          var xpos = parseInt($('.viewport ul#slip').css('left')),
              unit = $('.viewport ul#slip li').width(),
              kids = $('.viewport ul#slip').children('li').length,
              end = kids * unit * -1 + unit;
          $('.vimeo').fadeOut();
          $('.vimeo').siblings().show();
          $('div.viewport').animate({height: '255px'});      
          $('.viewport ul#slip').animate({left: unit * indexClicked});        
  
          var moog = vimeoPlayer.getHandle($('object:visible').attr('id'));
          if (moog){
            moog.api_pause();
            $('object').hide();
            $('img.center:hidden').show();        
          }
  
          $(window).scrollTop(0);
  
  
          return false;
        }
      
      );


    
    }    
  
  
  }
  
);


function slide(x){

  var xpos = parseInt($('.viewport ul#slip').css('left')),
      unit = $('.viewport ul#slip li').width(),
      kids = $('.viewport ul#slip').children('li').length,
      end = kids * unit * -1 + unit;
  $('.vimeo').fadeOut();
  $('.vimeo').siblings().show();
  $('div.viewport').animate({height: '255px'});      

  if (x.attr('class') == 'left'){

    if (xpos != 0){
      $('.viewport ul#slip').stop().animate({left: xpos + unit},function(){  $('a.left').one('click',function(){ slide($(this)); }); });        
    }else {
      $('.viewport ul#slip').stop().animate({left: end},function(){  $('a.left').one('click',function(){ slide($(this)); }); });
    }  
  
  }else if (x.attr('class') == 'right'){

    if (xpos != end){
      $('.viewport ul#slip').animate({left: xpos - unit},function(){  $('a.right').one('click',function(){ slide($(this)); }); });        
    }else {
      $('.viewport ul#slip').animate({left: 0},function(){  $('a.right').one('click',function(){ slide($(this)); }); });
    }        
  
  }
        
  var moog = vimeoPlayer.getHandle($('object:visible').attr('id'));
  if (moog){
    moog.api_pause();
    $('object').hide();
    $('img.center:hidden').show();        
  }
  
}

function manualMove(indexClicked){
        var xpos = parseInt($('.viewport ul#slip').css('left')),
            unit = $('.viewport ul#slip li').width(),
            kids = $('.viewport ul#slip').children('li').length,
            end = kids * unit * -1 + unit;
        $('.vimeo').fadeOut();
        $('.vimeo').siblings().show();
        $('div.viewport').animate({height: '255px'});      
        $('.viewport ul#slip').animate({left: unit * indexClicked});        

        var moog = vimeoPlayer.getHandle($('object:visible').attr('id'));
        if (moog){
          moog.api_pause();
          $('object').hide();
          $('img.center:hidden').show();        
        }

        $(window).scrollTop(0);
}


var moogaloop;

vimeoPlayer = {
    
    setup: function(video_id){
  		var swf_id = video_id,
      		new_id = swf_id.substr(1);
  	 	var flashvars = {
  	        clip_id: new_id,
  	        show_portrait: 1,
  	        show_byline: 1,
  	        show_title: 1,
  			js_api: 1, // required in order to use the Javascript API
  			js_onLoad: 'vimeo_player_loaded',  			
  			js_swf_id: 'moogaloop' // this will be passed into all event methods so you can keep track of multiple moogaloops (optional)
  	    };
  		var params = {
  			allowscriptaccess: 'always',
  			allowfullscreen: 'true'
  		};
  		var attributes = {};
  			
  		// For more SWFObject documentation visit: http://code.google.com/p/swfobject/wiki/documentation
  		//swfobject.embedSWF("http://vimeo.com/moogaloop.swf", swf_id, "924", "389", "9.0.0","expressInstall.swf", flashvars, params, attributes);
    },
    
    getHandle: function(id){
  		return document.getElementById(id);      
    }    
}

function vimeo_player_loaded(){
  vimeoPlayer.listen();
}

$.fn.reorder = function() {
 
  // random array sort from
  // http://javascript.about.com/library/blsort2.htm
  function randOrd() { return(Math.round(Math.random())-0.5); }
 
  return($(this).each(function() {
    var $this = $(this);
    var $children = $this.children();
    var childCount = $children.length;
 
    if (childCount > 1) {
      $children.remove();
 
      var indices = new Array();
      for (i=0;i<childCount;i++) { indices[indices.length] = i; }
      indices = indices.sort(randOrd);
      $.each(indices,function(j,k) { $this.append($children.eq(k)); });
 
    }
  }));
}
