// Fancy javascript and jQuery for Wordpress by Tim Ridgway, www.madeglobal.com

themepath = "http://www.hiltonlaw.co.uk/wp-content/themes/hilton_v2/";

$(document).ready(function() {
  //Fixes an animation glitch caused by the
  //div's dynamic height.  Need to set the
  //height style so the slide functions work
  //correctly.
  $("#confirmation").css("height", "30px");  
  $("#newsfailed").css("height", "30px");  
  $("#rightsidebar> div.accordion").each(function(){
    $(this).css("height", $(this).height() + "px");
    });
    
  // hide all the boxes before the page opens   
  $('#rightsidebar> div.accordion').hide();
  
  //add a class closedy to all the h2 headers
  $('#rightsidebar>h2').addClass('closedy');
  
  // animate the right sidebar headings
  $('#rightsidebar> h2').click(function() {
     $(this).toggleClass('closedy').toggleClass('openy');
     $(this).next('div.accordion').stop().slideToggle('fast', 'linear');
  });
  
  //expand all and contract all button
  $('#expandall').click(function() {
    if( $(this).hasClass('expandy') ) {
        $(this).addClass('contracty');
        $(this).removeClass('expandy');
        $(this).html("contract all");
        $(this).siblings('h2').addClass('openy').removeClass('closedy');
        $(this).siblings('div.accordion').stop().slideDown();
    } else {
      $(this).addClass('expandy');
      $(this).removeClass('contracty');
      $(this).html("expand all");
      $(this).siblings('h2').addClass('closedy').removeClass('openy');
      $(this).siblings('div.accordion').stop().slideUp(); 
    }
  });
  // text replacement in contact forms by Tim Ridgway
    function textReplacement(input){
     var originalvalue = input.val();
     input.focus( function(){
      if( $.trim(input.val()) == originalvalue ){ input.val(''); }
     });
     input.blur( function(){
      if( $.trim(input.val()) == '' ){ input.val(originalvalue); }
     });
    }
    textReplacement($('#tjihdk-tjihdk'));
    textReplacement($('#newslettername'));
    textReplacement($('#sidename'));
    textReplacement($('#sideemail'));
    textReplacement($('#sidemessage'));
  
  // make the EXP submit form AJAX enabled...
  $("#newssend").click(function() {	
			
			// First, disable the form from submitting
			$('form#subForm').submit(function() { return false; });
			
			// Grab form action
			formAction = $("form#subForm").attr("action");
			
			emailId = "tjihdk-tjihdk";
			
			// Validate email address with regex
			if (!mycheckEmail(emailId)) 
			{
				$("#newssend").val("Email Invalid - please retry").css({'background-color' : 'red'});  // Shows "Email error" div
				$("#tjihdk-tjihdk").focus(); // and focus on the email address field
        return;
			}
  		$("#newssend").val("Thanks!").css({'background-color' : 'green'}); 
      
			// Serialize form values to be submitted with POST
			var str = $("form#subForm").serialize();
			
			// Add form action to end of serialized data
			final = str + "&action=" + formAction;
			
			// Submit the form via ajax
			$.ajax({
				url: themepath+"proxy.php",
        type: "POST",
				data: final,
				success: function(html){
					$("#theForm").hide(); // If successfully submitted hides the form
          $("#confirmation").slideDown("slow");  // Shows "Thanks for subscribing" div
				}
			});
		})
    
    function mycheckEmail(email)
	  {	
		var pattern = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		var emailVal = $("#" + email).val();
		return pattern.test(emailVal);
	  }
   
});


