/**
 * @author Daniele Corti - ildenae@yahoo.it
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *    
 */

 /***
 *  MooRounder Class : create a group of tags that wrap the original Element
 *  and give the effect of the round border. The color of the borders is the 
 *  color of the original element.
 *  
 *  Based on the structure suggested by Gran Yosler in his article:
 *  "CSS Round Corners" 
 *  <http://blog.yosle.com/2007/09/20/css-round-corners/>
 *  
 *  In order to use it, inport MooTools 1.2 (*) and create the object with this 
 *  notation:
 *  
 *  new MooRounder(element, args);
 *  
 *  (*) You'll need those parts of MooTools 1.2:
 *  	- Core
 *  	- Native
 *  	- Class
 *  	- Element
 *  	- Utilities (Selector)
 *  
 *  @param element: String or Object - the element that will be wrapped by the new 
 *  container
 *  @param args: Object - an object with the following arguments:
 *  	type: String - it represents the type of the round exec: 
 *  			- all (default) : all corners will be rounded
 *  			- top : only the top corners
 *  			- botton : only the bottom corners
 *  			- left : only the left corners
 *  			- right : only the right corners
 *  			- tl : only the top left corner
 *  			- tr : only the top right corner
 *  			- bl : only the bottom left corner
 *  			- br : only the bottom right corner
 *  	border: Boolean - set if the element will be wrapped with a border
 *  			- true : the element will be bordered
 *  			- false (default): the element won't be bordered
 *  	borderColor : String or Color - set the color of the border 
 *  		(the default value id "#888")
 *  
 */
