//console.log("Hi");
window.addEvent('domready', function() {	
	//console.log("HELLO dom");
	//new MoveMenu($$('.mainmenu'), 'mainmenu');
	new ArrangeMenu();
	
	new CleanMissingImages($$('.columns'));
	new mooShadow();
	
	new HappyForm();
	//new HappyCertificate();
	
	new FlvPlayer();
	
	new AccordionAndZoom();
	
	new NiceLangs();
	
	
});

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

	new HappyCertificate();

	
});



////////////////////////////////////////////////////////////////////////////



var NiceLangs = new Class(
{

	initialize: function(){		
		
		this.langs_a = $$('ul.lang li a');
		if (!this.langs_a) return;
		
		this.langs_a.each(function(a){
			var t = a.get('text');
			switch (t)
			{
			case 'fr':
				a.set('text', 'Français');
			break;
			case 'de':
				a.set('text', 'Deutsch');
			break;
			case 'en':
				a.set('text', 'English');
			break;
			case 'pt':
				a.set('text', 'Português');
			break;
			default:
				
			break;
			}
		});
		
		
	} 
			
});// end class





////////////////////////////////////////////////////////////////////////////




var AccordionAndZoom = new Class(
{

	initialize: function(){		
		
		
		
		var myAccordion = new Accordion('.opener', '.drawer', '.content', {
		    onComplete: function(){
		       // this.openBindedBlock('.content')
		    }
	    });
	    
	    ///////
		
		this.imgs = $$('.doyouknow');
		
		
		this.mask =   new Element('div').addClass('bgmask').inject(window.document.body).setStyle('opacity',0.01);
		this.maskfx = new Fx.Morph(this.mask,{duration: 700, transition: Fx.Transitions.Sine.easeInOut});
		this.mask.addEvent('click', function(){							
				this.closeZoom();
		}.bind(this));
		
		
		this.imgs.each(function(certif){	
			
			certif.img =  certif.getElement('img');
			
			certif.imgx = certif.img.getSize().x;
			certif.imgy = certif.img.getSize().y;
			
			if (certif.imgy > certif.imgx) {
				certif.img.setStyles({'width':'auto','height':160});
			}
			// console.log('certif.img'+certif.img.getSize().y);
		
			certif.btn = this.createZoomButton(certif);
			certif.btn.addEvent('click', function(){							
				this.zoom(certif);
			}.bind(this));
			
		}.bind(this));
		
		this.zoomimg = new Element('img').addClass('zoomer').inject(window.document.body);
		this.zoomimgfx = new Fx.Morph(this.zoomimg,{duration: 450, transition: Fx.Transitions.Sine.easeInOut});
		this.zoomisopen = false;




		
	},
	
	
	createZoomButton:function(certif){
		var btn = certif.img; 
		return btn;
	},
	
	closeZoom:function(certif){
		this.zoomimg.setStyles({
			'top': -10000
		});
		this.mask.setStyles({
			'display': 'none'
		});
		this.zoomisopen = false;
	},
	
	zoom:function(certif){
		
		if (this.zoomisopen == true) return;
		
		this.zoomimg.set('src', certif.img.getProperty('src'));
		this.zoomimg.setStyles({
			'width': 'auto',
			'height': 'auto'
		});
		var finalx = this.zoomimg.getSize().x;
		var finaly = this.zoomimg.getSize().y;
		var finaltop =  (window.getSize().y-finaly) / 2 + window.getScroll().y;
		var finalleft = (window.getSize().x-finalx) / 2;
		
		this.zoomimg.setStyles({
			'top': certif.img.getCoordinates().top-5,
			'left': certif.img.getCoordinates().left-5,
			'width': certif.imgx,
			'height': certif.imgy
		});
		
		this.zoomimgfx.start({
			'width': finalx,
			'height': finaly,
			'top': finaltop,
			'left':finalleft
		});
		
		this.maskfx.start({
			'display':'block',
			'opacity': 0.7
		});
		
		this.zoomisopen = true;
		
		this.zoomimg.addEvent('click', function(){							
				this.closeZoom();
		}.bind(this));
		
	}
			
});// end class




////////////////////////////////////////////////////////////////////////////



var FlvPlayer = new Class(
{

	initialize: function(){		
		
		this.player = $('player');
		if (!this.player) return;
		
		this.isclosed = true;
		//this.playerfx = new Fx.Reveal(this.player, {duration: 500, mode: 'horizontal'});

		//el.reveal(); //show the element
		//el.dissolve(); //hide it
		
		var btn = $('videobtn');

		btn.addEvent('click', function(){
			if (this.isclosed == true){
				this.player.setStyle('display','block');
				this.isclosed = false;
			}else{
				this.player.setStyle('display','none');
				this.isclosed = true;
			}
		}.bind(this));
		
	} 
			
});// end class




