// Global helper functions for enhanced page display

$(document).ready(function() {

  // adds class of 'active' to navigation link relating to the current page - relies on class of body tag matching id of link
  $("ul[class *= 'nav'] a").each(function() { if ($(this).attr('name') == $('body').attr('id') && $('body').attr('id') != '') { $(this).addClass('active') }; })

  /* ==================================================================================================================================================*/
  // Applies the pngFix plugin to the document if it exists
  if (typeof ($(document).pngFix()) == 'function' && $.browser.msie && $.browser.version < 7) { $(document).pngFix(); $('a').css({ position: 'relative', zIndex: 1000 }); }
  if (typeof ($('.tabs').tabs()) == 'function') { $('.tabs').tabs(); }
  /* ==================================================================================================================================================*/
  // Adds dividers to the .net generated navigation
  $('.navbar li').each(function(i) { if (i != 0) { $(this).prepend("<span class='divider'>&nbsp;</span>") } })

  $('.price-wrapper').each(function() { if ($(this).find('.was').length > 0) { $(this).addClass('rrp') } })
  // safari specific
  if ($.browser.safari) {
    $('div.col-wrapper').each(function(i) {
      $(this).children('div.col').each(function(j) {
        if (j != 0) { $(this).children('div.inner').css({ position: 'relative', left: '10px' }) };
      });
    });
    $('.searchbox ul li a').css({ width: '102px' });
  }
});


$(document).ready(function() {
  $('table.carlist tr').each(function() {
    if ($(this).children().length == 0) {
      $(this).remove();
    } else {
      var tdcount = $('td', $(this)).length;
      var loopcount = ($('td', $(this)).length < 4) ? 4 - tdcount : 0;
      for (var i = 0; i < loopcount; i++) {
        $(this).append("<td class='item'></td>");
      }
    }
  });
  var maxHeight = 0;
  $('table.carlist tr div.height-fix').each(function(i) { if ($(this).height() > maxHeight) maxHeight = $(this).height(); });
  $('table.carlist tr div.height-fix').each(function(i) { $(this).height(maxHeight); $(this).next('a').css({ position: 'relative' }); });
  $('img', $('td.item')).css({ opacity: "0.9" })
  $('td.item').hover(rollOverHandler, rollOutHandler);


});
function rollOverHandler(evt) {
  $("img", this).fadeTo(200, 1);
  $(this).css({ backgroundColor: "#FFFFFF" });
  $('h3', $(this)).css({ color: "#435888" });
  $('p.price a span', $(this)).css({ color: "#C7493C" });
};

function rollOutHandler(evt) {
  $("img", this).fadeTo(200, 0.9);
  $(this).css({ backgroundColor: "#FEFEFE" });
  $('h3', $(this)).css({ color: "#213866" });
  $('p.price a span', $(this)).css({ color: "#A5271A" });
  $('p.price a span.from', $(this)).css({ color: "#666" });
};