window.addEvent('domready', function(){
	$$('.external').setProperty('target', '_blank');
	
	var tooltip = new Element('div', {'class':'tooltip'}).inject($(document.body));
	var tooltip_sleeve = new Element('div', {'class':'tooltip_sleeve'}).inject(tooltip);
	var tooltip_morph = new Fx.Morph(tooltip, {duration:100, transition:'sine:out', link:'cancel'});
	/* INFO BOXES */
	/*
	$$('.info_box').each(function(el){
																
		el.addEvents({
			'mouseover':function(){
				var tooltip_content = this.getElements('.tooltip_content')[0].get('html');
				tooltip_sleeve.set('html', tooltip_content);
				var this_pos = this.getPosition();
				var this_size = this.getSize();
				var tooltip_size = tooltip.getSize();
				var top = this_pos.y-tooltip.getSize().y+17;
				tooltip.setStyles({
					'top':top+20,
					'left':this_pos.x+(this_size.x/2)-(tooltip_size.x/2),
					'opacity':0
				});
				tooltip_morph.start({
					'top':top,
					'opacity':1
				});	
				return false;
			},
			'mouseleave':function(){
				tooltip.setStyle('left',-9999);
			}
			
		});
	});
	*/
	$$('.subnav_box span').each(function(el){
		var subnav_morph = new Fx.Morph(el, {duration:100, transition:'sine:out', link:'cancel'});
		el.setStyle('backgroundPosition', '-20px 8px');
		el.addEvents({
			'mouseenter':function(){
				subnav_morph.start({'paddingLeft': 32, 'backgroundPosition': '17px 8px'});
			},
			'mouseleave':function(){
				subnav_morph.start({'paddingLeft': 16})
				el.setStyle('backgroundPosition', '-20px 8px');
			}
		});
	});
	
	
	var nav_width = 0;
	$$('#nav>ul>li').each(function(el){
		nav_width += el.getSize().x;
	});
	$$('#nav>ul').setStyle('width', nav_width);
	$('nav').setStyle('visibility', 'visible');
	
	//sub nav
	var shadow_offset = 10;
	var subnav_box_width = 292;
	var sub_nav_box_x = -9999;
	
	$$('#nav>ul>li').addEvents({
		'mouseenter':function(){
			//if there is a subnav for that mainnav item
			var subnav_box = this.getElements('.subnav_box')[0];
			if(subnav_box){
				var nav_item_x = this.getPosition(this.getParents('ul')[0]).x;
				var nav_item_w = this.getSize().x;
				//if subnav goes off screen to right
				//if (nav_item_x+subnav_box_width > nav_width){
					//if(Browser.Engine.trident4) //ie6
						//sub_nav_box_x = nav_item_x-subnav_box_width+nav_item_w+(shadow_offset*2);
					//else
						//sub_nav_box_x = nav_item_x-subnav_box_width+nav_item_w+shadow_offset;
				//}
				//else{
					if(Browser.Engine.trident4) //ie6
						sub_nav_box_x = nav_item_x;
					else
						sub_nav_box_x = nav_item_x-shadow_offset;
				//}
				subnav_box.setStyle('left', sub_nav_box_x);	
				this.getElement('a').addClass('over');
			}

		},
		'mouseleave':function(){
			var subnav_box = this.getElements('.subnav_box')[0];
			if(subnav_box){
				subnav_box.setStyle('left', -9999);
			}
			this.getElement('a').removeClass('over');

				
		}
	});
	
	//for keyboard navigation
	$$('#nav>ul>li>a').addEvents({
		'focus':function(){
			//if there is a subnav for that mainnav item
			var subnav_box = this.getNext();
			if(subnav_box){
				var nav_item_x = this.getPosition(this.getParents('ul')[0]).x;
				var nav_item_w = this.getSize().x;
				//if subnav goes off screen to right
				if (nav_item_x+subnav_box_width > nav_width)
					sub_nav_box_x = nav_item_x-subnav_box_width+nav_item_w+shadow_offset;
				else
					sub_nav_box_x = nav_item_x-shadow_offset;
				subnav_box.setStyle('left', sub_nav_box_x);				
				this.addClass('over');
			}
		},
		'blur':function(){
			var subnav_box = this.getNext();
			if(subnav_box){
				subnav_box.setStyle('left', -9999);
			}
			this.removeClass('over');

				
		}
	});
	
	$$('#nav ul li div a').addEvents({
		'focus':function(){
			var nav_item_x = this.getParents('li')[1].getPosition(this.getParents('ul')[1]).x;
			var nav_item_w = this.getParents('li')[1].getSize().x;
			//if subnav goes off screen to right
			if (nav_item_x+subnav_box_width > nav_width)
				sub_nav_box_x = nav_item_x-subnav_box_width+nav_item_w+shadow_offset;
			else
				sub_nav_box_x = nav_item_x-shadow_offset;
			this.getParents('.subnav_box')[0].setStyle('left', sub_nav_box_x);
			var nav_a = this.getParents('li')[1].getElements('a')[0];
			nav_a.addClass('over');

		},
		'blur':function(){
			this.getParents('.subnav_box')[0].setStyle('left', -9999);
			var nav_a = this.getParents('li')[1].getElements('a')[0];
			nav_a.removeClass('over');

				
		}
	});
	
	
});
window.addEvent('load', function(){
	
	var SlideyGallery = new Class({
		initialize: function(params){
			this.gallery = params.gallery;
			this.move_in_px = params.move_in_px || 80;
			this.moving = false;
			this.window_width = this.gallery.getElements('.gallery_window')[0].getSize().x;
			this.slidey_width = 0;
			this.gallery.getElements('li').each(function(li){ this.slidey_width += li.getSize().x; }.bind(this));
			
			this.extent = this.slidey_width-this.window_width;
			this.gallery.getElements('li:last-child').addClass('last');
			this.slidey = params.gallery.getElements('.slidey')[0];
			this.slidey.setStyle('width', this.slidey_width);
			this.fx_slidey = new Fx.Tween(this.slidey, {transition:'sine:out', duration:500, onComplete:function(){this.moving = false;}.bind(this)});
			this.index = 0;
			this.thumbs_goto = 0;
			this.right_reached = false;
			this.gallery.getElements('.scroll_right')[0].addEvent('click', this.next.bind(this));
			this.gallery.getElements('.scroll_left')[0].addEvent('click', this.previous.bind(this)); 
		},
		next: function (){
			if (this.slidey_width>this.window_width){
		
				if (!this.right_reached && !this.moving){
					this.index++;
					if ((this.move_in_px*this.index)>this.extent){
						this.thumbs_goto = -this.extent;
						this.right_reached = true;
					}
					else
						this.thumbs_goto = this.index*-this.move_in_px;
					this.moving = true;
					this.fx_slidey.start('left', this.thumbs_goto);
				}
			}
			return false;
		},
		previous: function (){
			if (this.slidey_width>this.window_width){
		
				if (this.index!=0 && !this.moving){
					this.index--;
					if (this.index == 0){
						this.thumbs_goto = 0;
					}
					else				
						this.thumbs_goto = this.index*-this.move_in_px;
					this.right_reached = false;
					this.moving = true;
					this.fx_slidey.start('left', this.thumbs_goto);
				}
			}
			return false;
		}
	});
	if($$('.gallery')[0])
		new SlideyGallery({gallery: $$('.gallery')[0], move_in_px:350});
	$$('.scroll_content').each(function(el){
		var scroll_bar = new Element('div', { 'class':'scrollbar' });
		var handle = new Element('div', { 'class':'handle' });
		handle.inject(scroll_bar);
		scroll_bar.inject(el.getParent(), 'top');
		makeScrollbar(el, scroll_bar, handle);
	});
});
		
function makeScrollbar(content,scrollbar,handle,horizontal,ignoreMouse){
	if (content.getChildren('.content_container_sleeve').length > 0){
		if (content.getChildren('.content_container_sleeve')[0].getSize().y<content.getSize().y)
			scrollbar.setStyle('display','none');
	}	
	var steps = horizontal?(content.getScrollSize().x - content.getSize().x):(content.getScrollSize().y - content.getSize().y);
	var slider = new Slider(scrollbar, handle, {	
		steps: steps,
		mode: (horizontal?'horizontal':'vertical'),
		onChange: function(step){
			// Scrolls the content element in x or y direction.
			var x = horizontal?step:0;
			var y = horizontal?0:step;
			content.scrollTo(x,y);
		}
	});
	if(!(ignoreMouse)){
		// Scroll the content element when the mousewheel is used within the 
		// content or the scrollbar element.
		$$(content, scrollbar).addEvent('mousewheel', function(e){	
			e = new Event(e).stop();
			var step = slider.step - e.wheel * 30;	
			slider.set(step);	
		});
	}
	// Stops the handle dragging process when the mouse leaves the document body.
	$(document.body).addEvent('mouseleave',function(){slider.drag.stop()});
}