/*******************************************
	Image Gallery
*******************************************/
imageGallery = new Class({
	Implements: Options,
	
	options:{
		'otabs_animate_start_index':0,
		'otabs_animate_duration':750,
		'otabs_animate_auto_rotate':true,
		'otabs_animate_rotation_timing':5000,
		'button_direction':'next'
	},

	initialize: function(tabs,content,options){
		this.setOptions(options);

		this.tabs = tabs;
		this.content = content;
		this.auto_rotate_index = this.options.otabs_animate_start_index;
		this.periodical = 0;
		this.fx_array = [];
		this.is_open = [];
		
		this.tabs.each(function(el,index){
			this.fx_array[index] = new Fx.Morph(this.content[index],{'duration':this.options.otabs_animate_duration,'transition':Fx.Transitions.Quint.easeOut});
			el.setStyles({'cursor':'pointer'});
			if(this.options.otabs_animate_start_index == index){ 
				this.is_open[index] = true;
				this.content[index].setStyles({'opacity':1,'display':'block'});
			} else {
				this.is_open[index] = false;
				this.content[index].setStyles({'opacity':0});
			}
			el.addEvent('click',function(){
				if(this.options.button_direction == 'next'){
					this.new_index = index+=1;
					if(this.new_index >= this.tabs.length){
						this.new_index = 0;
					}
				} else {
					this.new_index = index-=1;
					if (this.new_index < 0){
						this.new_index = this.tabs.length-1;
					}
				}
				this.clearPeriodical();
				this.setActive(this.new_index);
			}.bind(this));
			this.content[index].addEvent('click',function(){
				this.clearPeriodical();
			}.bind(this));
		}.bind(this));
		
		this.setActive(this.options.otabs_animate_start_index);
		
		// auto rotate
		if(this.tabs[0].periodical === undefined){ 
			this.tabs[0].periodical = [];
		}
		if(this.options.otabs_animate_auto_rotate){ this.tabs[0].periodical[this.tabs[0].periodical.length] = this.rotateBanner.periodical(this.options.otabs_animate_rotation_timing,this); }
	},
	
	clearPeriodical: function(){
		for(var i=0;i<this.tabs[0].periodical.length;++i){
			$clear(this.tabs[0].periodical[i]);
		}
	},
	
	setActive: function(index){
		for(var i=0;i<this.tabs.length;++i){
			this.fx_array[i].pause(); this.fx_array[i].start({'opacity':0});
			this.tabs[i].removeClass('gallery-active-buttons');
			this.is_open[i] = false;
			this.content[i].removeClass('gallery-active-image');
		}
		
		this.tabs[index].addClass('gallery-active-buttons');
		this.is_open[index] = true;
		this.fx_array[index].pause();
		this.fx_array[index].start({'opacity':1,'display':'block'});
		this.content[index].addClass('gallery-active-image');
	},
	
	rotateBanner: function(){
		this.auto_rotate_index += 1;
		if(this.auto_rotate_index >= this.tabs.length){ this.auto_rotate_index = 0; }
		this.setActive(this.auto_rotate_index);
	}
});




/************************************
	Tabs
************************************/
OTabsAnimate = new Class({
	Implements: Options,
	
	options:{
		'otabs_animate_start_index':0,
		'otabs_animate_duration':1000,
		'otabs_animate_auto_rotate':true,
		'otabs_animate_rotation_timing':6000
	},

	initialize: function(tabs,content,options){
		this.setOptions(options);
		
		this.tabs = tabs;
		this.content = content;
		this.auto_rotate_index = this.options.otabs_animate_start_index;
		this.periodical = 0;
		this.fx_array = [];
		this.is_open = [];
		
		this.tab_length = tabs.length;

		this.tabs.each(function(el,index){
			this.fx_array[index] = new Fx.Morph(this.content[index],{'duration':this.options.otabs_animate_duration,'transition':Fx.Transitions.Quint.easeOut});
			el.setStyles({'cursor':'pointer'});
			if(this.options.otabs_animate_start_index == index){ 
				this.is_open[index] = true;
				this.content[index].setStyles({'opacity':1,'display':'block'});
			} else {
				this.is_open[index] = false;
				this.content[index].setStyles({'opacity':0});
			}
			el.addEvent('click',function(){
				this.setActive(index);
				this.clearPeriodical();
			}.bind(this));
			this.content[index].addEvent('click',function(){
				this.clearPeriodical();
			}.bind(this));
			
			
		}.bind(this));
		
		this.setActive(this.options.otabs_animate_start_index);
			
		// auto rotate
		
		if(this.tabs[0].periodical === undefined){ 
			this.tabs[0].periodical = [];
		}
		if(this.options.otabs_animate_auto_rotate){ this.tabs[0].periodical[this.tabs[0].periodical.length] = this.rotateBanner.periodical(this.options.otabs_animate_rotation_timing,this); }
	},
	
	clearPeriodical: function(){
		for(var i=0;i<this.tabs[0].periodical.length;++i){
			$clear(this.tabs[0].periodical[i]);
		}
	},
	
	setActive: function(index){
		for(var i=0;i<this.tabs.length;++i){
			if(this.is_open[i] && i != index){ this.fx_array[i].pause(); this.fx_array[i].start({'opacity':0}); }
			this.tabs[i].removeClass('otabs-tab-active');
			this.is_open[i] = false;
			this.content[i].removeClass('otabs-content-active');
		}
		
		this.tabs[index].addClass('otabs-tab-active');
		this.is_open[index] = true;
		this.fx_array[index].pause();
		this.fx_array[index].start({'opacity':1,'display':'block'});
		this.content[index].addClass('otabs-content-active');
	},
	
	rotateBanner: function(){
		this.auto_rotate_index += 1;
		if(this.auto_rotate_index >= this.tab_length){ this.auto_rotate_index = 0; }
		this.setActive(this.auto_rotate_index);
	}
});



/************************************
	Navigation
************************************/
ONavV = new Class({
	Implements: Options,
      
    options:{
        'enable_shadow':false
    },
    
    initialize: function(onav_vert,options){
        this.setOptions(options);
		
		this.buildVertSubMenus(onav_vert);
	},
	
	buildVertSubMenus: function(el){
	    if($type(el) == 'element'){
			var children = el.getChildren('li');
			var containers = [];
			var togglers = [];
			var children_bullet = [];
			
			children.each(function(el,i){				
				// creates togglers only for list-items that have submenus
				if(el.getChildren('ul').length > 0){
					togglers[togglers.length] = new Element('a',{'class':'ONav-toggler ONav-item-closed'});
					togglers[togglers.length-1].inject(el,'top');
					togglers[togglers.length-1].addEvent('click',function(e){ e.preventDefault(); })
					containers[containers.length] = el.getChildren('ul')
				} else {
					children_bullet = new Element('div',{'class':'ONav-bullet'});
					children_bullet.inject(el,'top');
				}
			}.bind(this))
			
			var accordion = new Accordion(togglers,containers,{
				'display':false,
				'alwaysHide':true,
				'onActive':function(toggler){
					toggler.removeClass('ONav-item-closed');
					toggler.addClass('ONav-item-open');
				},
				'onBackground':function(toggler){
					toggler.removeClass('ONav-item-open');
					toggler.addClass('ONav-item-closed');
				}
			});
	    }
	}
});
