var dialogResPath = dialogResPath == undefined?'/resources/js/dialogs/':dialogResPath;

var Dialog = function(params){
	
	//---------------- init params -------------------------
	this.id = params.id;
	
	if(!isNaN(params.width)) this.width = params.width;
	else throw new Error('Dialog error! Width should be integer');
	
	if(!isNaN(params.height)) this.height = params.height;
	else throw new Error('Dialog error! Height should be integer');
	
	if(params.top!=null) this.top = params.top;
		
	if(params.left!=null) this.left = params.left;
		
	this.headerText = params.headerText;
	
	if(params.imagePath!=null) this.imagePath = params.imagePath;
	else this.imagePath = dialogResPath+'close.gif';

	if(params.parent!=null) this.parent = params.parent;
	else this.parent = null;
	
	this.onClose = params.onClose;
	
	this.domNode = null;
	this.contentNode = null;
	this.contentDiv = null;
	this.header = null;
	this.topBorder = null;
	this.bottomBorder = null;
	this.leftBorder = null;
	this.rightBorder = null;
	this.cornerImg = null;
		
	//---------------- Elements creation --------------------
	this.createDomNode();
	this.createHeader();
	if(params.drag) this.setDrag();
	if(params.resizable) this.setResizable();
}

Dialog.instances = {};

Dialog.create = function(params){
	if(!Dialog.instances[params.id]) Dialog.instances[params.id] = new Dialog(params);
	return Dialog.instances[params.id];
}

Dialog.get = function(id){
	if (!Dialog.instances[id]){
		throw new Error('Dialog with id = "' + id + '" is not exist.');
	}
	return Dialog.instances[id]; 
}

Dialog.prototype.createDomNode = function(){
	this.domNode = document.createElement('div');
	this.domNode.id = this.id;
	this.domNode.style.position = 'absolute';
	this.domNode.style.width = this.width + 'px';
	this.domNode.style.height = this.height + 'px';
	if(this.top!=null) this.domNode.style.top = this.top;
	if(this.left!=null) this.domNode.style.left = this.left;
	this.domNode.style.zIndex = '1000';
	this.domNode.style.backgroundColor = '#d4d0c8';
	
	// borders creation
	this.rightBorder = document.createElement('div');
	this.rightBorder.style.height = this.height + 'px';
	this.rightBorder.className = 'borderVertical';
	this.rightBorder.style.top = '0px';
	this.rightBorder.style.right = '0px';
	this.rightBorder.style.backgroundImage = 'url('+dialogResPath+'right_border.jpg)';
	this.domNode.appendChild(this.rightBorder);
	
	this.leftBorder = document.createElement('div');
	this.leftBorder.style.height = this.height + 'px';
	this.leftBorder.className = 'borderVertical';
	this.leftBorder.style.top = '0px';
	this.leftBorder.style.left = '0px';
	this.leftBorder.style.backgroundImage = 'url('+dialogResPath+'left_border.jpg)';
	this.domNode.appendChild(this.leftBorder);
	
	this.topBorder = document.createElement('div');
	this.topBorder.style.width = this.width + 'px';
	this.topBorder.className = 'borderHorisontal';
	this.topBorder.style.top = '0px';
	this.topBorder.style.left = '0px';
	this.topBorder.style.backgroundImage = 'url('+dialogResPath+'top_border.jpg)';
	this.domNode.appendChild(this.topBorder);
	
	this.bottomBorder = document.createElement('div');
	this.bottomBorder.style.width = this.width + 'px';
	this.bottomBorder.className = 'borderHorisontal';
	this.bottomBorder.style.bottom = '0px';
	this.bottomBorder.style.left = '0px';
	this.bottomBorder.style.padding = '0px';
	this.bottomBorder.style.backgroundImage = 'url('+dialogResPath+'bottom_border.jpg)';
	this.domNode.appendChild(this.bottomBorder);
}

Dialog.prototype.getDomNode = function(){
	return this.domNode;
}

