/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Collapse Block Classes
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
Collapse.Block = new Class({
	
	Implements: Options,
	
	options: {
		size: 30
	},
	
	initialize: function(options){
		this.setOptions(options);
		this.type = '';
		this.size = this.options.size;
		this.element = new Element('div', {
			styles: {
				width: this.size,
				height: this.size
			}
		}).store('block', this);
	},
	
	setPos: function(col, row){
		this.col = col;
		this.row = row;
		return this;
	},
	
	setType: function(type){
		this.type = type;
		this.element.set('class', type);
	},
	
	process: function(columns){
		return this.collect(columns, [this]);
	},
	
	collect: function(){
		return [];
	}
		
});

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Colored Block
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
Collapse.Block.Color = new Class({
	
	Extends: Collapse.Block,
	
	initialize: function(color, options){
		this.parent(options);
		this.setType(color);
	},
	
	process: function(columns){
		var collection = this.parent(columns);
		return collection.length > 2 ? collection : [];
	},
	
	collect: function(columns, collection, type){
		var block, column;
		type = type || this.type;
		
		column = columns[this.col - 1];
		block = column && column.blocks[this.row];
	 	if (block && block.type === type && !collection.contains(block)){
			collection.push(block);
			collection = block.collect(columns, collection, type);
		}
		
		column = columns[this.col];
		block = column && column.blocks[this.row + 1];
		if (block && block.type === type && !collection.contains(block)){
			collection.push(block);
			collection = block.collect(columns, collection, type);
		}
		
		column = columns[this.col];
		block = column && column.blocks[this.row - 1];
	 	if (block && block.type === type && !collection.contains(block)){
			collection.push(block);
			collection = block.collect(columns, collection, type);
		}
		
		column = columns[this.col + 1];
		block = column && column.blocks[this.row];
	 	if (block && block.type === type && !collection.contains(block)){
			collection.push(block);
			collection = block.collect(columns, collection, type);
		}		

		return collection;
	}
	
});

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Bomb Block
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
Collapse.Block.Bomb = new Class({
	
	Extends: Collapse.Block,
	
	initialize: function(radius, options){
		this.parent(options);
		this.setType('bomb');
		this.radius = radius;
	},
	
	collect: function(columns, collection){
		var col, row, column, block, index = 1;

		//Blocks left of the Clicked Block and the Clicked Block Column
		for (col = this.col - this.radius + 2; col <= this.col; col++){
			for (row = index; row >= -index; row--){
				column = columns[col];
				block = column && column.blocks[this.row + row];
				if (block && !collection.contains(block)){
					collection.push(block);
					if (block.type == 'bomb') collection = collection.combine(block.collect(columns, collection));
				}
			}
			if (index < this.radius - 2) index++;
		}
		
		//Blocks right of the Clicked Block
		index = this.radius - 2;
		for (col = this.col + 1; col < this.col + this.radius - 1; col++){
			for (row = index; row >= -index; row--){				
				column = columns[col];
				block = column && column.blocks[this.row + row];
				if (block && !collection.contains(block)){
					collection.push(block);
					if (block.type == 'bomb') collection = collection.combine(block.collect(columns, collection));
				}
			}
			index--;
		}
		
		return collection;
	}
	
});

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Colored Bomb Block
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
Collapse.Block.ColorBomb = new Class({
	
	Extends: Collapse.Block,
	
	initialize: function(color, options){
		this.parent(options);
		this.setType(color + ' bomb');
	},
	
	collect: function(columns, collection, type){
		type = type || this.type.split(' ')[0];
		collection = [];
		
		columns.each(function(column){
			column.blocks.each(function(block){
				if (block.type.test(type)) collection.push(block);
			});
		});
		
		return collection;
	}
	
});

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Column Bomb
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
Collapse.Block.Column = new Class({
	
	Extends: Collapse.Block,
	
	initialize: function(options){
		this.parent(options);
		this.setType('column');
	},
	
	collect: function(columns){
		return $A(columns[this.col].blocks);
	}
	
});

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Row Bomb
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
Collapse.Block.Row = new Class({
	
	Extends: Collapse.Block,
	
	initialize: function(options){
		this.parent(options);
		this.setType('row');
	},
	
	collect: function(columns){
		return columns.map(function(column){
			return column.blocks[this.row];
		});
	}
	
});

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Collapse Block Types
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
Collapse.Blocks = new Hash({ 0: '', 1: 'red', 2: 'green', 3: 'blue', 4: 'yellow', 5: 'bomb' });

