var Rotator = Class.create({
	imageList: null,
	current: null,
	next: null,
	count:null, 
	timer: null,
	initialize: function ( timeout ) {
		this._timeout = timeout * 1000 || 8000;
		this.imageList = [];
		var images = this.getElementsByTagName( 'li' );
		this._title = _$( this.id + '-title' );
		this._content = _$( this.id + '-content' );
		this.count = images.length;
		for( var i = 0; i < images.length; i++ ) {
			var image = Rotator.Image.applyTo( images[i], i );
			if( i > 0 ) image.setOpacity( 0 );
			image.show();
			this.imageList.push( image );
		}
		this.setCurrent( 0 );
		this.signalEnd( this.getCurrent() );
		this.processor = new Rotator.Processor( this );
		this.run();
	},
	signalEnd: function( item ) {
		this.setSection( this._title, item.getTitle() );
		this.setSection( this._content, item.getContent() );
	},
	setSection: function( section, data ) {
		if( section && data ) {
			section.innerHTML = data.innerHTML;
		}
	},
	setCurrent: function ( current ) {
		this.current = current < this.count ? current : 0;
		this.next = this.current + 1 < this.count ? this.current + 1 : 0;
	},
	getCurrent: function() {
		return this.imageList[ this.current ];
	},
	run: function() {
		var _self = this;
		this.timer = setTimeout( function() { _self.process() }, this._timeout );
	},
	process: function() {
		this.processor.start( this.getCurrent(), this.imageList[ this.next ] )
		this.setCurrent( this.current + 1 );
		this.run();
	},
	stop: function () {
		if( this.timer ) clearTimeout( this.timer );
	}
});
		
Rotator.Processor = Class.create( {
	_current: null,
	_next: null,
	_pos: null,
	initialize: function( parent ) {
		this._parent = parent;
	},
	start: function( current, next ) {
		if( this.timer ) {
			this._finalize()
		}
		this._current = current;
		this._next = next;
		this._pos = 0;
		this.process()
		this._parent.signalEnd( this._next );
	},
	process: function () {
		this._pos += 10;
		if( this._pos < 100 ) {
			var _self = this;
			this._setPos();
			this.timer = setTimeout( function() { _self.process() }, 50 );
		} else {
			this._finalize();
		}
	},
	_setPos: function() {
		if( this._current ) this._current.setOpacity( 100 - this._pos );
		if( this._next ) this._next.setOpacity( this._pos );
	},
	_finalize: function() {
		clearTimeout( this.timer );
		this._pos = 100;
		this._setPos();
//		this._parent.signalEnd( this._next );
	}
});

Rotator.Image = Class.create( {
	initialize: function ( index ) {
		this.image = this.getElementsByTagName( 'img' )[ 0 ]; 
		this._title = this.getElementsByTagName( 'span' )[ 0 ]; 
		this._content = this.getElementsByTagName( 'p' )[ 0 ]; 
		this._detach( this._content );
		this._detach( this._title );
		if( index !=  0 ) {
			this.setOpacity( 0 );
		}
	},
	getTitle: function()  {
		return this._title;
	},
	getContent: function()  {
		return this._content;
	},
	_detach: function( node ) {
		if( node ) {
			node.parentNode.removeChild( node );
		}
	},
	setOpacity: function ( value ) {
		this.style.opacity = value/100;
		this.style.filter = 'alpha(opacity=' + value + ')';
	},
	show: function () {
		this.style.display = 'block';
	}
});
var _gaq=_gaq||[];_gaq.push(["_setAccount","UA-1488792-1"]);_gaq.push(["_setDomainName","dragon-community.net"]);_gaq.push(["_trackPageview"]);
(function(){var ga=document.createElement("script");ga.type="text/javascript";ga.async=true;ga.src=("https:" == document.location.protocol ? "https://ssl" : "http://www") + ".google-analytics.com/ga.js";
var s=document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(ga, s);
})();

