$(document).ready(function(){

  // Search Box text input default text
  $('input[type=text].default').defaultInput();
  
  // Cartbox links to View Cart
  $('#cartbox').click(function() { window.location.href='/store/cart.php?m=view'; });
  
  // Category tree widget starts out expanded
  var category_id = $('body').attr('class').split('_')[1];
  var category_tree_parent = '#categoryId_' + category_id;
  $(category_tree_parent).show();
  
  // Highlight appropriate instrument
  if($('#widgetCategories').length > 0) {
    var instrument_category = $('#widgetCategories').attr('class').split('_')[1];
    var instrument_nav_selector = '.nav_instruments .instrument_' + instrument_category;
    $(instrument_nav_selector).addClass('selected');

    // Prevent the widgetCategories from opening/closing (just link)
    $('#widgetCategories>.body>ul>li>a').click(function() { window.location.href = $(this).attr('href'); });
  }
  
  // Trim the digiShop combined product title/breadcrumb trail
  var pTitle = $('#productList h2').text().split('>').pop();
  $('#productList h2').text(pTitle);
  
  // Remove the category name from the beginning of the product name on product list
  // ie. for category "Aricore", change product name from "Aricore E String" to "E String"
  $('#productList .title a').each(function() {
    var cat_title = $.trim($('#productList h2').text());
    var r = new RegExp(cat_title, 'i');
    var new_title = $(this).text().replace(r,'');
    $(this).text(new_title);
  });
  
  // Add classes to form field containers
  $('form .form-label').add('form .form-label-required').parent('div').addClass('form_item');
  
  // Highlight whole product list row on mouseover
  // Allow to click anywhere on row
  if($('#products_table').length > 0) {
    $('#products_table td').hover(
      function() { $(this).addClass('highlight'); },
      function() { $(this).removeClass('highlight'); }
    ).click(function() {
      var prod_url = $(this).find('.title a').attr('href');
      window.location.href = prod_url;
    });
  }

  /* ::: productDetail ::: */
  
  // Highlight Quantity field on page load
  if($('#productDetail input[name=quantity]')) {
    $('#productDetail input[name=quantity]').focus();
  }

});
