/* lightbox (c) martin kufner 2008 */
/* Usage
	Lightbox.create(html|id[,options])
	options:
		id: sets id
		className: sets Classname
		closename: content of close-button. might be html
		onCreate: function executed after creation
		onClose: function executed after closing
		writeback: true only usable when created from an object
		noShadow: true|false shall i create a shadow?

		dimensions: object alters positioning
			left:, top:, width:, height: numeric or String starting with numbers
			clone: id|object clone position of the object
			with clone left:, top:, width:, height: true means which property to clone.
*/
var Lightbox = function(el,options) {return Lightbox.create(el,options);}
Object.extend(Lightbox,{
	baseZ: 10000,
  defaults: {
		id: 'lightbox',
		className: 'lightbox',
		closename: 'Close',
		onCreate: null,
		onClose: null,
		writeback: false,
		position: function(dims) {
			var vpd = document.viewport.getDimensions();
			var bd = document.body.getDimensions();
			var vpso = document.viewport.getScrollOffsets();
			this.scrolling = this.down(".lightboxScrolling");
			if(this.scrolling) this.scrolling.hide();

			this.shadow.setStyle({
					top: 0+'px',
					left: 0+'px',
					height: Math.max(bd.height,vpd.height)+'px',
					width: Math.max(bd.width,vpd.width)+'px'
			});

			var lpd = this.getDimensions();

			lpd.left = vpd.width / 2 - lpd.width / 2;
			lpd.top = 0;

			if(dims && dims.clone) {
				if(dims.clone = $(dims.clone)) {
					if(dims.width == true || dims.height == true) {
						var d = dims.clone.getDimensions();
						if(dims.width == true) dims.width = d.width;
						if(dims.heigth == true) dims.height = d.height;
					}
					if(dims.top == true || dims.left == true) {
						var p = dims.clone.cumulativeOffset();
						if(dims.top == true) dims.top = p.top;
						if(dims.left == true) dims.left = p.left;
					}
				}
			}
			Object.extend(lpd,dims);

			var position = 'absolute'
			if(/MSIE (5\.5)|[6]/.test(navigator.userAgent) && navigator.platform == "Win32") {
				var vpd = document.viewport.getScrollOffsets();
				lpd.top =  vpd.top + 70;
				position = 'absolute';
			}

			if(lpd.left < 0) lpd.left = 0;
//			if(this.closeButton) {
//				var lpcd = this.closeButton.getDimensions();
//				if(lpd.top < lpcd.height + 10) lpd.top = lpcd.height + 10;
lpd.top = document.viewport.getScrollOffsets().top + 30;
//			}
var maxheight = vpd.height - 60;
			this.container.setStyle( {
					position:position,
					left: parseInt(lpd.left) + 'px',
					top: parseInt(lpd.top) + 'px',
					maxHeight: maxheight + 'px'
			});

			if(dims && (dims.width || dims.height)) {
				var st = {}
				if(lpd.width) st.width = parseInt(lpd.width)+'px';
				if(lpd.height) st.height = parseInt(lpd.height)+'px';
				this.setStyle(st);
			}

			if(this.scrolling) {
				this.scrolling.setStyle({maxHeight: maxheight - this.container.getHeight() + 'px'}).show();
			}


//			if(this.closeButton) this.closeButton.setStyle({position:'absolute',right:'0px',top:(-lpcd.height)+'px'});
		},
		close: function() {Lightbox.close(this);},
		content: function() {return this.innerHTML;}
  },
  close: function(lightbox) {
		if(!lightbox) lightbox = this.defaults.id;
		lightbox = $(lightbox);
		Event.stopObserving(window,"resize",lightbox.resizeMe);
		Event.stopObserving(window,"scroll",lightbox.scrollMe);

		if(!lightbox) return;
		if(lightbox.onClose) try { lightbox.onClose(lightbox.content()) } catch(e) {
			if(console && console.log) console.log(e);
		};
		rv = lightbox.innerHTML;
		lightbox.layer.remove();
		if(lightbox.writeback) $(lightbox.writeback).update( rv );
		$$('object').invoke('show');
		return rv;
	},
	create: function(el,options) {
		options = Object.extend(Object.clone(this.defaults),options || {});
		if(typeof(options.onCreate) == 'string') eval("options.onCreate = "+options.onCreate);
		if(typeof(options.onClose) == 'string') eval("options.onClose = "+options.onClose);
		var lightbox = $(options.id);
		var body = $(document.body);

		if(!lightbox) {
			lightbox = $(document.createElement("div"));
			Object.extend(lightbox,options);
			lightbox.setStyle({position: 'relative'});
			lightbox.className = lightbox.className + ' lightbox channelBorder';

			lightbox.container = $(document.createElement("div"));
			lightbox.container.setStyle({ position: 'absolute', zIndex: Lightbox.baseZ+2 });
			lightbox.container.className = options.id+'_container lightboxContainer';

			lightbox.layer = $(document.createElement("div"));
			lightbox.layer.setStyle({visibility: 'hidden',position:'absolute',top:'0px',left:'0px',zIndex:Lightbox.baseZ});
			lightbox.layer.className = options.id+'_layer lightboxLayer';

			lightbox.shadow = $(document.createElement("div"));
			if(lightbox.noShadow) {
				lightbox.shadow.hide();
			} else {
				lightbox.shadow.setStyle({ position: 'absolute', zIndex: Lightbox.baseZ+1, textAlign:'right' });
				lightbox.shadow.className = options.id+'_shadow lightboxShadow';
				lightbox.shadow.lightbox = lightbox;
				if(options.outSideClickCloses) lightbox.shadow.onclick = function(){this.lightbox.close();return false;}
			}

			if(!options.noCloseButton) {
				lightbox.closeButton = $(document.createElement("a"));
				lightbox.closeButton.onclick = function(){this.lightbox.close();return false;}
				lightbox.closeButton.lightbox = lightbox;
				lightbox.closeButton.setStyle({position: 'absolute', display: 'block'});
				lightbox.closeButton.className = options.id+'_close lightboxClose';
				lightbox.closeButton.innerHTML = lightbox.closename;

				lightbox.container.appendChild(lightbox.closeButton);
			}

			lightbox.container.appendChild(lightbox);
			lightbox.layer.appendChild(lightbox.container);
			lightbox.layer.appendChild(lightbox.shadow);
			body.insert({top: lightbox.layer});
		} else {
			Object.extend(lightbox,options);
			lightbox.update('');
			lightbox.layer.setStyle({zIndex:Lightbox.baseZ});
			lightbox.shadow.setStyle({zIndex: Lightbox.baseZ+1});
			lightbox.container.setStyle({zIndex: Lightbox.baseZ+2 });
		}
		Lightbox.baseZ = Lightbox.baseZ+3;

		if(el) {
			if(typeof(el) == 'string') {
				lightbox.update(el);
			} else if( typeof(el) == 'object') {
				lightbox.update(el.innerHTML);
				el.update();
				if(lightbox.writeback == true) lightbox.writeback = el;
			} else lightbox.writeback = false;
		}

		lightbox.position(lightbox.dimensions);

		if(lightbox.onCreate) lightbox.onCreate();
		$$('object').invoke('hide');
		lightbox.select('object').invoke('show');
		lightbox.layer.setStyle({visibility: 'visible'});
//		if(lightbox.closeButton) lightbox.closeButton.scrollTo();


		lightbox.resizeMe = function(event) {
			var vpd = document.viewport.getDimensions();
			var bd = document.body.getDimensions();
			this.shadow.style.height = Math.max(bd.height,vpd.height)+'px';
			this.shadow.style.width = Math.max(bd.width,vpd.width)+'px';
			var maxheight = vpd.height - 60;
			this.container.setStyle({ maxHeight: maxheight + 'px'});

			if(this.scrolling) {
				this.scrolling.setStyle({maxHeight: maxheight - this.container.getHeight() + 'px'});
			}

		}
		Event.observe(window,"resize",lightbox.resizeMe.bind(lightbox));

		lightbox.scrollMe = function(event) {
			var vpso = document.viewport.getScrollOffsets();
			this.container.style.top = vpso.top + 30 + "px";
		}
		Event.observe(window,"scroll",lightbox.scrollMe.bind(lightbox));
		return lightbox;
	}
});
