var Menu = new Class({
	initialize:function(el){
		this.wrapper=el;
	
		var texture=new Element('img',{'src':'/images/underline.png'});
	
		var underline=new Element('div').setStyles({
			'position':'absolute',
			'width':0,
			'height':4,
			'zIndex':200,
			'overflow':'hidden'
		}).grab(texture);
		
		this.underline=underline;		
		$(el).grab(this.underline);
		
		$$('#'+el+' a').setStyles({
			'position':'relative',
			'z-index':210
		}).addEvents({
			'mouseenter':function(){
				if( ! this.hasClass('current')){
					this.setStyle('color','#EAE9E1');
					
					var coords=this.getCoordinates();
					underline.setStyles({
						'width':0,
						'top':coords.bottom-9,
						'left':coords.left+this.getStyle('paddingLeft').toInt()
					});
					
					texture.set('width',this.getStyle('width')).set('height',4);
					
					underline.set('tween',{'duration':220}).tween('width',0,this.getStyle('width'));
				}
			},
			'mouseleave':function(){
				if( ! this.hasClass('current')){
					this.setStyle('color','#fff');
					
					var out=underline.clone();
					$(el).grab(out);
					underline.setStyle('width',0);
					
					new Fx.Tween(out,{'duration':220})
						.start('opacity',0)
						.chain(function(){out.dispose()});
				}
			},
			'click':function(e){
				if( ! this.hasClass('current_tab')){
					$$('#'+el+' .current_tab').removeClass('current_tab');
					this.getParent().addClass('current_tab');
					this.setStyle('color','#fff');
					
					$$('#header_sub_nav .current_tab').removeClass('current_tab');
					$('sub_nav_' + this.get('text').toLowerCase()).addClass('current_tab');
				}
			}
		});
		
		$$('#header_sub_nav a').addEvents({
			'mouseover':function(){
				this.setStyle('color','#fff');
			},
			'mouseleave':function(){
				this.setStyle('color','#eaeaea');
			}
		});
	}
});






window.addEvent('domready',function(){
	
	new Menu('header_nav');
	
	$$('#bottom_bar a','#header_sub_nav a','#footer a','#side_bar a','#content a').addEvents({
		'mouseover':function(){
			this.setStyle('textDecoration','underline');
		},
		'mouseleave':function(){
			this.setStyle('textDecoration','none');
		}
	});
	
	if($('client_slide')){
		var el=$('client_slide');
		var cur_pic=0;
		var max_pic=(el.getChildren('div.cell')).length;
		var scroll_step=1;

		el.setStyle('width',max_pic*116);

		$$('a.arrow.left').setStyles({'opacity':'0','visibility':'hidden'});
		
		$$('a.arrow.left').addEvent('click',function(e){
			e.stop();
			if(cur_pic>0){
				cur_pic-=scroll_step;
				if(cur_pic<0)
					cur_pic=0;
				if(cur_pic==0)
					this.fade('out');
				this.getNext('a.arrow').fade('in');
				el.set('tween',{'duration':'350'}).tween('left',cur_pic*-116);
			}
			return false;
		});

		$$('a.arrow.right').addEvent('click',function(e){
			e.stop();
			if(cur_pic<max_pic-scroll_step){
				cur_pic+=scroll_step;
				if(cur_pic>max_pic-scroll_step)
					cur_pic=max_pic-scroll_step;
				if(cur_pic==max_pic-scroll_step)
					this.fade('out');
				this.getPrevious('a.arrow').fade('in');
				el.set('tween',{'duration':'350'}).tween('left',cur_pic*-116);
			 }
			 return false;
		});
		
		$$('#client_slide .cell').addEvents({
			'mouseover':function(){
				this.setStyle('backgroundPosition','top');
			},
			'mouseleave':function(){
				this.setStyle('backgroundPosition','bottom');
			}
		});
	}
	
    if($$('a.external')){
		$$('a.external').addEvent('click',function(){
			window.open(this.get('href'));
			return false;
		});
	}
    
	if($('login_tab')){
		var slide_top = $('login_slide').getStyle('top');
		
		$$('#login_tab > a').addEvent('click',function(){
			var slide=$('login_slide');
			if(slide.hasClass('current')){
				$('login_slide').set('tween',{'duration':280}).tween('top',slide_top);
				$('login_slide').removeClass('current');								
			}
			else {
				$('login_slide').set('tween',{'duration':280}).tween('top',0);
				$('login_slide').addClass('current');
			}
			
			//var slide_in=new Fx.Tween('login_slide',{'duration':'260','wait':'false'});
			//slide_in.start({'top':'0'});
		});
	}
	
	if($('feedback')){
		var fb=$('feedback');
	
		fb.set('tween',{onComplete:function(){
			fb.set('tween',{duration:'1500'}).fade(0);
		},duration:'2200'}).fade(1);
		
		fb.addEvent('click',function(){
			this.set('tween',{duration:'700'}).fade(0);
		});
	}

	
	$$('a','button').addEvent('select',function(){
		return false;
	});
	
});

