/* AUTO HEIGHT */
(function($) {
  $.fn.autoHeight = function(type) {
    return this.each(function(index, elm) {

      var parent_sibl_height = 0;
      var margins = 0;

      $(this).parent().siblings().each(function() {
        parent_sibl_height += $(this).height();

        /* ADDING TOP MARGIN TO HEIGHT */
        margins = $(this).css("margin-top");
        margins = margins.replace('px', '');

        if (margins != 'auto') {
          margins = parseInt(margins);

          parent_sibl_height += margins;
        }

        /* ADDING BOTTOM MARGIN TO HEIGHT */
        margins = $(this).css("margin-bottom");
        margins = margins.replace('px', '');

        if (margins != 'auto') {
          margins = parseInt(margins);

          parent_sibl_height += margins;
        }

      });

      $(this).parent().css("height", ($(window).height() - parent_sibl_height));

      var sibl_height = 0;
      var parent_height = $(this).parent().height();
      var margins = 0;

      $(elm).siblings().each(function() {
        sibl_height += $(this).height();

        /* ADDING TOP MARGIN TO HEIGHT */
        margins = $(this).css("margin-top");
        margins = margins.replace('px', '');

        if (margins != 'auto') {
          margins = parseInt(margins);

          sibl_height += margins;
        }

        /* ADDING BOTTOM MARGIN TO HEIGHT */
        margins = $(this).css("margin-bottom");
        margins = margins.replace('px', '');

        if (margins != 'auto') {
          margins = parseInt(margins);

          sibl_height += margins;
        }

        if (type == 'scroll') {
          $(elm).parent().css("min-height", ($(window).height() + $(window).scrollTop()) + 'px');
        }

      });

      $(this).css("min-height", (parent_height - sibl_height) + 'px');

    });
  }
})(jQuery);

$(document).ready(function() {
  $(".auto-height").autoHeight();
});

$(window).scroll(function() {
  $(".auto-height").autoHeight('scroll');
});

$(window).resize(function() {
  $(".auto-height").autoHeight();
});





/* A TARGET */
(function($) {
  $.fn.target = function() {
    return this.each(function(index, elm) {
      if (substr($(this).attr("class"), 0, 7) == 'target-') {
        $(this).attr("target", substr($(this).attr("class"), 7));
      }
    });
  }
})(jQuery);

$(document).ready(function() {
  $("a").target();

  $("div.language").children("div").children("img").bind("click", function() {
    var id = $(this).attr("id");

    var values = {
      sl_id: id
    }

    $.POST(window.location.href, values);
  });

  $(":input[name='search_query']").bind("keyup", function(evt) {
    if(evt.keyCode == 13) {
      $("div.button").children("img").trigger("click");
    }
  });


  $("div.button").children("img").bind("click", function() {
    if ($(":input[name='search_query']").val().length > 0) {
      var query = $(":input[name='search_query']").val();

      window.location.href = '/search/'+ query +'/';
    }
  });

  $("a#print").bind("click", function() {
    window.print();
  });
});