/* moo.rd DOWNLOAD BUILDER SYSTEM by Riccardo Degni (http://www.riccardodegni.it/) */

new Asset.images(
		['../images/moo_buttons.gif',
		 '../images/zip.gif',
		 '../images/compression.gif'
		 ]);

/* 
	class Downloadbuilder
	methods:
				attach: call the select method for each cell
				select: attach to each cell the click, mouseover and mouseout events
				choose: select a component
				get: create a cascade of affected elements. The first element must be the element whose click event
					 select all the others element specified as array of strings in the second argument
*/
var DownloadBuilder = new Class({
								
		initialize: function(items) { 
			this.items = items;
		},
		
		attach: function() {
			this.items.each(function(td) {
				this.select.pass(td).attempt();
			}, this);
		},
				
		select: function(id) {
			var td = $(id);
			
			var check = td.getParent().getFirst();
			var middle = td.getParent().getChildren()[1];
			var next = middle.getNext();
			var tds = $$(check, middle, next);
			
			var i = td.getParent().getChildren()[1].getChildren()[1];
			
			td.addEvent('click', function() {
				check.toggleClass('checked');
				(i.getProperty('value') == '') ? i.setProperty('value', 'ok') : i.setProperty('value', '');
				tds.toggleClass('clicked');
				tds.removeClass('hovered');
			});
			
			td.addEvent('mouseover', function() {
				if(!td.getClassName().contains('clicked')) tds.addClass('hovered');
			});
			
			td.addEvent('mouseout', function() {
				tds.removeClass('hovered');
			});
			
		},
		
		choose: function(id) {
			var td = $(id);
			
			var check = td.getParent().getFirst();
			var middle = td.getParent().getChildren()[1];
			var next = middle.getNext();
			var tds = $$(check, middle, next);
			
			var i = td.getParent().getChildren()[1].getChildren()[1];
			
			check.addClass('checked'); 	// check.toggleClass('checked');
			i.setProperty('value', 'ok');
			tds.addClass('clicked');	// tds.toggleClass('clicked');
			tds.removeClass('hovered');
		},
		
		unchoose: function(id) {
			var td = $(id);
			
			var check = td.getParent().getFirst();
			var middle = td.getParent().getChildren()[1];
			var next = middle.getNext();
			var tds = $$(check, middle, next);
			
			var i = td.getParent().getChildren()[1].getChildren()[1];
			
			check.removeClass('checked');
			i.setProperty('value', '');
			tds.removeClass('clicked');
			tds.removeClass('hovered');
		},
		
		get: function(id, choosing) {
			var check = $('for_'+ id);
			
			var affected = $$(check, check.getNext(), check.getNext().getNext());
			
			var choosing = choosing.extend(['constructors']);
			
			choosing.each(function(el) {
				var to = 'for_' + el;
				
				var f = $('for_' + el);
				var s = f.getNext();
				var t = f.getNext().getNext();
				
				var base = $$(f, s, t);
				
				affected.addEvent('click', this.choose.bind(this, to));	
				base.addEvent('click', this.unchoose.bind(this, affected));
			}, this);
		}
		
});

/*
	class CompressionBuilder
	methods: 
			attach: toggle the compression type
*/
var CompressionBuilder = DownloadBuilder.extend({
													
	initialize: function(items) {
		this.parent(items);
	},
	
	attach: function() {
		this.items.each(function(td) {
			var check = td.getParent().getFirst();
			var middle = check.getNext();
			var next = middle.getNext();
			var tds = $$(check, middle, next);
			
			var items = this.items;
			
			var i = td.getParent().getFirst().getNext().getFirst();
			
			td.addEvent('click', function() {
				
				items.each(function(td) {
					var check = td.getParent().getFirst();
					var middle = check.getNext();
					var next = middle.getNext();
					var tds = $$(check, middle, next);	
					
					var i = td.getParent().getFirst().getNext().getFirst();
					i.setProperty('value', '');
					
					tds.removeClass('clicked').removeClass('hovered').removeClass('checked');
				}, this);
				
				check.addClass('checked');
				tds.addClass('clicked');
				tds.removeClass('hovered');
				i.setProperty('value', 'ok');
			});
			
			td.addEvent('mouseover', function() {
				if(!td.getClassName().contains('clicked')) tds.addClass('hovered');
			});
			
			td.addEvent('mouseout', function() {
				tds.removeClass('hovered');
			});					 
		}, this);
	}
});

