/**
 * Usher - Resizable
 * non obstrusive on MooTools 1.2
 * written by Laurent Schmutz - for Visarte Fribourg
 *
 * version 2.3 without factory for IE6+	
 */

var Usher = {
	options: {},
	marges: { top: 0, left: 0, right: 0, bottom: 0 },
	stack: [],
	content_element: null,
	header_element: null,
	
	initialize: function() {
	},

	check: function() {
		alert('I am Usher!');
	},
	
	set: function(element, options) {
		var component = new Ushable(element, options);
		this.stack.push(component);
	},
	
	calcultate_marges: function() {
		for(var i=0; i<this.stack.length; i++) {
			if ( this.stack[i].position == 'header' || this.stack[i].position == 'headerover' ) {
				this.marges.top = this.stack[i].element.getCoordinates().height;
			}
			if ( this.stack[i].position == 'left' ||  this.stack[i].position == 'leftover' ) {
				this.marges.left = this.stack[i].element.getCoordinates().width;
			}
			if ( this.stack[i].position == 'right' || this.stack[i].position == 'rightover' ) {
				this.marges.right = this.stack[i].element.getCoordinates().width;			
			}
			if ( this.stack[i].position == 'footer' || this.stack[i].position == 'footerover' ) {
				this.marges.bottom = this.stack[i].element.getCoordinates().height;
			}
		}
		return this.marges;
	},

	resize: function() {
		for(var i=0; i<this.stack.length; i++) this.stack[i].setPosition();
		for(var i=0; i<this.stack.length; i++) this.stack[i].place();
	},
	
	header: function() {
		if ( this.header_element == null ) 
			for(var i=0; i<this.stack.length; i++) if ( this.stack[i].position == 'header' || this.stack[i].position == 'headerover' ) this.header_element = this.stack[i];
		return this.header_element;
	},
	content: function() {
		if ( this.content_element == null )
			for(var i=0; i<this.stack.length; i++) if ( this.stack[i].position == 'content' ) this.content_element = this.stack[i];
		return this.content_element;
	}

};

