// JavaScript Document
(function($){
  $.fn.quotator = function(options){
    var container = this;
    var defaults = 
    {
      speed : 5000,
      json : "/zgh/public/shared/js/quotator_quotes.js"
    }
    
    var options = $.extend(defaults, options);
    
    var quotes_json = options.json;
    var quotes;
    
    $.getJSON(quotes_json, function(data){
    var quotesobject = eval(data.quotes);
    var rand=Math.floor(Math.random()*quotesobject.length);
   
    var index = rand;
    
    
    setInterval(changeQuote, options.speed);
    
    container.html("<div id='say_what'><div class='head'></div><div class='middle'>"+quotesobject[index].quote + "</div><div class='foot'></div></div><div id='author'>-" + quotesobject[index].author + "</div><div id='title'>" + quotesobject[index].title + "</div>");
    
    
    function changeQuote(){
      container.fadeOut(function(){
        container.html("<div id='say_what'><div class='head'></div><div class='middle'>"+quotesobject[index].quote + "</div><div class='foot'></div></div><div id='author'>" + quotesobject[index].author + "</div><div id='title'>" + quotesobject[index].title + "</div>").fadeIn();
      });
      
      if(index == quotesobject.length - 1){
        index = 0;
      } else{
        index++;
      }
    }
      
  });
  return container;
}
})(jQuery);
