// JavaScript Document
(function(jQuery){
  jQuery.fn.quotator = function(options){
    var container = this;
    var defaults = 
    {
      speed : 30000,
      json : "quotator_quotes.js"
    }
    
    var options = jQuery.extend(defaults, options);
    
    var quotes_json = options.json;
    var quotes;
    
    jQuery.getJSON(quotes_json, function(data){
    var quotesobject = eval(data.quotes);
    var index = 0;
	index = Math.floor(quotesobject.length*Math.random());
    var new_index = 0;
    
    setInterval(changeQuote, options.speed);
    
    container.html("<p>&ldquo;" + quotesobject[index].quote + "&rdquo;</p><div id='author'>" + quotesobject[index].author + "</div>");
    
    
    function changeQuote(){
      container.fadeOut(function(){
        container.html("<p>&ldquo;" + quotesobject[index].quote + "&rdquo;</p><div id='author'>" + quotesobject[index].author + "</div>").fadeIn();
      });
      
	 //random rotation     
	 new_index = Math.floor(quotesobject.length*Math.random());
	 if (index == new_index) {
		if (new_index == 0) {
			index = new_index +1;
			}
		if (new_index == quotesobject.length) {
			index = new_index - 1;
			}
		}
	 else {
		index = new_index;
		}
      
      //loop in order
      //if(index == quotesobject.length - 1){
      //  index = 0;
      //} else{
      //  index++;
      //}
    }
      
  });
  return container;
}
})(jQuery);