window.addEvent('domready', function() {
	
	document.body.quote = new CenterQuote();
	
	new DisplayEventList();
	
	var myAccordion = new Fx.Accordion($$('.track'), $$('.drawer'), {
	    display: 0,
	    alwaysHide: true
	});
	
	new HappyForm();
	
	
});

window.addEvent('load', function() {
	
	document.body.quote.activate();
	
});







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


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

		this.form = $('command');
		if (!this.form) return;
		
		
		this.albums = $$('.album');
		
		
		
		
		this.albums.each(function(album){
			album.cb = album.getElement('.cb');
			album.n = album.getElement('.n');
			album.name = album.getElement('.name').get('html');
			album.price = album.getElement('.price').get('html').toInt();
					
				
			album.cb.addEvent('click', function(){
				if (album.cb.checked){
					album.n.set('value', 1);
				}else{
					album.n.set('value', '');
				}
				this._calculateAmount();
				this._updateAlbumsField();
			}.bind(this));
			
			album.n.addEvent('blur', function(){
				if ((album.n.value =='')||(album.n.value.toInt() ==0)){
					album.n.value ='';
					album.cb.checked = false;
				}else{
					album.cb.checked = true;
				}
				this._calculateAmount();
				this._updateAlbumsField();
			}.bind(this));
			
			
		}.bind(this));
		
		this._selectFirstAlbum();
		this._calculateAmount();
		this._updateAlbumsField();
		
		/*;*/
	},
	
	_updateAlbumsField:function(){
		
		var selectedalbums = this.form.getElements('input:checked');
		
		var albumsstring = "";
		
		selectedalbums.each(function(album){
			var n = album.getParent('.album').n.value;
			var name = album.getParent('.album').name;
			albumsstring +=	"<strong>"+name+"</strong> (nombre: "+n+")\n";
		});
		
		$('albums').set('value', albumsstring);
		
	},
	
	_selectFirstAlbum:function(){
		
		var post_album_id = $('selectedcd').get('value');
		
		
		if (post_album_id){
			$(post_album_id).cb.checked = true;
			$(post_album_id).n.value = 1;
		}
	},
	
	_calculateAmount:function(){
		
		var sum = 0;
		
		this.albums.each(function(album){	
			if(isNaN(parseInt(album.n.value))){ 
				var n = 0;
			}else {
				var n = album.n.value.toInt();
			}
			sum += n * album.price;
		});
		
		$('sum').set('html', sum);
		$('amount').value = sum;
	
	}
	
	/*
	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);
			
			
			
	},
	
	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 DisplayEventList = new Class({
	initialize: function(){
		
		if (!$$('.events')[0]) return;
		
		this.events = $$('.event');
		
		this.baseurl = this.getBaseURL();
		
		//console.log(this.baseurl);
		
		this.events.each(function(event){
			event.img = event.getElement('.col1 img');
			if (event.img.src == this.baseurl) this.deleteColumn(event);	
		}.bind(this));
		
		/*
		this.container=$('quote');
		this.box=$('quote-block');
		
		this.box.fx = new Fx.Morph(this.box, {duration: 600, transition: Fx.Transitions.Sine.easeOut});
		this.box.set({'display':'none'});
		
		this.container.h = this.container.getStyle('height').toInt();
		this.box.h = this.box.getStyle('height').toInt();
	
		//console.log("hi: "+this.box.h);
		
		var newpadding = Math.floor((this.container.h-this.box.h)/2);
		
		
		//this.box.fx.start({});
		this.box.fx.start({
			'opacity':[0,1],
			'margin-top':newpadding
		});
		*/	
			
		
	},
	deleteColumn:function(target){
		
			var col1 = target.getElement('.col1');
			var col2 = target.getElement('.col2');

			col1.destroy();
			
			col2.setStyles({
				'float'  : 'left',
				'width'  : '500px'
			});
	},
	
	getBaseURL:function(){
		
	    var url = location.href;  // entire url including querystring - also: window.location.href;
	    var baseURL = url.substring(0, url.indexOf('/', 14));
	
	
	    if (baseURL.indexOf('http://localhost') != -1) {
	        // Base Url for localhost
	        var url = location.href;  // window.location.href;
	        var pathname = location.pathname;  // window.location.pathname;
	        var index1 = url.indexOf(pathname);
	        var index2 = url.indexOf("/", index1 + 1);
	        var baseLocalUrl = url.substr(0, index2);
	
	        return baseLocalUrl + "/";
	    }
	    else {
	        // Root Url for domain name
	        return baseURL + "/";
	    }
	

	}
});




//*******************************************


var CenterQuote = new Class({
	initialize: function(){
		
		this.container=$('quote');
		this.box=$('quote-block');
		
		this.box.fx = new Fx.Morph(this.box, {duration: 600, transition: Fx.Transitions.Sine.easeOut});
		this.box.set({'display':'none'});			
		
	},
	activate:function(){

		this.container.h = this.container.getStyle('height').toInt();
		this.box.h = this.box.getStyle('height').toInt();
	
		//console.log("hi: "+this.box.h);
		
		var newpadding = Math.floor((this.container.h-this.box.h)/2);

		this.box.fx.start({
			'opacity':[0,1],
			'margin-top':newpadding
		});
	}
});



