var mccCarousel = new Class(
{
	Implements: Options, 
	
	options: 
    {  	

    },
    
    initialize: function(options)
    {
    	this.setOptions(options);
    	this.container = this.options.container;
    	this.cfg = this.options.cfg;
    	this.controls = {};
    	this.entryheight=0;
    	this.setup();
    	this.index = 0;
    	(this.layers.length > 0) ? this.slidein(this.index,'bottom') : null;
    	if(this.total > 1) {
    	this.controls.next.addEvent('click',function(){this.next();}.bind(this));
    	this.controls.prev.addEvent('click',function(){this.prev();}.bind(this));
    	}
    	else {
    		this.controls.next.dispose();
    		this.controls.prev.dispose();
    	}
    },
    
    setup: function()
    {
    	
    	$(this.container).setStyles({
    		'width':this.cfg.w,
    		'position':'relative',
    		'overflow':'hidden'
    	});
  	
    	this.controls.next = new Element('a',{
    		'href':'javascript:void(0);',
    		'html':'next',
    		'styles':{
    			'display':'block',
    			'background':'url(/brs/assets/images/next.png) no-repeat 50% 50%',
    			'height':'16px',
    			'text-indent':-9999,
    			'position':'relative',
    			'outline':'none'
    		}
    	});
    	
    	this.controls.prev = new Element('a',{
    		'href':'javascript:void(0);',
    		'html':'previous',
    		'styles' : {
    			'display':'block',
    			'background':'url(/brs/assets/images/prev.png) no-repeat 50% 50%',
    			'height':'16px',
    			'text-indent':-9999,
    			'position':'relative',
    			'outline':'none'
    		}   		
    	});
    	
    	this.viewport = new Element('div',{
    		'styles':{
    			'position':'relative',
				'width':'100%',
    			'clear':'both',
    			'overflow':'hidden'
    		}
    	});
    	
    	this.layers = $$('div.entry');

    	$(this.container).adopt(this.controls.prev,this.viewport,this.controls.next);

    	this.truesize = $(this.container).getSize();

    	this.layers.each(function(item,index) {
    		
    		item.setStyles({
    			'margin-top':this.truesize.y,
    			'position':'absolute',
    			'display':'',
    			'z-index':999
    		});
    		var moving = item.dispose();
    		this.viewport.grab(moving);
    		this.entryheight = (item.getSize().y > this.entryheight) ? item.getSize().y : this.entryheight;
    	}.bind(this));
    	
    	this.viewport.setStyle('height',this.entryheight);
    	this.total = this.layers.length;
    },
    
    next: function()
    {
    	
    	this.controls.next.removeEvents();
    	this.controls.prev.removeEvents();
    	
    	var next = ((this.index + 1) > (this.total - 1)) ? 0 : (this.index + 1);
    	this.index = next;
    	var prev = ((this.index - 1) < 0) ? (this.total - 1) : (this.index - 1);

    	this.slidein(next,'bottom').addEvent('onComplete',function(){
    		
    		this.controls.next.addEvent('click',function(){
    			this.next();
    		}.bind(this)); 	
    		
    		this.controls.prev.addEvent('click',function(){
    			this.prev();
    		}.bind(this));
    		
    	}.bind(this));
    	this.slideout(prev,'bottom');
    },
    
    prev: function()
    {
    	
    	this.controls.next.removeEvents();
    	this.controls.prev.removeEvents();
    	
    	var prev = ((this.index - 1) < 0) ? (this.total - 1) : (this.index - 1);
    	this.index = prev;
    	var next = ((this.index + 1) > (this.total - 1)) ? 0 : (this.index + 1);
    	
    	this.slidein(prev,'top').addEvent('onComplete',function(){
    		
    		this.controls.next.addEvent('click',function(){
    			this.next();
    		}.bind(this)); 	
    		
    		this.controls.prev.addEvent('click',function(){
    			this.prev();
    		}.bind(this));
    		
    	}.bind(this));

    	this.slideout(next,'top');
    },
    
    slidein: function(key,pos)
    {
    	switch(pos)
    	{
    		default:
    		case 'top':
    		var top = this.layers[key].getSize();
    		this.layers[key].setStyles({'margin-top':-top.y});
    		break;
    		
    		case 'bottom':
			this.layers[key].setStyles({'margin-top':this.truesize.y});
    		break;
    	}
    	var fx_intoview = new Fx.Morph(this.layers[key],{duration:800,transition:'sine:out'});

    	return fx_intoview.start({'margin-top':0,'opacity':1});
    },
    
    slideout: function(key,pos)
    {	
    	
    	switch(pos)
    	{
    		default:
    		case 'top':
    		var value = this.truesize.y;
    		break;
    		
    		case 'bottom':
    		var top = this.layers[key].getSize();
			var value = -top.y;
    		break;
    	}
    	
    	var fx_outview = new Fx.Morph(this.layers[key],{duration:800,transition:'sine:out'});
    	
    	return fx_outview.start({'margin-top':value,'opacity':0});
    }
 
});