// Manuel Pozo / info@poz.ch
// this scripts use mootools javascript library
// -------------------------------------------- //
// edited by Friend's Of Ed
// isbn-13	: 978-1-59059-533-6
// isbn 	: 1-59059-533-5
function addLoadEvent(func){
	var oldonload = window.onload;
	if(typeof window.onload != 'function'){
		window.onload = func;
	}else{
		window.onload = function(){
			oldonload();
			func();
		}
	}
}

// -------------------------------------------- //

function getElementsByClassName(strClass, strTag, objContElm){
	strTag = strTag || "*";
	objContElm = objContElm || document;
	var objColl = objContElm.getElementsByTagName(strTag);
	if (!objColl.length &&  strTag == "*" &&  objContElm.all) objColl = objContElm.all;
	var arr = [];
	var delim = strClass.indexOf('|') != -1  ? '|' : ' ';
	var arrClass = strClass.split(delim);
	for (var i = 0, j = objColl.length; i < j; i++) {
		var arrObjClass = objColl[i].className.split(' ');
		if (delim == ' ' && arrClass.length > arrObjClass.length) continue;
		var c = 0;
		comparisonLoop:
		for (var k = 0, l = arrObjClass.length; k < l; k++) {
			for (var m = 0, n = arrClass.length; m < n; m++) {
				if (arrClass[m] == arrObjClass[k]) c++;
				if (( delim == '|' && c == 1) || (delim == ' ' && c == arrClass.length)) {
					arr.push(objColl[i]);
					break comparisonLoop;
				}
			}
		}
	}
	return arr;
}
// To cover IE 5.0's lack of the push method
Array.prototype.push = function(value){
  this[this.length] = value;
}
// end 
// -------------------------------------------- //
// check browser stuff
function check_browser_support(){
	if(!document.getElementById) return false;
	if(!document.createElement) return false;
	if(!document.createTextNode) return false;
	if(!document.getElementsByTagName) return false;
	var version = navigator.appVersion;
	if (version.toLowerCase().indexOf("win")!=-1){
		_mac = false;
		if (navigator.appVersion.indexOf("MSIE")!=-1){
			var temp = navigator.appVersion.split("MSIE");
			_IE  = true; 
		}else{
			_IE = false;
		}
	}else{
		_mac = true;
	}
	return true;
}
// set external links to open in new window
function links_init(){
	$each($$('a'),function(e){
		if(e.getProperty('href') && e.getProperty('rel') == 'cfbox'){
			e.addEvent('click',function(ev){
				var ev = new Event(ev);
				ev.preventDefault();
				cfbox(e.getProperty('href'));
			});
		}else if(e.getProperty('href') && e.getProperty('rel') == 'external'){
			 e.setProperty('target','_blank');
		}else if(e.hasClass('video_object')){
			e.addEvent('click',function(ev){
				var ev = new Event(ev);
				ev.preventDefault();
				injectvideo(this);
			});
		}
	});
}
// video injection
function injectvideo(e){
	var src,w,h,vid;
	src= e.getProperty('rel');
	w = e.getFirst().getProperty('width');
	h = e.getFirst().getProperty('height');
	if(!_IE){
		vid = new Element('object',{
			'width':w,
			'height':h,
			'type':'application/x-shockwave-flash',
			'data':src
		});
		new Element('param',{'name':'movie','value':src}).inject(vid);
		new Element('param',{'name':'wmode','value':'window'}).inject(vid);
		new Element('param',{'name':'bgcolor','value':'#FFFFFF'}).inject(vid);
		e.replaceWith(vid);
	}else{
		vid = new Element('object');
		
		var mov,bg,wm;
		mov = new Element('param');
		bg = new Element('param');
		wm = new Element('param');
		
		mov.setAttribute('name','movie');
		mov.setAttribute('value',src);
		
		bg.setAttribute('name','bground');
		bg.setAttribute('value','#FFFFFF');
		
		wm.setAttribute('name','wmode');
		wm.setAttribute('value','window');
		
		mov.inject(vid);
		bg.inject(vid);
		wm.inject(vid);
		
		// set these attributes at the end as IE
		// erase them if we inject a param object afterward
		vid.setAttribute('width',w);
		vid.setAttribute('height',h);
		vid.setAttribute('data',src);
		vid.setAttribute('type','application/x-shockwave-flash');
		
		e.parentNode.replaceChild(vid,e);
	}
}
function cfbox(url){
	var bg = $('cfboxbg');
	var box = new Element('div',{
		'id':'cfbox'
	}).injectAfter(bg);
	
	var bgFx = new Fx.Styles(bg, {duration: 500,transition: Fx.Transitions.Cubic.easeInOut});
	var boxFx = new Fx.Styles(box, {duration: 500,transition: Fx.Transitions.Cubic.easeInOut});
	
	bgFx.start({
		'width':window.getWidth(),
		'height':window.getScrollHeight(),
		'opacity':0.6
	}).chain(function(){
		boxFx.start({
			'top':window.getScrollTop(),
			'width':750,
			'height':600,
			'opacity':1
		}).chain(function(){
			var rand,ifr,cb,arg;
			url.contains('?') ? arg = '&cfbox' : arg = '?cfbox';
			rand = new Date().getTime();
			ifr = new Element('iframe',{
				'id':'cfboxiframe',
				'name':'cfiframe'+rand,
				'width':730,
				'height':555,
				'src':url+arg+rand+'='+rand
			}).inject(box);
			
			cb = new Element('a',{
				'id':'cfboxcloser',
				'href':'#'
			}).inject(box);
			cb.setHTML('fermer');
			
			cb.addEvent('click',function(ev){
				var ev = new Event(ev);
				ev.preventDefault();
				ifr.remove(); // bye bye iframe
				boxFx.start({
					'width':1,
					'height':1,
					'opacity':0
				}).chain(function(){
					bgFx.start({
						'width':1,
						'height':1,
						'opacity':0
					}).chain(function(){
						box.remove(); // bye bye cfbox
					});
				});
			});
		});
	});
}
function metanav_init(){
	$('logger_bt').addEvent('click',function(){
		$('logger').getStyle('display') != 'block'  ? $('logger').setStyle('display','block') : $('logger').setStyle('display','none');
		return false;
	});
}
function calendartips_init(){
	var tips = getElementsByClassName('tips','td',$('grilleprog'));
	
	var mytips = new Tips(tips,{
		maxTitleChars: 150,
		initialize:function(){
			this.fx = new Fx.Style(this.toolTip, 'opacity', {duration: 500, wait: false}).set(0);
		},
		onShow: function(toolTip){
			this.fx.start(0.8);
		},
		onHide: function(toolTip){
			this.fx.start(0);
		}
	});
}
function agenda_init(){
	var scroller = new Fx.Scroll(window,{duration:500,transition:Fx.Transitions.Circ.easeInOut});
	var events = getElementsByClassName('event','ul',document.getElementById('content'));
	if(events.length>0){
		var togglers = getElementsByClassName('toggler');			// collect all togglers
		var stretchers = getElementsByClassName('stretcher');		// collect all stretchers
		var myaccordion = new Fx.Accordion(togglers,stretchers,{
			duration:500,
			onActive: function(tog,el){
				_active = tog;
				tog.setStyle('background-color','#f0f0f0');
			},
			onBackground: function(tog,el){
				tog.setStyle('background-color','#FFFFFF');
			},
			onComplete: function(){
				var offset;
				//togglers.indexOf(_active) ? offset = 0 : offset = 140; // scroll click top --- poz
				scroller.scrollTo(0,_active.getTop()-offset);
			}
		});
	}
	if($('pastevents')){
		var oldtogglers = getElementsByClassName('oldtoggler');			// collect all togglers
		var oldstretchers = getElementsByClassName('oldstretcher');		// collect all stretchers
		var myoldaccordion = new Fx.Accordion(oldtogglers,oldstretchers,{
			alwaysHide:true,
			display:false,
			duration:500,
			onActive: function(tog,el){
				_oldactive = tog;
			},
			onComplete: function(){
				scroller.scrollTo(0,_oldactive.getTop());
			}
		});	
	}
}
// initialisation
function romandie_init(){
	if(!check_browser_support()) return false;
	links_init();
	metanav_init();
	calendartips_init();
	agenda_init();
}
var _mac,_IE,_active,_oldactive;

addLoadEvent(romandie_init);