// Lewjax library: dialogs
// flexible sexy dialog/popup thingy.
// dependencies: lewjax.util.js

function ljDialog() { // [id, [width, height, [top, left]]]
	if (arguments.length>0)
		if (arguments[0] != null)
			this.id = arguments[0];
	this.width = 100;
	this.height = 100;
	this.top = 10;
	this.left = 10;
	this.bordercolor = 'ffffff';
	this.borderopacity = '100';
	this.bgcolor = '323232';
	this.titleopacity = '90';
	this.bgopacity = '90';
	this.titlebgcolor = '940101';
	this.titlecolor = 'ffffff';
	this.titlebordercolor=this.bgcolor;
	this.titleborderopacity=this.bgopacity;
	this.title = '';
	this.titlebar = null;
	this.basenodelength = 0;
	if (arguments.length >= 3) {
		this.width = arguments[1];
		this.height = arguments[2];
	}
	if (arguments.length >= 5) {
		this.top = arguments[3];
		this.left = arguments[4];
	}
	this.dialog = newElement('div', [['id', this.id],['class','draggable ljDialog']], 'width: '+this.width+'px; height: '+this.height+'px; position: absolute; top: '+this.top+'px; left: '+this.left+'px; overflow: hidden; display: none', null, [['mousedown', associateObjWithEvent(this, 'bringtofront', null)]]);
	if (window['lewlibs_zindexmeter']) {
		++window['lewlibs_zindexmeter'];
	}
	else 
		window['lewlibs_zindexmeter'] = 1;
	this.zindex = window['lewlibs_zindexmeter'];
	this.dialog.style.zIndex = this.zindex;
	this.construction = null
	this.loader=null;
	this.loaderqueue=null;
	document.getElementsByTagName('body').item(0).appendChild(this.dialog);
}

