/*
 * Pet Chat Service
 * version 0.1
 * Marshall Uy Alexander
 *
 * Assumes YUI Loader exists
 * 
 */

// Get an iframe id, create a clone, replace, cycle
DTM = YAHOO.namespace("DTM");
DTM.utility = YAHOO.namespace("DTM.utility");

(function() {
	var Y = YAHOO.util;
	DTM.utility.AdRotator = function() {
    /*
     * There may be a need to have multiple delay lengths in the future
     * (possibly per adFrame), but for now we will just use a global setting.
     */
    this.CYCLE_LENGTH = 15000;
    
		// Stop ad rotation after displaying 10 ads
		this.MAX_CYCLES = 9;

	
		// Configuration
		var self = this;
 
    /*
		 * adFrames will be a hash with frame IDs as keys and will refer to all of
		 * the current frame elements, including what rotation cycle they are on.
		 */
		this.adFrames = new Array();
		
    this.rotationStarted = false;
    
		var addAdFrame = function(id) {
			/* Do nothing if we already know about this frame. */
			var iframe = YAHOO.util.Dom.get(id);
			if (! adFrames[id] && iframe != null) {
				adFrames[id] = {
					id: id,
					currentFrame: iframe,
					rotationCount: 0
				};
			}

      if (! rotationStarted) {
        startRotation();
      }
		}
    
		var startRotation = function() {
//console.log("Starting rotation.");
      if (rotationStarted) {
//console.log("Rotation already started. Skipping redundant start.");
        return;
      }
      
			rotationStarted = true;
      
//console.log("Setting timeout for _rotationCycle in %o", CYCLE_LENGTH);
      setTimeout(DTM.utility.AdRotator._rotationCycle, CYCLE_LENGTH);
		}
    
    var doRotation = function () {
      for (var id in adFrames) {
				if (adFrames[id].rotationCount < MAX_CYCLES &&
						adFrames[id].currentFrame != null) {
	        var orig_frame = adFrames[id].currentFrame;
//console.log("Trying to refresh the frame: %o", orig_frame);

          /* We perform a rotation by:
           * 1. creating a new iframe
           * 2. setting its attributes to be the same as the original
           * 3. replacing the original with the new iframe
           * 4. setting the ID of the new iframe to the same as the ID of the
           *   original.
           */
          var new_frame = document.createElement('iframe');
          for (var j=0; j<orig_frame.attributes.length; j++) {
            var attr = orig_frame.attributes[j];
//console.log("Copying attribute '%o' from %o to %o", attr, orig_frame, new_frame);
            new_frame.setAttribute(attr.nodeName, attr.nodeValue);
          }

					adFrames[id].currentFrame = new_frame;
					adFrames[id].rotationCount++;
					
          orig_frame.parentNode.replaceChild(new_frame, orig_frame);
//console.log("replacement complete, new node is %o", new_frame);
        }
      }
    }
 
		return { 		
			init : function() {
        startRotation();
			},
      
      _rotationCycle : function() {
//console.log("_rotationCycle called, doing rotation and recursively calling");
        doRotation();
        setTimeout(DTM.utility.AdRotator._rotationCycle, CYCLE_LENGTH);
      },
      
      // For debugging we will expose doRotate for manual execution
      rotate : function() {
        doRotation();
      },

		  // rotateAd is how rotation is kicked off for a given iframe
      rotateAd : function(id) {
//console.log("Adding frame '%o': %o", id, YAHOO.util.Dom.get(id));
        addAdFrame(id);
      }
		}	
			
		
	}();
})();
YAHOO.register('DTM.utility.AdRotator', DTM.utility.AdRotator, { version : '0.1', build : '1' } );

