var newsMagic = Class.create({
	
	options: {
		animation: {
			direction: "top", // if type = scroll, set: top || left
			amount: 1,// if type = scroll, set the amount to scroll
			opacity: 0.9,
			durationBefore: 0.4,
			duration: 0.4,
			delay: .25,
			durationAfter: 0.2
		},
		rotate: {
			type: "auto",// auto || manual
			interval: 5,// if type = auto, set the interval (s)
			onMouseOver: "stop"// if type = auto, set the onmouseover behavior: stop || proceed
		},
		item: {
			klass: "carousel_item", // default value
			size: 172 // default value
		},

		Previous: "previous",
		Next: "next",

		onClickPrevious: Class.empty,
		onClickNext: Class.empty,
		onPrevious: Class.empty,
		onNext: Class.empty,
		onGoTo: Class.empty
	},
	

	initialize:function(container, options){
		this.setOptions(options);
		this.container = $(container);
		this.Items = $$('.'+ this.options.item.klass);
		
		if(this.options.rotate.type == 'auto'){
			this.autoRotate();
};
		
		
// Previous - check if value is not "undefined" before start search the dom with $()

		if(this.options.Previous != "undefined" && $(this.options.Previous))		
			$(this.options.Previous).observe('click', function(event) {
				Event.stop(event);
				this._previous();
				this.fire("onClickPrevious");
			}.bind(this));

// Next Button - check if value is not "undefined" before start search the dom with $()

		if(this.options.Next != "undefined" && $(this.options.Next))
			$(this.options.Next).observe('click', function(event) {
				Event.stop(event); 
				this._next();
				this.fire("onClickNext");
			}.bind(this));	
			
				
			
/// I figured out how to copy code to where it clones the html element and inserts it.

		(2).times(function() {
			this.Items.each(function(item) {
				this.container.insert(item.inspect() + $H(item).get('innerHTML') + "</li>");
			}.bind(this));
		}.bind(this));
		
	this.Items = $$('.'+ this.options.item.klass);		
	this.atScreen = this.Items.length / 3;
	this.container.setStyle(this.options.animation.direction + ':' + (- this.atScreen * this.options.item.size) + 'px');
	},

	
	// Auto-rotate feature if set to auto

	autoRotate: function() {	
		
	var a =	new PeriodicalExecuterToggled(function(pe) {
				this._next();
		}.bind(this), this.options.rotate.interval);
		
	},
	
	goTo: function(n) {
		this.atScreen = Math.abs(n % (this.Items.length / 3));
		this.atScreen += this.Items.length / 3;
		this._animate(this.atScreen);
	},
	
	_previous: function() {
		this.atScreen -= this.options.animation.amount;
			if(this.atScreen < this.Items.length / 3) {
				this.container.setStyle(this.options.animation.direction + ':' + (- this.options.item.size * this.Items.length * 2 / 3) + 'px');
				this.atScreen = this.Items.length * 2 / 3 - this.options.animation.amount;
			}
			this._animate(this.atScreen);
				
		//		this.fire("onPrevious");
	},
	
	_next: function() {
		this.atScreen += this.options.animation.amount;
			if(this.atScreen > this.Items.length * 2 / 3) {
				this.container.setStyle(this.options.animation.direction + ':' + (- this.options.item.size * this.Items.length / 3) + 'px');
				this.atScreen = this.Items.length / 3 + this.options.animation.amount;
			}
			this._animate(this.atScreen);
				
		//		this.fire("onNext");
	},
	
	_animate: function(current){ 
		var that = this;		
		this.container.morph("opacity:" + this.options.animation.opacity, {duration: this.options.animation.durationBefore, 
		afterFinish: function() {
			that.container.morph(that.options.animation.direction  + ':' + (- current * that.options.item.size) + 'px', {
				duration: that.options.animation.duration,
				delay: that.options.animation.delay,
				afterFinish: function() {
		            that.container.morph("opacity:1", {
						duration: that.options.animation.durationAfter
					});
				}
			});
		}});				
	},

		
	setOptions:function(options) {
		Object.extend(this.options, options);
	}

	
});

// Helper for PeriodicalExecuter - Thanks goes to Elf M. Sternberg at http://elfs.livejournal.com

PeriodicalExecuterToggled = Class.create();
Object.extend(Object.extend(PeriodicalExecuterToggled.prototype, PeriodicalExecuter.prototype), 
 {
     initialize: function(callback, frequency) {
         this.callback = callback;
         this.frequency = frequency;
         this.currentlyExecuting = false;
         this.timer = null;
         this.registerCallback();
     },
 
     registerCallback: function() {
         this.timer = setInterval(this.onTimerEvent.bind(this), this.frequency * 1000);
     },
 
     clearCallback: function() {
         clearInterval(this.timer);
         this.timer = null;
     },
 
     setFrequency: function(f) {
         this.frequency = f;
         if (this.timer != null) {
             this.clearCallback();
             this.registerCallback();
         }
     }
 });
