var nzWindow = Class.create({
	initialize: function(element, content, options){
		if (! document.nzWinCount ) document.nzWinCount = 0;
		document.nzWinCount++;		
		this.nzWinID = document.nzWinCount;	
		
		if (! options.float) options.float = "left";
		if (! options.offset) options.offset = {top: 0, left: 0};
		if (! options.position) options.position = "absolute";
		if (! options.className) options.className = "";
		
		this.options = options;
		this.refElement = $(element);
		this.element = new Element("div", {className: "nzWinContainer"});
		this.element.insert('<div class="nzWindow"><a href="" class="xChiudi">X</a></div>');
		contBox = new Element('div');
		contBox.addClassName(options.className);
		this.element.down().insert(contBox);
		//this.element.setStyle({position: options.position});
		this.initialContent = content;
		this.element.down().nzWindow = this;
		if (typeof(content) == "object"){
			content.insert({after: '<div id="nzWinContPos' + this.nzWinID + '"></div>'});
			contBox.update(content);
		} else {
			contBox.insert(content);
		}
		
		this.element.down().down().observe("click", 
			this.close.bindAsEventListener(this).wrap(function(p,event){event.stop(); p(event);}));
	},
	show: function(){
		//centra la finestra se position=="center"
		if ("center" == this.options.offset){
			this.element.down().setStyle({float: "left"});
			//devo inserirla nel dom se gi� non c'� per conoscerne le dimensioni, ma la nascondo
			//con un offset molto alto, che poi toglier�
			if (! this.element.descendantOf(document.body)){
				this.element.down().setStyle({left: "-10000px"});
				this.refElement.insert({top: this.element});
			}
			
			if ("fixed" == this.options.position){
				offleft = document.viewport.getScrollOffsets().left + (document.viewport.getWidth() - this.element.down().getWidth())/2;
				offtop = document.viewport.getScrollOffsets().top + (document.viewport.getHeight() - this.element.down().getHeight())/2;
				this.element.setStyle({height: $(document.body).getHeight() + "px"});
			} else {		
				offleft = (this.refElement.getWidth() - this.element.down().getWidth())/2;
				offtop = (this.refElement.getHeight() - this.element.down().getHeight())/2;
			}
			this.element.down().setStyle({left: String(offleft)+'px', top: String(offtop)+'px'});
		} 
		else 
		{
			this.element.down().setStyle({float: this.options.float});
			this.element.down().setStyle(this.options.offset);
			this.refElement.insert({top: this.element});			
		}
	},
	close: function(){
		winPos = $('nzWinContPos' + String(this.nzWinID));
		if (winPos){
			winPos.replace(this.initialContent);
		}
		this.element.remove();
	},
	getContent: function(){
		return this.element.down().down(1);
	},
	setContent: function(content){
		this.element.down().down(1).remove();
		this.element.down().insert(new Element('div', {className: this.options.className}));
		this.element.down().setStyle({left: "-10000px"});
		this.element.down().down(1).insert(content);
		this.show();
	
	}
});