var IE = (document.all);
var NS = (document.layers);
var NS_new = (!IE && !document.layers && (document.getElementById))?true:false;
var layers_brain = new Array();
var layers_brain_idx = -1;

function getObj(id)
{
//  alert('getObj id='+id);
  if((id == null) || id=='null' || id=='' || id ==-1) return null;
  if (document.getElementById)
  {
	this.obj = document.getElementById(id);
	if (this.obj !=null){
		this.style = document.getElementById(id).style;
	}
  }
  else if (document.all)
  {
	this.obj = document.all[id];
	if (this.obj !=null){
		this.style = document.all[id].style;
	}
  }
  else if (document.layers)
  {
	this.obj = document.layers[id];
	if (this.obj !=null){
		this.style = document.layers[id];
	}
  }else{
	  // this = null;
  }
}
function setCSSDisplay(id,value){
//    alert('id='+id);
    var element = new getObj(id);
	element.obj.style.display=value;
}
function swapTRvisibility(id){
	var element = new getObj(id);
	var disp = element.obj.style.display;
	//alert(id+' = '+disp);
	if(disp==''){
		element.obj.style.display='none';
	}else{
		element.obj.style.display='';
	}
}
function saveID(id){
	if (typeof(window.idx_id)=='undefined'){
		window.idx_id = -1;
		window.container = new Array();
	}
	window.idx_id++;
	window.container[window.idx_id] = id;
}
function collapseTRall(){
	for(var i=0;i<=window.idx_id;i++){
		setCSSDisplay(window.container[i],'none');
	}
}
function openTRall(){
	for(var i=0;i<=window.idx_id;i++){
		setCSSDisplay(window.container[i],'');
	}
}

