// JavaScript Document
var mOver = false;
(function($){
  $.fn.quotator = function(options){
    var container = this;
    var defaults = 
    {
      speed : 5000,
      json : "quotator_quotes.js"
    }
    
    container.mouseover(function(){
      mOver = true;
    }).mouseout(function(){
      mOver = false;
    });
    
    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;
    
    
    setInterval(changeQuote, options.speed);
    
    container.html(getQuotatorImageTag(quotesobject[index].quoteImg, quotesobject[index].author) +
        	"&#147;" + quotesobject[index].quote + "&#148;" + "<div id='author'>" + quotesobject[index].author + "</div>");

    
    function changeQuote(){
      if(mOver){
	    return;
	  }

      container.fadeOut(function(){
        container.html( getQuotatorImageTag(quotesobject[index].quoteImg, quotesobject[index].author) +
        	"&#147;" + quotesobject[index].quote + "&#148;" + "<div id='author'>" + quotesobject[index].author + "</div>").fadeIn();
      });
      
      if(index == quotesobject.length - 1){
        index = 0;
      } else{
        index++;
      }
    }
      
  });
  return container;
}
})(jQuery);

function getQuotatorImageTag(imgSrc, authorString )
{
	if ( imgSrc == '' || imgSrc == 'NOIMG' )
	{
		return '';
	}
	else
	{
		return "<img id='quoteImage' src='" + imgSrc + "' width='100' alt='" + authorString  + "' />";
	}
}