// verify namespace
if (typeof WIREDRIVE != 'object'){
    var WIREDRIVE = function() { 
        return {}; 
    }();
}

WIREDRIVE.rotator = function(){
    

    var itemRotator = function(p,my){
        
        //
        // NOTES:  since the rotator uses
        // inline markup as its datasource
        // the first item should be set to 
        // visible, and the others hidden
        // using the css opacity attributes
        //
        // this can be done inline or in the
        // css stylesheet using code like this:
        //
        // opacity:1;filter:alpha(opacity=100);
        //
        // all items should be absolutely positioned
        // in relationship to the parent element
        // if they are meant to be shown in the same
        // space.  
        //
        // The itemRotator is only responsible for 
        // hiding and showing elements, not 
        // positioning them.
        //
        
        var that = {};
        my = my || {};
        p = p || {};
        
        // Provide default input values
        // TODO : validate types and reasonable values
        p.list = p.list || "rotatorlist";
        p.pausetime = p.pausetime || 7000;
        p.initialpause = p.initialpause || p.pausetime;
        p.fadeintime = p.fadeintime || 2;
        p.fadeouttime = p.fadeouttime || 1;
        p.height = p.height || 400;
        p.maxcycles = p.maxcycles || 200;
        
        // START CYCLE COUNTER
        my.count = 0;
        
        // Get the items to loop through
        my.listitems = YAHOO.util.Dom.getChildren(p.list);

        // Fade in item [num] and trigger the fadedown after a pause
        my.fadeup = function(num){
          	var anim = new YAHOO.util.Anim(my.listitems[num], {
        		opacity: {from:0, to:1}
        	}, p.fadeintime);
        	anim.animate();
        	my.count++;
        	if (my.count <= p.maxcycles){
                my.timeout = setTimeout(function(){my.fadedown(num);}, p.pausetime, this);
        	}
        };
        
        // Fade out item [num] and trigger the fadein of the next item
        my.fadedown = function(num){
          	var anim2 = new YAHOO.util.Anim(my.listitems[num], {
        		opacity: {from:1, to:0}
        	}, p.fadeouttime);
        	anim2.animate();
            var nextnum = num + 1;
            if (nextnum >= my.listitems.length){
                nextnum = 0;
            }
        	my.fadeup(nextnum);
        };
        
        // Trigger the fadeout of the first item after a pause
        my.timeout = setTimeout(function(){my.fadedown(0);}, p.initialpause, this);

        // TODO : provide command hooks to start and stop the rotation of images,
        // and to manually trigger the next and previous images
        
        return that;

    };


    return {
        createItemRotator : function(p){
            var rotator = itemRotator(p);
            return rotator;
        }
    }
}();


