var dWindowZIndex = 1000;
var closePOP = new Image();
closePOP.src="imgs/closePOP.png";
var nxnx = 1;

var dWindow = new Class({
	options: {
		minWidth : 150,
		minHeight : 150,
		width : 200,
		height : 200,
		top : 0,
		left : 0,
		resizable : true,
		statusBar : true,
		content : '',
		id : ''
	},
	
	handle : null,
	
	drag : null,
	
	dragParams : {},

	initialize: function(options){
		this.setOptions(options);
	},
	
	_create : function(id){
		var target = $(id);
		if (!target)
			return;

		wxNew = ((window.getSize().x / 2) - (this.options.width / 2)) + nxnx;
		wyNew = this.options.top + nxnx;
		nxnx = nxnx + 5;

		this.handle = new Element('div', {
								'class':'dWindow',
								'id' : this.options.id,
								styles : {
									'width':this.options.width,
									'left':wxNew,
									'top':wyNew,
									'z-index':dWindowZIndex
								}
							});
		dWindowZIndex++;

		this.handle.addEvent('mousedown', this.up.bind(this));
	
		var bar = new Element('div', {'class':'topBar', styles : {'width':this.options.width}});

		bar.appendText(this.options.topContent);

		var closeDiv = new Element('div', {'class':'closeBtn'});
		var closeBtn = new Element('img', {'src':closePOP.src});
		if (!this.options.CloseFunc) {
			closeBtn.addEvent('click', this.close.bind(this));
		} else {
			var CloseFunc = this.options.CloseFunc;
			var RemoveDivID = this.options.id;
			closeBtn.addEvent('click', function(e) { new Event(e).stop(); CloseDivEvent(RemoveDivID,CloseFunc); });
		}
		closeBtn.injectInside(closeDiv);

		closeDiv.injectInside(bar);

		var table = new Element('table',{
			'class':'dContainer',
			'cellpadding':0,
			'cellspacing':0,
			'border':0
		});

		var tbody = new Element('tbody');

		var row = new Element('tr');
		var leftBorder = new Element('td', {'rowspan':2,'class':'leftBorder'});
		var rightBorder = new Element('td', {'rowspan':2,'class':'rightBorder'});
		var center = new Element('td');
		var content = new Element('div',{'id' : 'iDiv['+this.options.NewCount+']', 'class':'centralArea', styles : {'width':this.options.width, 'height':this.options.height}});
		content.set('html',this.options.content);
		center.adopt(content);
		
		row.adopt([leftBorder, center, rightBorder]);
		
		row.injectInside(tbody);
	
		tbody.injectInside(table);
		
		this.handle.adopt([bar, table]);
		
		this.handle.injectInside(target);

	    bpost('iDiv['+this.options.NewCount+']',this.options.params);

		this.drag = new Drag.Move(this.handle, {
		    snap: 0,
		    handle: bar,
			container: target,
		    onSnap: function(el){
		        el.addClass('dragging');
		    },
		    onComplete: function(el){
		        el.removeClass('dragging');
		    }
		});
	},
	
	_destroy : function(){
		if ($type(this.handle) == 'element'){
			this.handle.destroy();
		}
		this.handle = null;
	},
	
	open : function(elem){
		if (!this.handle)
			this._create(elem);
	},
	
	close : function(){
		this._destroy();
	},

	up : function(){
		this.handle.setStyle('z-index', ++dWindowZIndex);
	},
	
	Implements : [Options, Events]
});