$(document).ready(function() {
  
  // Decorate table
  $('table.product-table tr:nth-child(odd)').addClass('table-highlight');
  
  // Close accordian on load
  var $trigger = '#accordian-trigger';
  var $wrapper = '#products div.product-wrapper';
  var wrapper = $('#products div.product-wrapper');
  var trigger = $('#accordian-trigger');
 
  //hide the price list
 wrapper.hide(); 

  // Events when Booking Form tab is clicked
  trigger.live('click', function(event) {
         wrapper.slideToggle();
         trigger.toggleClass('open');
  }); 

  // Events when Booking Form tab is clicked, when form is open
  $('#accordian-trigger.active').live('click', function() {
      $('#products div.product-wrapper').animate({
          height:'0'
      },350, function() {
          $('#accordian-trigger').removeClass().addClass('closed');
          $('#products div.product-wrapper').css('display','none');
          $('#accordian-trigger').css('top', '0');
      });
      var src = $('#accordian-trigger img').attr("src").replace("accordian-close.png", "accordian-open.png");
      $('#accordian-trigger img').attr("src", src);
  });

  // Contact form submit
 $("#contact-form").submit(function(){

                $(".submit-indicator").css('display', 'inline');

                $(".indicator").css('display', 'inline'); // display indicator
                $(".success").css('display', 'none'); // make sure that divs are hidden (in case second time submitting form)
                $(".failed").css('display', 'none');
                $(".validate").css('display', 'none');

                dataString = $("#contact-form").serialize(); // get the form data in the format name=billycontent=dfjvfdkgjbfjkngbf
                
		$.ajax({
			type: "POST",
			url: "http://mobojo.co.uk/form_action.php", //posting data to this form
                            dataType : 'json', //our return type will be json
			data: dataString,
                             contentType: "application/x-www-form-urlencoded;charset=utf-8",
			success: function(data){ // on success i.e form was posted without any issues
                            setTimeout(function(){ //set time our to see indicator

                                if (data.error === true){ //if return code is true then display success
                                    $(".indicator").css('display', 'none');
                                    $(".success").css('display', 'block');
                                    $(".error").css('display', 'none');
                                    $(".notice").css('display', 'none');
                                    $(".submit-indicator").css('display', 'none');
                                }else if(data.error === 'validate'){ //else if return code is 'validate' then diaply that div
                                    $(".indicator").css('display', 'none');
                                    $(".notice").css('display', 'block');
                                     $(".error").css('display', 'none');
                                    $(".success").css('display', 'none');
                                    $(".submit-indicator").css('display', 'none');
                                }else { // else display failure message
                                    $(".indicator").css('display', 'none');
                                    $(".error").css('display', 'block');
                                     $(".success").css('display', 'none');
                                    $(".notice").css('display', 'none');
                                    $(".submit-indicator").css('display', 'none');
                                }

                            }, 800);
			},
                        // this code below will trigger if there was a network issue for instance. it will display failed.
                        error: function(XMLHttpRequest, textStatus, errorThrown){

                                $(".failed").css('display', 'inline-block');
			}
		});
		return false;
	});
    
    
    
      $('#photos').cycle();
    
});























