(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery)


jQuery.preLoadImages("/dev/wp-content/themes/template/images/green-camp.png","/dev/wp-content/themes/template/images/green-camp-active.png","/dev/wp-content/themes/template/images/green-camp-down.png");

function checkEmail(email) { 
    var pattern = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    var emailVal = jQuery("#" + email).val();
    return pattern.test(emailVal);
}

jQuery(document).ready(function() {
	// submenu
	jQuery('#sub').superfish({
		autoArrows:    false,    
    	dropShadows:   false,
    	delay:	0
	});

	// video slideshow
	jQuery('#videos').cycle({
		fx: 'fade',
		timeout: 5000,
		slideExpr: 'div.vid'
	});

    // community page
	if (jQuery('body').hasClass('page-id-20')) {    
	    jQuery('#case-studies').cycle({
   		     fx:       'fade',
   		     timeout:   10000,
   		     next:	   'a.case-next',
    	    prev:	   'a.case-previous'        
    	});
    	
    	//community tabs
		jQuery('#tabs').tabs();
    }
	
	
	//alternate table cells
	jQuery("#content tr:odd").addClass('odd');
	
	// add last class
	jQuery('#content .posts .post:last').addClass('last');

	// calendar listing accordian
	if (jQuery('body').hasClass('archive')) { 	
		jQuery("#calendar-listing").accordion();
	}

	// next gen nav skin
	if (jQuery('body').hasClass('page-id-14')) {  
		jQuery('.ngg-navigation a:not(.prev, .next), .ngg-navigation span').after(' . ');
		jQuery('.ngg-navigation .prev').text('Previous');
		jQuery('.ngg-navigation .next').text('Next');
	}
	
	// comment form validate
	if (jQuery('body').hasClass('single')) {
		jQuery("#author").addClass('required');
		jQuery("#email").addClass('required email');
		jQuery("#commentform").validate();	
	}



	// newsletter signup
    jQuery("#subForm input:submit").click(function() { 
      
      // First, disable the form from submitting
      jQuery('form#subForm').submit(function() { return false; });
      
      // Grab form action
      var formAction = jQuery("form#subForm").attr("action");
      
      // Hacking together id for email field
      var id = "bujkhd";
      var emailId = id + "-" + id;
      
      // Validate email address with regex
      if (!checkEmail(emailId)) {
        jQuery('div.error').slideDown('slow').animate({
            opacity: 1
          }, 5000, function() {
            jQuery('div.error').fadeOut();
         });
        
        return;
      }
      
      // Serialize form values to be submitted with POST
      var str = jQuery("form#subForm").serialize();
      
      // Add form action to end of serialized data
      // CDATA is used to avoid validation errors
      //<![CDATA[
      var serialized = str + "&action=" + formAction;
      // ]]>
      
      // Submit the form via ajax
      jQuery.ajax({
        url: "/dev/process/proxy.php",
        type: "POST",
        data: serialized,
        success: function(data){
          // Server-side validation
          if (data.search(/invalid/i) != -1) {
            alert('The email address you supplied is invalid.');
          }
          else
          {
            jQuery("#confirmation").slideDown("slow").animate({
            	opacity: 1
        	  }, 5000, function() {
         	   jQuery('#confirmation').fadeOut();
         	});  // Shows "Thanks for subscribing" div
            jQuery("#confirmation").tabIndex = -1;
            jQuery("#confirmation").focus(); // For screen reader accessibility
            // Fire off Google Analytics fake pageview
            //var pageTracker = _gat._getTracker("UA-XXXXX-X");
            //pageTracker._trackPageview("/newsletter_signup");
          }
        }
      });
    });	

});