ljDialog.prototype.constructTitleless = function() {
	while (this.dialog.childNodes.length>0) {
		this.dialog.removeChild(this.dialog.childNodes[0]);
	}
	// top left
	this.dialog.appendChild( newElement('div', null, 'position: absolute; width: 4px; height: 4px;', 
		[
		newElement('img', [['src','/lewlibs/core/alphapng.php?o='+this.borderopacity+'&c='+this.bordercolor],['className', 'alphapng']], 'position: absolute; display: block; top: 2px; left: 1px; width: 1px; height: 2px;'),
		newElement('img', [['src','/lewlibs/core/alphapng.php?o='+this.borderopacity+'&c='+this.bordercolor],['className', 'alphapng']], 'position: absolute; display: block; top: 1px; left: 2px; width: 2px; height: 1px;'),
		newElement('img', [['src','/lewlibs/core/alphapng.php?o='+this.bgopacity+'&c='+this.bgcolor],['className', 'alphapng']], 'position: absolute; display: block; top: 2px; left: 2px; width: 2px; height: 2px;')
		]) );

	// top middle
	this.dialog.appendChild( newElement('div', null, 'position: absolute; left: 4px; width: '+(this.width-8)+'px; height: 4px;', 
		[
		newElement('img', [['src','/lewlibs/core/alphapng.php?o='+this.borderopacity+'&c='+this.bordercolor],['className', 'alphapng']], 'position: absolute; display: block; width: '+(this.width-8)+'px; height: 1px;'),
		newElement('img', [['src','/lewlibs/core/alphapng.php?o='+this.bgopacity+'&c='+this.bgcolor],['className', 'alphapng']], 'position: absolute; display: block; top: 1px; width: '+(this.width-8)+'px; height: 3px;')
		]) );

	// top right
	this.dialog.appendChild( newElement('div', null, 'position: absolute; left: '+(this.width-4)+'px; width: 4px; height: 4px;', 
		[
		newElement('img', [['src','/lewlibs/core/alphapng.php?o='+this.borderopacity+'&c='+this.bordercolor],['className', 'alphapng']], 'position: absolute; display: block; top: 1px; width: 2px; height: 1px;'),
		newElement('img', [['src','/lewlibs/core/alphapng.php?o='+this.borderopacity+'&c='+this.bordercolor],['className', 'alphapng']], 'position: absolute; display: block; top: 2px; left: 2px; width: 2px; height: 2px;'),
		newElement('img', [['src','/lewlibs/core/alphapng.php?o='+this.bgopacity+'&c='+this.bgcolor],['className', 'alphapng']], 'position: absolute; display: block; top: 2px; width: 2px; height: 2px;')
		]) );
	
	// mid left
	this.dialog.appendChild( newElement('div', null, 'position: absolute; top: 4px; width: 4px; height: '+(this.height-8)+'px;', 
		[
		newElement('img', [['src','/lewlibs/core/alphapng.php?o='+this.borderopacity+'&c='+this.bordercolor],['className', 'alphapng']], 'position: absolute; display: block; width: 1px; height: '+(this.height-8)+'px;'),
		newElement('img', [['src','/lewlibs/core/alphapng.php?o='+this.bgopacity+'&c='+this.bgcolor],['className', 'alphapng']], 'position: absolute; display: block; left: 1px; width: 3px; height: '+(this.height-8)+'px;')
		]) );
	
	// mid middle
	this.dialog.appendChild( newElement('div', null, 'position: absolute; top: 4px; left: 4px; width: '+(this.width-8)+'px; height: '+(this.height-8)+'px;', 
		[
		newElement('img', [['src','/lewlibs/core/alphapng.php?o='+this.bgopacity+'&c='+this.bgcolor],['className', 'alphapng']], 'position: absolute; display: block; width: '+(this.width-8)+'px; height: '+(this.height-8)+'px;')
		]) );

	// mid right
	this.dialog.appendChild( newElement('div', null, 'position: absolute; left: '+(this.width-4)+'px; top: 4px; width: 4px; height: '+(this.height-8)+'px;', 
		[
		newElement('img', [['src','/lewlibs/core/alphapng.php?o='+this.borderopacity+'&c='+this.bordercolor],['className', 'alphapng']], 'position: absolute; display: block; left: 3px; width: 1px; height: '+(this.height-8)+'px;'),
		newElement('img', [['src','/lewlibs/core/alphapng.php?o='+this.bgopacity+'&c='+this.bgcolor],['className', 'alphapng']], 'position: absolute; display: block; width: 3px; height: '+(this.height-8)+'px;')
		]) );

	// bottom left
	this.dialog.appendChild( newElement('div', null, 'position: absolute; top: '+(this.height-4)+'px; width: 4px; height: 4px;', 
		[
		newElement('img', [['src','/lewlibs/core/alphapng.php?o='+this.borderopacity+'&c='+this.bordercolor],['className', 'alphapng']], 'position: absolute; display: block; left: 1px; width: 1px; height: 2px;'),
		newElement('img', [['src','/lewlibs/core/alphapng.php?o='+this.borderopacity+'&c='+this.bordercolor],['className', 'alphapng']], 'position: absolute; display: block; left: 2px; top: 2px; width: 2px; height: 1px;'),
		newElement('img', [['src','/lewlibs/core/alphapng.php?o='+this.bgopacity+'&c='+this.bgcolor],['className', 'alphapng']], 'position: absolute; display: block; left: 2px; width: 2px; height: 2px;')
		]) );

	// bottom middle
	this.dialog.appendChild( newElement('div', null, 'position: absolute; top: '+(this.height-4)+'px; left: 4px; width: '+(this.width-8)+'px; height: 4px;', 
		[
		newElement('img', [['src','/lewlibs/core/alphapng.php?o='+this.borderopacity+'&c='+this.bordercolor],['className', 'alphapng']], 'position: absolute; display: block; top: 3px; width: '+(this.width-8)+'px; height: 1px;'),
		newElement('img', [['src','/lewlibs/core/alphapng.php?o='+this.bgopacity+'&c='+this.bgcolor],['className', 'alphapng']], 'position: absolute; display: block; width: '+(this.width-8)+'px; height: 3px;')
		]) );

	// bottom right
	this.dialog.appendChild( newElement('div', null, 'position: absolute; top: '+(this.height-4)+'px; left: '+(this.width-4)+'px; width: 4px; height: 4px;', 
		[
		newElement('img', [['src','/lewlibs/core/alphapng.php?o='+this.borderopacity+'&c='+this.bordercolor],['className', 'alphapng']], 'position: absolute; display: block; left: 2px; width: 1px; height: 2px;'),
		newElement('img', [['src','/lewlibs/core/alphapng.php?o='+this.borderopacity+'&c='+this.bordercolor],['className', 'alphapng']], 'position: absolute; display: block; top: 2px; width: 2px; height: 1px;'),
		newElement('img', [['src','/lewlibs/core/alphapng.php?o='+this.bgopacity+'&c='+this.bgcolor],['className', 'alphapng']], 'position: absolute; display: block; width: 2px; height: 2px;')
		]) );
	this.construction = 'titleless';
	this.basenodelength=this.dialog.childNodes.length;
}