var IncludeBuilder = CompressionBuilder.extend({
													
	initialize: function(items) {
		this.parent(items);
	},
	
	attach: function() {
		this.parent();
		this.items.each(function(td) {
			td.addEvent('click', function() {
				
				this.items.each(function(td) {
					var check = td.getParent().getFirst();
					var middle = check.getNext();
					var next = middle.getNext();
					var tds = $$(check, middle, next);	
					
					if(check.getProperty('id') == 'for_mootools_full' || check.getProperty('id') == 'for_mootools_rd') {
						if($('for_onlydocs').hasClass('checked'))
							$('for_compression').fireEvent('click');	
					}
				}, this);
		
			}.bind(this));
		}, this);
	}
});
									
window.addEvent('domready',function() {
									
	// image fix for Safari
	// if(window.webkit) $('zip').setStyle('top', '0px');
	
	// ----------	INPUT ------------------------
	$$('input').each(function(el) {
		if(el.getProperty('type') == 'hidden' && el.id != 'rev')
			el.setProperty('value', '');	
	}); 
	
	// -----------		COMPRESSION	---------------		
	
	$('for_compression').addClass('checked');
	$('is_packed').setProperty('value', 'ok');
	
	// -----------		INCLUDE MOOTOOLS	---------------
	$('is_mootools_rd').setProperty('value', 'ok');

	// -----------		REQUEST --------------------
	if($('version_number')) {
		$('build').addEvent('click', function(event) {
			var event = new Event(event);
			
			var controlCheck = $$('.check').some(function(el) {
				return el.hasClass('checked');  
			});
			
			var controlCompression = $$('.compr').some(function(el) {
				return el.hasClass('checked');  
			});
			
			if((!$('for_compression').hasClass('checked')) && (!$('for_sourcecode').hasClass('checked')) && (!$('for_nodocs').hasClass('checked')) && (!$('for_onlydocs').hasClass('checked'))) {
				event.stop();
			}
			
			if(!controlCheck) {
				event.stop();
				alert('You must select at least one component');
			}
			
			if(!controlCompression) {
				event.stop();
				alert('You must choose a compression type');
			}
		});
	}
	else {
		$('build').addEvent('click', function(event) {
			var event = new Event(event);
			
			var controlCheck = $$('.check').some(function(el) {
				return el.hasClass('checked');  
			});
			
			var controlCompression = $$('.compr').some(function(el) {
				return el.hasClass('checked');  
			});
			
			if(!controlCheck) {
				event.stop();
				alert('You must select at least one component');
			}
			else if(!controlCompression) {
				event.stop();
				alert('You must choose a compression type');
			}
			else {
				//var res = $('results');
				//res.setHTML('<p><img src="/images/loading.gif" alt=""></p>');
				// request
				/*new Ajax('/build_svn.php', {
					postBody: $('fm_built'),
					onRequest: function() {
						res.setHTML('<p><img src="/images/loading.gif" alt=""></p>');
					},
					onComplete: function() {
						res.setHTML('<p>Download Completed!</p>');
					}
				}).request();	*/
			}
		});
	}
});

var download;
var include;
var compression;

// download assets
window.addEvent('domready', function() {
	// initialize the download builder
	download = new DownloadBuilder($$('table.download_files tr td'));
	download.attach();
	
	$('for_constructors').fireEvent('click');
	
	// initialize the MooTools inclusion
	if($('table_include_mootools')) {
		include = new IncludeBuilder($$('table.download_files_mootools tr td'));
		include.attach();
		
		// don't include MooTools if the user choose only docs
		var onlydocs = $('for_onlydocs');
		var onlymiddle = onlydocs.getNext();
		var onlyright = onlymiddle.getNext();
		
		$$(onlydocs, onlymiddle, onlyright).addEvent('click', function() {
			$('for_mootools_out').fireEvent('click');					
		});
	}
	
	// initliaze the compression builder
	compression = new CompressionBuilder($$('table.download_files_compression tr td'));
	compression.attach();
});