var MooRounder = new Class({
	Implements : Options,
	options : {
		type : 'all',
		border : false,
		borderColor : '#888'
	},
	initialize : function(elem, args){
		this.setOptions(args)
		this.element = $(elem);
		if(!this.element)
		{
			throw "L'elemento e' nullo";
			return;
		}
		
		this.allContent = new Element('div', {
			'styles' : {				
				'text-align' : 'left',
				'position' : this.element.getStyle('position'),
				'float' : this.element.getStyle('float'),
				'margin' : this.element.getStyle('margin')
			}
		})
		this.element.setStyle('margin', 0);
		
		this.maxLength = this.element.getStyle('width').toInt();
		
		this.container = new Element('div', {
			'class' : this.options.border ? 'contentb' : 'contentf',
			'styles': {'height' : this.element.getStyle('position').toLowerCase() != 'static' ? this.element.getStyle('height').toInt() : 'auto'}
		});
		
		this.clear = new Element('br', {'styles' : {'clear' : 'both', 'height' : 0, 'margin' : 0, 'padding' : 0}});
				
		this.allContent.inject(this.element, 'before');
		this.container.inject(this.allContent);
		this.element.dispose().inject(this.container);
		
		this.bElements = {};
		
		this.options.width = this.element.getStyle('width').toInt();					
		
		switch(this.options.type)
		{
			case 'bottom':
				this.setBottom(true, true);
				this.container.setStyle('width', this.options.width + 10);
				this.maxLength = this.maxLength < this.options.width + 10 ? this.options.width + 10 : this.maxLength;
				if(this.options.border)
					this.container.setStyle('border-top', '5px solid '+this.options.borderColor);
			break;
			case 'top':
				this.setTop(true, true);
				this.container.setStyle('width', this.options.width + 10);
				this.maxLength = this.maxLength < this.options.width + 10 ? this.options.width + 10 : this.maxLength;
				if(this.options.border)
					this.container.setStyle('border-bottom', '1px solid '+this.options.borderColor);
			break;
			case 'left':
				this.setTop(true, false);
				this.setBottom(true, false);
				this.container.setStyle('width', this.options.width + 5);
				this.maxLength = this.maxLength < this.options.width + 5 ? this.options.width + 5 : this.maxLength;
			break;
			case 'right':
				this.setTop(false, true);
				this.setBottom(false, true);
				this.container.setStyle('width', this.options.width + 5);
				this.maxLength = this.maxLength < this.options.width + 5 ? this.options.width + 5 : this.maxLength;
			break;
			case 'tl':
				this.setTop(true, false);
				this.container.setStyle('width', this.options.width + 5);
				if(this.options.border)
					this.container.setStyle('border-bottom', '1px solid '+this.options.borderColor);
				this.maxLength = this.maxLength < this.options.width + 5 ? this.options.width + 5 : this.maxLength;	
			break;
			case 'tr':
				this.setTop(false, false);
				this.container.setStyle('width', this.options.width + 5);
				if(this.options.border)
					this.container.setStyle('border-bottom', '1px solid '+this.options.borderColor);
				this.maxLength = this.maxLength < this.options.width + 5 ? this.options.width + 5 : this.maxLength;
			break;
			case 'bl':
				this.setBottom(true, false);
				this.container.setStyle('width', this.options.width + 5);
				if(this.options.border)
					this.container.setStyle('border-top', '1px solid '+this.options.borderColor);
				this.maxLength = this.maxLength < this.options.width + 5 ? this.options.width + 5 : this.maxLength;
			break;
			case 'br':
				this.setBottom(false, false);
				this.container.setStyle('width', this.options.width + 5);
				if(this.options.border)
					this.container.setStyle('border-top', '1px solid '+this.options.borderColor);
				this.maxLength = this.maxLength < this.options.width + 5 ? this.options.width + 5 : this.maxLength;
			break;
			default:
				this.setBottom(true, true);
				this.setTop(true, true);
				this.container.setStyle('width', this.options.width + 10);
				this.maxLength = this.maxLength < this.options.width + 10 ? this.options.width + 10 : this.maxLength;
			break;
		}
		
		this.colorThem();
		
		this.allContent.setStyle('width', this.maxLength);
		this.clear.inject(this.container, 'bottom');
		
	},
	setTop: function(withMargin, fullSize){
		this.bElements.b1top = new Element('b', {
			'class': this.options.border ? 'b1' : 'b1f'
		})
		
		if (!withMargin) 
			this.bElements.b1top.setStyle('margin-left', 0);
		
		this.bElements.b1top.setStyle('width', this.options.width + (this.options.border ? 2 : 0));
		
		this.bElements.b2top = new Element('b', {
			'class': this.options.border ? 'b2' : 'b2f'
		})
		
		if (!withMargin) 
			this.bElements.b2top.setStyle('margin-left', 0);
		
		this.bElements.b2top.setStyle('width', this.options.width + (this.options.border ? fullSize ? withMargin ? 2 : 0 : 0 : (fullSize ? withMargin ? 4 : 2 : 2)));
		
		this.bElements.b3top = new Element('b', {
			'class': this.options.border ? 'b3' : 'b3f'
		})
		
		if (!withMargin) 
			this.bElements.b3top.setStyle('margin-left', 0);
		
		this.bElements.b3top.setStyle('width', this.options.width + (fullSize ? withMargin ? 6 : 3 : 3));
		
		this.bElements.b4top = new Element('b', {
			'class': this.options.border ? 'b4' : 'b4f'
		})
		
		if (!withMargin) 
			this.bElements.b4top.setStyle('margin-left', 0);
		
		this.bElements.b4top.setStyle('width', this.options.width + (fullSize ? withMargin ? 8 : 4 : 4));
		
		if (!withMargin) 
			this.container.setStyle('margin-left', 0);
		
		this.bElements.b1top.inject(this.container, 'before');
		this.bElements.b2top.inject(this.container, 'before');
		this.bElements.b3top.inject(this.container, 'before');
		this.bElements.b4top.inject(this.container, 'before');
	},
	setBottom: function(withMargin, fullSize){
		this.bElements.b1bottom = new Element('b', {
			'class': this.options.border ? 'b1' : 'b1f'
		})
		
		if (!withMargin) 
			this.bElements.b1bottom.setStyle('margin-left', 0);
		
		this.bElements.b1bottom.setStyle('width', this.options.width + (this.options.border ? 2 : 0));
		
		this.bElements.b2bottom = new Element('b', {
			'class': this.options.border ? 'b2' : 'b2f'
		})
		
		if (!withMargin) 
			this.bElements.b2bottom.setStyle('margin-left', 0);
		
		this.bElements.b2bottom.setStyle('width', this.options.width + (this.options.border ? fullSize ? withMargin ? 2 : 0 : 0 : (fullSize ? withMargin ? 4 : 2 : 2)));
		
		this.bElements.b3bottom = new Element('b', {
			'class': this.options.border ? 'b3' : 'b3f'
		})
		
		if (!withMargin) 
			this.bElements.b3bottom.setStyle('margin-left', 0);
		
		this.bElements.b3bottom.setStyle('width', this.options.width + (fullSize ? withMargin ? 6 : 3 : 3));
		
		this.bElements.b4bottom = new Element('b', {
			'class': this.options.border ? 'b4' : 'b4f'
		})
		
		if (!withMargin) 
			this.bElements.b4bottom.setStyle('margin-left', 0);
		
		this.bElements.b4bottom.setStyle('width', this.options.width + (fullSize ? withMargin ? 8 : 4 : 4));
		
		if (!withMargin) 
			this.container.setStyle('margin-left', 0);
		
		this.bElements.b1bottom.inject(this.container, 'after');
		this.bElements.b2bottom.inject(this.container, 'after');
		this.bElements.b3bottom.inject(this.container, 'after');
		this.bElements.b4bottom.inject(this.container, 'after');
	},
	colorThem: function(){
		var e = this.element;
		var b = this.options.border;
		var c = this.options.borderColor;
		$H(this.bElements).each(function(value, key){
			if (!b || (key != 'b1bottom' && key != 'b1top')) {
				value.setStyle('background-color', e.getStyle('background-color'));
				if (b) 
					value.setStyle('border-color', c);
			}
			else {
				if (b) 
					value.setStyle('background-color', c);
			}
		})
		this.container.setStyle('background-color', e.getStyle('background-color'));
		if (b) 
			this.container.setStyle('border-color', c);
	}
});