
$(document).ready(function() {

  $(window).bind("resize", resizer);
  resizer();

  var currentinfodiv = null;
  $('div.cat_product_thumb a.info').click(function() {
    var thumbdiv = $(this).parents('div.cat_product_thumb');
    var infodiv = thumbdiv.children('div.info');
    if (currentinfodiv && !infodiv.attr('current')) {
      currentinfodiv.parent('div.cat_product_thumb').css({'zIndex': 0});
      currentinfodiv.css({'display': 'none'});
      currentinfodiv.removeAttr('current');
      currentinfodiv = null;
    }
    if (infodiv.css('display')!='block') {
      thumbdiv.css({'zIndex': 10});
      infodiv.css({'display': 'block'});
      infodiv.attr('current', true);
      currentinfodiv = infodiv;
    }
    else {
      thumbdiv.css({'zIndex': 0});
      infodiv.css({'display': 'none'});
      infodiv.removeAttr('current');
    }
    return false;
  });

  $('div.cat_product_thumb div.info a').click(function() {
    $(this).parent('div.info').css({'display': 'none'});
  });

  var i = 0;
  $('div.cat_product h2.toggle').each(function() {
    if (i) {
      $(this).next('div').css({'display': 'none'});
      $(this).prepend('<img src="/style/item-collapsed.gif" alt=""/>');
    }
    else {
      $(this).prepend('<img src="/style/item-expanded.gif" alt=""/>');
    }
    i = 1;
  });

  $('div.cat_product h2.toggle').hover(function() { $(this).addClass('hover'); }, 
                                       function() { $(this).removeClass('hover'); });

  $('div.cat_product h2.toggle').click(function() {
    if ($(this).next('div').css('display') != 'block') {
      $(this).next('div').css({'display': 'block'});
      $('img', this).attr('src', "/style/item-expanded.gif");
    }
    else {
      $(this).next('div').css({'display': 'none'});
      $('img', this).attr('src', "/style/item-collapsed.gif");
    }
  });
});

function resizer() {
  var sidebar = $('#sidebar');
  var content = $('#content');
  var tabheight = $('#sidebar_bottom').outerHeight();
  var windowheight = $(window).height() - (window.scrollMaxX>0? 17:0)
  var sidebarminheight = windowheight - sidebar.offset().top - tabheight;
  sidebar.css({paddingBottom: tabheight, minHeight: sidebarminheight});
  var matchedheight = Math.max(sidebar.outerHeight(), content.outerHeight());
  $('#body').height(matchedheight);
}
