/**
 * Decorator - Resizable
 * non obstrusive on MooTools 1.2
 * written by Laurent Schmutz - for Brandt
 *
 * version 2.3 without factory for IE6+	
 */

var Decorator = {
	options: {},
	stack: [],
	names: [],
	content_element: null,
	header_element: null,
	counter: 0,
	
	initialize: function() {
	},

	check: function() {
		alert('I am Usher!');
	},
	
	set: function(name, decoration) {
		this.names.push(name);
		this.stack.push(decoration);
	}
};

var Decoration = {

	Implements: [Events, Options, Chain],

	options: {},
	
	initialize: function(options) {
		this.options = {
			position: 'content',
			resizeOptions: {},
			fxOptions: {},
			closer: true,
			type: 'image'
		};

		this.setOptions(options);
		if (this.options.parse) {
			var obj = this.element.getProperty(this.options.parse);
			if (obj && (obj = JSON.decode(obj, this.options.parseSecure))) this.setOptions(obj);
		}
	}
}

var DecorationHLine = {

	Implements: [Decoration],

	initialize: function(options) {
		this.options = {
			position: 'content',
			resizeOptions: {},
			fxOptions: {},
			closer: true,
			type: 'image'
		};

		this.setOptions(options);
		if (this.options.parse) {
			var obj = this.element.getProperty(this.options.parse);
			if (obj && (obj = JSON.decode(obj, this.options.parseSecure))) this.setOptions(obj);
		}
		var hline = new Element('div', {
			'id': name,
		    'styles': {
				'height': 1,
				'width': 0,
				'background-color': '#ebebeb',
				'position': 'absolute',
				'top': this.options.y,
				'left': this.options.x,
				'z-index': -1
		    }
		});
		
		if ( this.options.length + this.options.x + 5 > document.getCoordinates().width ) this.options.length = document.getCoordinates().width - this.options.x - 5;   
		hline.inject($('decoration'));
		
		var duration = 2000;
		if ( this.options.duration ) duration = this.options.duration; 
		
		var entrance = new Fx.Tween(hline, {duration: duration }); 
		entrance.start('width', 0, this.options.length); 
	}
}

var DecorationVLine = {

	Implements: [Decoration],

	initialize: function(options) {
		this.options = {
			position: 'content',
			resizeOptions: {},
			fxOptions: {},
			closer: true,
			type: 'image'
		};

		this.setOptions(options);
		if (this.options.parse) {
			var obj = this.element.getProperty(this.options.parse);
			if (obj && (obj = JSON.decode(obj, this.options.parseSecure))) this.setOptions(obj);
		}

		var vline = new Element('div', {
			'id': name,
		    'styles': {
				'height': 0,
				'width': 1,
				'background-color': '#ebebeb',
				'position': 'absolute',
				'top': this.options.y,
				'left': this.options.x,
				'z-index': -1
		    }
		});
		
		if ( this.options.length + this.options.y + 5> document.getCoordinates().height ) this.options.length = document.getCoordinates().height - this.options.y - 5;   
		vline.inject($('decoration'));
		
		var duration = 2000;
		if ( this.options.duration ) duration = this.options.duration; 
		
		var entrance = new Fx.Tween(vline, {duration: duration });
		entrance.start('height', 0, this.options.length); 
	}
}

var DecorationImage = {

	Implements: [Decoration],

	initialize: function(options) {
		this.setOptions(options);
		if (this.options.parse) {
			var obj = this.element.getProperty(this.options.parse);
			if (obj && (obj = JSON.decode(obj, this.options.parseSecure))) this.setOptions(obj);
		}

		var myimagecontainer = new Element('div',{
				'id': name,
				'z-index': -1,
			    'styles': {
					'position': 'absolute',
					'top': this.options.y,
					'left': this.options.x,
					'height': 0,
					'width': 0,
					'z-index': -1
			    }
			}
		);		
		
		var myimage = new Element('img',{
				'src': this.options.url,
				'height': 0,
				'width': 0
			}
		);		

		myimagecontainer.inject($('decoration'));
		myimage.inject(myimagecontainer);
		var sizing = '100%';
		if ( this.options.sizing ) sizing = this.options.sizing; 

		var duration = 2000;
		if ( this.options.duration ) duration = this.options.duration; 

		var entrance = new Fx.Morph(myimage, {duration: duration });

		entrance.start({
				'width': sizing,
				'height': sizing
			}
		); 
	}
}

var DecorationBackground = {

	Implements: [Decoration],

	initialize: function(options) {
		this.setOptions(options);
		if (this.options.parse) {
			var obj = this.element.getProperty(this.options.parse);
			if (obj && (obj = JSON.decode(obj, this.options.parseSecure))) this.setOptions(obj);
		}


		var myimagecontainer = new Element('div',{
				'id': name,
				'z-index': -1,
			    'styles': {
					'position': 'absolute',
					'top': 0,
					'left': 0,
					'height': document.getCoordinates().height -3,
					'width': document.getCoordinates().width -1,
					'z-index': -1
			    }
			}
		);		

		
		var myimage = new Element('img',{
				'src': this.options.url,
				'height': (document.getCoordinates().height-3) + 'px',
				'width': (document.getCoordinates().width-1) + 'px'
			}
		);		

		myimagecontainer.inject($('decoration'));
		myimage.inject(myimagecontainer);


		var duration = 2000;
		if ( this.options.duration ) duration = this.options.duration; 

		var entrance = new Fx.Tween(myimagecontainer, {duration: duration });
		if ( Browser.Engine.trident ) {
//			myimagecontainer.style.filter = 'alpha(opacity=' + 1 * 100 + ')';
		} else {
			entrance.start('opacity', 0, 1); 
		}

	}
}

Decorator = new Class(Decorator);
Decoration = new Class(Decoration);
DecorationHLine = new Class(DecorationHLine);
DecorationVLine = new Class(DecorationVLine);
DecorationImage = new Class(DecorationImage);
DecorationBackground = new Class(DecorationBackground);

Decorator.make = function(what, name, options) {
		if ( !window["theDecoratorByLaurent"] ) {
			window["theDecoratorByLaurent"] = new Decorator();
		}
		var theDecorator = window["theDecoratorByLaurent"];
		var theDecoration = null;
		switch(what) {
			case 'hline':
				theDecoration = new DecorationHLine(options);
			break;
			
			case 'vline':
				theDecoration = new DecorationVLine(options);
			break;
			
			case 'image':
				theDecoration = new DecorationImage(options);
			break;
			
			case 'background':
				theDecoration = new DecorationBackground(options);
			break;
			
			default:
			break;
		}
		theDecorator.set(name,theDecoration);
};


