var flagrot = function (divid, delay, imagePref){
   // make us globally visible
   FLAGROT = this;
   this.flags = new Array();
   this.index = -1;
   this.divid = divid
   this.imagePref = '/';
   this.delay = 3*1000;
   
   if (typeof delay != 'undefined'){
      this.delay = delay*1000;
   }
   
   if (typeof imagePref != 'undefined'){
      this.imagePref = imagePref;
   }
   
   this.timer = setInterval ( "FLAGROT.rotate();", this.delay );
   
   this.rotate = function(){
      elements = $(FLAGROT.divid+" img");
      
      // Handle boundry conditions by wrapping around
      var newidx = FLAGROT.index + 1;
      if (newidx > elements.length - 1) newidx = 0;
      if (newidx < 0) newidx = elements.length - 1;
      
      elements
         .eq(FLAGROT.index).fadeOut('slow', function(){
         //console.log('done');
         elements
            .eq(newidx)
            .fadeIn('slow');
      });
      FLAGROT.index = newidx;
   }
   
   this.addFlag = function(image){
      // We really don't need to keep the flags array
      this.flags.push(image);
      $(this.divid).append($('<img src="'+this.imagePref+image+'">').hide());
      
      if (this.index < 0) $(this.divid+" img").eq(0).show();
      this.index = 0;
   }
}
