// 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({'border-top-width':4,'padding-top':1});
				},
				'mouseleave':function(){
					fx[this.i].start({'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('tween', {duration: 'short', transition: 'sine:out',link:'cancel'});
		if(!el.hasClass('sel')){
			el.addEvents({
				'mouseenter':function(){
					this.tween('background-color','#bb856d');
				},
				'mouseleave':function(){
					this.tween('background-color','#f1cb73');
				},
				'click':function(){
					window.location = this.getFirst().getAttribute('href');
				}
			});
		}
	});
	$$('img.insertedImg').addEvent('click',function(){
		if(this.get('src').test('/mini_')){
			var s = this.getSize();
			poper(this.get('src').replace('/mini_','/'),s.x*5,s.y*5);
		}
	});
	// --- googlemap ---
	if($('gmap')){
		loadGmap();
	}
}
function loadGmap(){
	if (GBrowserIsCompatible()){
		var map = new GMap2($('gmap'),{mapTypes:[G_HYBRID_MAP,G_NORMAL_MAP]});
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		var point = new GLatLng(46.965369,6.858602);
		map.setCenter(point, 17);
		var m = new GMarker(point);
		map.addOverlay(m);
		GEvent.addListener(m, "click", function(){
			var win = window.open('http://maps.google.com/maps?f=q&source=s_q&hl=fr&geocode=&q=&aq=&sll=47.014205,6.839676&sspn=0.173455,0.361176&ie=UTF8&ll=46.965369,6.858602&spn=0.002804,0.005643&t=h&z=18','_blank');
		});
		document.body.addEvent('unload',function(){
			GUnload();
		});
	}
}
function poper(source,w,h){
	if(!_pops.contains(source) && _popready){
		_pops.include(source);
		_popready = false;
		var img,x,y;
		img = new Element('img',{
			'id':'imgbox',
			'width':1,
			'height':1,
			'src':source,
			'style':{
				'background-color':'#ccc',
				'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,
			'background-color':'#ccc',
			'opacity':1
		});
	}
	return false;
}
window.addEvent('domready', function(){
	_pops = [];
	_popready = true;
	linksInit();
	siteInit();
});
