// JavaScript Document
function linksInit(){
	$each($$('#site a'),function(el){
		if(el.getAttribute('rel') == 'external'){
			el.setAttribute('target','_blank');
		}
	});
}
function siteInit(){
	var fx =[];
	var timer = 0;
	// menu button anim
	$$('#menu li').each(function(el,index){
		el.i = index;
		timer += 150;
		fx[index] = new Fx.Morph(el,{duration:650,transition:'back:in:out',link:'cancel'});
		if(!location.search){
			fx[index].set({'top':40});
			fx[index].start.delay(timer,fx[index],{'top':0});
		}
		fx[index].setOptions({duration:300});
		if(!el.hasClass('sel')){
			el.addEvents({
				'mouseenter':function(){
					fx[this.i].start({'background-color':'#ddd','border-top-width':4,'padding-top':1});
				},
				'mouseleave':function(){
					fx[this.i].start({'background-color':'#ccc','border-top-width':0,'padding-top':5});
				},
				'click':function(){
					window.location = this.getFirst().getAttribute('href');
				}
			});
		}
	});
	// submenu anim
	$$('#sousmenu li.sub').each(function(el,index){
		el.set('morph', {duration: 'short', transition: 'sine:out',link:'cancel'});
		if(!el.hasClass('sel')){
			el.addEvents({
				'mouseenter':function(){
					this.morph({'border-bottom-color':'#f5f','background-color':'#ddd'});
				},
				'mouseleave':function(){
					this.morph({'border-bottom-color':'#000','background-color':'#ccc'});
				},
				'click':function(){
					window.location = this.getFirst().getAttribute('href');
				}
			});
		}
	});
}
function poper(source,type,w,h,id){
	if(!_pops.contains(source) && _popready){
		_pops.include(source);
		_popready = false;
		var img,x,y;
		
		img = new Element('img',{
			'id':'imgbox',
			'alt':'',
			'width':1,
			'height':1,
			'src':source,
			'style':{
				'opacity':0,
			}
		}).inject($('site'));
		
		var imgFx = new Fx.Morph(img,{transition:'cubic:in:out'});
		
		img.addEvent('click',function(ev){
			var ev = new Event(ev).stop();
			imgFx.start({
				'width':1,
				'height':1,
				'opacity':0
			}).chain(function(){
				img.remove(); // bye bye img
				_pops.remove(source);
				_popready = true;
			});
		});
		
		x = Math.ceil((window.getHeight()-h)/2)+window.getScrollTop();
		y = Math.ceil((window.getWidth()-w)/2)+window.getScrollLeft();
		if(x<0) x = 10;
		if(y<0) y = 10;
	
		imgFx.start({
			'top':x,
			'left':y,
			'width':w,
			'height':h,
			'opacity':1
		});
	}
	return false;
}
function blinkIt(el){
	el.highlight('#d7a','#111');
}
function bannerInit(){
	if(!location.search){
		var timer = 100;
		var fx = [];
		$$('#barbs img').each(function(el,index){
			el.setStyle('left',-950);
			fx[index] = new Fx.Tween(el,'left',{duration:250,transition:'back:out'});
			fx[index].start.delay(timer,fx[index],0);
			timer += 300;
		});
		$$('#banner div.bar').each(function(el,index){
			el.setStyle('background-color','transparent');
			blinkIt.delay(timer,blinkIt,el);
			timer += 200;
		});
		var b = new Fx.Tween($('bent'),'opacity');
		b.set(0);
		b.start.delay(timer,b,.9);
	}
}
window.addEvent('domready', function(){
	_pops = [];
	_popready = true;
	bannerInit();
	linksInit();
	siteInit();
});