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

;(function($){
  $(document).ready(function(){
   // social links bounce
   $('#header .links').delegate('li.social', 'mouseenter', function(event){
     var $obj = $(this);
     if($obj.is(":not(:animated)")){
      $(this).effect("bounce", 250);
     }
    });
   // search
   $('#header .search')
    .find('.text')
     .bind("focus blur", function(event){
      var $obj = $(this), val = $obj.val();
      $obj.val((val == "search our site...") ? "" : (val || "search our site..."));
     });
   // newsletter signup
   $('ul.callouts .newsletter')
    .find('input.email')
     .bind("focus blur", function(event){
      var $obj = $(this), val = $obj.val();
      $obj.val((val == "enter your email address") ? "" : (val || "enter your email address"));
     })
     .end()
    .find('span.go')
     .bind("click", function(event){
      var $obj = $(this), $email = $obj.prev('input.email'), email = $email.val(), $parent = $obj.parent(), re = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
      $parent.find("p.registered").remove();
      if(re.test(email)){
       $.ajax({
        url: "/newsletter-signup/",
        dataType: "json",
        type: "POST",
        data: {
         email: email
        },
        error: function(xhr, status, error){
         $parent.append("<p class=\"registered error\">There was a problem saving your email address.</p>");
         console.log(status);
         console.log(error);
        },
        success: function(data){
         if(data.success == "true"){
          $parent.append("<p class=\"registered success\">Thank you for signing up for our Newsletter!</p>");
          $email.val("");
         }
         else{
          $parent.append("<p class=\"registered error\">There was a problem saving your email address.</p>");
         }
        },
        complete: function(){
         $obj.attr('disabled', false);
        }
       });
      }
      else{
       $parent.append("<p class=\"registered error\">Your email address is invalid.</p>");
      }
     });
   // date time blogs
   $('.post time').each(function(i, v){
    var $obj = $(this), text = $obj.text().split(" "), $postdate = $('<div class="postdate"></div>');
    if(text.length == 3){
     $postdate.append( "<span class=\"month\">" + text[0] + "</span><span class=\"day\">" + text[1] + "</span><span class=\"year\">" + text[2] + "</span>");
     $obj.replaceWith($postdate);
    }
   });
   // daily inspiration
   $('#inspiration').delegate("a.get", "click", function(event){
    event.preventDefault();
    var $obj = $(this), id = $obj.attr("rel"), direction = $obj.hasClass("prev") ? "prev" : "next";
    $.ajax({
    url: "/index.cfm?action=main.inspiration-get",
    dataType: "json",
    type: "POST",
    data: {
     id: id,
     direction: direction
    },
    error: function(xhr, status, error){
    },
    success: function(data){
     var $quote = $("#inspiration blockquote"), $navs = $('#inspiration a.get');
     console.log($navs);
     $quote.empty();
     $quote.append(data.quote);
     $quote.append("<cite>" + data.source + "</cite>");
     $navs.attr("rel", data.quoteid);
    },
    complete: function(){
    }
   });
   });
  });
})(jQuery);
