var S2MRotator = new Class({

	Implements: [Options, Events],
	
	options: {
		timerDelay: 3000,
		animationDelay: 1500
	},
	
	_timeoutId 	: null,
	_items		: null,
	effect		: null,
	hover		: false,
	
	initialize: function(options) {
		this.setOptions(options);
		window.addEvent('domready', this._setupRotator.bind(this));
	},
	
	/*
	 * Hide all but the first image, set z-index to higher number
	 * add active class to relative list item
	 * start timer. On timer complete call change image
	 */
	_setupRotator : function() {
	
		/* 
		 * retrieve all image item elements
		 */
		this._items = $$('div.s2m-rotator div.s2mRotItems div.s2mRotItem');
		if (this._items.length < 2) {
			return;
		}
		
		/*
		 * Hookup the events
		 */
		this._addEvents();
		
		/*
		 * Start at 1 since the first one should be active
		 */
		for (var x=1; x<this._items.length; x++) {
			this._items[x].setStyle('display', 'none');
		}
		if (this._items[0]) {
			this._items[0].setStyle('z-index', '10');
			this._items[0].addClass('rotator-active');
		}
		this._timeoutId	= this._changeImage.delay(this.options.timerDelay, this);

        this._setNavigationActive('-1');
	},
	
	_setNavigationActive : function(id) {
		var parts	= id.split('-');
		var listItems	= $$('ul.link-container li');
		
		for (var x=0; x<listItems.length; x++) {
			listItems[x].removeClass('active');
			if (listItems[x].id == 'list-' + parts[1]){
				listItems[x].addClass('active');
			}
		}

	},

	/*
	 * Find active image, get next image. if this is the last image get the first image
	 * set next image z-index to current image z-index minus 1. animate opacity current 
	 * image to 0, set current image to display none, remove z-index, and change opacity 
	 * back to 1. set z-index of next image to same number of current image and start 
	 * timer again.
	 */
	_changeImage : function(){
		var active 	= $$('div.s2m-rotator div.s2mRotItems div.rotator-active');
		var next	= null;
		if (active[0]) {
			next = active[0].getNext();
		}
		if (!next) {
			//There was no next element, first one should be made active again
			next	= this._items[0];
		}
		
		next.setStyles({
		    'z-index': (active[0].getStyle('z-index')-1),
		    'display': 'block',
		    'visibility': 'visible'
        });

		active[0].setStyle('visibility', 'visible');
		active[0].toggleClass('rotator-active');
		next.toggleClass('rotator-active');
		
		this.effect = new Fx.Morph(
			active[0].id, 
			{
				duration	: this.options.animationDelay, 
				transition	: Fx.Transitions.Linear,
				onComplete 	: function() {
				    active[0].setStyles({
				        'z-index': 0,
				        'display': 'none',
				        'opacity': '1'
				    });
					next.setStyles({
					   'z-index': '10',
					   'opacity': '1'
				    });
					this._timeoutId	= this._changeImage.delay(this.options.timerDelay, this);
				}.bind(this)
			}
		);
		this._setNavigationActive(next.id);
		 
		this.effect.start({
		    'opacity': 0
		});
	},
	
	/*
	 * @todo everything
	 * @todo add handlers for list items
	 * @todo stop any timer and set current image to li relative image
	 */
	_addEvents : function() {
		//Retrieve all anchor tag elements
		var link 	= $$('div.s2m-rotator ul.link-container li');

		//Loop all elements and attach events and event handlers
		for (var x=0; x<link.length; x++) {
			
			link[x].addEvent('mouseenter', function(event, id) {
				this._timeoutId = null;

                var parts = id.toString().split('-');
                id = parts[1];

                $('image-' + id).setStyles({
        		    'z-index': 99,
        		    'display': 'block',
        		    'visibility': 'visible'
                });
				
			}.bindWithEvent(this, link[x].get('id')));
			
			link[x].addEvent('mouseleave', function(event, fid) {
                var parts = fid.toString().split('-');
                id = parts[1];

                if (!$(fid).hasClass('active')) {
                    $('image-' + id).setStyles({
            		    'z-index': 0,
            		    'display': 'none'
                    });
                }

			}.bindWithEvent(this, link[x].get('id')));
		}
	}
	
});

var rotator = new S2MRotator();