function start_layer_html(id,parent_id,visibility){
	var html = '';
	var comment = '';
	if (typeof(visibility)=='string'){
			eval('visibility = '+visibility+';');
	}
	if (visibility){
		var str_position = 'static';
	}else{
		var str_position = 'absolute';
	}
	if(IE || NS_new){
		if (visibility){
			var str_visibility = 'visible';
		}else{
			var str_visibility = 'hidden';
		}
	}
	if(NS){
		if (visibility){
			var str_visibility = 'show';
		}else{
			var str_visibility = 'hide';
		}
	}
	if (IE || NS_new){
		html = '<div id="'+id+'" style="position: '+str_position+'; visibility : '+str_visibility+';">\r\n';
	}
	if(NS){
		html = '<layer id="'+id+'" style="position: '+str_position+'; visibility : '+str_visibility+';">\r\n';
	}
	return html;
}
function start_layer(id,parent_id,visibility){
	var html = start_layer_html(id,parent_id,visibility);
	var comment = '';
	comment = '<!--start_layer(\''+id+'\',\''+parent_id+'\','+(visibility?'true':'false')+')-->';
//	document.write(html);
	document.write(comment);
}
function end_layer_html(){
	var html = '';
	if(IE || NS_new){
		html = '</div>\r\n';
	}
	if(NS){
		html = '</layer>\r\n';
	}
	return html;
}
function end_layer(){
	var html = end_layer_html();
	comment = '<!--end_layer()-->';
//	document.write(html);
	document.write(comment);
}
function isVisible(id){
    if((id == null) || id=='null' || id=='' || id ==-1) return false;
	obj = new getObj(id);
//	alert(obj);
	if 	(
		 	(obj) && 
			(
				(obj.style.visibility =='hidden') ||
				(obj.style.visibility =='hide') 
		  	)
		){
			return false;
	}else if 	(
					(obj) && 
					(
						(obj.style.visibility =='visible') ||
						(obj.style.visibility =='show') 
					)
				){
			return true;
	}else{
		alert('error : function isVisible(id) : špatné id="'+id+'"');
	}
}
function swap_layer_visibility(id){
    if((id == null) || id=='null' || id=='' || id ==-1) return;
	var re = new RegExp ('layer_(.+)$','');
//	alert('id='+id);
	re.exec(id);
	editor_id = RegExp.$1;
	eval('type=typeof(editor_'+editor_id+');');
	if ((type!='undefined')&&(isVisible(id))){
			eval('editor = editor_'+editor_id+';');
			editor.setMode('textmode');
	}
	
	obj = new getObj(id);
	var str = '';
	str += 'visibility = '+obj.style.visibility+'\r\n';
	str += 'IE = '+IE+'\r\n';
	str += 'NS = '+NS+'\r\n';
	str += 'NS_new = '+NS_new+'\r\n';
//	alert(str);
	if(IE || NS_new){
		if (obj.style.visibility =='hidden'){
			obj.style.position = 'static';
			obj.style.visibility = 'visible';
			for(i=0;i<layers_brain.length;i++) if (layers_brain[i].parent != 'null'){
				if( (layers_brain[i].id == id) && !isVisible(layers_brain[i].parent) ){
					swap_layer_visibility(layers_brain[i].parent);
				}
			}
		}else{
			obj.style.visibility = 'hidden';
			obj.style.position = 'absolute';
			for(i=0;i<layers_brain.length;i++){
				if( (layers_brain[i].parent == id) && isVisible(layers_brain[i].id) ){
					swap_layer_visibility(layers_brain[i].id);
				}
			}
		}
	}
	if(NS){
		if (obj.style.visibility =='hide'){
			obj.style.visibility = 'show';
		}else{
			obj.style.visibility = 'hide';
		}
	}
	if ((type!='undefined')&&(isVisible(id))){
			eval('editor = editor_'+editor_id+';');
			editor.setMode('wysiwyg');
	}
	
}
function replace_layer_comments(str){
//	str =' ksdfjhsdkjh sdklh  <!--start_layer(\'layer_01\',\'layer_00\')--> asdfkasd as as ashf\r\n';
//	str+=' ksdfjhsdkjh sdklh  <!--start_layer(\'layer_020\',\'layer_000\')--> asdfkasd as as ashf\r\n';
	// ******************************************************************************************************
	var idx;
	re = new RegExp ('<\\!--start_layer\\(\'([^\']+)\',\'([^\']+)\',(true|false)\\)-->', 'i');
	loop=true;
	while (loop) {
		tmp = str.search(re);
		loop = (tmp != -1)?true:false;
		if (loop){
			re.exec(str);
//			alert(start_layer_html(RegExp.$1,RegExp.$2,RegExp.$3));
			str=str.replace(re,start_layer_html(RegExp.$1,RegExp.$2,RegExp.$3));
			idx = ++layers_brain_idx;
			layers_brain[idx]= new Object();
			layers_brain[idx].id = RegExp.$1;
			layers_brain[idx].parent = RegExp.$2;
		}
	}
	re = new RegExp ('<script language="javascript">start_layer\\([^\\)]\\);<\\/script>', 'g');
	str=str.replace(re,'');
	// ******************************************************************************************************
	elh = end_layer_html();
	re = new RegExp ('<\\!--end_layer\\(\\)-->', 'g');
	loop=true;
	while (loop) {
		tmp = str.search(re);
		loop = (tmp != -1)?true:false;
		if (loop){
			re.exec(str);
			str=str.replace(re,elh);
		}
	}
//	re = new RegExp ('<script language="javascript">end_layer[ \r]*\\([ \t]*\\);[ \t]*<\\/script>', 'g');
//	re = new RegExp ('<script language=\\"javascript\\">end_layer\\(\\);<\\/script>', 'g');
	re = new RegExp ('<script[^\\/]*\\/script>', 'g');

	str=str.replace(re,'');
	// ******************************************************************************************************
//	alert(str);
	return str;
}




function set_visible(id, value){
	obj = new getObj(id);

	if(IE || NS_new){
		
		if (value == 0){
			obj.style.display = 'none';
		}else{
			obj.style.display = 'block';
		}
	}
	if(NS){
		if (value == 0){
			obj.style.display = 'none';
		}else{
			obj.style.display = 'block';
		}
	}
	
}

function toggle_visible(id){
	obj = new getObj(id);

	if(IE || NS_new){
		if (obj.style.display == 'none'){
			obj.style.display = 'block';
		}else{
			obj.style.display = 'none';
		}
	}
	if(NS){
		if (obj.style.display == 'none'){
			obj.style.display = 'block';
		}else{
			obj.style.display = 'none';
		}
	}
	
}

function DivSetVisible(idd,idifr,state)
{
		   var DivRef = document.getElementById(idd);
		   var IfrRef = document.getElementById(idifr);
		   if(state)
		   {
		    DivRef.style.display = "block";
		    IfrRef.style.width = DivRef.offsetWidth;
		    IfrRef.style.height = DivRef.offsetHeight;
		    IfrRef.style.top = DivRef.style.top;
		    IfrRef.style.left = DivRef.style.left;
		    IfrRef.style.zIndex = DivRef.style.zIndex - 1;
		    IfrRef.style.display = "block";
		   }
		   else
		   {
		    DivRef.style.display = "none";
		    IfrRef.style.display = "none";
		   }
}













