/**
 * @author Ideatronic
 */
var Acc = new Class({
	options:{
		period:5000,
		autorotate:true,
		eventType:'over'
	},
	initialize: function(main_id, handler_class, box_class, options){
		this.setOptions(options);
		var $this = this;
		this.eventType = this.options.eventType;
		this.autorotate = this.options.autorotate;
		this.period = this.options.period;
		this.togglers = $$(handler_class);
		this.counter = 0;
		this.accTimer = 0;
		this.ac = new Accordion($(main_id), handler_class, box_class, {
			opacity:false,
			onActive: function(toggler, element){
				toggler.className = 'accTogglerH'
			},
			onBackground: function(toggler, element){
				toggler.className = 'accToggler';
				$this.counter = this.previous+1;
			}
		});
		if(this.autorotate){
			this.startTimer();
			$(main_id).addEvents({
				'mouseenter': function(e){
					ev = new Event(e);
					ev.stopPropagation();
					$this.stopTimer();
				},
				'mouseleave': function(e){
					ev = new Event(e);
					ev.stopPropagation();
					$this.startTimer(e);
				}
			});
		}
		this.makeEv();
	},
	makeEv: function(){
		var $this = this;
		this.togglers.each(function(item){
			if($this.eventType=='over'){
				item.addEvent('mouseenter',function(e){
					ev = new Event(e);
					ev.stop();
					if($this.autorotate)
						$this.stopTimer();
					item.fireEvent('click');
				});
				item.addEvent('mouseleave',function(e){
					ev = new Event(e);
					ev.stop();
					if($this.autorotate)
						$this.startTimer();
				});
			}
			if($this.eventType=='click'){
				item.addEvent('click',function(e){
					ev = new Event(e);
					ev.stop();
					if($this.autorotate)
						$this.stopTimer();
				});
				item.addEvent('mouseleave',function(e){
					ev = new Event(e);
					ev.stop();
					if($this.autorotate)
						$this.startTimer();
				});
			}

		})
	},
	stopTimer:function(){
		$clear(this.accTimer);
	},
	startTimer:function(){
		var $this = this;
		$clear(this.accTimer);
		this.accTimer = (function(){
			$this.fireDelay();
		}).periodical(this.period);
	},
	fireDelay: function(){
		if(!this.autorotate)
			return
		if(this.counter<this.togglers.length){
			this.togglers[this.counter].fireEvent('click');
			return;
		}
		if(this.counter>=this.togglers.length){
			this.counter=0;
			this.togglers[this.counter].fireEvent('click');
		}
	}
});
Acc.implement(new Options);
