/**
 * Custom effects for the collapse/expand for articles on the 
 * apfoodingredients home page.
 *
 * @author Ben Barber (bbarber@i2rd.com)
 * @require cms-collapsiblecontainer.js, jquery.js
 */
(function($){
  
  // override the visiblity check
  cms.Collapsible.prototype.ensureVisible = function(){};
  
  // Show/Hide function to do shrinking/fading.
  jQuery.extend(cms.Collapsible.prototype, {
    showElement: function(el) {
      var target = $(el.parentNode);
      $(el).find('img').fadeIn("medium");
      $(el).slideDown("medium", updateArrow);
    },
    hideElement: function(el) {
      $('#bp_slider .eg').hide();
      $(el).find('img').fadeOut("medium");
      $(el).slideUp("medium");
    }
  });

  // Function to reposition the arrow.
  var updateArrow = function() {
    // Artificial lag to make sure the slideUp effect is finished.
    window.setTimeout(function() {
      var arrowHeight = 72;
      var ccHeight = $('div.collapsible.shown').height();
      var ccOffset = $('div.collapsible.shown').position().top;
      var compensation = 2; // for borders
      var offset = ccOffset + (ccHeight/2) - (arrowHeight/2) + compensation;
      $('#bp_slider .eg').css({top:offset});
      $('#bp_slider .eg').show();
    }, 25);
  };

  // Add "arrow" images inline, since we can't add them in the article header.
  $(function(){
    var src = (document.getElementById('ie6only')) ? "/resources/dyn/files/155919ze1d1a379/_fn/slider_arrow.gif" : "/resources/dyn/files/157139z77fd58c8/_fn/slider_arrow.png"; 
    $("#bp_slider div.collapsible-control .article_heading").append(' <img src="'+src+'" />');
    $("#bp_slider div.collapsible-control").css({backgroundImage: 'none'});
  });

})(jQuery);