ljDialog.prototype.construct = function() {
	while (this.dialog.childNodes.length>0) {
		this.dialog.removeChild(this.dialog.childNodes[0]);
	}
	//top
	
	var el = document.createElement('img');
	el.style.display='block';
	el.className='alphapng';
	el.style.position='absolute';
	el.style.left='4px';
	el.style.top='1px';
	el.style.width=(this.width-8)+'px';
	el.style.height='3px';
	el.src='/lewlibs/core/alphapng.php?o='+this.titleopacity+'&c='+this.titlebgcolor;
	this.dialog.appendChild(el);
	el = el.cloneNode(true);
	el.style.left='1px';
	el.style.top='4px';
	el.style.width=(this.width-2)+'px';
	el.style.height='10px';
	this.dialog.appendChild(el);
	el = el.cloneNode(true);
	el.style.left='1px';
	el.style.top='14px';
	el.src='/lewlibs/core/alphapng.php?o='+this.titleborderopacity+'&c='+this.titlebordercolor;
	el.style.height='1px';
	el.style.width=(this.width-2)+'px';
	this.dialog.appendChild(el);
	el = document.createElement('div');
	el.style.display='block';
	el.style.position='absolute';
	el.style.backgroundColor='#'+this.bordercolor;
	el.style.width=(this.width-8)+'px';
	el.style.height='1px';
	el.style.top='0px';
	el.style.left='4px';
	el.style.overflow='hidden';
	this.dialog.appendChild(el);
	el = el.cloneNode(true);
	el.style.left='9px';
	el.style.top='1px';
	el.style.width=(this.width-20)+'px';
	el.style.height='13px';
	el.style.fontFamily='Verdana, Arial, Helvetica, sans-serif';
	el.style.backgroundColor='transparent';
	el.style.fontSize='10px';
	el.style.lineHeight='13px';
	var title = document.createElement('span');
	title.appendChild(document.createTextNode(this.title));
	title.style.cursor='default';
	el.appendChild(title);
	this.dialog.appendChild(el);
	
	//top left
	el = el.cloneNode(false);
	el.style.left='2px';
	el.style.top='1px';
	el.style.width='2px';
	el.style.height='1px';
	el.style.backgroundColor='#'+this.bordercolor;
	this.dialog.appendChild(el);
	el = el.cloneNode(true);
	el.style.left='1px';
	el.style.top='2px';
	el.style.width='1px';
	el.style.height='2px';
	this.dialog.appendChild(el);
	el = el.cloneNode(true);
	el.style.left='0px';
	el.style.top='4px';
	el.style.width='1px';
	el.style.height=(this.height-8)+'px';
	this.dialog.appendChild(el);
	el = document.createElement('img');
	el.style.display='block';
	el.style.position='absolute';
	el.className='alphapng';
	el.style.left='2px';
	el.style.top='2px';
	el.style.width='2px';
	el.style.height='2px';
	el.src='/lewlibs/core/alphapng.php?o='+this.titleopacity+'&c='+this.titlebgcolor;
	this.dialog.appendChild(el);
	
	// top right
	el = el.cloneNode(true);
	el.style.left=(this.width-4)+'px';
	el.style.top='2px';
	el.style.width='2px';
	el.style.height='2px';
	this.dialog.appendChild(el);
	el=document.createElement('div');
	el.style.display='block';
	el.style.position='absolute';
	el.style.left=(this.width-4)+'px';
	el.style.top='1px';
	el.style.width='2px';
	el.style.height='1px';
	el.style.backgroundColor='#'+this.bordercolor;
	el.style.overflow='hidden';
	this.dialog.appendChild(el);
	el = el.cloneNode(true);
	el.style.left=(this.width-2)+'px';
	el.style.top='2px';
	el.style.width='1px';
	el.style.height='2px';
	this.dialog.appendChild(el);
	el = el.cloneNode(true);
	el.style.left=(this.width-1)+'px';
	el.style.top='4px';
	el.style.height=(this.height-8)+'px';
	this.dialog.appendChild(el);
	
	//middle
	el = document.createElement('img');
	el.style.display='block';
	el.style.position='absolute';
	el.className='alphapng';
	el.style.left='1px';
	el.style.top='15px';
	el.style.width=(this.width-2)+'px';
	el.style.height=(this.height-19)+'px';
	el.src='/lewlibs/core/alphapng.php?o='+this.bgopacity+'&c='+this.bgcolor;
	this.dialog.appendChild(el);
	
	//bottom
	el=el.cloneNode(true);
	el.style.left='4px';
	el.style.top=(this.height-4)+'px';
	el.style.width=(this.width-8)+'px';
	el.style.height='3px';
	this.dialog.appendChild(el);
	el = document.createElement('div');
	el.style.display='block';
	el.style.position='absolute';
	el.style.left='4px';
	el.style.top=(this.height-1)+'px';
	el.style.width=(this.width-8)+'px';
	el.style.height='1px';
	el.style.backgroundColor='#'+this.bordercolor;
	el.style.overflow='hidden';
	this.dialog.appendChild(el);
	
	// bottom left
	el=el.cloneNode(true);
	el.style.left='1px';
	el.style.top=(this.height-4)+'px';
	el.style.width='1px';
	el.style.height='2px';
	this.dialog.appendChild(el);
	el=el.cloneNode(true);
	el.style.left='2px';
	el.style.top=(this.height-2)+'px';
	el.style.width='2px';
	el.style.height='1px';
	this.dialog.appendChild(el);
	el = document.createElement('img');
	el.style.display='block';
	el.style.position='absolute';
	el.style.className='alphapng';
	el.style.left='2px';
	el.style.top=(this.height-4)+'px';
	el.style.width='2px';
	el.style.height='2px';
	el.src='/lewlibs/core/alphapng.php?o='+this.bgopacity+'&c='+this.bgcolor;
	this.dialog.appendChild(el);
	
	// bottom right
	el=el.cloneNode(true);
	el.style.left=(this.width-4)+'px';
	this.dialog.appendChild(el);
	el=document.createElement('div');
	el.style.display='block';
	el.style.position='absolute';
	el.style.left=(this.width-4)+'px';
	el.style.top=(this.height-2)+'px';
	el.style.width='2px';
	el.style.height='1px';
	el.style.overflow='hidden';
	el.style.backgroundColor='#'+this.bordercolor;
	this.dialog.appendChild(el);
	el=el.cloneNode(true);
	el.style.left=(this.width-2)+'px';
	el.style.top=(this.height-4)+'px';
	el.style.width='1px';
	el.style.height='2px';
	this.dialog.appendChild(el);
	this.basenodelength=this.dialog.childNodes.length;
}