Dialog.prototype.createHeader = function(){
	this.header = document.createElement('div');
	this.header.style.width = (this.width - 11) + 'px';
	this.header.style.height = '18px';
	this.header.appendChild(document.createTextNode(this.headerText));
	this.header.style.position = 'absolute';
	this.header.style.top = '2px';
	this.header.style.left = '2px';
	this.header.className = 'dialogHeader';
	var img = document.createElement('img');
	img.src = this.imagePath;
	img.style.position = 'absolute';
	img.style.right = '0px';
	img.style.top = '2px';
	img.style.cursor = 'pointer';
	var dialog = this;
	img.onclick = function(){dialog.close();}
	
	this.header.appendChild(img);
	
	this.contentDiv = document.createElement('div');
	this.contentDiv.style.position = 'absolute';
	this.contentDiv.style.top = '24px';
	this.contentDiv.style.zIndex = '10';
	this.contentDiv.style.left = '3px';
	this.contentDiv.style.width = (this.width - 6) + 'px';
	this.contentDiv.style.height = (this.height - 27) + 'px';
	this.contentDiv.className = 'dialogContent';
	this.contentDiv.style.backgroundColor = '#d4d0c8';
	
	this.domNode.appendChild(this.header);
	this.domNode.appendChild(this.contentDiv);
}

Dialog.prototype.insertContent = function(node){
	this.contentNode = node;
	this.contentDiv.appendChild(this.contentNode);
}

Dialog.prototype.show = function(){
	if(this.parent == null) document.body.appendChild(this.domNode);
	else this.parent.appendChild(this.domNode);
}

Dialog.prototype.close = function(){
	if(typeof this.onClose == 'function'){
	 	this.onClose();
	}else{
    	this.destroy();
    }
}

Dialog.prototype.destroy = function(){
	if(document.getElementById(this.id)) this.domNode.parentNode.removeChild(this.domNode);
	this.domNode = null;
	this.contentDiv = null;
	Dialog.instances[this.id] = null;
}

Dialog.prototype.setDrag = function(){
	var dialog = this;
	this.header.style.cursor = 'move';
	this.header.onmousedown = function(e){
		e = new Event(e).stop();
		var domNode = dialog.getDomNode();
		    
		domNode.addEvent('emptydrop', function(){
			this.removeEvents();
		});
		var drag = domNode.makeDraggable();
		drag.start(e);
	}
}

Dialog.prototype.setSize = function(width,height){
	this.width = parseInt(width);
	this.height = parseInt(height);
	this.header.style.width = (this.width - 11) + 'px';
	this.contentDiv.style.width = (this.width - 8) + 'px';
	this.contentDiv.style.height = (this.height - 28) + 'px';
	
	this.topBorder.style.width = this.width + 'px';
	this.bottomBorder.style.width = this.width + 'px';
	this.leftBorder.style.height = this.height + 'px';
	this.rightBorder.style.height = this.height + 'px';
}

Dialog.prototype.setResizable = function(){
	var dialog = this;
	
	this.rightBorder.style.cursor = 'e-resize';
	this.bottomBorder.style.cursor = 's-resize';
	this.cornerImg = document.createElement('img');
	this.cornerImg.src = dialogResPath+'corner.jpg';
	this.cornerImg.style.position = 'absolute';
	this.cornerImg.style.zIndex = '15';
	this.cornerImg.style.right = '0px';
	this.cornerImg.style.bottom = '0px';
	this.cornerImg.style.cursor = 'se-resize';
	this.domNode.appendChild(this.cornerImg);
	
	this.bottomBorder.onmousedown = function(){
		var domNode = dialog.getDomNode();
		domNode.makeResizable({
			modifiers: {x: false, y: 'height'},
			onDrag: function(){
				dialog.setSize(this.element.style.width,this.element.style.height);
			},
			onComplete: function(){
				this.element.removeEvents();
			}
		});
	}
	
	this.rightBorder.onmousedown = function(){
		var domNode = dialog.getDomNode();
		domNode.makeResizable({
			modifiers: {x: 'width', y: false},
			onDrag: function(){
				dialog.setSize(this.element.style.width,this.element.style.height);
			},
			onComplete: function(){
				this.element.removeEvents();
			}
		});
	}
	
	this.cornerImg.onmousedown = function(){
		var domNode = dialog.getDomNode();
		domNode.makeResizable({
			modifiers: {x: 'width', y: 'height'},
			onDrag: function(){
				dialog.setSize(this.element.style.width,this.element.style.height);
			},
			onComplete: function(){
				this.element.removeEvents();
			}
		});
	}
}

document.write('<style type="text/css"> .borderHorisontal{position: absolute; background-color: #0a246a; height: 3px;} .borderVertical{width: 3px; position: absolute; background-color: #0a246a;} .dialogHeader{background-image: url('+dialogResPath+'header.jpg); font-family:tahoma; font-size: 11px; font-weight: bold; color: WHITE; padding-left: 6px; padding-top: 2px;} .dialogContent{overflow: auto; border: none;}</style>');