(function ($, Drupal, undefined) {
  $.fn.getBgImage = function(callback) {

    // If splash doesn't have an image return
    if (typeof $(this).css('background-image') == 'undefined') {
      return;
    }
  
    // Create an image element with background url, so we can get properties
      var height = 0;
      var path = $(this).css('background-image').replace('url', '').replace('(', '').replace(')', '').replace('"', '').replace('"', '');
      var tempImg = $('<img />');
      tempImg.hide(); //hide image
      tempImg.bind('load', callback);
      $('body').append(tempImg); // add to DOM before </body>
      tempImg.attr('src', path);
      $('#tempImg').remove(); //remove from DOM
  };

  $.fn.useImageAsBackground = function(callback) {
    // For custom block that have images, use them as backgrounds.

    if ($(this).length == 0) {
      return;
    }

    var imageHeight = $(this).height();
    var imagePath = $(this).attr("src");
    $(this).parents('.block').css('background','url('+imagePath+') no-repeat left top');
    $(this).parents('.block').css('padding-top',imageHeight-60);
    $(this).remove();
  };


  $(window).load(function(){

    $(".with-splash-bg-image #splash").getBgImage(function() {
      // background image height
      var height = $(this).height();
      // get size of padding
      $padding_bottom = $(".with-splash-bg-image #splash").css("padding-bottom").replace("px", "");
      $padding_top = $(".with-splash-bg-image #splash").css("padding-top").replace("px", "");
      // subtract padding px and give height
      $(".with-splash-bg-image #splash").css('min-height', height-$padding_bottom-$padding_top);
    });

    // Give program block 100% height.
    if ($('.program-block').length > 0) {
      $('.program-block').css('min-height',$('#program').height()-$(".program-block").css("padding-bottom").replace("px", "")-$(".program-block").css("padding-top").replace("px", "")-$(".program-block").css("margin-bottom").replace("px", ""));
    }
    
    // Give custom sidebar block image background
    //$('.sidebar .block-environment-custom img').useImageAsBackground();

    // Animate Sign up block
    $('#block-custom-signup h2.block-title').click(function() {
      $('#block-custom-signup .content').toggle('slow', function() {
        // Animation complete.
      });
    });
    
    if ($('.node-type-page.splash .splash-title-summary .page-title').length > 0) {
      summary_area = $('.node-type-page .splash-title-summary').outerHeight();
      $('.splash-title-summary').prependTo('#content');
      $('.node-type-page .splash-title-summary').css('margin-top',-(summary_area));
    }

    if ($('.node-type-issue .splash-title-summary .page-title').length > 0) {
      summary_area = $('.node-type-issue .splash-title-summary').outerHeight();
      $('.splash-title-summary').prependTo('#content');
      $('.node-type-issue .splash-title-summary').css('margin-top',-(summary_area));
    }

 });
})(jQuery, Drupal);
;