ljDialog.prototype.constructGraphical = function(bg) {
	while (this.dialog.childNodes.length>0) {
		this.dialog.removeChild(this.dialog.childNodes[0]);
	}
	//top
	var el = document.createElement('img');
	el.style.display='block';
	el.className='alphapng';
	el.style.position='absolute';
	el.style.left='0px';
	el.style.top='0px';
	el.style.width=this.width+'px';
	el.style.height=this.height+'px';
	el.src=bg;
	this.dialog.appendChild(el);
	el = document.createElement('div');
	el.style.display='block';
	el.style.position='absolute';
	el.style.left='9px';
	el.style.top='1px';
	el.style.width=(this.width-20)+'px';
	el.style.height='13px';
	el.style.fontFamily='Verdana, Arial, Helvetica, sans-serif';
	el.style.backgroundColor='transparent';
	el.style.fontSize='10px';
	el.style.lineHeight='13px';
	var titlebar = document.createElement('div');
	titlebar.innerHTML = this.title;
	titlebar.style.cursor='default';
	el.appendChild(titlebar);
	this.titlebar = titlebar;
	this.dialog.appendChild(el);
	this.basenodelength=this.dialog.childNodes.length;
}

ljDialog.prototype.setTitle = function (t) {
	this.title = t;
	if (this.titlebar!=null) {
		this.titlebar.innerHTML = t;
		if (arguments.length>=2) {
			this.titlebar.style.font=arguments[1];
		}
		if (arguments.length >=3) {
			this.titlebar.style.height=arguments[2];
			this.titlebar.style.lineHeight=arguments[2];
		}
	}
}

