if("Effect" in window)
{
	Effect.Resize = Class.create();
	Object.extend(Object.extend(Effect.Resize.prototype, Effect.Base.prototype), {
		initialize: function(element, newX, newY) {
			this.element = $(element);
			this.targetDims = [newX, newY];
			var options = arguments[3] || {};
			this.start(options);
		},
		setup: function() {
			this.restoreAfterFinish = this.options.restoreAfterFinish || false;
			this.elementPositioning = this.element.getStyle('position');

			this.originalStyle = {};
			['top','left','width','height','fontSize'].each( function(k) {
				this.originalStyle[k] = this.element.style[k];
			}.bind(this));

			this.originalTop  = this.element.offsetTop;
			this.originalLeft = this.element.offsetLeft;

			this.originalDims = [this.element.offsetWidth, this.element.offsetHeight];
			this.dims = [this.element.offsetWidth, this.element.offsetHeight];
		},
		update: function(position) {
			if(this.targetDims[0])
			{
				this.dims[0] = this.originalDims[0] - (this.originalDims[0] - this.targetDims[0]) * position;
			}
			if(this.targetDims[1])
			{
				this.dims[1] = this.originalDims[1] - (this.originalDims[1] - this.targetDims[1]) * position;
			}
			this.setDimensions();
		},
		finish: function(position) {
			if (this.restoreAfterFinish) this.element.setStyle(this.originalStyle);
		},
		setDimensions: function() {
			var d = {};
			if(this.targetDims[0])
			d.width = this.dims[0] + "px";

			if(this.targetDims[1])
			d.height = this.dims[1] + "px"
			this.element.setStyle(d);
		}
	});
}

Arland.Overlay = Class.create({
	initialize: function(className, container, w) 
	{
		this.w = w;
		this.visible = false;
		this.className = className;
		this.hiddenElements = new Array();
		this.container = $(container);
		if(this.container) 
		{
			if(this.container.overlaySpinner)
			{
				try 
				{
					this.container.overlaySpinner.hide();			
				}
				catch(ex) {}
			}	
			this.container.overlaySpinner = this;		
		}				
	},
	dispose: function() 
	{
		this.container.overlaySpinner = null;	
	},
	show: function()
	{
		if(!this.visible && this.container) 
		{
			var doc = this.getDocument();			
			
			this.overlayBackgroundElement = doc.createElement("div");
			this.overlayBackgroundElement.className = this.className;
			
			this.hideProblematicElements();			
			this.adjustSizeAndPosition();
			
			this.container.insertBefore(this.overlayBackgroundElement, this.container.firstChild);
			this.visible = true;
		}
	},	
	adjustSizeAndPosition: function()
	{	
		// TODO: Left and Top computation
		//			 if container has padding design issues occur
		Element.setStyle(this.overlayBackgroundElement, {
			width: this.container.offsetWidth + "px",
			height: this.container.offsetHeight + "px"
		});
	},
	hideProblematicElements: function() 
	{
		var doc = this.getDocument();
		
		var ell = this.container ? this.container.getElementsByTagName('object') : doc.getElementsByTagName('object');
		this.hideElements(ell, function(el) { 
			var l = el.getElementsByTagName('param'); 
			for(var i=0; i < l.length; i++)
				if(l[i].name.toLowerCase() == 'wmode' && l[i].value.toLowerCase() == 'transparent')
					return false;
			return true;
		});
		
		var ell = this.container ? this.container.getElementsByTagName('embed') : doc.getElementsByTagName('embed');
		this.hideElements(ell, function(el) { return !el.getAttribute('wmode') || el.getAttribute('wmode').toLowerCase() != "transparent"});
		
		
		ell = this.container ? this.container.getElementsByTagName('select') : doc.getElementsByTagName('select');
		//this.hideElements(ell);
	},	
	hideElements: function(ell, condition) 
	{
		for(var i=0; i < ell.length; i++) 
		{
			el = ell[i];
			if(condition)
				if(!condition(el))
					continue;
			el.style.visibility='hidden';
			this.hiddenElements.push(el);
		}	
	},
	hide: function() 
	{
		if(this.visible && this.overlayBackgroundElement) 
		{
			this.overlayBackgroundElement.parentNode.removeChild(this.overlayBackgroundElement);
			this.visible = false;
			for(var i=0; i < this.hiddenElements.length; i++) {
				this.hiddenElements[i].style.visibility = 'visible';
			}
		}
	},
	getDocument: function() 
	{
		if(this.w) 
		{
			return (this.w.document ? this.w.document : this.w.contentDocument);
		}
		else
			return document;	
	},
	getWindow: function() 
	{
		if(this.w) return this.w;
		return window;
	}	
});

