/**
 * @author MIVEC
 * @classDescription  js dialog
 */

var MPanel = function(cfg) {
	this.cfg = cfg;
	this.panel = {};
	this.colpsed = false;
	this.body = $(document.body);
	
	this.init();
	//this.setTitle();
}

MPanel.prototype.init = function() {
	var objstyle = {};
		objstyle.w = this.body.clientWidth;
		objstyle.h = this.body.clientHeight;
	
		objstyle.left = parseInt(objstyle.w/3);
		objstyle.top = parseInt(objstyle.h/3) ;
		
	var pos = {};
		pos.top = typeof(this.cfg.top) != 'undefined' ? this.cfg.top : objstyle.top;
		pos.left = typeof(this.cfg.top) != 'undefined' ? this.cfg.left : objstyle.left;
		
	this.panel.Container = $('<div></div>')
		.attr({'id':'dialog'})
		.css({
			'width'  : this.cfg.width,
			'height' : 'auto',
			'top'	 : pos.top,
			'left'	 : pos.left,
			'position'	: 'absolute',
			'border'	: '1px solid #ccc',
			'background' : '#fff',
			'z-index'	: '100'
		});
	
	this.setTitle();
	this.setContent();
	this.setBottom();
	this.panel.Container.appendTo(this.body);
}

MPanel.prototype.setTitle = function() {
	this.panel.title = $('<div></div>',{'id':'title'})
		.attr("unselectable","on")
		.css({
			"-moz-user-select":"none",
			'weight' : 'hold',
			"height":"25px",
			"line-height":"25px",
			//"background-color":"#F5F5F8",
			"background-image":"",
			"border-bottom":"1px solid #eee",
			"cursor":"move",
			"overflow":"visible",
			"clear":"both",
			"padding-left" : "20px",
			'color':'#ff0000',
			'font-weight' : 'bold'
		})
		.html(this.cfg.title);
	this.panel.title.appendTo(this.panel.Container);
}

MPanel.prototype.setContent = function() {
	this.panel.content = $('<div></div>',{'id':'content'})
		.css({
			"height" : "85%",
			"width"  : "96%",
			"background-color" : "#fff",
			"padding" : "5px",
			"overflow":"hidden"
		});
	
	typeof(this.cfg.content) == 'object' ? this.cfg.content.appendTo(this.panel.content) : this.panel.content.html(this.cfg.content);
	
	this.panel.content.appendTo(this.panel.Container);
}

MPanel.prototype.setBottom = function() {
	this.panel.bottom = $('<div></div>',{'id':'bottom'})
		.css({
			"width" : "auto",
			"margin" : "10px",
			'padding' : '5px',
			"text-align" : "center",
			"border-top" : "1px solid #ccc"
		});
		
		
		var button = $('<a></a>')
				.css({
					'background': '#fff',
					//'border' : '1px solid #e8e8e8',
					'width'	: '30',
					'height'	: '30'
				})
				.html('close');
		
		button.click(function() {
			document.body.removeChild(document.getElementById('dialog'));
			//document.getElementById('dialog').style.display = 'none';
		});
		
	button.appendTo(this.panel.bottom);
	
	this.panel.bottom.appendTo(this.panel.Container);
}

function getMousePos(ev) {
	if(ev.pageX || ev.pageY){
	    return {x:ev.pageX, y:ev.pageY};
	}
	return {
	    x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
	    y:ev.clientY + document.body.scrollTop - document.body.clientTop
	  };
}

function getAbsoluteLeft( ob ){    
	if(!ob){return null;}    
		var obj = ob;    
		var objLeft = obj .offsetLeft;    
		while( obj != null && obj .offsetParent != null && obj .offsetParent.tagName != "BODY" ){    
		objLeft += obj .offsetParent.offsetLeft;    
		obj = obj .offsetParent;    
	}    
	return objLeft ;
}
   
function getAbsoluteTop( ob ){    
	if(!ob){return null;}    
		var obj = ob;
		var objTop = obj .offsetTop;    
		while( obj != null && obj .offsetParent != null && obj .offsetParent.tagName != "BODY" ){    
		objTop += obj .offsetParent.offsetTop;    
		obj = obj .offsetParent;    
	}    
	return objTop ;    
}   
