// JavaScript Document
(function($){
  $.fn.quotator = function(options){
    var container = this;
    var longQuoteLength = 175;
    var defaults = 
    {
      speed : 5000,
      json : "quotator_quotes.js"
    }
    
    var options = $.extend(defaults, options);
    
    var quotes_json = options.json;
    var quotes;
    
    $.getJSON(quotes_json, function(data){
    var quotesobject = data;
    var index = 0;
    setInterval(changeQuote, options.speed);
    
    var q = quotesobject[index].testimonial + quotesobject[index].client + quotesobject[index].area;
    if (q.length > longQuoteLength) {
    	container.addClass('long_quote');
    }
    container.html('<a href="/testimonials/#t_' + quotesobject[index].id + '">&#8220;' + quotesobject[index].testimonial + "&#8221; - " + quotesobject[index].client + "; " + quotesobject[index].area + "</a>");
    currentTestimonialId = quotesobject[index].id;

    function changeQuote(){
      container.fadeOut(function(){
      	currentTestimonialId = quotesobject[index].id;
	    var q = quotesobject[index].testimonial + quotesobject[index].client + quotesobject[index].area;
	    if (q.length > longQuoteLength) {
	    	container.addClass('long_quote');
	    }
    	else {
    		container.removeClass('long_quote');
    	}
        container.html('<a href="/testimonials/#t_' + quotesobject[index].id + '">&#8220;' + quotesobject[index].testimonial + "&#8221; - " + quotesobject[index].client + "; " + quotesobject[index].area + "</a>").fadeIn();
      });
      
      if(index == quotesobject.length - 1){
        index = 0;
      } else{
        index++;
      }
    }
      
  });
  return container;
}
})(jQuery);
