var IT = {
	
	data: null,
	current: 0,
	rotate: true,
	speed: 2,
	updater: null,
	direction: 'next',
	
	initialize: function(){
		
		if( $('it') && !$('adminbar') )
		{
			
			$('it').className = 'scroller-list';
			
			$('scroller_container').style.display = 'block';
			
			this.getItems();
			this.setItems();
			
			IT.updater = new PeriodicalExecuter(IT.next, 3);
			
			$('listcontainer').onmouseover = function()
			{
				IT.updater.stop();
			}
			$('listcontainer').onmouseout = function()
			{
				IT.updater = new PeriodicalExecuter(IT.next, 3);
			}
			
			
		}else if( $('it') && $('adminbar') ){
			
			$('it').className = 'scroller-gallery';
			
			$('scroller_container').style.display = 'block';
			
		}
		
	},
	
	getItems: function(){
		
		var _c = $$('#it ul');
		
		_c[0].id = "listcontainer";
		
		IT.data = $$('#it ul li');
		
		IT.output("Elements Found: " + IT.data.length );
		
		IT.data.each( function(item,index){
		
			item.id = 'item_' + index;
			item.onclick = function(){ IT.rotate = false; }
			
		});
		
	},
	
	
	setItems: function()
	{
		
		var list = $('listcontainer');
		
		while( list.hasChildNodes() )
		{
			list.removeChild( list.lastChild );
		}
		
		list.appendChild( IT.data[ IT.data.length-2 ] ); // append second to last item
		list.appendChild( IT.data.last() ); // append last item
		
		for( var i=0; i < 4; ++i )
		{
			list.appendChild( IT.data[i] );
		}
		
		var itemsnew = $$('#listcontainer li');
		
		itemsnew.each( function(item,index){
			
				var to = (index*160) - 160;
				
				$(item.id).morph('left:' + to + 'px', { duration: 0.4 } );
			
		});

		
	},
	

	previous: function(){
		
		IT.updater.stop();
		
		IT.output("current item: " + IT.current );
		
		$('listcontainer').removeChild( $('listcontainer').lastChild );
		
		var items = $$('#it ul li');
		
		IT.output('items listed: ' + items.length );

		
		items.each( 
							 
			function(item,index){
				
				var to = (index*160) - 160;
				
				IT.output('item: ' + item.id + ' | set - left: ' + to + ' | order: ' + (index+1) );
				
				$(item.id).morph('left:' + to + 'px', { duration: 0.4 } );

			}
		);
		
		var addnext = ( IT.current-4 < 0 ? (IT.current-4)+IT.data.length : IT.current-4 );
		
		IT.output('next to add: ' + addnext );
		
		$('listcontainer').insertBefore( IT.data[addnext], $('listcontainer').firstChild );

		if( IT.current==0 )
		{
			IT.current = IT.data.length-1;
		}else{
			--IT.current;
		}
		
		
		IT.direction = 'previous';
		
	},

	next: function(){
		
		IT.output("current item: " + IT.current );
		
		$('listcontainer').removeChild( $('listcontainer').firstChild );
		
		var items = $$('#it ul li');
		
		IT.output('items listed: ' + items.length );
		
		items.each( 
							 
			function(item,index){
				
				IT.output('item: ' + item.id + ' | set - left: ' + ((index*160)-160) );
				
				$(item.id).morph('left:' + ((index*160)-160) + 'px', { duration: 0.4 } );

			}
		);
		
		var addnext = ( IT.current+4 > IT.data.length-1 ? (IT.current+4)-IT.data.length : IT.current+4 );
		
		IT.output('next to add: ' + addnext );
		
		$('listcontainer').appendChild( IT.data[addnext] );
		
		$('listcontainer').lastChild.style.left = (160 * 3) + 'px'
		
		if( IT.current+1 == IT.data.length )
		{
			IT.current = 0
		}else{
			++IT.current;
		}
		
		IT.direction = 'next';
		
	},
	
	autoRotate: function()
	{
		if( IT.rotate == true )
		{
			setTimeout('IT.next()',IT.speed*1000);
		}
	},
		
	
	outputCount: 1,
	
	output: function(msg){
		var line = document.createElement('div');
		line.innerHTML = IT.outputCount + '. ' + msg;
		$('output').insertBefore(line, $('output').firstChild);
		++IT.outputCount;
	}
											
}

Event.observe(window, 'load', function() { IT.initialize(); } );