var headLines = new Class ({

	options: {
		master:$('headlines'),
		container: $('imgcontainer'),
		menu:$$('#headlines ul.menu li h2 a'),
		layer:$('layer'),
		contents:$$('.contents'),
		marge:560,
		position: 0,
		duration:4000
	},
	fx:{contents:''},
	
	
	initialize: function(options){
		this.setOptions(options);
	
		
		
		
		this.initEvents();
		this.initFxs();
		this.rotateStart();
		
								
	},
	
	initFxs:function()
	{
		
		this.options.layer.setStyle('opacity','0.8');
		this.fx={	
					container: new Fx.Morph(this.options.container,{duration:700,transition: Fx.Transitions.Quint.easeOut,wait:false}),
					contents : []
				}
		
		var batch = [];
		this.options.contents.each(function(el,i){
			
			batch[i] = new Fx.Morph(el, {duration:500, transition: Fx.Transitions.Sine.easeOut,wait:false}).set({'opacity':0,'margin-top':-110,'height':0});
			el.setStyle('display','block');
			});
			
		this.fx.contents = 	batch;
		
		/* show back first */
		this.fx.contents[0].set({'opacity':1,'margin-top':-110,'height':110});
		
		
		
		
	},
	
	initEvents:function()
	{
		
		for(i = 0; i < this.options.menu.length; i++) 
		{
			
			this.menuEvent(i);
			
		}
		
		this.options.master.addEvents({
			
			mouseover: this.rotateStop.bind(this), 
			mouseleave: this.rotateStart.bind(this) 

		});	
		
		
	
		
	},
	
	menuEvent:function(i)
	{
		
		return this.options.menu[i].addEvents({
			
			mouseover: this.linkover.bind(this,i) 

		});
		
	},
	
	linkover:function(i)
	{
		this.rotateStop();
		this.show(i);
		
	},
	
	show:function(i)
	{
		
		if(i==null) 
		i = this.setPosition();
		
		this.options.menu.each(function(el,i){el.getParent('li').removeClass('current');});
		this.options.menu[i].getParent('li').addClass('current');		
		this.fx.container.start({'margin-left':-(this.options.marge*i)});
		/*this.fx.contents[this.options.position].start({'opacity':0,'margin-top':0});*/
		this.fx.contents[this.options.position].start({'opacity':0});
		/*this.fx.contents[i].start({'opacity':1,'margin-top':-110});*/
		this.fx.contents[i].start({'opacity':1});
		
		this.options.position = i;
	},
	
	setPosition:function()
	{
		if(this.options.position<this.options.menu.length-1)		
		i = this.options.position+1;
		else
		i =0;

		
		return i;
	},
	
	rotateStart:function(){
		
		
		
		this.periodical = this.show.periodical(this.options.duration, this);
	},
	
	
	rotateStop:function(){
		
		this.periodical = $clear(this.periodical);
	}

	/*******************************/
	


});

headLines.implement(new Events, new Options);
