// $Id$
(function ($) {

$(document).ready(function() {

  // Add external_link class and open external hyperlinks in new window
	$("a").filter(function() {
		return this.hostname && this.hostname !== location.hostname;
	}).addClass("external_link").attr("target", "_blank");
  
  //add span tags to the main menu links
  $(".main-menu a").wrapInner("<span>");
  
  //add hover state to postscript blocks
  $(".zone-postscript .block").hover(
    function() { $(this).addClass("over"); },
    function() { $(this).removeClass("over"); }
  )
    .click(function(event) {
      var href = $(this).find(".block-title-link").attr("href");
      var $target = $(event.target);
      if (!$target.is(".contextual-links a")) {
        window.location = href;
        return false;
      }
    });
    
  
  //resize the slideshow height on window resize
  function slideshow_resize(textheight) {
    var imageheight = $('.view-slideshow .views-field-field-image img:visible').height();
    if ((imageheight) > 0){
      //console.log('text: ' + textheight);
      var frame = $('.views-slideshow-cycle-main-frame').css('height');
      //console.log('height: ' + frame);
      
      $('.views-slideshow-cycle-main-frame').css('height', imageheight + textheight);
      //console.log('new height: ' + $('.views-slideshow-cycle-main-frame').css('height'));
    }
  }
  
  //get the height of the slideshow text
  function slideshow_textheight() {
    var height = 0;
    $('.view-slideshow .views-field-body').each(function() {
      var $this = $(this);
      
      //get height
      var height_new = $this.height();
      
      // if height is zero, then we're dealing with a hidden element
      if (height_new == 0) {
        var copied_elem = $this.clone()
              .attr("id", false)
              .css({visibility:"hidden", display:"block", position:"absolute"});
        $("body").append(copied_elem);
        height_new = copied_elem.height();
        copied_elem.remove();
      }

      //only use the tallest text
      if (height_new > height) {
        height = height_new;
      } 
    });

    return height + 100; //add some pixels for the header tags and buttons
  }
  
  //Adjusts to slideshow frame when browser resized
  if ($('body.front').length) {
    //get the initial height of the slideshow text
    var textheight = slideshow_textheight();
    
    $(window).bind("resize load", function(){ 
      if ($('body').hasClass('responsive-layout-mobile')) {
        slideshow_resize(textheight);
      } else {
        $('.views-slideshow-cycle-main-frame').css('height', 358);
      }
    });
  }


//end $(document).ready();
});

})(jQuery); // end jquery enclosure;

