// JavaScript Document
window.addEvent('domready', function(){
	links_init();
	accordion_init();
});
function links_init(){
	var fxs =[];
	$each($$('a'),function(e,i){
		if(e.getProperty('href') && e.getProperty('rel') == 'cfbox'){
			fxs[i] = new Fx.Styles(e.getFirst(),{duration:300});
			e.num = i;
			e.addEvent('click',function(ev){
				var ev = new Event(ev).stop();
				cfbox(e.getProperty('href'));
			});
		}else if(e.getProperty('href') && e.getProperty('rel') == 'external'){
			 e.setProperty('target','_blank');
		}
	});
}

function cfbox(url){
	var w,h;
	if(url.contains('.mp3') || url.contains('.m4a')){
		w = 350;
		h = 50;
	}else if(url.contains('.mp4')){
		w = 340;
		h = 282;
	}else{
		w = 820;
		h = 600;
	}
	var bg = $('content');
	var box = new Element('div',{
		'id':'cfbox'
	}).injectAfter(bg);
	
	var boxFx = new Fx.Styles(box,{duration:500,transition: Fx.Transitions.Cubic.easeInOut});
	boxFx.start({
		'top':Math.ceil((window.getHeight()-h)/2),
		'left':Math.ceil((window.getWidth()-w)/2),
		'width':w+10,
		'height':h+20,
		'opacity':1
	}).chain(function(){
		var rand,ifr,cb;
		// we could define what is called here... mp3, mp4, jpeg, etc.
		url = 'libraries/pages/iframe.php?'+url;
		rand = new Date().getTime();
		ifr = new Element('iframe',{
			'id':'cfboxiframe',
			'name':'cfiframe'+rand,
			'frameborder':0,
			'width':w,
			'height':h,
			'src':url+'/'+rand
		}).inject(box);
		
		cb = new Element('a',{
			'id':'cfboxcloser',
			'href':'#'
		}).inject(box);
		cb.setText('fermer');
		
		cb.addEvent('click',function(ev){
			var ev = new Event(ev).stop();
			ifr.remove(); // bye bye iframe
			this.remove();
			boxFx.start({
				'width':1,
				'height':1,
				'opacity':0
			}).chain(function(){
				box.remove(); // bye bye cfbox
			});
		});
	});
}

function accordion_init(){
	var accordion = new Accordion($$('.toggler'),$$('.stretcher'), {
		opacity: false,
		onActive: function(toggler,element){
			toggler.removeClass('dim');
		},
		onBackground: function(toggler,element){
			toggler.addEvent('mouseover',function(){
				if(this.hasClass('dim')) this.setStyle('opacity','0.9');
			});
			toggler.addEvent('mouseout',function(){
				if(this.hasClass('dim')) this.setStyle('opacity','0.75');
			});
			toggler.setStyle('opacity','0.75');
			toggler.addClass('dim');
		}
	}, $('coit'));
}