  var rotators = new Array();
    
  function rotator(rotateInterval, elementId, domElement, rotatorType){
    this.rotateInterval = rotateInterval || 5000;
    this.fadeInInterval = 2000;
    this.fadeOutInterval = 2000;
    this.currentItem = 0;
    this.previousItem = 0;
    this.elementId = elementId;
    this.domElement = domElement;
    this.rotatorType = rotatorType;
    this.items = $('.rotatorItem', this.domElement).length;	
    if(this.domElement.hasClass('loop')){
      this.loop = true;
      }
    else{
      this.loop = false;
      }	
    }
	
  rotator.prototype.startRotating = function(){
    $('.rotatorItem', this.domElement).hide();
    if(this.rotatorType == 1){
      var theRotator = this;	  
	  $('.rotatorItem:eq(0)', this.domElement).show();	
	  var tester = setTimeout(function(){theRotator.rotateItems();}, theRotator.rotateInterval);
	  }
	else{
	  var randNum = Math.floor(Math.random()*this.items)
	  $('.rotatorItem:eq(' + randNum + ')', this.domElement).show();	
	  }
	}
	
  rotator.prototype.rotateItems = function(){
    this.currentItem = (this.previousItem + 1) % this.items;
	$('.rotatorItem:eq(' + this.previousItem + ')', this.domElement).fadeOut(this.fadeOutInterval);
	$('.rotatorItem:eq(' + this.currentItem + ')', this.domElement).fadeIn(this.fadeInInterval);	
	this.previousItem = this.currentItem;
	var theRotator = this;
	if(!(!this.loop && this.currentItem == this.items - 1)){
	  var tester = setTimeout(function(){theRotator.rotateItems();}, theRotator.rotateInterval);
	  }
	}
  
  $(document).ready(function(){
    getRotators();
	for(var i=0; i<rotators.length; i++){
	  rotators[i].startRotating();
	  }	
    });	
	
  function getRotators(){
    $('.rotator').each(function(){
	  rotators.push(new rotator($(this).attr('title'), $(this).attr('id'), $(this), 1));
	  $(this).css('position', 'relative');
	  $(this).find('.rotatorItem').each(function(){
	    $(this).css('position', 'absolute').css('left', '0').css('top', '0');
	    });
	  });
	$('.rotatorRefresh').each(function(){
	  rotators.push(new rotator($(this).attr('title'), $(this).attr('id'), $(this), 2));  
	  });
    }	