var stopScripts = false;

var parAcc = new Class({
    initialize:function(acc1,acc2)
    {
        this.acc1 = acc1;
            this.acc1.first = true;
            this.acc1.parent = this;
        this.acc2 = acc2;
            this.acc2.second = true;
            this.acc2.parent = this;
        
         this.acc2.makeEv();
        this.acc1.makeEv();     
       
    },
    parAcc:function(offerID){
        var el = this.acc2.boxesIDs[offerID];
        $(el).fireEvent('click');
    }
});

var Acc = new Class({
	options:{
		period:5000,
		autorotate:true,
		eventType:'over'
	},
	initialize: function(main_id, handler_class, box_class, options, display){
		this.setOptions(options);
		var $this = this;
		this.first = false;
		this.second = false;
		this.parent = false;
		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.boxes = $$(box_class);
		this.ac = new Accordion($(main_id), handler_class, box_class, {
			opacity:false,
			onActive: function(toggler, element){
				toggler.addClass('active');
			},
			onBackground: function(toggler, element){
				toggler.removeClass('active');
				$this.counter = this.previous+1;
			},
			display: display
		});
		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.boxesIDs = new Array();
		this.moreInfo = new Array();
		//this.makeEv();
	},
	makeEv: function(){
		var $this = this;
		
		this.boxes.each(function(item,index){
		    if($this.first == true)
             {
                var mi = item.getElement('a.offerMore');
                if($chk(mi))
                    $this.moreInfo[index] = mi;
             }
		});
		
		this.togglers.each(function(item,index){		    
			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();
				});
			}
			
		});
		if(this.second == true)
		{
		    this.togglers.each(function(box){
		        var id = box.id.replace('offerMore','');
		        $this.boxesIDs[id] = box;
		    });
		}
		if(this.moreInfo.length>=1 && this.first == true)
		{
		    this.moreInfo.each(function(item,index){
		       var it = item;
		       item.addEvent('click',function(){
		            $this.clickMi(it);
		       });
		        
		    });
		}
		    
	},
	clickMi:function(obj)
	{
	    var id1 = obj.href.substr(obj.href.lastIndexOf('#')+1).replace(this.hashParam+'=','');
	    var id2 = id1.replace('offer','');
	    return this.parent.parAcc(id2);
	},
	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);