var Ushable = {
	
	Implements: [Events, Options, Chain],

	initialize: function(element, options) {
		this.element = $(element);
		if ( this.element == null ) return;

		this.options = {
			position: 'content',
			resizeOptions: {},
			fxOptions: {},
			closer: true,
			type: 'image'
		};
		this.coord = { x: -1, y: -1, w: 0, h: 0 };
		this.marging = { left: 0, right: 0, top: 0, bottom: 0 };
	
		this.pre_coord = null;

		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);
		}
		
		this.position = this.options.position;

		this.marging.left = parseInt(this.element.getStyle('margin-left'));
		this.marging.right = parseInt(this.element.getStyle('margin-right'));
		this.marging.top = parseInt(this.element.getStyle('margin-top'));
		this.marging.bottom = parseInt(this.element.getStyle('margin-bottom'));
	},

	setPosition: function() {
		if ( this.element == null ) return;

		this.height = this.element.getCoordinates().height;
		this.width = this.element.getCoordinates().width;
		
		var myUsher = window["theUsherByLaurent"];

		var content = myUsher.content();

		myUsher.calcultate_marges();

		switch ( this.position ) {
			case 'content':
				if ( myUsher.marges.left >= 0 && myUsher.marges.right >= 0 ) {
					this.coord.x = document.getCoordinates().width/2 - (myUsher.marges.left + this.width + myUsher.marges.right)/2 + myUsher.marges.left;
				}
				if ( this.options.absbottom ) {
					this.coord.y = document.getCoordinates().height - this.height - this.options.absbottom;
					if ( this.coord.y < 0 ) this.coord.y = 0;
				} else {
					if ( myUsher.marges.top >= 0 && myUsher.marges.bottom >= 0 ) {
						this.coord.y = document.getCoordinates().height/2 - (myUsher.marges.top + this.height + myUsher.marges.bottom)/2 + myUsher.marges.top;
					}
				}
			break;

			case 'header':
				this.coord.x = document.getCoordinates().width/2 - (this.width)/2;
				this.coord.y = content.coord.y - this.height;
			break;
			
			case 'headerover':
				if ( myUsher.marges.left >= 0 && myUsher.marges.right >= 0 && content.width >= 0 ) {
					this.width = myUsher.marges.left + content.marging.left + content.width + content.marging.right + myUsher.marges.right;
					this.coord.x = content.coord.x - myUsher.marges.left;
				}
				this.coord.y = content.coord.y - this.height;
			break;

			case 'headerupper':
				var h = myUsher.header();
				if ( h ) {
					this.width = h.width;
					this.coord.y = h.coord.y - this.height;
					this.coord.x = h.coord.x;
				}
			break;
			
			
			case 'absoluteleft':
			case 'absoluteright':
				this.coord.x = 0;
				this.height = content.height;
				this.coord.y = (document.getCoordinates().height - this.height)/2;
			break;
			
			case 'left':
				this.coord.y = content.coord.y + content.marging.top;
				if (myUsher.marges.right >= 0 && content.width >= 0) {
					this.coord.x = document.getCoordinates().width/2 - (this.width + content.width + myUsher.marges.right)/2;
				}
			break;
			case 'leftover':
				if ( myUsher.marges.top >= 0  && myUsher.marges.bottom >= 0 && content.heigth >= 0 ) {
					this.coord.y = content.coord.y - myUsher.marges.top;
					this.height = myUsher.marges.top + content.heigth + myUsher.marges.bottom;
				}
				if (myUsher.marges.right >= 0 && content.width >= 0) {
					this.coord.x = document.getCoordinates().width/2 - (this.width + content.width + myUsher.marges.right) / 2;
				}
			break;

			case 'right':
				this.coord.y = content.coord.y;
				if (myUsher.marges.left >= 0 && content.width >= 0) {
					this.coord.x = document.getCoordinates().width/2 - (this.width + content.width + myUsher.marges.left) / 2 + this.width;
				}
			break;
			case 'rightover':
				if ( myUsher.marges.top >= 0 && myUsher.marges.bottom >= 0 && content.height >= 0 ) {
					this.coord.y = content.coord.y - myUsher.marges.top;
					this.height = myUsher.marges.top + content.height + myUsher.marges.bottom;
				}
				if ( myUsher.marges.left >= 0 && content.width >= 0) {
					this.coord.x = document.getCoordinates().width/2 - (this.width + content.width + myUsher.marges.left ) / 2 + this.width;
				}
			break;

			case 'footer':
				this.coord.x = content.coord.x;
				this.coord.y = content.coord.y + content.height;
			break;

			case 'footerover':
				if ( myUsher.marges.left >= 0 && myUsher.marges.right >= 0 && content.width >= 0 ) {
					this.width = myUsher.marges.left + content.width + myUsher.marges.right;
					this.coord.x = content.coord.x - myUsher.marges.left;
				}
				this.coord.y = content.coord.y + content.height;
			break;
		}
	},

	place: function() {
		if ( this.element == null ) return;
		var myUsher = window["theUsherByLaurent"];
		var content = myUsher.content();
		this.element.setStyle('position', 'absolute');

		if ( !this.options.shiftright ) {
			this.shiftright = 0;
		} else {
			this.shiftright = this.options.shiftright;
		}

		switch(this.position) {
			case 'bottomfull':
				this.element.setStyle('width', document.getCoordinates().width + 'px');
				this.element.setStyle('left', 0);

				if ( content.coord.y + content.height + 20 > document.getCoordinates().height ) {
					this.element.setStyle('top', content.coord.y + content.height + 20);
				}  else {
					this.element.setStyle('bottom', 1);
				}
			break;
			
			case 'bottomblock':
				if ( content.coord.y + content.height + 20 > document.getCoordinates().height ) {
					this.element.setStyle('top', content.coord.y + content.height + 20);
				}  else {
					this.element.setStyle('bottom', 1);
				}
			break;
			
			case 'topfull':
				this.element.setStyle('width', document.getCoordinates().width + 'px');
				this.element.setStyle('left', 0);
				this.element.setStyle('top', 0);
			break;
			
			case 'absoluteleft':
				this.element.setStyle('height', this.height);
				this.element.setStyle('top', this.coord.y);
				this.element.setStyle('left', 0);
			break;
			
			case 'absoluteright':
				this.element.setStyle('height', this.height);
				this.element.setStyle('top', this.coord.y);
				this.element.setStyle('right', 0);
			break;

			case 'header':
				this.element.setStyle('top', this.coord.y + 'px');
				this.element.setStyle('left', this.coord.x + this.shiftright + 'px');
			break;

			case 'headerover':
			case 'headerupper':
				this.element.setStyle('top', this.coord.y + 'px');
				this.element.setStyle('left', this.coord.x + this.shiftright + 'px');
				this.element.setStyle('width', this.width + 'px')
			break;

			case 'content':
				if ( this.options.move ) {
					if ( this.pre_coord == null ) {
						
					} else {
						var mover = new Fx.Move(this.element, {
							offset: { x: this.pre_coord.x - this.coord.x, y: this.pre_coord.y - this.coord.y }
						});
						mover.start(); //moves to the new location
					}
				}
				if ( this.coord.x >= 0 && this.coord.y >= 0 ) {
					this.element.setStyle('left', this.coord.x + 'px');
					this.element.setStyle('top', this.coord.y + 'px')
				} else {
					
				}
				this.pre_coord = this.coord;

				/** place the admin/edit button **/
				var eb = $('admin');
				if ( eb ) {
					eb.setStyle('position', 'absolute');
					eb.setStyle('top', this.coord.y + 10 + 'px');
					eb.setStyle('left', this.coord.x + this.width + 20 + 'px');
				} else {
					// alert('problem: no admin button tag!');
				}
			break;
			
			case 'left':
				this.element.setStyle('top', this.coord.y + 'px');
				if (this.coord.x < 0 ) {
					this.element.setStyle('left', 0 + this.shiftright + 'px');
				} else {
					this.element.setStyle('left', this.coord.x + this.shiftright + 'px');
				}
			break;
			
			case 'leftover':
				this.element.setStyle('width', this.width + 'px')
				this.element.setStyle('top', this.coord.y + 'px');
				this.element.setStyle('left', this.coord.x + this.shiftright + 'px');
			break;
			
			case 'right':
				this.element.setStyle('top', this.coord.y);
				this.element.setStyle('right', this.coord.x);
			break;

			case 'rightover':
				this.element.setStyle('width', this.width + 'px')
				this.element.setStyle('top', this.coord.y + 'px');
				this.element.setStyle('right', this.coord.x + 'px');
			break;

			case 'footer':
				this.element.setStyle('bottom', this.coord.y + 'px');
				this.element.setStyle('left', this.coord.x + this.shiftright + 'px');
			break;

			case 'footerover':
				this.element.setStyle('bottom', this.coord.y + 'px');
				this.element.setStyle('left', this.coord.x + this.shiftright + 'px');
				this.element.setStyle('width', this.width + 'px');
			break;
		}
		
		
		if ( this.options.shadow && !Browser.Engine.trident4) {
			/* top, left, width, height, right, and bottom */
			var coord = this.element.getCoordinates(); 
			
			if ( this.options.shadow.w ) {
				if ( !this.shadow_w ) this.shadow_w = Element('div', {'class': 'shadow-bg-w'});
				this.shadow_w.setStyle('position', 'absolute');
				this.shadow_w.setStyle('left', (coord.left - 33) + 'px');
				this.shadow_w.setStyle('top', (coord.top) + 'px');
				this.shadow_w.setStyle('width', 33);
				this.shadow_w.setStyle('height', this.height);
				this.shadow_w.setStyle('zIndex', this.element.getStyle('zIndex')-9);
				this.shadow_w.inject(document.body);
			}

			if (this.options.shadow.n) {
				if (!this.shadow_n) 
					this.shadow_n = Element('div', {
						'class': 'shadow-bg-n'
					});
				this.shadow_n.setStyle('position', 'absolute');
				this.shadow_n.setStyle('left', (coord.left) + 'px');
				this.shadow_n.setStyle('top', (coord.top - 33) + 'px');
				this.shadow_n.setStyle('width', this.width);
				this.shadow_n.setStyle('height', 33);
				//			this.shadow_e.setStyle('background-color', '#ff0000');
				this.shadow_n.setStyle('zIndex', this.element.getStyle('zIndex')-9);
				this.shadow_n.inject(document.body);
			}

			if (this.options.shadow.e) {
				if (!this.shadow_e) 
					this.shadow_e = Element('div', {
						'class': 'shadow-bg-e'
					});
				this.shadow_e.setStyle('position', 'absolute');
				this.shadow_e.setStyle('left', (coord.left + this.element.getSize().x) + 'px');
				this.shadow_e.setStyle('top', (coord.top) + 'px');
				this.shadow_e.setStyle('width', '33px');
				this.shadow_e.setStyle('height', this.height);
				//			this.shadow_e.setStyle('background-color', '#ffff00');
				this.shadow_e.setStyle('zIndex', this.element.getStyle('zIndex')-9);
				this.shadow_e.inject(document.body);
			}

			if (this.options.shadow.s) {
				if (!this.shadow_s) 
					this.shadow_s = Element('div', {
						'class': 'shadow-bg-s'
					});
				this.shadow_s.setStyle('position', 'absolute');
				this.shadow_s.setStyle('left', (coord.left) + 'px');
				this.shadow_s.setStyle('top', (coord.top + this.element.getSize().y) + 'px');
				this.shadow_s.setStyle('width', this.width);
				this.shadow_s.setStyle('height', '33px');
				//			this.shadow_s.setStyle('background-color', '#ffff00');
				this.shadow_s.setStyle('zIndex', this.element.getStyle('zIndex')-9);
				this.shadow_s.inject(document.body);
			}

			if (this.options.shadow.w && this.options.shadow.n) {
				if (!this.shadow_nw) 
					this.shadow_nw = Element('div', {
						'class': 'shadow-bg-nw'
					});
				this.shadow_nw.setStyle('position', 'absolute');
				this.shadow_nw.setStyle('left', (coord.left - 33) + 'px');
				this.shadow_nw.setStyle('top', (coord.top - 33) + 'px');
				this.shadow_nw.setStyle('width', 33);
				this.shadow_nw.setStyle('height', 33);
				//			this.shadow_nw.setStyle('background-color', '#ff0000');
				this.shadow_nw.setStyle('zIndex', this.element.getStyle('zIndex')-8);
				this.shadow_nw.inject(document.body);
			}

			if (this.options.shadow.e && this.options.shadow.n) {
				if (!this.shadow_ne) 
					this.shadow_ne = Element('div', {
						'class': 'shadow-bg-ne'
					});
				this.shadow_ne.setStyle('position', 'absolute');
				this.shadow_ne.setStyle('left', (coord.left + this.element.getSize().x) + 'px');
				this.shadow_ne.setStyle('top', (coord.top - 33) + 'px');
				this.shadow_ne.setStyle('width', 33);
				this.shadow_ne.setStyle('height', 33);
				//			this.shadow_ne.setStyle('background-color', '#ff0000');
				this.shadow_ne.setStyle('zIndex', this.element.getStyle('zIndex')-8);
				this.shadow_ne.inject(document.body);
			}

			if (this.options.shadow.w && this.options.shadow.s) {
				if (!this.shadow_sw) 
					this.shadow_sw = Element('div', {
						'class': 'shadow-bg-sw'
					});
				this.shadow_sw.setStyle('position', 'absolute');
				this.shadow_sw.setStyle('left', (coord.left - 33) + 'px');
				this.shadow_sw.setStyle('top', (coord.top + this.element.getSize().y) + 'px');
				this.shadow_sw.setStyle('width', 33);
				this.shadow_sw.setStyle('height', 33);
				//			this.shadow_sw.setStyle('background-color', '#ff0000');
				this.shadow_sw.setStyle('zIndex', this.element.getStyle('zIndex')-8);
				this.shadow_sw.inject(document.body);
			}

			if (this.options.shadow.e && this.options.shadow.s) {
				if (!this.shadow_se) 
					this.shadow_se = Element('div', {
						'class': 'shadow-bg-se'
					});
				this.shadow_se.setStyle('position', 'absolute');
				this.shadow_se.setStyle('left', (coord.left + this.element.getSize().x) + 'px');
				this.shadow_se.setStyle('top', (coord.top + this.element.getSize().y) + 'px');
				this.shadow_se.setStyle('width', 33);
				this.shadow_se.setStyle('height', 33);
				//			this.shadow_se.setStyle('background-color', '#ff0000');
				this.shadow_se.setStyle('zIndex', this.element.getStyle('zIndex')-8);
				this.shadow_se.inject(document.body);
			}
		}
	}
};

