// Cutdown Accordion sidebar for Wordpress by Tim Ridgway, www.madeglobal.com

//simultaneous showing and hiding
$(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.
  $("#sidebar ul li> ul").each(function(){
    $(this).css("height", $(this).height() + "px");
    });
  
  // hide all the boxes before the page opens   
  $('#sidebar ul> li.accordion.hideit').hide();

  // set the initial look of the header buttons
  $('#sidebar ul li> h2').each(function(){
      if ( $(this).hasClass('open_accordion') ) {
        $(this).css( {backgroundPosition: "0 -21px"} );
      } else {
        $(this).css( {backgroundPosition: "0 0"} );      
      }
   });
  
  // animate the opening of the contents of the widgets
  $('#sidebar ul li> h2').click(function() {
     //first change their CSS classes
     if ( $(this).hasClass('open_accordion')) {
       $(this).removeClass('open_accordion');
       $(this).addClass('closed_accordion');
       $(this).css( {backgroundPosition: "0 0"} );
     } else {
       $(this).removeClass('closed_accordion');
       $(this).addClass('open_accordion');
       $(this).css( {backgroundPosition: "0 -63px"} );
       if ( $(this).siblings('h2').hasClass('open_accordion') ) {
         $(this).siblings('h2').removeClass('open_accordion');
         $(this).siblings('h2').addClass('closed_accordion');
         $(this).siblings('h2').css( {backgroundPosition: "0 0"} );       
       }
     }
     // now do the actual animation of the children boxes
     $(this).parent().next('li.accordion').stop().slideToggle('fast', 'linear')
     .siblings('li.accordion:visible').stop().slideUp('fast', 'linear');
  });
    
  // adjust the backgrounds of the headers for mouse over in each state
  $('#sidebar ul li> h2').hover(
       function(){
					if ( $(this).hasClass('closed_accordion') ) {
            $(this).css( {backgroundPosition: "0 -42px"} );
          } else {
            $(this).css( {backgroundPosition: "0 -63px"} );
          }
        },function() {
					if ( $(this).hasClass('closed_accordion') ) {
            $(this).css( {backgroundPosition: "0 0"} );
          } else {
            $(this).css( {backgroundPosition: "0 -21px"} );
          }          
        });
  
  // text replacement in WPCF7 contact form 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($('#contname'));
    textReplacement($('#contemail'));
    textReplacement($('#contcompany'));
    textReplacement($('#contposition'));
    textReplacement($('#contnumber'));
    textReplacement($('#contmessage'));
    textReplacement($('#contradio'));
    textReplacement($('#newslettername'));
    textReplacement($('#trktlh-trktlh')); //the email address for the newsletter

  // 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 = "trktlh-trktlh";
			
			// Validate email address with regex
			if (!mycheckEmail(emailId)) 
			{
				$("#newsfailed").slideDown("slow");  // Shows "Email error" div
				return;
			}
			
			// 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: "http://www.jamiebakerphotography.com/wp-content/themes/jamie_baker/proxy.php",
        type: "POST",
				data: final,
				success: function(html){
					$("#theForm").hide(); // If successfully submitted hides the form
	        $("#newsfailed").slideUp("fast");  // hides error div if showing
          $("#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);
	  }
    
});




