// IE max-width fix
// IE 6,7,8 does not work the css style 'max-width' correctly.
// required jQuery
$(document).ready(function() {
  if (typeof $.browser.msie != 'undefined') {
    //alert($.browser.version);
    var ends_with = function(str, val) {
      return val == str.substring(str.length - val.length);
    }
    var remove_px = function(str) {
      if (typeof str == 'string' && ends_with(str, 'px')) {
        str = str.substr(0, str.indexOf('px'));
      }
      return str;
    }
    var set_max_width = function(elem) {
      var max_width = elem.css('max-width');
      if (max_width && max_width != 'none') {
        var resize_img = function() {
          max_width = remove_px(max_width);
          var width = elem.width();
          width = remove_px(width);
          elem.css('max-width','none');
          if (width >= max_width) {
            elem.width(max_width+'px');
          }
        };
        elem.one('load', resize_img).each(function() {
          //IE caching tweaks http://api.jquery.com/load-event/
          if (this.complete) {
              $(this).trigger('load');
          }
        });
      }
    }
    //select target element
    $("img").each(function() {
      var img = $(this);
      set_max_width(img);
    });
    $('.news_list').bind('ajaxComplete', function() {
      $("img").each(function() {
        var img = $(this);
        set_max_width(img);
      });
    });
  }
});

