Event.observe(window, 'load', function() 
{
	var newAnimation = new Animation();
	newAnimation.slideshow(1,3,3000,'trailer');	
});


var Animation = Class.create(
{
	initialize: function() 
	{
		this.go_to 		= null;
		this.freeze		= false;
		
		this.last		= null
		this.interval	= null;
		this.target		= null;
	},
	slideshow: function(start,last,interval,target)
	{
		this.last		= 0;
		
		$$('.li_trailer').each(function(item, index)
		{
			this.last 	= this.last + 1;
		}.bind(this));
		
		last		= this.last;
		
		this.interval	= interval;
		this.target		= target;
		
		var frame 		= start;   
		var nextframe 	= start+1;   
		var that		= this;

		this.attachEvents(target);
		
		if ($(target+frame))
		{
			Effect.Appear(target+frame,{duration:.5,from:0.0,to:1.0});   
			Effect.Appear(target+'title'+frame,{duration:.5,from:0.0,to:1.0});   
			
			setInterval(function()
			{
				// when freezed, nothing will happen
				if (!that.freeze || that.go_to != null)
				{
					// an other next frame will be set
					if (that.go_to != null)
					{
						nextframe 	= that.go_to;
						that.go_to	= null;
					}
					$('slidebutton'+frame).src= '/images/slide_button.png';
					Effect.Fade(target+frame,
					{
						duration:.5,
						from:1.0,
						to:0.0,
						afterFinish:function()
						{
							
							$(target+frame).hide();
							$(target+'title'+frame).hide();
							
						    frame = nextframe;
						    nextframe = (frame == last) ? start : nextframe+1;
						},
						beforeStart:function()
						{
						    $('slidebutton'+nextframe).src= '/images/slide_button_active.png';
							Effect.Appear(target + nextframe,{duration:.5,from:0.0,to:1.0});
						    Effect.Appear(target+'title'+nextframe,{duration:.5,from:0.0,to:1.0});
						    
						    
						    $(target + '_li_' + frame).removeClassName('active');
						    $(target + '_li_' + nextframe).addClassName('active');
						    
//						    $('newstrailer_title_' + frame).removeClassName('newstrailer_title_active');
//						    $('newstrailer_title_' + frame).addClassName('newstrailer_title');
//						    
//						    $('newstrailer_title_' + nextframe).removeClassName('newstrailer_title');
//						    $('newstrailer_title_' + nextframe).addClassName('newstrailer_title_active');
							//sIFR.redraw();

						}
					});
				}
			}, interval);
		}
		return; 
	},
	attachEvents: function()
	{
		$$('.li_trailer').each(function(item, index)
		{
//			Event.observe(item.id, 'mouseover', this.goToFrame.bindAsEventListener(this, item));
//			Event.observe(item.id, 'mouseover', this.freezeFrame.bindAsEventListener(this, item));
//			Event.observe(item.id, 'mouseout', this.freezeFrame.bindAsEventListener(this, item));
		}.bind(this));
	},
	goToFrame: function(event, elm)
	{
		var exploded 	= elm.id.split('_');
		this.go_to 		= parseInt(exploded[2]);
	},
	freezeFrame: function(event, elm)
	{
		if (event.type == 'mouseover')
		{
			this.freeze = true;
		}
		if (event.type == 'mouseout')
		{
			this.freeze = false;
		}
	}
});
