var duration = 750;
var ease = 'easeInOutQuad';
var curPanel = null;



$(function(){
    // SET UP REEL SCROLLING
    var newShootWidth = 0;
    $("#shoot_reel_wrap").children().each(function(){
	newShootWidth += ($(this).width() + parseInt($(this).css("margin-right")) + parseInt($(this).css("margin-left")));
	//console.log(newShootWidth);
    });
    $("#shoot_reel_wrap").css("width", newShootWidth);
    shoot_reel_scroll = new iScroll('shoot_reel');
    
    var newEditWidth = 0;
    $("#edit_reel_wrap").children().each(function(){
	newEditWidth += ($(this).width() + parseInt($(this).css("margin-right")) + parseInt($(this).css("margin-left")));
	//console.log(newEditWidth);
    });
    $("#edit_reel_wrap").css("width", newEditWidth);
    edit_reel_scroll = new iScroll('edit_reel');
    
    //SET UP REEL CLICK HANDLING
    $("#shoot_reel_wrap img, #edit_reel_wrap img").click(function(){
	playVideo($(this).attr("sspid"), $(this).attr("caption"));
    });
    
    // EXPAND COLLAPSE
    $(".section > h1").each(function(){
	var trigger = $(this);
	var content = trigger.next();
	
	content.hide();
	
	trigger.click(function(){
	    if(curPanel) $(curPanel).slideUp(duration, ease);
	    myslideToggle($(this).next());
	});
    });

});

/////////////
// FUNCTIONS
/////////////

//fix for "jumping" on slideDown via http://blog.pengoworks.com/index.cfm/2009/4/21/Fixing-jQuerys-slideDown-effect-ie-Jumpy-Animation
function myslideToggle(el, bShow, dur){
  var $el = $(el), height = $el.data("originalHeight"), visible = $el.is(":visible");
  
  // if the bShow isn't present, get the current visibility and reverse it
  if( arguments.length == 1 ) bShow = !visible;
  
  // if the current visiblilty is the same as the requested state, cancel
  if( bShow == visible ) return false;
  
  // get the original height
  if( !height ){
    // get original height
    height = $el.show().height();
    // update the height
    $el.data("originalHeight", height);
    // if the element was hidden, hide it again
    if( !visible ) $el.hide().css({height: 0});
  }

  // expand the knowledge (instead of slideDown/Up, use custom animation which applies fix)
  if( bShow ){
    $el.show().animate({height: height}, {duration: duration, easing:ease, complete: function(){curPanel = el} });
  } else {
    $el.animate({height: 0}, {duration: duration, easing:'easeInOutQuad', complete:function (){
        $el.hide();
      }
    });
  }
}
