$(document).ready(function(){
  /* Navigation Scripts */
  $('div#navigation ul li ul li:has(ul) > a').append('<span style="position: absolute; right: 5px;"> >></span>'); //Add >> to nav items that contain nested lists
  
  /* Form Scripts */

  //Displays Products on Form pages
  /*
  getProductList = function(obj){
    $.ajax({
        type: "GET",
        url: 'product_list.php',
        dataType: "xml",
        success: function(xmlData){
          xmlDataSet = xmlData;
          listProducts(obj);
        }
    });
  }   
  listProducts = function(obj){
    productList = "";
    $("product",xmlDataSet).each(function(){
      productList += '<option value="' + $(this).find('id').text() + '">';
      productList += $(this).find('name').text();
      productList += '</option>';
    });
    $(obj).html(productList);
    return productList;
  }
  $('#product_list').each(function(){getProductList($(this))});*/
  switch ($.query.get('message')){
    case 'Success':
      $('div.form').before('<h3 class="alert">Thank you for contacting us!</h3>');
      $('div.form').hide();
    break;
    case 'EmailFailed':
      $('div.form').prepend('<h3 class="alert">There was an error with the form submission.</h3>');
    break;
    case 'ValidationFailed':
      $('div.form').prepend('<h3 class="alert">Please fill in all of the required fields.</h3>');
    break;
  }
  $('div.form form').validate();
  var emptyfields = $.query.get('field');
  for(i = 0; i < emptyfields.length; i++) {
    $('label[for=' + emptyfields[i] + ']').addClass('empty');
    $('label[for=' + emptyfields[i] + ']').addClass('alert');
  }
  $('.empty + input, .empty + textarea').keydown(function(){
    $(this).prev('label').removeClass('empty').removeClass('alert');
  });

  /* General Scripts */
  $('a[href$=.pdf]').attr('target', '_blank'); //Open pdf links in a new window

  $('a.email').each(function(){ //Finds links with the email class and replaces the text with the email address - helps prevent spam
      var link = $(this).html();  //Links should be formatted just like this: name AT domain DOT com
      link = link.replace(/ AT /, '@'); //Place the text for the link in the title attribute
      link = link.replace(/ DOT /, '.');
      var linktitle = $(this).attr('title');
      if (linktitle != '') {
        $(this).html(linktitle);
      } else {
        $(this).html(link);
      }
      $(this).attr('href', 'mailto:' + link);
  });
});
