// JavaScript Document
(function($){
  $.fn.quotator = function(options){
    var container = this;
    var defaults = 
    {
      speed : 5000,
      json : "/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 index = 0;
    var newImage = document.createElement('img');
	newImage.src = '/skin/frontend/htcc/default/images/star.png';
	var blank = document.createElement('img');
	blank.src = '/skin/frontend/htcc/default/images/star_blank.png';
    
    setInterval(changeQuote, options.speed);
    
    //container.html(quotesobject[index].date + "<div class='date'>" + quotesobject[index].quote + "<div class='author'>" + quotesobject[index].author + "</div>");
    //showStars(quotesobject[index].rating);
	jQuery(container).html("<div class='date'>" + quotesobject[index].date + "</div>");
	jQuery(container).html(jQuery(container).html()	+ "<div class='quote'>" + quotesobject[index].quote + "</div><br>");
	showStars(quotesobject[index].rating);
	jQuery(container).html(jQuery(container).html() + "<br>" + "<div class='author'>" + quotesobject[index].author + "</div>");
    
    function changeQuote(){
      jQuery(container).fadeOut(function(){
        jQuery(container).html(quotesobject[index].date + "<div class='date'>" + quotesobject[index].quote + "<div class='author'>" + quotesobject[index].author + "</div>").fadeIn();
		showStars(quotesobject[index].rating);
		jQuery(container).html("<div class='date'>" + quotesobject[index].date + "</div>");
		jQuery(container).html(jQuery(container).html()	+ "<div class='quote'>" + quotesobject[index].quote + "</div><br>");
		showStars(quotesobject[index].rating);
		jQuery(container).html(jQuery(container).html() + "<br>" + "<div class='author'>" + quotesobject[index].author + "</div>");
	  });
      
      if(index == quotesobject.length - 1){
        index = 0;
      } else{
        index++;
      }
    }
	
	function showStars(numstars)
	{
		var i=1;
		for (i=1;i<=5;i++)
		{
			if (i<=numstars) {
				jQuery(container).html(container.html() + "<img src='/skin/frontend/htcc/default/images/star.png'>");
			}else {
				jQuery(container).html(container.html() + "<img src='/skin/frontend/htcc/default/images/star_blank.png'>");
			}
		}
	}
      
  });
  return container;
}
})(jQuery);