////////////////////////////////////////////////////////////////////////////



var HappyCertificate = new Class(
{

	initialize: function(){		

		this.form = $('certificateForm');
		if (!this.form) return;
		
		
		this.certifs = $$('.certificate');
		
		
		this.mask =   new Element('div').addClass('bgmask').inject(window.document.body).setStyle('opacity',0.01);
		this.maskfx = new Fx.Morph(this.mask,{duration: 700, transition: Fx.Transitions.Sine.easeInOut});
		this.mask.addEvent('click', function(){							
				this.closeZoom();
		}.bind(this));
		
		this.actualselectedcertif = 'undefined';
		this.certifs.each(function(certif){	
			
			certif.addEvent('click', function(){							
				this.selectCertif(certif);
			}.bind(this));
			
			certif.input = certif.getElement('input'); 
			certif.img =  certif.getElement('img');
			certif.img.setStyle('display','block');
			certif.imgx = certif.img.getSize().x;
			certif.imgy = certif.img.getSize().y;
			
			if (certif.imgy > certif.imgx) {
				certif.img.setStyles({'width':'auto','height':160});
			}
			// console.log('certif.img'+certif.img.getSize().y);
		
			certif.btn = this.createZoomButton(certif);
			certif.btn.addEvent('click', function(){							
				this.zoom(certif);
			}.bind(this));
			
		}.bind(this));
		
		this.zoomimg = new Element('img').addClass('zoomer').inject(window.document.body);
		this.zoomimgfx = new Fx.Morph(this.zoomimg,{duration: 450, transition: Fx.Transitions.Sine.easeInOut});
		this.zoomisopen = false;
		
		var radioscertif = this.form.getElements('input[name=certificate]');
		//radioscertif[0].setProperty('eform', 'Choix du modèle de certificatCACA::1');
		
		this.moneyChooser();
		
		this.checkDifferentAdress();
		
		this.checkTopTitle();
		this.checkText();
		
		
	},
	
	selectCertif:function(certif){
		//console.log(certif+' :certif');
		if  (this.actualselectedcertif != 'undefined') {
			this.actualselectedcertif.img.removeClass('thumbselected');
			this.actualselectedcertif.input.setProperty('checked','');
		}
		
		certif.img.addClass('thumbselected');
		certif.input.setProperty('checked','checked');
		this.actualselectedcertif = certif;
	},
	
	
	checkText:function(){
		
		var cbtext = $('notextwantedcheckbox');
		var txt = $('textwantedarea');
		
		cbtext.addEvent('click', function(){
			
			if (cbtext.getProperty('checked')==true){
				txt.setProperty('disabled','disabled');
			}else{
				txt.setProperty('disabled','');
			}
		});
	},
	
	
	checkTopTitle:function(){
		
		var toptitleradios = this.form.getElements('input[name=toptitle]');
		var toptitle = $('toptitlearea');
		
		
		toptitleradios.addEvent('click', function(radiobtn){
			//alert('click!'+ $('toptitleradio_on').getProperty('checked'));
			if ($('toptitleradio_on').getProperty('checked')==true){
				toptitle.setProperty('disabled','');
			}else{
				toptitle.setProperty('disabled','disabled');
			}
		});
	},
	
	
	
	checkDifferentAdress:function(){
		var checkboxadress = $('directsend');
		var otheradress = $('otheradress');
		
		checkboxadress.addEvent('click', function(){
			if (checkboxadress.getProperty('checked')==true){
				otheradress.setStyle('display','block');
			}else{
				otheradress.setStyle('display','none');
			}
		});
	},
	
	moneyChooser:function(){
			var devisechf = $('deviseCHF')
			devisechf.setProperty('checked','checked');
			
			this.currency = $('currency');
			this.amount   = $('totalamount');
			this.infosCH  = $('paymentinfo_CH');
			this.infosFR = $('paymentinfo_FR');
			this.ntree = $('totaltree');
			this.currentmoney = 'CHF';
			this.treeprice=20;
			
			this.infosFR.setStyle('display','none');
			
			var radios = this.form.getElements('input[name=devise]');
		
			radios.addEvent('click', function(){							
				if (devisechf.getProperty('checked')==true){
					this.chooseCHF();
				}else{
					this.chooseEUR();
				}
				this.calculateAmount();
			}.bind(this));
			
			(function(){this.calculateAmount(); }.bind(this)).periodical(1000);
			
			/*this.ntree.addEvent('change', function(){							
				this.calculateAmount();
			}.bind(this));*/
			
	},
	
	calculateAmount:function(){
		var price = this.ntree.value * this.treeprice;
		this.amount.set('html',price);
		$('amount').value = price; //hidden input
		
	},
	
	chooseCHF:function(){
		this.currentmoney = 'CHF';
		this.treeprice=20;
		this.infosCH.setStyle('display','');
		this.infosFR.setStyle('display','none');
		this.currency.set('html','CHF');
	},
	
	chooseEUR:function(){
		this.currentmoney = 'EUR';
		this.treeprice=15;
		this.infosCH.setStyle('display','none');
		this.infosFR.setStyle('display','');
		this.currency.set('html','EUR');
	},
	
	
	
	
	createZoomButton:function(certif){
		var btn = new Element('a').addClass('zoom').inject(certif,'top'); 
		return btn;
	},
	
	closeZoom:function(certif){
		this.zoomimg.setStyles({
			'top': -10000
		});
		this.mask.setStyles({
			'display': 'none'
		});
		this.zoomisopen = false;
	},
	
	zoom:function(certif){
		
		if (this.zoomisopen == true) return;
		
		this.zoomimg.set('src', certif.img.getProperty('src'));
		this.zoomimg.setStyles({
			'width': 'auto',
			'height': 'auto'
		});
		var finalx = this.zoomimg.getSize().x;
		var finaly = this.zoomimg.getSize().y;
		var finaltop =  (window.getSize().y-finaly) / 2 + window.getScroll().y;
		var finalleft = (window.getSize().x-finalx) / 2;
		
		this.zoomimg.setStyles({
			'top': certif.img.getCoordinates().top-7,
			'left': certif.img.getCoordinates().left-7,
			'width': certif.imgx,
			'height': certif.imgy
		});
		
		this.zoomimgfx.start({
			'width': finalx,
			'height': finaly,
			'top': finaltop,
			'left':finalleft
		});
		
		this.maskfx.start({
			'display':'block',
			'opacity': 0.7
		});
		
		this.zoomisopen = true;
		
		this.zoomimg.addEvent('click', function(){							
				this.closeZoom();
		}.bind(this));
		
	}
			
});// end class