Arland.Overlay.FixPngTransparency = function (el) {
	var f = 'DXImageTransform.Microsoft.AlphaImageLoader';

	function filt(el, s, m)
	{
	 if (el.filters[f])
	 {
		el.filters[f].enabled = s ? true : false;
		if (s) with (el.filters[f]) { src = s; sizingMethod = m }
	 }
	 else if (s) 
		el.style.filter = 'progid:'+f+'(src="'+s+'",sizingMethod="'+m+'")';
	}

	function fix(el){
		if (!/MSIE (5\.5|6\.)/.test(navigator.userAgent)) return;
		var bgImg = el.currentStyle.backgroundImage;
		if (bgImg.match(/^url[("']+(.*\.png)[)"']+$/i))
		{
			var s = RegExp.$1;
			if (el.currentStyle.width == 'auto' && el.currentStyle.height == 'auto')
				el.style.width = el.offsetWidth + 'px';
			el.style.backgroundImage = 'none';
			filt(el, s, 'scale');
		}
	}
	fix(el);
}

Arland.OverlaySpinner = Class.create(Arland.Overlay, {	
	initialize: function($super, className, container, w) {
		$super(className, container, w);
	},
	update: function(content)
	{
		if(this.visible)
			this.overlaySpinnerElement.innerHTML = content;
	},
		
	show: function(content)
	{
		if(!this.visible && this.container) 
		{
			var doc = this.getDocument();			
			
			this.overlayBackgroundElement = doc.createElement("div");
			this.overlayBackgroundElement.className = this.className + "Bg";
			this.overlaySpinnerElement = doc.createElement("div");
			this.overlaySpinnerElement.className = this.className;
			this.overlaySpinnerElement.appendChild(doc.createTextNode(content));
			
			this.overlayBackgroundElement.appendChild(this.overlaySpinnerElement);
			
			this.hideProblematicElements();			
			this.adjustSizeAndPosition();
			
			this.container.insertBefore(this.overlayBackgroundElement, this.container.firstChild);
			this.visible = true;
		}
	}
});

Arland.OverlayMessageBox = Class.create(Arland.Overlay, {
	initialize: function($super, className, container, w, options) 
	{
		$super(className, container, w);
		this.options = options || {};
		if(this.container) 
		{
			if(this.container.overlaySpinner)
			{
				try 
				{
					this.container.overlaySpinner.hide();			
				}
				catch(ex) {}
			}	
			this.container.overlaySpinner = this;		
		}				
	},
	setButtons: function(buttonArray)
	{
		this.buttons = buttonArray;
	},
	setTitle: function(title)
	{
		this.title = title;
	},
	setOkButtonTitle: function(title)
	{
		this.okButtonTitle = title;
	},
	setOkButtonCallback: function(cb)
	{
		this.okButtonCallback = cb;
	},
	setDescription: function(description)
	{
		this.description = description;
	},
	setImage: function(image)
	{
	    this.image = image;
	},
	update: function()
	{
	},		
	show: function()
	{
		if(!this.visible && this.container) 
		{
			var doc = this.getDocument();			
			
			this.overlayBackgroundElement = $(doc.createElement("div"));
			this.overlayBackgroundElement.className = this.className + "Bg";
			this.overlayMessageBoxElement = $(doc.createElement("div"));
			this.overlayMessageBoxElement.className = "messagebox";

			this.overlayTitleElement = doc.createElement("div");
			this.overlayTitleElement.className = "title";
			if(this.title)
				this.overlayTitleElement.appendChild(doc.createTextNode(this.title));

            this.overlayInnerElement = doc.createElement("div");
            this.overlayInnerElement.className = "inner";
            
			this.overlayDescriptionElement = doc.createElement("div");
			this.overlayDescriptionElement.className = "description";
			if(this.image)
			{
			    this.imageElement = doc.createElement("img");
			    this.imageElement.className = "image";
			    this.imageElement.src = this.image;
				this.overlayInnerElement.appendChild(this.imageElement);
			}
			if(this.description)
				this.overlayDescriptionElement.appendChild(doc.createTextNode(this.description));

			this.buttonsElement = doc.createElement("div");
			this.buttonsElement.className = "buttons";			
			var okButton = this.createButton(doc, this.okButtonTitle ? this.okButtonTitle : "OK", "okay", (function() 
				{ 
					this.hide();
				}).bind(this));
			this.buttonsElement.appendChild(okButton);

			this.overlayMessageBoxElement.appendChild(this.overlayTitleElement);
			this.overlayInnerElement.appendChild(this.overlayDescriptionElement);
			this.overlayMessageBoxElement.appendChild(this.overlayInnerElement);
			this.overlayMessageBoxElement.appendChild(this.buttonsElement);
			this.overlayBackgroundElement.appendChild(this.overlayMessageBoxElement);
			
			this.hideProblematicElements();
			
			//this.container.insertBefore(this.overlayMessageBoxElement, this.container.firstChild);
			if(this.options.fadein && "Effect" in window)
			{
				this.overlayBackgroundElement.hide();
				this.overlayMessageBoxElement.hide();
			}
			this.container.appendChild(this.overlayBackgroundElement, this.container);
			this.container.appendChild(this.overlayMessageBoxElement, this.container);
			if(this.options.fadein && "Effect" in window)
			{
				Effect.Appear(this.overlayBackgroundElement, {
					to: 0.7, 
					duration: this.options.fadein / 3
				});
				Effect.Appear(this.overlayMessageBoxElement, {
					duration: this.options.fadein
				});
			}
			this.adjustSizeAndPosition();

			Event.observe(window, "resize", this.adjustSizeAndPosition.bind(this));
			this.visible = true;
		}
	},
	hide: function() 
	{
		if(this.visible && this.overlayBackgroundElement) 
		{
			if(this.options.fadeout && "Effect" in window)
			{
				Effect.Fade(this.overlayMessageBoxElement, {
					duration: this.options.fadein,
					afterFinish: (function() {
						this.overlayBackgroundElement.parentNode.removeChild(this.overlayBackgroundElement);
						this.visible = false;
						for(var i=0; i < this.hiddenElements.length; i++) {
							this.hiddenElements[i].style.visibility = 'visible';
						}
						if(this.okButtonCallback) { 
							this.okButtonCallback(); 
						}
					}).bind(this)
				});
			}
			else
			{
				this.overlayBackgroundElement.parentNode.removeChild(this.overlayBackgroundElement);
				this.visible = false;
				for(var i=0; i < this.hiddenElements.length; i++) {
					this.hiddenElements[i].style.visibility = 'visible';
				}
				if(this.okButtonCallback) { 
					this.okButtonCallback(); 
				}
			}
		}
	},
	adjustSizeAndPosition: function() 
	{
		var doc = this.getDocument();
		var win = this.getWindow();
		var containerSize = Element.getDimensions(this.container);
		var messageboxSize = Element.getDimensions(this.overlayMessageBoxElement);
		
		var top = ((containerSize.height - messageboxSize.height) / 2);
		var left = ((containerSize.width - messageboxSize.width) / 2);
		//top += window.pageYOffset ? window.pageYOffset : document.body.scrollTop;
		
		
		this.overlayMessageBoxElement.style.top = top + "px";
		this.overlayMessageBoxElement.style.left = left + "px";
//		this.overlayBackgroundElement.style.height = "0px";
	},
	createButton: function(doc, title, cssClass, callback)
	{
		var button = doc.createElement("button");
		button.className = cssClass;
		button.appendChild(doc.createTextNode(title));
		Event.observe(button, "click", callback);
		return button;
	}
});

Arland.OverlayMessageBox.showMessageBox = function(options)
{
	var mb = new Arland.OverlayMessageBox (options.cssClass, options.container, options.window || window, options)
	if(options.title)
		mb.setTitle(options.title);
	if(options.description)
		mb.setDescription(options.description);
	if(options.image)
		mb.setImage(options.image);
	if(options.okButtonTitle)
		mb.setOkButtonTitle(options.okButtonTitle);
	if(options.okButtonCallback)
		mb.setOkButtonCallback(options.okButtonCallback);
	if(options.additionalButtons)
		mb.setButtons(options.additionalButtons);
	mb.show();
	return mb;
}

/**
 *
 */
Arland.FullscreenOverlaySpinner = Class.create(Arland.OverlaySpinner, {
	initialize: function($super, className, w, options) 
	{
		this.visible = false;
		this.className = className;
		this.hiddenElements = new Array();
		this.w = w;
		this.options = options || {};
		// Only one overlay spinner per window allowed, so if one exists already, hide it
		if(this.getWindow().overlaySpinner)
			this.getWindow().overlaySpinner.hide();
		this.getWindow().overlaySpinner = this;
	},
	hideProblematicElements: function() 
	{
		var doc = this.getDocument();
		this.hideProblematicElementsForDoc(doc);
		var frames = this.getWindow().frames;
		for(var i=0; i < frames.length; i++)
		{
			var frame = frames[i];
			this.hideProblematicElementsForDoc(frame.document);
		}
	},
	hideProblematicElementsForDoc: function(doc)
	{
		var ell = this.container ? this.container.getElementsByTagName('object') : doc.getElementsByTagName('object');
		this.hideElements(ell, function(el) { 
			var l = el.getElementsByTagName('param'); 
			for(var i=0; i < l.length; i++)
				if(l[i].name.toLowerCase() == 'wmode' && l[i].value.toLowerCase() == 'transparent')
					return false;
			return true;
		});
		
		var ell = this.container ? this.container.getElementsByTagName('embed') : doc.getElementsByTagName('embed');
		this.hideElements(ell, function(el) { return !el.getAttribute('wmode') || el.getAttribute('wmode').toLowerCase() != "transparent"});
		
		
		ell = this.container ? this.container.getElementsByTagName('select') : doc.getElementsByTagName('select');
		this.hideElements(ell);
	},
	dispose: function() 
	{
		this.getWindow().overlaySpinner = null;	
	},
	
	update: function(content) 
	{
		if(this.visible)
			this.overlaySpinnerElement.innerHTML = content;
	},
	
	show: function(content)
	{
		if(!this.visible) 
		{
			var doc = this.getDocument();			
			var win = this.getWindow();
			
			
			this.__boundAdjustSizeAndPosition = this.adjustSizeAndPosition.bind(this);
			Event.observe(win, "resize", this.__boundAdjustSizeAndPosition);
			Event.observe(win, "scroll", this.__boundAdjustSizeAndPosition);
			Event.observe(win, "load", this.__boundAdjustSizeAndPosition);
				
			this.overlayBackgroundElement = $(doc.createElement("div"));
			this.overlayBackgroundElement.className = this.className + "Bg";

			if(this.options["onClick"])
				this.overlayBackgroundElement.observe("click", (function(e) {
					this.options["onClick"](this, e);
				}).bind(this));
			
			if(this.options["content"])
			{
				this.overlaySpinnerElement = $(this.options["content"]);
				this.overlaySpinnerElement.parentNode.removeChild(this.overlaySpinnerElement);
				this.overlaySpinnerElement.addClassName(this.className);
				this.overlaySpinnerElement.show();
			}
			else
			{
				this.overlaySpinnerElement = $(doc.createElement("div"));
				this.overlaySpinnerElement.className = this.className;
				
				this.overlaySpinnerElement.appendChild(doc.createTextNode(content));
			}

			this.overlayBackgroundElement.appendChild(this.overlaySpinnerElement);
			
			this.hideProblematicElements();		
			
			doc.body.style.overflow = "hidden";

			doc.body.insertBefore(this.overlayBackgroundElement, doc.body.firstChild);			
			Arland.Overlay.FixPngTransparency(this.overlayBackgroundElement);
			this.adjustSizeAndPosition();
			
			win.scrollTo(0,0);
			this.visible = true;
		}
	},
	hide: function() 
	{
		if(this.visible && this.overlayBackgroundElement) 
		{
			this.overlaySpinnerElement.parentNode.removeChild(this.overlaySpinnerElement);
			this.overlayBackgroundElement.parentNode.removeChild(this.overlayBackgroundElement);
			this.visible = false;
			this.getDocument().body.style.overflow = "auto";
			for(var i=0; i < this.hiddenElements.length; i++) {
				this.hiddenElements[i].style.visibility = 'visible';
			}

			if(this.__boundAdjustSizeAndPosition)
			{
				var win = this.getWindow();
				Event.stopObserving(win, "resize", this.__boundAdjustSizeAndPosition);
				Event.stopObserving(win, "scroll", this.__boundAdjustSizeAndPosition);
				Event.stopObserving(win, "load", this.__boundAdjustSizeAndPosition);
			}
		}
	},	
	adjustSizeAndPosition: function() 
	{
		var doc = this.getDocument();
		var win = this.getWindow();

		var width = win.innerWidth ||                      // alle Browser
			(doc.documentElement.clientWidth ||     // IE im Standard-Mode
			doc.body.clientWidth);                 // IE im Quirks-Mode
		var height = win.innerHeight || 
		   (doc.documentElement.clientHeight || 
			doc.body.clientHeight);
		
		var top = ((height-this.overlaySpinnerElement.offsetHeight) / 2);
		var left = ((width-this.overlaySpinnerElement.offsetWidth) / 2);		
		
		this.overlaySpinnerElement.style.top = top + "px";
		this.overlaySpinnerElement.style.left = left + "px";
	}
});


Arland.ImageSequenceFullscreenOverlaySpinner = Class.create(Arland.FullscreenOverlaySpinner, {
	initialize: function($super, className, w, sequenceImagePath, numSequenceImages, fps)
	{
		this.visible = false;
		this.className = className;
		this.hiddenElements = new Array();
		this.w = w;
		// Only one overlay spinner per window allowed, so if one exists already, hide it
		if(this.getWindow().overlaySpinner)			
			this.getWindow().overlaySpinner.hide();			
		this.getWindow().overlaySpinner = this;

		this.sequenceImagePath = sequenceImagePath;
		this.sequenceImageIndex = 0;
		this.numSequenceImages = numSequenceImages;
		this.fps = fps;
	},
	show: function(content)
	{
		this.SUPER(content);
		if(this.visible) 
		{
			var doc = this.getDocument();
			this.sequenceImage = doc.createElement("img");
			this.messageElement = doc.createElement("div");
			this.messageElement.className = "msg";
			this.sequenceImage.className = "image";
			this.sequenceImage.src = this.sequenceImagePath.replace("[[0]]", "00");
			this.messageElement.innerHTML = this.overlaySpinnerElement.innerHTML;
			this.overlaySpinnerElement.innerHTML = "";

			this.overlaySpinnerElement.appendChild(this.sequenceImage);
			this.overlaySpinnerElement.appendChild(this.messageElement);

			if(!this.sequenceImageInterval && !(Arland.BrowserInfo.Browser == "MSIE" && Arland.BrowserInfo.Version == "6.0"))
			{
				this.sequenceImageInterval = window.setInterval(this.updateSequenceImage.bind(this), 1000 / this.fps);
			}
			else
				this.sequenceImage.src = this.sequenceImagePath.replace("[[0]]", "ie60").replace(".png", ".gif");
		}
	},
	update: function(content)
	{
		this.messageElement.innerHTML = content;
	},
	hide: function()
	{
		this.SUPER();
		window.clearInterval(this.sequenceImageInterval);
		this.sequenceImageInterval = null;
	},
	updateSequenceImage: function() {
		this.sequenceImageIndex++;
		if(this.sequenceImageIndex >= this.numSequenceImages)
			this.sequenceImageIndex = 0;
		this.sequenceImage.src = this.sequenceImagePath.replace("[[0]]", (this.sequenceImageIndex < 10 ? "0" : "") + this.sequenceImageIndex);
	}
});
