$.extend((function() {
  return {
    json : function(url, func) {
      return $.get(url, {}, func, 'json');
    }
  };
})());

var quote = (function() {
  var cur_idx = 0;
  var count = 0;
  var loading = 0;

  return {
   init : function(i_cur_idx, i_count) {
     cur_idx = i_cur_idx;
     count = i_count;
     $(".testimonial a:last").live("click", quote.full);
     $("#bottom_left p.next a").live("click", quote.next);
   },
   full : function() {
     if (loading || $(".testimonial_holder:animated, .testimonial_holder *:animated").length) return;
     loading = 1;

     $.json($._ + "json/quote.php?idx=" + cur_idx, function(data) {
       var start_height = $(".testimonial:first").height();
       var end_height = $(".testimonial:first").html(data.quote).height();

       $(".testimonial:first")
       .height(start_height)
       .css({overflow:'hidden'})
       .animate({
         height : end_height + "px"
       }, function() {
         $(".testimonial:first").css({overflow : 'visible', height : 'auto'});
       });
       loading = 0;
     });
   },
   next : function() {
     if (loading || $(".testimonial_holder:animated, .testimonial_holder *:animated").length) return;
     loading = 1;

     if (++cur_idx == count) cur_idx = 0;
     $.json($._ + "json/quote.php?idx=" + cur_idx, function(data) {

       $(".testimonial_holder:first").clone().appendTo("#testimonial_slides");
       $(".testimonial:last").html(data.quote_snippet);
       $(".testimonial_author:last").html("<p>"+data.quote_by+"</p>");
       
       $(".testimonial:first")
       .css({overflow:'hidden'})
       .animate({ height : $(".testimonial:last").height()+"px" }, 100, function() {
         $(".testimonial_holder:first").animate({ "margin-left" : -$(".testimonial_holder:first").outerWidth(true) }, function() {
           $(".testimonial_holder:first").remove();
         });
       });
       loading = 0;
     });
   }
 };
})();