(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-school.png","/dev/wp-content/themes/template/images/green-school-active.png","/dev/wp-content/themes/template/images/green-school-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,
        	 pager:  '#nav',
			 slideExpr: 'div.slide' 
    	});
    	
    	jQuery('#nav a:not(:last)').after(' . ');
    	
    	//community tabs
		jQuery('#tabs').tabs();
		
		// community slideshow
		jQuery('#cols .left ul').cycle({
			fx: 'fade',
			timeout: 5000,
			slideExpr: 'li'
		});		
		

		jQuery('#cols .right ul').cycle({
			fx: 'fade',
			delay: 2500,
			timeout: 5000,
			slideExpr: 'li'
		});		
		
		jQuery("ul.listing li:first").addClass('first');
		
    }
    
    // community login
     
    	jQuery('p.filter a.login').toggle(
    		function () {
    		  jQuery('div.login').slideDown();
    		},
    		function () {
    		  jQuery('div.login').slideUp();
    		}
    	);
   		
   		jQuery("div.select").hover(
   		   function () {
   		     jQuery(this).animate( { height: jQuery('div.select ul').height() }, 250 ); 
   		   }, 
   		   function () {
  		     jQuery(this).animate( { height: '32px' }, 250 ); 
   		   }
   		 );
    	
	
	//alternate table cells
	jQuery("#content table:not(.calendar) 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 = "utdkjh";
      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");
          }
        }
      });
    });	

});




