
	var PopCon = {
	};
	
	
	function PopUp(src) {		
		this.src = $(document.body).insert(src);
		this.src = Element.extend(this.src.lastChild);
		
		this.id = "popup" + Math.round(10000 * Math.random());
		this.src.writeAttribute("id", this.id);
		
		////////////////
		// ZUM CONTAINER
		////////////////
		PopCon[this.id] = this;
		
		
		///////////////
		// VERSCHIEBBAR
		///////////////
		new Draggable(this.id, {handle: "head", onDrag: this.overlap.bind(this)});
		
		
		////////////
		// SCHLIEßEN
		////////////
		var handles = this.src.select(".closeHandle");
		for(var i = 0; i < handles.length; i++) {
			handles[i].observe("click", this.close.bind(this));
		}
		
		
		////////////////
		// POSITIONIEREN
		////////////////
		var dim = document.viewport.getDimensions();
		var scl = document.viewport.getScrollOffsets();
		var dim2 = this.src.getDimensions();
		
		var newpos = {
			left: (dim.width / 2) - (dim2.width / 2),
			top: (dim.height / 2) - (dim2.height / 2) + scl.top
		}
		
		if(newpos.top < scl.top) {
			newpos.top = scl.top;
		}
		
		
		this.src.setStyle({
			left: newpos.left + "px",
			top: newpos.top + "px"
		});
	}
	
	
	PopUp.prototype.show = function() {
		this.src.setStyle({opacity: 0.0});
		this.src.show();
		
		///////////////////////////
		// SELECT FÜR IE VERSTECKEN
		///////////////////////////
		this.overlap();
		
		this.src.appear({duration: 0.5});
	}
	
	
	PopUp.prototype.close = function() {
		this.src.fire("popup:close", {id: this.id});
		this.src.fade({duration: 0.5, afterFinish: function(src) {
			Element.extend(src.element).remove();
		}});
		
		$$("select").each(function(sel) {
			sel.setStyle({visibility: "visible"});
		});
	}
	
	
	PopUp.prototype.overlap = function() {		
		if (typeof document.body.style.maxHeight != "undefined") {
			return false;
		}
		
		var sel = $$("select");
		
		if(!sel.length) {
			return false;
		}
		
		function getBox(elem) {
			var pos = elem.cumulativeOffset();
			var dim = elem.getDimensions();
			
			var box = {
				x1: pos.left,
				y1: pos.top,
				x2: pos.left + dim.width,
				y2: pos.top + dim.height
			}
			
			return box;
		}
		
		
		var pop = getBox(this.src);
		
		
		
		for(var i = 0; i < sel.length; i++) {
			var box = getBox(sel[i]);
						
			if(box.y1 < pop.y1 && box.y2 < pop.y1) {
				sel[i].setStyle({
					visibility: "visible"
				});
				
				continue;
			}
			
			if(box.y1 > pop.y2) {
				sel[i].setStyle({
					visibility: "visible"
				});
				
				continue;
			}	
			
			if(box.x1 < pop.x1 && box.x2 < pop.x1) {
				sel[i].setStyle({
					visibility: "visible"
				});
				
				continue;
			}	
			
			if(box.x1 > pop.x2) {
				sel[i].setStyle({
					visibility: "visible"
				});
				
				continue;
			}	
			
					
			sel[i].setStyle({
				visibility: "hidden"
			});
		}
	}
	
	
	