Effect.QueueThis = Class.create();
Object.extend( Object.extend( Effect.QueueThis.prototype, Effect.Base.prototype ), {
    initialize: function(element) {
        this.element = $(element);
        this.func = arguments[1];
        this.start(arguments[2]);
    },
    finish: function(position){
        this.func(this.element);
    }
});

var Switcher = Class.create();

Switcher.prototype = {
	initialize: function(elementId, transition, interval, random) 
	{
	    this.element = $(elementId);
		this.transition = transition;
		this.interval = interval;
		this.scope = this.elementId + 'queue';
		this.firstElement = this.element.firstDescendant();
		if(random)
		{
		    this.size = this.element.childElements().size();
	        this.currentElement = this.element.childElements()[((this.size)*Math.random()).floor()];
	        new Effect.Appear(this.currentElement, 
	                            { 
	                                duration: 1.0, 
	                                transition: Effect.Transitions.linear, 
	                                fps: 10,
	                                queue:
	                                    {
	                                        position:'end',
	                                        scope:this.scope
	                                    }
	                             });
		}else{
		    this.currentElement = this.firstElement;
		}
	},
	next: function()
	{
	    nextElement = this.currentElement.next();
	    if(!nextElement){
	        nextElement = this.firstElement;
	    }
	    oldElement = this.currentElement;
	    this.currentElement = nextElement;
        new Effect.Parallel(
	        [	
	            new Effect.Fade(oldElement, {sync: true}),
		        new Effect.Appear(nextElement, {sync: true})
		    ],
	        { 
		        duration: 1.0,
		        transition: Effect.Transitions.linear,
		        fps:10,
		        queue:
		        {
			        position:'end', 
			        scope: this.scope
		        }
	        }
	    );
	},
	previous: function()
	{
	    previousElement = this.currentElement.previous();
	    if(!previousElement){
	        previousElement = this.element.childElements().last();
	    }
        oldElement = this.currentElement;
        new Effect.Parallel(
	        [	
	            new Effect.Fade(oldElement, {sync: true}),
		        new Effect.Appear(previousElement, {sync: true})
		    ],
	        { 
		        duration: 1.0,
		        transition: Effect.Transitions.linear,
		        fps:10,
		        queue:
		        {
			        position:'end', 
			        scope: this.scope
		        }
	        }
	    );
	    this.currentElement = previousElement;
	},
    run: function()
    {
        if(this.currentElement.next() || !this.runOnce){
            this.next();
        }else{
            this.stop();
            this.next();
            if(this.stopCall){
                this.stopCall();
            }
            return;
        }
    },
	stop: function(callback)
	{
	    clearTimeout(this.timer);
	    if(callback){
	        callback();
	    }
	},
	start: function(callback)
	{
	    this.timer = setInterval(this.run.bind(this), this.interval*1000);
	    if(callback){
	        callback();
	    }
    }
}

/*var Button = Class.create();
Button.prototype = {
    initialize: function()
    {
    
    }
}*/