////////////////////////////////////////////////////////////////////////////



var HappyForm = new Class(
{

	initialize: function(){		

		this.form = $('Form');
		if (!this.form) return;
		
		this.radio_otheramount = $('amount3');
		this.otheramount = $('other_amount');
		this.currency = $('currency');

		this.otheramount.setStyle('opacity', 0.5);
		this.currency.setStyle('opacity', 0.5);

		this.radios = this.form.getElements('input[name=donation_amount]');
		
		this.radios.each(function(radiobtn){	
			
			radiobtn.addEvent('click', function(){							
				if(radiobtn == this.radio_otheramount){
					this.otheramount.setStyle('opacity', 1);
					this.currency.setStyle('opacity', 1);
					this.otheramount.focus();
				}else{
					this.otheramount.setStyle('opacity', 0.5);
					this.currency.setStyle('opacity', 0.5);
					this.otheramount.value = "";
					this.currency.value = "CHF";	
				}
			}.bind(this));
			
			
		}.bind(this));

		
		/*
				
		this.otheramount.setStyle('opacity', 0.5);
		this.currency.setStyle('opacity', 0.5);
		
		
		this.radios.addEvent('click', function(){	
			console.log('click')						
				this.otheramount.setStyle('opacity', 1);
				this.currency.setStyle('opacity', 1);
		}.bind(this));
		*/
	} 
			
});// end class




//////////////////////////////////////////////////////////////////////

/*
	Class:  MooShadow
		Add images shadow
		
	Usage:
		Add a shadow to any element with a class 'shadow'
	
	Exemple:
		(code)
		<script type="text/javascript">
			window.addEvent('domready', function() {
				var MooShadow = new MooShadow();
			});
		</script>	
		(end code)
	
	About:
		mooShadow.js v.1.0 for mootools v1.1 06 / 2007
		
		by Floor SA (http://www.floor.ch) MIT-style license
		
		Created by Luca Pillonel and David Mignot
*/