ljDialog.prototype.setWidth = function (w) {
	this.width = w;
	this.dialog.style.width = w+'px';
}

ljDialog.prototype.setHeight = function (h) {
	this.height = h;
	this.dialog.style.height = h+'px';
}

ljDialog.prototype.setDimensions = function () {
	if (arguments.length==2) {
		var w=arguments[0];
		var h = arguments[1];
	}
	if (arguments.length ==1){
		var w=arguments[0][0];
		var h=arguments[0][1];
	}
	this.width = w;
	this.dialog.style.width = w+'px';
	this.height = h;
	this.dialog.style.height = h+'px';
	if (this.construction == 'titleless') {
		// top middle
		this.dialog.childNodes[1].style.width=(w-8)+'px';
		this.dialog.childNodes[1].childNodes[0].style.width=(w-8)+'px';
		this.dialog.childNodes[1].childNodes[1].style.width=(w-8)+'px';
		//top right
		this.dialog.childNodes[2].style.left=(w-4)+'px';
		//mid left
		this.dialog.childNodes[3].style.height=(h-8)+'px';
		this.dialog.childNodes[3].childNodes[0].style.height=(h-8)+'px';
		this.dialog.childNodes[3].childNodes[1].style.height=(h-8)+'px';
		//mid middle
		this.dialog.childNodes[4].style.width=(w-8)+'px';
		this.dialog.childNodes[4].style.height=(h-8)+'px';
		this.dialog.childNodes[4].childNodes[0].style.width=(w-8)+'px';
		this.dialog.childNodes[4].childNodes[0].style.height=(h-8)+'px';
		//mid right
		this.dialog.childNodes[5].style.left=(w-4)+'px';
		this.dialog.childNodes[5].style.height=(h-8)+'px';
		this.dialog.childNodes[5].childNodes[0].style.height=(h-8)+'px';
		this.dialog.childNodes[5].childNodes[1].style.height=(h-8)+'px';
		//bottom left
		this.dialog.childNodes[6].style.top=(h-4)+'px';
		//bottom middle
		this.dialog.childNodes[7].style.width=(w-8)+'px';
		this.dialog.childNodes[7].style.top=(h-4)+'px';
		this.dialog.childNodes[7].childNodes[0].style.width=(w-8)+'px';
		this.dialog.childNodes[7].childNodes[1].style.width=(w-8)+'px';
		//bottom right
		this.dialog.childNodes[8].style.left=(w-4)+'px';
		this.dialog.childNodes[8].style.top=(h-4)+'px';
	}
}

ljDialog.prototype.setTop = function (t) {
	this.top = t;
	this.dialog.style.top = t+'px';
}

ljDialog.prototype.setLeft = function (l) {
	this.left = l;
	this.dialog.style.left = l+'px';
}

ljDialog.prototype.setPosition = function (t,l) {
	this.top = t;
	this.dialog.style.top = t+'px';
	this.left = l;
	this.dialog.style.left = l+'px';
}


ljDialog.prototype.show = function () { // [top, left]
	if (arguments.length ==2)
		this.setPosition(arguments[0], arguments[1]);
	this.dialog.style.display = 'block';
	this.bringtofront();
}

ljDialog.prototype.hide = function () {
	this.dialog.style.display = 'none';
}

ljDialog.prototype.clear = function () {
	while(this.dialog.childNodes.length >this.basenodelength) {
		this.dialog.removeChild(this.dialog.childNodes[this.basenodelength]);
	}
}

ljDialog.prototype.destroy = function () {
	this.clear();
	this.dialog.parentNode.removeChild(this.dialog);
}

ljDialog.prototype.bringtofront = function () {
	this.zindex = ++window['lewlibs_zindexmeter'];
	this.dialog.style.zIndex = this.zindex;
}