function initHovers(selector, parentClass, addClass) {
  $(selector).each(function(i, v) {
    $(this).mouseover(function(e) {
      $(this).parents(parentClass).addClass(addClass);
    });
    $(this).mouseout(function(e) {
      $(this).parents(parentClass).removeClass(addClass);
    });
	});
}

// Assign min-height to content-browse div
function setMinHeight(target, ref) {
  if ($(target)) {
    if ($(ref)) {
      if ($.browser.msie && parseInt($.browser.version) == 6) {
        $(target).css('height', $(ref).getHeight() + 'px');
      } else {
        $(target).css('minHeight', $(ref).getHeight() + 'px');
      }       
    }
  }       
}

// Initialize search controls and navigation
function initSearch() {
  $('a.showhide').toggle(function(e) {
    e.preventDefault();
    $('#' + $(this).attr('id').replace(/_showhide/, '_more')).show();
  }, function(e) {
    e.preventDefault();
    $('#' + $(this).attr('id').replace(/_showhide/, '_more')).hide();
  });
  
  $('.content-search input').change(function() {
    var sort_value = $(this).attr('value');
    var newQuery = "" + $.query.set('sort', sort_value);
    if (sort_value == 'pubdate:DESC') {
      newQuery = "" + $.query.REMOVE('sort');
    }
    window.location = location.protocol + '//' + location.host + location.pathname + newQuery;
  });
}


$(document).ready(function() {
  initHovers('a.link-hover', 'div.link-block', 'link-block-on');
  initSearch();
  $('a.figure-popup').click(function() {
    var figId = '#' + $(this).attr('rel');
    var width = parseInt($(figId).attr('width'));
    var height = parseInt($(figId).attr('height'));
    var nw = width;
    var nh = height;
    if ($(this).attr('class').match(/large/)) {
      nw = nw * 2;
      nh = nh * 2;
    }

    nw += 150;
    nh += 250;

    var options = "status=no,scrollbars=yes,menubar=no,location=no,toolbar=no,directories=no,resizable=yes,width=" + nw + ",height=" + nh;

    window.open($(this).attr('href'), "physicsimage", options);
    return false;
  });
});