Usher = new Class(Usher);
Ushable = new Class(Ushable);

Usher.check = function() {
		if ( !window["theUsherByLaurent"] ) {
			alert('Usher not set!');
		} else {
			window["theUsherByLaurent"].check();	
		}
};

Usher.set = function(element, options) {
		if ( !window["theUsherByLaurent"] ) {
			window["theUsherByLaurent"] = new Usher();
		}
		window["theUsherByLaurent"].set(element, options);	
};

Usher.resize = function() {
	if ( !window["theUsherByLaurent"] ) return;
	window["theUsherByLaurent"].resize();
}

Usher.getCoordinates = function () {
	return document.getCoordinates();
};

Usher.content = function () {
	if ( !window["theUsherByLaurent"] ) return;
	return window["theUsherByLaurent"].content();
}

Usher.injectSlidingTop = function (tag, entrance) {
	if ( !window["theUsherByLaurent"] ) return;
	var myUsher = window["theUsherByLaurent"];
	
	if ( $(tag+'-container') && $(tag) && $(tag+'-toggler')) {
		var content = myUsher.content();
		var slidingtop = $(tag);
		var container = $(tag+'-container');
		var toggler = $(tag+'-toggler');

		container.setStyle('position','absolute');
		container.setStyle('top',0);

		container.setStyle('z-index','9999999');

		var mySlide = new Fx.Slide(slidingtop);

		var requieredWidth = content.width;
		var requieredHeight = slidingtop.getStyle('height');

		var openSlide = true;
		container.setStyle('height',requieredHeight);
		if (!entrance) {
			mySlide.hide();
			openSlide = false;
			container.setStyle('height',0);
		}
		if ( document.getCoordinates().width - content.width < 250*2 ) {
			if ( document.getCoordinates().width - content.width <  250 ) {
				container.setStyle('right',0);
				requieredWidth = document.getCoordinates().width-250;
				if ( openSlide ) container.setStyle('width',requieredWidth);
				else container.setStyle('width',0);
			} else {
				container.setStyle('left',250);
				requieredWidth = content.width;
				if ( openSlide ) container.setStyle('width',requieredWidth);
				else container.setStyle('width',0);
			}
		} else {
				container.setStyle('left',content.coord.x);
				requieredWidth = content.width;
				if ( openSlide ) container.setStyle('width',requieredWidth);
				else container.setStyle('width',0);
		}

		$('flashtoggle').addEvent('click', function(e){
			if (openSlide) {
				openSlide = false;
				container.setStyle('width',0)
				container.setStyle('height',0);
			} else {
				openSlide = true;
				container.setStyle('width',requieredWidth);
				container.setStyle('height',requieredHeight);
			}

			e = new Event(e);
			mySlide.toggle();
			e.stop();
		});	
	}
}

