(function() {
	SAB.widget.Nightbox = {};

	SAB.widget.Nightbox.img = function(src, cfg) {
		cfg = cfg || {};
		cfg.id = cfg.id || YAHOO.util.Dom.generateId();
		this.config = cfg;
		this.container = new YAHOO.widget.Panel(cfg.id, this.containerConfig);
		this.show();
		this.image = new Image();
		YAHOO.util.Event.addListener(this.image, 'load', this.ready, this, true);
		YAHOO.util.Event.addListener(this.image, 'error', this.error, this, true);
		this.image.src = src;
	};

	SAB.widget.Nightbox.img.prototype = {
		container: null,
		image: null,
		width: 0,
		height: 0,
		loaded: false,
		config: null,
		containerConfig: {
			fixedcenter: true,
			modal: true,
			// width: '200px',
			height: '150px',
			contraintoviewport: true,
			visible: true,
			zIndex: 65000
		},
		setClass: function(className) {
			this.config.className = className;
			return this;
		},
		show: function() {
			var Dom = YAHOO.util.Dom;
			var Event = YAHOO.util.Event;
			this.container.setHeader(this.config.title || '');
			if (this.loaded) {
				this.container.setBody('<img src=\"' + this.image.src + '\" />');
			} else {
				this.container.setBody('<img src=\"/images/loading_big.gif\" />');
			}
			// this.container.setFooter('');
			this.container.render('body');
			var maskname = this.container.id + '_mask';
			Event.on(maskname, 'click', function() {
				this.container.hide();
			}, this, true);
			if (this.config.className) {
				Dom.addClass(this.container.id, this.config.className);
			}
			this.container.show();
			var width = Dom.getStyle(this.container.id, 'width');
			Dom.setStyle(this.container.body, 'width', width);
			Dom.setStyle(this.container.id, 'height', '');
			Dom.setStyle(this.container.id, 'width', '');
			Dom.setStyle(this.container.body, 'text-align', 'center');
			return this;
		},
		ready: function(e) {
			var Dom = YAHOO.util.Dom;

			this.loaded = true;

			var vpWidth = Dom.getViewportWidth();
			var vpHeight = Dom.getViewportHeight();

			var width = this.image.width;
			var height = this.image.height;

			var maxWidth = vpWidth - 100;
			var maxHeight = vpHeight - 200;

			if (width > maxWidth) {
				height = maxWidth / width * height;
				width = maxWidth;
			}

			if (height > maxHeight) {
				width = maxHeight / height * width;
				height = maxHeight;
			}

			var xpos = (vpWidth - width) / 2 + Dom.getDocumentScrollLeft();
			var ypos = (vpHeight - height) / 2 + Dom.getDocumentScrollTop();

			this.width = width;
			this.height = height;

			Dom.setStyle(this.image, 'width', width + 'px');
			Dom.setStyle(this.image, 'height', height + 'px');

			var resize_cfg = {
				width: { to: width + 10 },
				height: { to: height }
			};
			var move_cfg = {
				points: { to: [xpos, ypos] }
			};

			window.bla = this;

			var anim = new YAHOO.util.Anim(this.container.body, resize_cfg, .3, YAHOO.util.Easing.easeOut); 
			var move = new YAHOO.util.Motion(this.container.element, move_cfg, .3, YAHOO.util.Easing.easeOut); 
			anim.onComplete.subscribe(this.resized, this, true);
			move.animate();
			anim.animate();
		},
		resized: function(e) {
			var Dom = YAHOO.util.Dom;
			Dom.setStyle(this.image, 'opacity', 0);
			this.container.setBody(this.image);

			Dom.setStyle(this.container.body, 'width', (this.width + 10) + 'px');
			Dom.setStyle(this.container.body, 'height', this.height + 'px');

			Dom.setStyle(this.container.id, 'height', '');
			Dom.setStyle(this.container.id, 'width', '');

			var anim = new YAHOO.util.Anim(this.image, { opacity: { from: 0, to: 1 } }, 1, YAHOO.util.Easing.easeOut); 
			anim.animate();
		},
		error: function(e) {
			this.container.setBody('<div style=\"font-weight:bold;text-align:center;\">Dieses Bild kann leider nicht angezeigt werden.</div>');
		}
	};

	
	SAB.widget.Nightbox.Basic = function() {
		return {
			isInit:false
			,container: null
			,id: null
			,containerConfig: {
				fixedcenter: true,
				modal: true,
				width: '800px',
				contraintoviewport: true,
				visible: true,
				zindex:65384
			}
			,content: {
				hd: 'Titel'
				,bd: 'Body'
				,ft: 'Footer'
			}
			,init: function() {
				this.isInit = true;
				this.container = new YAHOO.widget.Panel(this.id || YAHOO.util.Dom.generateId(),this.containerConfig);
			}
			,show: function() {
				if(!this.isInit) { this.init();}
				this._show();
				this.container.render('body');
				this.container.show();

			}
			, hide: function() {
				this._hide();
				// ..
			}
			,_show: function() {
				this.container.setHeader(this.content.hd);
				this.container.setBody(this.content.bd);
				this.container.setFooter(this.content.ft);
			}
			,_hide: function() {

			}
		};
	}	

	SAB.widget.Nightbox.Image = function(){
		return {
			o: null
			,image: null
			,show: function(name,url,width,height,title,caption) {
				var nightbox = new SAB.widget.Nightbox.Basic();
				var image = new Image();
				var timeout = 600;
				var margin_x = 10;
				var margin_y = 10;
				this.o = nightbox;
				this.image = image;
				this.image.src = url;
				if (title !== undefined) {
					this.o.content.hd = title;
				}
				if (caption !== undefined) {
					this.o.content.ft = caption;
				}
				if (this.o.content.ft) {
					margin_y += 5;
				}
				this.o.content.bd = "<div id='" + name + "'" +
					" style='text-align:center;'>" +
					// "<img id='" + name + "-img'" +
					// " src='/images/loading_big.gif'>" +
					"</div>";

				this.o.id = name + '_box';
				this.o.containerConfig.draggable = false;
				if (width && height) {
					this.o.containerConfig.width = (width + margin_x*2) + "px";
					this.o.containerConfig.height = (height + margin_y*2) + "px";
					this.o.show();

					var e = YAHOO.util.Dom.get(name);
					e.appendChild(image);
					// var e = YAHOO.util.Dom.get(name+'-img');
					// if (e && this.image) { e.src = this.image.src; }
				} else {
					this.o.containerConfig.width = (120 + margin_x*2) + 'px';
					this.o.containerConfig.height = (60 + margin_y*2) + 'px';

					this.o.show();
					var c = function(){
						if (image.width > 0) {
							var e = YAHOO.util.Dom.get(name+"-img");
							e.src = image.src;
							nightbox.container.cfg.setProperty("width", (image.width + margin_x*2) + "px");
							nightbox.container.cfg.setProperty("height", (image.height + margin_y*2) + "px");
							nightbox.container.cfg.refresh();
						} else {
							if (--timeout) {
								window.setTimeout(c, 100);
							} else {
								nightbox.container.hide();
							}
						}
					}
					// window.setTimeout(c, 2000);
					c();
				}
				// if (e && this.image) { e.src = this.image.src; }
			}
		};
	}

	SAB.widget.Nightbox.Image2 = function(){
		return {
			o: null
			,show: function(name,messg,width,height,title,caption) {
				var nightbox = new SAB.widget.Nightbox.Basic();
				this.o = nightbox;
				if (title !== undefined) {
					this.o.content.hd = title;
				}
				if (caption !== undefined) {
					this.o.content.ft = caption;
				}
				if (this.o.content.ft) {
					margin_y += 5;
				}
				this.o.content.bd = "<div id='" + name + "'" +
					" style='text-align:center;'>" +
					"<img id='" + name + "-img'" +
					" src='/images/loading_big.gif'>" +
					"</div>";

				if (width && height) {
					this.o.containerConfig.width = (width + margin_x*2) + "px";
					this.o.containerConfig.height = (height + margin_y*2) + "px";
					this.o.show();

					var e = YAHOO.util.Dom.get(name+"-img");
					if (e && this.image) { e.src = this.image.src; }
				} else {
					this.o.containerConfig.width = (120 + margin_x*2) + "px";
					this.o.containerConfig.height = (60 + margin_y*2) + "px";

					this.o.show();
					var c = function(){
						if (image.width > 0) {
							var e = YAHOO.util.Dom.get(name+"-img");
							e.src = image.src;
							nightbox.container.cfg.setProperty("width", (image.width + margin_x*2) + "px");
							nightbox.container.cfg.setProperty("height", (image.height + margin_y*2) + "px");
							nightbox.container.cfg.refresh();
						} else {
							if (--timeout) {
								window.setTimeout(c, 100);
							} else {
								nightbox.container.hide();
							}
						}
					}
					// window.setTimeout(c, 2000);
					c();
				}
				// if (e && this.image) { e.src = this.image.src; }
			}
		};
	}

	SAB.widget.Nightbox.toggle = function(cls, id, cfg) {
		box = YAHOO.util.Dom.get(id);
		if (box && box.Nightbox) {
			box.Nightbox.destroy();
			box.Nightbox = undefined;
		} else {
			var panel = new cls(id, cfg);
			box = YAHOO.util.Dom.get(id);
			if (box) {
				box.Nightbox = panel;
			}
		}
	}

	SAB.widget.Nightbox.Panel = function(id, cfg) {
		id = id || YAHOO.util.Dom.generateId();
		SAB.widget.Nightbox.Panel.superclass.constructor.call(this, id, cfg);
	}

	YAHOO.lang.extend(
		SAB.widget.Nightbox.Panel,
		YAHOO.widget.Panel,
		{
			defcfg: {
				width: "538px"
				, height: "405px"
				, fixedcenter: true
				, modal: true
				, contraintoviewport: true
				, visible: true
				, zindex: 32000
				, dragable: false
			}
			// , renderBox: null
			, init: function(id, cfg) {
				cfg = cfg || {};
				for (k in this.defcfg) {
					if (!cfg[k]) {
						cfg[k] = this.defcfg[k];
					}
				}
				cfg.renderObject = cfg.renderObject || document.body;

				if (YAHOO.util.Dom.getStyle(id, 'display') == 'none') {
					YAHOO.util.Dom.setStyle(id, 'display', '');
				}

				/* super.init */
				SAB.widget.Nightbox.Panel.superclass.init.call(this, id, cfg);

				var div = YAHOO.util.Dom.get(id);
				if (div.getAttribute('rendered') == true) {
					this.show();
					return;
				}

				if (cfg.header) {
					this.setHeader(cfg.header);
				}
				if (cfg.body) {
					this.setBody(cfg.body);
				}
				this.render(cfg.renderObject);
				div.setAttribute('rendered', true);
				this.show();
			}
		}
	);
	
	SAB.widget.Nightbox.ImageNG = function(id, cfg) {
		SAB.widget.Nightbox.ImageNG.superclass.constructor.call(this, id, cfg);
	}

	YAHOO.lang.extend(
		SAB.widget.Nightbox.ImageNG,
		SAB.widget.Nightbox.Panel,
		{
			url: null
			, divId: null
			, load: function() {
				var image = new Image();
				image.src = this.url;
				YAHOO.util.Event.onAvailable(this.divId, function(img) { this.appendChild(img); }, image);
			}
			, init: function(id, cfg) {
				if (cfg && cfg.url) {
					this.url = cfg.url;
				} else {
					return;
				}

				this.divId = id + '_inner';
				cfg.body = '<div id="' + this.divId + '" style="text-align:center;"></div>';
				/* super.init */
				SAB.widget.Nightbox.ImageNG.superclass.init.call(this, id, cfg);
				this.load();
			}
		}
	);
	
	SAB.widget.Nightbox.URLview = function(id, cfg) {
		SAB.widget.Nightbox.URLview.superclass.constructor.call(this, id, cfg);
	}

	YAHOO.lang.extend(
		SAB.widget.Nightbox.URLview,
		SAB.widget.Nightbox.Panel,
		{
			defcfg: {
				visible: true
				, zindex: 32000
				, dragable: false
				, contraintoviewport: true
			}
		}
	);

	
	SAB.widget.Nightbox.Dialog = function(id, renderObject, handels, cfg) {
		var buttons = [];
		id = id || YAHOO.util.Dom.generateId();
		for (var k = 0; k < handels.length; k++) {
			buttons.push({handler:handels[k]});
		}
		cfg = cfg || {};
		cfg.renderObject = renderObject;
		cfg.buttons = buttons;
		SAB.widget.Nightbox.Dialog.superclass.constructor.call(this, id, cfg);
	}

	YAHOO.lang.extend(
		SAB.widget.Nightbox.Dialog,
		YAHOO.widget.SimpleDialog,
		{
			defcfg: {
				fixedcenter: true
				, modal: true
				, contraintoviewport: true
				, visible: true
				, zindex: 32000
				, dragable: false
			}
			, button: function(ev, args) {
				args[1].call(args[0]);
			}
			, configButtons: function (type, args, obj) {
				var Button,
					aButtons = args[0],
					nButtons,
					Buttons,
					Lang = YAHOO.lang,
					Event = YAHOO.util.Event,
					Dom = YAHOO.util.Dom,
					fn,
					i;
				
				Buttons = Dom.getChildren(this.footer);
				nButtons = Buttons.length;
				
				for (i = 0; i < nButtons; i++) {
					Button = Buttons[i];
					fn = aButtons[i].handler;
					Event.removeListener(Button, "click");
					Event.addListener(Button, "click", this.buttonPressed, fn, this);
				}
				
				this.setFirstLastFocusable();
				
				this.cfg.refireEvent("iframe");
				this.cfg.refireEvent("underlay");
			}
			, buttonPressed: function (o, fn) {
				if (fn.call(this) !== false) {
					this.hide();
				}
			}
			, init: function(id, cfg) {
				var Dom = YAHOO.util.Dom;

				cfg = cfg || {};
				for (var k in this.defcfg) {
					if (!cfg[k]) {
						cfg[k] = this.defcfg[k];
					}
				}
				cfg.renderObject = cfg.renderObject || document.body;

				if (YAHOO.util.Dom.getStyle(id, 'display') == 'none') {
					YAHOO.util.Dom.setStyle(id, 'display', '');
				}

				/* super.init */
				SAB.widget.Nightbox.Dialog.superclass.init.call(this, id, cfg);
				this.render(cfg.renderObject);
				this.show();
			}
		}
	);
})();

