window.addEvent('domready', function(){

	if($chk($('intro_overlay'))){ 
		$('intro_overlay').setStyle('visibility', 'visible');
		resize_overlay();
		position_container();
		window.addEvent('resize', function(){resize_overlay(); position_container();});
	
		$('intro_top').getElement('a[class=close]').addEvent('click', function(){$('intro_overlay').fade('out')});	
	}

// add smooth scrolling for internal links
	new SmoothScroll({ duration:700 }, window);

	$('services').addEvents({
		'mouseenter': function(){
			this.addClass('over');
		},
		'mouseleave': function(){
			this.removeClass('over');	
		}
	});

// animate service links
	$$('#services li a').each(function(el, x) {  
		var fx = new Fx.Morph(el,{ duration:300, link:'cancel' });						
		var myFx = new Fx.Scroll("service_header", {wait:false, duration:500, transition:Fx.Transitions.Cubic.easeInOut});
		el.addEvents({
			'mouseenter': function() {
				fx.start({ 'padding-left': '40px' }); 
				id = el.getParent().get('id').replace("s", "");		
				myFx.toElement("sh" + id);				
			},
			'mouseleave': function() { 
				fx.start({ 'padding-left': '14px' }); 				
				(function(){
					if($("service_header").getFirst().get('id').replace("sh","") != 1 && !$('services').hasClass('over')){
						myFx.toElement("sh"	+ $("service_header").getElement('li').get('id').replace("sh",""));
					}
				 }).delay(500);					
			}
		});
	}); 

// focus com logos
	img_focus($('com_logos'), {opacity: 0.2});
	
// process 'external' links	
	var t = $$("a[rel=external]");
	t.each(function(lnk, nr){
		lnk.set({'target':'_blank'});									
	});	

// scroll spy
	$('gototop').set('opacity','0').setStyle('display','block');
	
	/* scrollspy instance */
	var ss = new ScrollSpy({
		min: 200,
		onEnter: function(position,enters) {
			$('gototop').fade('in');
		},
		onLeave: function(position,leaves) {
			$('gototop').fade('out');
		},
		onTick: function(position,state,enters,leaves) {
		},
		container: window
	});

// togglers
	var myAccordion = new Fx.Accordion($$('.toggler'), $$('.toggle_container'), {
		onActive: function(toggler, element){
			toggler.addClass('sel');
		},
		onBackground: function(toggler, element){
			toggler.removeClass('sel');
		}													  
	});
	$$('.toggler').each(function(el){
		el.addClass('cursor');
		el.addEvents({
					 'mouseenter': function(){if(!this.hasClass('sel')){this.addClass('hover');}},
					 'mouseleave': function(){this.removeClass('hover');}
		});
	});

});

function img_focus(el, options){
	$(el).getElements('li').each(function(child) {
		var siblings = child.getParent().getElements('li').erase(child);
		child.addEvents({
			mouseenter: function() { siblings.tween('opacity',options.opacity); },
			mouseleave: function() { siblings.tween('opacity',1); }
		});
	});	
}

function resize_overlay(){
	var scrollSize = $(window).getScrollSize().y;
	var scrollTop = $(window).getScroll().y;
	$('intro_overlay').setStyles({ 'height':scrollSize+scrollTop,'top':-scrollTop });
}

function position_container(){
	var top = $(window).getSize().y/2;
	$('intro_container').setStyles({'top':top});
}
