var clase=Array('a','b','c','d');
var classe=Array('ab','bb','cb','db');
var sstart=0;
function bannerRotator(selector, scrollTime, pauseTime, nav){
  
  $(selector+" li:first").css("display", "block"); //show the first list item
  var count = $(selector+" li").size(); //get total number of list items
  if(count > 1){ //dont do anything if there is only one list item.  
	
	  if(scrollTime == null){ var scrollTime=500; } //default scroll time (length of transition)
    if(pauseTime== null){ var pauseTime=5000; } //default pause time (how long to hold the image between transitions)
    
    $(selector+" li").each(function( intIndex ){ $(this).attr('rel', (intIndex+1)); }); //add the list position to the each of the items

    if(nav != null){
      var i = 1;
      $(selector).append("<div id='bannerNav'></div>"); //create navigation buttons
      while(i <= count){
	      if(i == $(selector+" li:visible").attr('rel')){  //if its the nav item that belongs to the visible image, mark it as the active nav item
          $('#bannerNav').append("<a class='"+classe[i-1]+"' rel='"+i+"' href='#'></a> ");
		
	      } 
	      else{
	        $('#bannerNav').append("<a class='"+clase[i-1]+"'  rel='"+i+"' href='#'></a> ");
	      }
	      i++;
   	  }
     

      $("#bannerNav a").click(function () { //handle navigation by clicking nav items
          var currentClassName = $(selector+" li:visible").attr('rel');
	      var nextClassName = $(this).attr('rel');
		  if (currentClassName==1)  if (sstart==0) {$("#bannerNav a."+classe[currentClassName-1]).addClass(clase[0]); 
		  sstart=1;
		
		  }
		$("#bannerNav a."+classe[currentClassName-1]).removeClass(classe[currentClassName-1]);		
		
	      $(this).addClass(classe[nextClassName-1]); //move the active nav item to this item
	    
	      var storedTimeoutID = $("#bannerNav").attr('timeoutID');

	      clearTimeout(storedTimeoutID);//stop the images from looping when a nav button is pressed
	      
	
	      if( nextClassName != currentClassName ){ //only change images if they clicked on a new item (not the one they are viewing)
		    $(selector+" li:visible").fadeOut(scrollTime);
	        $(selector+" li[rel="+nextClassName+"]").fadeIn(scrollTime);
	      }
	      return false;
      });
    
  
    }
    
    var timeout = setTimeout(function(){ 
      scrollImages(count, selector, scrollTime, pauseTime);
    }, pauseTime);
    
    $("#bannerNav").attr('timeoutID', timeout); //save the timeout id so we can cancel the loop later if a nav button is pressed
  }
}

function scrollImages(count, selector, scrollTime, pauseTime){
  currentClass = $(selector+" li:visible").attr('rel'); //get the list position of the current image
  	  if (currentClass==1)  if (sstart==0) {$("#bannerNav a."+classe[currentClass-1]).addClass(clase[0]); 
		  sstart=1;
		
		  }
  nextClass = $(selector+" li:visible").attr('rel'); //open a new variable for the next class
  if (currentClass == count ){ nextClass=1; } //if you've reached the end of the images... start from number 1 again
  else{ nextClass++; } //if not just add one to the last number
    
  $(selector+" li[rel="+currentClass+"]").fadeOut(scrollTime); //fade out old image
  $("#bannerNav a."+classe[currentClass-1]).removeClass(classe[currentClass-1]); //remove active class from our nav
	
  $(selector+" li[rel="+nextClass+"]").fadeIn(scrollTime); //fade in new image
  $("#bannerNav a[rel="+nextClass+"]").addClass(classe[nextClass-1]); //add new active class to the next nav item
  
  var timeout = setTimeout(function(){ scrollImages(count, selector, scrollTime, pauseTime); }, pauseTime); //scroll the banners again after waiting for pauseTime  
  $("#bannerNav").attr('timeoutID', timeout); //save the timeout id so we can cancel the loop later if a nav button is pressed
}


