// create.js - Create layers on the fly

// startup flag
var gMod_Create = true;

// prerequisites
var gMod_Detect;
if (!gMod_Detect){alert("You must load detect.js before write.js");}

/////////////////////////////////////////////////////////////////////////////////////////////

function css(id,left,top,width,height,color,vis,z,other) {
	if (id=="START"){return '<STYLE TYPE="text/css">\n';}
	if (id=="END"){return '</STYLE>';}

	var str = (left!=null && top!=null)? '#'+id+' {position:absolute; left:'+left+'px; top:'+top+'px;' : '#'+id+' {position:relative;';
	if (arguments.length>=4 && width!=null){str += ' width:'+width+'px;';}
	if (arguments.length>=5 && height!=null){
		str += ' height:'+height+'px;';
		if (arguments.length<9 || other.indexOf('clip')==-1) str += ' clip:rect(0px '+width+'px '+height+'px 0px);';
	}
	if (arguments.length>=6 && color!=null){str += (is.ns)? ' layer-background-color:'+color+';' : ' background-color:'+color+';';}
	if (arguments.length>=7 && vis!=null){str += ' visibility:'+vis+';';}
	if (arguments.length>=8 && z!=null){str += ' z-index:'+z+';';}
	if (arguments.length==9 && other!=null){str += ' '+other;}
	str += '}\n';
	return str;
}

function writeCSS(str,showAlert){
	str = css('START')+str+css('END');
	document.write(str);
	if (showAlert){alert(str);}
}

function createLayer(id,nestref,left,top,width,height,content,bgColor,visibility,zIndex) {
	if (is.ns5){
		var div = document.createElement("div");
		div.id = id;
		div.style.position = "absolute";
		div.style.left = left + "px"; 
		div.style.top = top + "px"
		if (width != null){div.style.width = width + "px";}
		if (height != null){div.style.height = height + "px";}
		if ((width != null) && (height != null)){div.style.clip = "rect(0px " + width + "px " + height + "px 0px)";}
		if (bgColor != null){div.style.backgroundColor = bgColor;}
		div.style.padding = "0px 0px 0px 0px";
		if (zIndex != null){div.style.zIndex = zIndex;}
		if (visibility != null){div.style.visibility = visibility;}

		div.innerHTML = content;

		var parentEl = document.body;
		if (nestref){parentEl = document.getElementById(nestref);}
		parentEl.appendChild(div);

	}else if (is.ns){
		if (nestref){
			var lyr = eval("document."+nestref+".document."+id+" = new Layer(width, document."+nestref+")");
		}else{
			var lyr = document.layers[id] = new Layer(width);
			eval("document."+id+" = lyr");
		}
		lyr.name = id;
		lyr.id = id;
		lyr.left = left;
		lyr.top = top;
		if (height!=null){lyr.clip.height = height;}
		if (bgColor!=null){lyr.bgColor = bgColor;}
		lyr.visibility = (visibility=='hidden')? 'hide' : 'show';
		if (zIndex!=null){lyr.zIndex = zIndex;}
		if (content){
			lyr.document.open();
			lyr.document.write(content);
			lyr.document.close();
		}
	}else if ((is.ie4) && (is.mac)){
		
		///////////////////////////////////////////////////////////////////////

		var str = '<DIV id="'+id+'" style="position:absolute; left:'+left+'; top:'+top;
		if (width != null){str += '; width:'+width;}
		if (height != null){str += '; height:'+height;}
		if ((width != null) && (height != null)){str += '; clip:rect(0,'+width+','+height+',0)';}
		if (bgColor!=null){str += '; background-color:'+bgColor;}
		if (zIndex!=null){str += '; z-index:'+zIndex;}
		if (visibility){str += '; visibility:'+visibility;}
		str += ';"></DIV>';

		if (nestref){
			index = nestref.lastIndexOf(".");
			var nestlyr = (index != -1)? nestref.substr(index+1) : nestref;
			nestlyr = document.all[nestlyr];
			nestlyr.insertAdjacentHTML("BeforeEnd",str);
		}else{
			document.body.insertAdjacentHTML("BeforeEnd",str);
		}
		var newobj = eval("document.all."+id);
		if (content){
			newobj.innerHTML = newobj.innerHTML + content;
		}

		///////////////////////////////////////////////////////////////////////

	}else if ((is.ie5) && (is.mac)){

		///////////////////////////////////////////////////////////////////////

		var div = document.createElement("div");
		div.id = id;
		div.style.position = "absolute";
		div.style.left = left + "px"; 
		div.style.top = top + "px"
		if (width != null){div.style.width = width + "px";}
		if (height != null){div.style.height = height + "px";}
		if ((width != null) && (height != null)){div.style.clip = "rect(0px " + width + "px " + height + "px 0px)";}
		if (bgColor != null){div.style.backgroundColor = bgColor;}
		div.style.padding = "0px 0px 0px 0px";
		if (zIndex != null){div.style.zIndex = zIndex;}
		if (visibility != null){div.style.visibility = visibility;}

		div.innerHTML = content;

		var parentEl = document.body;
		if (nestref){parentEl = document.getElementById(nestref);}
		parentEl.appendChild(div);

		///////////////////////////////////////////////////////////////////////

	}else if (is.ie){
		var str = '<DIV id="'+id+'" style="position:absolute; left:'+left+'; top:'+top;
		if (width != null){str += '; width:'+width;}
		if (height != null){str += '; height:'+height;}
		if ((width != null) && (height != null)){str += '; clip:rect(0,'+width+','+height+',0)';}
		if (bgColor!=null){str += '; background-color:'+bgColor;}
		if (zIndex!=null){str += '; z-index:'+zIndex;}
		if (visibility){str += '; visibility:'+visibility;}
		str += ';">'+((content)?content:'')+'</DIV>';
		if (nestref){
			index = nestref.lastIndexOf(".");
			var nestlyr = (index != -1)? nestref.substr(index+1) : nestref;
			document.all[nestlyr].insertAdjacentHTML("BeforeEnd",str);
		}else{
			document.body.insertAdjacentHTML("BeforeEnd",str);
		}
	}
}

function destroyLayer(id,nestref) {
	if (is.ns5){
		var elm = document.getElementById(id);
		elm.innerHTML = "";
		elm.outerHTML = "";
	}else if (is.ns){
		if (nestref){
			eval("document."+nestref+".document."+id+".visibility = 'hide'");
		}else{
			document.layers[id].visibility = "hide";
		}
	}else if ((is.ie5) && (is.mac)){
		var node = document.getElementById(id);
		node.parentNode.removeChild(node);
		delete node;
	}else if (is.ie){
		document.all[id].innerHTML = "";
		document.all[id].outerHTML = "";
	}
}

/////////////////////////////////////////////////////////////////////////////////////////////



