$(function(){
  
  var buttons = $('input[type="button"], input[type="submit"]');
  $.each(buttons, function(i, item){
    $(item).css('background', '#E8E5FC').css('borderColor', '#D4D0EE').css('cursor', 'pointer').css('border', '1px solid #D4D0EE');
  })
  
  var submits = $('input[type="submit"]');
  $.each(submits, function(i, item){
    $(item).css('background', '#E8E5FC').css('borderColor', '#D4D0EE').css('cursor', 'pointer').css('border', '1px solid #D4D0EE');;
  })
  
  $('div.answer').hide();
  $.each($('a.hideAnswer'), function(i, item){
    $(this).before('|| ');
  })
  
  $('a.showAnswer').click(function(){
    $(this).parent('p').parent('div').next('div.answer').slideDown('fast');
  });
  
  $('a.hideAnswer').click(function(){
    $(this).parent('p').parent('div').next('div.answer').slideUp('fast');
  });
  
  $('a.buySubLink').click(function(e){
    e.preventDefault();
    $('form[name="productForm"]').submit();
  })
  
  /* Utility functions */

  var setCookie = function(){
      var text = "";
      
      $.each($(".hide-content"), function(){
        if(!$(this).is(":hidden")){
          
          text += $(this).attr('id') + ',';
        }
      })
      
      if(text != "")
      {
        text = text.slice(0, text.length-1); 
      }
      
      // ensure domain is set without www so that requests sent without subdomain are handled appropriately
      $.cookie('openMenus', text, {domain: 'tomeloveme.com'} );
  }
  
  var showMenus = function(){
    var menuText = $.cookie('openMenus');
    if(menuText != null)
    {
      var menus = menuText.split(',');
      $.each(menus, function(i, menuId){
        if(menuId.length)
        {
          $('#' + menuId).show();
        }
      })
    }
  }
  
  /* LHS Menus */
  $('.hide-content').hide();
  showMenus();
  
  $(".show-hide").click(function(e){
      e.preventDefault();
      
      var div = $(this).next('div.hide-content');
      if(div.is(":hidden")){
          // we're using setCookie as the callback function here - if we call the function once afterwards,
          // it will trigger the cookie when the div is still being animated, meaning the wrong selections are stored! Bug-tastic!
          div.slideDown('normal', setCookie);
      }else{
          div.slideUp('slow', setCookie);
      }
  })
  
  /* Search form submit */
  $('a#search').click(function(e){
      e.preventDefault();
      if($('input#search-input').val() == '')
      {
        alert('Please enter a search term and try again.');
        return false;
      }else{
        $('form[name="ds_search"]').submit();
        return true;
      }
      
  })
  
  $('a[href="#"]').click(function(e){ e.preventDefault() });
  
  /* Contact Form */
  var hiddenQuestions = $('#hiddenQuestions');
  if($('input[value="general"]').is(":checked")){
    hiddenQuestions.hide();
  }
  
  
  $('input[value="skin"]').click(function(){
    hiddenQuestions.show();
  })
  
  $('input[value="general"]').click(function(){
    hiddenQuestions.hide();
  })
  
  $('form[name="assessment"]').submit(function(e){
    $('div.errors').remove();
    var requiredFields = ['input[name="custName"]', 'input[name="email"]'];
    var errors = [];
    
    $.each(requiredFields, function(i, item){
      var val = $(item).val();
      $(item).css('border', '1px solid #CCC');
      if(val=='')
      {
        errors.push('Please make sure you have filled in the field titled "' + $(item).parent('td').siblings('td').text() + '"');
        $(item).css('border', '1px solid red');
      }
    })
    
    if(errors.length)
    {
      var html = '<div class="errors" style="display:none">';
      
      html += '<p>Please correct the following errors:</p><ul style="margin:0">';
      
      $.each(errors, function(i, item){
        html += '<li>' + item + '</li>';
      })
      html += '</ul></div>';
      
      $('form[name="assessment"]').before(html);
      $('div.errors').slideDown('slow');
      
      e.preventDefault();
      scroll(0,0);
    }
    
    return true;
  })
})
