$(document).ready(function() {
  // inject the modal dialog waiting animation (spinner)
  $('#dialog').html('<img src="/img/accents/busy.gif" alt="loading..." />');
  $('#dialog-2').html('<img src="/img/accents/busy.gif" alt="loading..." />');
  
  // give the modal window nice rounded corners
  $('.jqmWindow').css({
    'border-radius':'12px',
    '-moz-border-radius':'12px',
    '-webkit-border-radius':'12px'
  });
  
  /**
   * Since we're hijacking the click function of the top-level links
   * we need to add an "overview" link to the beginning of each of 
   * the sub-menus
   */
  /*$('#overview ul li ul').each(function() {
    $thisURL = $(this).parent().find('a').attr('href');// grab the top-level link's URL
    $(this).prepend('<li><a href="' + $thisURL + '">Overview</a></li>');// generate a link and append to the list
  });*/
  
  // show/hide the sub-pages menus
  $('#overview ul li.page_item ul').hide();
  
  // when navigate to a child page show all pages
  $('#overview ul li.current_page_item').parent('ul').show();
  
  // show page list when toggle
  $('#overview ul li.active ul').show();
  
  // show current page's child page
  $('#overview ul li.current_page_item ul').show();
  
  $('#overview ul li').click(function() { 
    //alert($(this).children('ul'));
    //if($(this).children('ul').is(":contains('Overview')")){
      $(this).addClass('active');
      //$(this).children('ul').slideToggle('slow');
      //return false;
    //}
  });
  
  // set up the thank you HTML snip -- no sense crufting-up the HTML with needless junk
  $thanks_html = '<!-- thanks-container -->'+"\n\r"+
      '<div id="thanks-container" style="display: none;">'+"\n\r"+
        '<h2>Thank You for Subscribing!</h2>'+"\n\r"+
        '<p>Look for our latest newsletter real soon.</p>'+"\n\r"+
      '</div>'+"\n\r"+
      '<!-- /thanks-container -->'+"\n\r";
  
  $form_id           = 'form#form-newsletter';// The form ID
	$form_container    = '#form-container';// The form container ID
	$thanks_container  = '#thanks-container';// The thank you container ID
  
  // inject our newsletter Thank You into the page
  $('#callout-newsletter').append($thanks_html);
  
  // AJAX Email Newsletter Sign-Up Form
  $(function() {
  	
  	// Hook into the form submission
  	$($form_id + " :submit").click(function() {	
  		
  		// First, disable the form submission
  		$($form_id).submit(function() { return false; });
  		
  		// Grab our form's action
  		formAction = $($form_id).attr("action");
  		
  		// Piece together an id for the email field
  		emailId = formAction.replace("http://cpic.createsend.com/t/r/s/", "");
  		emailId = emailId.replace("/", "");
  		emailId = emailId + "-" + emailId;
  		
  		// Validate our email address
  		if (!checkEmail(emailId)) {
  			alert("Please enter a valid email address");
  			return;
  		}
  		
  		// Serialize form values to be submitted with POST
  		var str = $($form_id).serialize();
  		
  		// Add form action to end of serialized data
  		final = str + "&action=" + formAction;
  		
  		// Submit the form via ajax
  		$.ajax({
  			url: "/inc/proxy.php",
  			type: "POST",
  			data: final,
  			success: function(html){
  				$($form_container).hide(); // If successfully submitted let's hide the form
  				$($thanks_container).fadeIn("slow");  // And show a thank you message
  			}
  		});
  	});
  });
  
  // check email address
  function checkEmail(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);
  }
  
});