var mooShadow = new Class({
	initialize: function(options){
		var imgs = this._getImages();
	},
	_getImages:function(){
		var s = $$('.frame');
		
			s.each(function(el, n){
				
				
				if (el.get('tag')!= 'img') {
					
					if (el.getChildren().length == 0) return;
					this._addShadow(el);
					 
				}else {
					if (el.get('src')!=null){
					this._addShadow(el);
					}else{
						el.setStyle('display', 'none');
					}
				}
			}, this);
	},
	_addShadow:function(el){
		var iefix = "";
		if(window.ie != window.ie7){
		//	iefix = "-ie";
		}
		el.shadowTable = new Element('table').addClass('shadowTable').injectAfter(el);
		el.shadowtbody = new Element('tbody').addClass('shadowTable').injectInside(el.shadowTable);
		el.shadowTable.cellPadding ='0';
		el.shadowTable.cellSpacing ='0';
		el.shadowTable.border ='0';
		if(el.hasClass('left'))
			el.shadowTable.addClass('left');
		if(el.hasClass('right'))
			el.shadowTable.addClass('right');
		el.shadowTable.setStyle('position', el.getStyle('position'));
		el.shadowTable.setStyle('margin', el.getStyle('margin'));
		
		
		el.tr1 = new Element('tr').injectInside(el.shadowtbody);
			el.topLeft = new Element('td').addClass('tl'+iefix).injectInside(el.tr1);
			el.shtop = new Element('td').addClass('t'+iefix).injectInside(el.tr1);
			el.topRight = new Element('td').addClass('tr'+iefix).injectInside(el.tr1);
		el.tr2 = new Element('tr').injectInside(el.shadowtbody);
			el.shleft = new Element('td').addClass('l'+iefix).injectInside(el.tr2);
			el.shimg = new Element('td').addClass('img').setProperty('id', 'imgtd').injectInside(el.tr2);
			el.shright = new Element('td').addClass('r'+iefix).injectInside(el.tr2);
		el.tr3 = new Element('tr').injectInside(el.shadowtbody);
			el.bottomLeft = new Element('td').addClass('bl'+iefix).injectInside(el.tr3);
			el.shbottom = new Element('td').addClass('b'+iefix).injectInside(el.tr3);
			el.bottomRight = new Element('td').addClass('br'+iefix).injectInside(el.tr3);
		el.injectInside(el.shimg);
	}
});










///////////////////////////////////////////////////////////////////////



var CleanMissingImages = new Class(
{

	initialize: function(target){		

		if (target=="") return;
		
		this.target = target[0];
		
		var imgs = this.target.getElements('img');
		
		imgs.each(function(img){	
				if(img.get('src')== null ) {
					img.getParent().destroy();
				}
		});  	
	} 
			
});// end class


////////////////////////////////////////////////////////////////////////////


var ArrangeMenu = new Class(
{

	initialize: function(){		
		
		
		
		
		var ul = $$('#marginleft ul#menu');
		
		if (ul[0] != 'undefined'){
			
			if (ul.getElement('ul') == ''){
				ul.addClass('submenu');
			}else{
				$$('ul#menu li.top').each(function(li){
					var a = li.getFirst('a');
					a.set('href','javascript:void(0)');
					a.setStyle('cursor','default');
				});
			};
		}
		
		//// home: 
		
		var hul = $$('#projects ul');
		
		
		
		if (hul[0] != 'undefined'){
			hul.each(function(ul){
				if (ul.getElement('ul') == null){
					ul.addClass('submenu');
					
				}else{
					$$('#projects ul li.top').each(function(li){
						var a = li.getFirst('a');
						a.set('href','javascript:void(0)');
						a.setStyle('cursor','default');
					});
				};
			});
			var two_cols_lis = $$('#projects .col3-4 ul ul li')
			if (two_cols_lis.length == 0) return;
			
			if (! $('projects')) return;
			
			
			var half = Math.round(two_cols_lis.length/2);
			
			var leftcol = new Element('div').addClass('leftcol').inject($$('#projects .col3-4 ul ul')[0]);
			var rightcol = new Element('div').addClass('rightcol').inject($$('#projects .col3-4 ul ul')[0]);
			
			
			for (var i=0 ; i<half; i++) {
				two_cols_lis[i].inject(leftcol);
			}
			for (var half=0 ; i<two_cols_lis.length; i++) {
				two_cols_lis[i].inject(rightcol);
			}
			

		}
		
				
	} 
			
});// end class

/*


var MoveMenu = new Class(
{

	initialize: function(target, csssuffix){		
		
		this.csssuffix = csssuffix;
		
	 	target.each(function(menu){	
				this.as = menu.getElements('a');
				
				this.as.each(function(a){	
					
					a.effects = new Fx.Morph(a,{duration: 350, transition: Fx.Transitions.Sine.easeInOut});
		
					a.addEvent('mouseenter', function(){							
						a.effects.cancel();
						a.effects.set('.mouseenter_'+this.csssuffix);
					}.bind(this));
						
					a.addEvent('mouseleave',  function(){
						a.effects.cancel();
						a.effects.start('.mouseleave_'+this.csssuffix);
					}.bind(this));
					
				}.bind(this));

		}.bind(this));  	
	} 
			
});// end class

*/



