
// remap jQuery to $
(function($){

	// Twitter
	// ------------------------------------------------------------------------------------------------	
	$("#tweetlist").getTwitter({
		userName: "propellertheatr", 
		numTweets: 3,
		loaderText: "Loading twitter feed...",
		slideIn: true,
		slideDuration: 750,
		showHeading: false,
		headingText: "Tweets",
		showProfileLink: false,
		showTimestamp: true
	});  	
	
	
	// Scrollable
	// ------------------------------------------------------------------------------------------------	
	$(".scrollable").scrollable({circular: true, speed: 500}).autoscroll({ autoplay: true, interval: 6000});
	
	// Productions Overlays
	$("#video[rel]").overlay();
	$("#gallery[rel]").overlay();
	
	// Blog view comments toggle
   $("a.toggle_comments").toggle(function(){
	 var target = $(this).attr("rel");
	 $(target).slideToggle('slow');
   },function(){
	 var target = $(this).attr("rel");	   
	 $(target).slideToggle('fast');		 
   });
   
   
	// Misc
	// ------------------------------------------------------------------------------------------------	

	/* jquery paginate */
	if($('#page_container').length >=1){
		$('#page_container').pajinate({
			items_per_page : 5
		});
	};
	
	// jQuery Scrollable
	if($('.scrollable').length >=1){
		$(".scrollable").scrollable({circular: true}).autoscroll({ autoplay: true });
	}
	
	
	// Mailer aJax 
	// ------------------------------------------------------------------------------------------------	
    $("#subForm input:image").click(function() { 
      
      // First, disable the form from submitting
      $('form#subForm').submit(function() { return false; });
      
      // Grab form action
      var formAction = $("form#subForm").attr("action");
      
      // Hacking together id for email field
      // Replace the xxxxx below:
      // If your form action were http://mysiteaddress.createsend.com/t/r/s/abcde/, then you'd enter "abcde" below
      var id = "kyykkj";
      var emailId = id + "-" + id;
      
      // Validate email address with regex
      if (!checkEmail(emailId)) {
        alert("Please enter a valid email address");
        return;
      }
      
      // Serialize form values to be submitted with POST
      var str = $("form#subForm").serialize();
      
      // Add form action to end of serialized data
      // CDATA is used to avoid validation errors
      //<![CDATA[
      var serialized = str + "&action=" + formAction;
      // ]]>
	  
	  console.log(serialized);
      
      // Submit the form via ajax
      $.ajax({
        url: "http://dev.feastcreative.com/propeller.org.uk/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 and needs to be fixed before you can subscribe to this list.');
          }
          else
          {
			//console.log("well done");
            $("#theForm").hide(); // If successfully submitted hides the form
            $("#confirmation").slideDown("slow");  // Shows "Thanks for subscribing" div
            $("#confirmation").tabIndex = -1;
            $("#confirmation").focus(); // For screen reader accessibility
            // Fire off Google Analytics fake pageview
            //var pageTracker = _gat._getTracker("UA-XXXXX-X");
            //pageTracker._trackPageview("/newsletter_signup");
          }
        },
        error : function(jqXHR, textStatus, errorThrown){
          // Server-side validation
		  //console.log("jqXHR: " + jqXHR);
		  //console.log("textStatus: " + textStatus);
		  //console.log("errorThrown: " + errorThrown);		  		  
        }		
      });
    });	
	
})(this.jQuery);


  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);
  }



// usage: log('inside coolFunc',this,arguments);
// paulirish.com/2009/log-a-lightweight-wrapper-for-consolelog/
window.log = function(){
  log.history = log.history || [];   // store logs to an array for reference
  log.history.push(arguments);
  if(this.console){
    console.log( Array.prototype.slice.call(arguments) );
  }
};



// catch all document.write() calls
(function(doc){
  var write = doc.write;
  doc.write = function(q){ 
    log('document.write(): ',arguments); 
    if (/docwriteregexwhitelist/.test(q)) write.apply(doc,arguments);  
  };
})(document);



