var Gl_iebuffer=null;
var Gl_loadwait=null;
var Gl_movedata=null;
var Gl_loadtimer=0;
var Gl_loadattempts=0;

function G_Layer(Xstart,Ystart,W,H,NL_html)
{
	this.on=true;
	this.mouse=true;
	this.x=Xstart;
	this.y=Ystart;
	this.z=G_Layer_index;
	this.xmax=1000;
	this.xmin=0;
	this.ymax=1000;
	this.ymin=0;
	this.URL="";
	this.lock=null;
	this.lockxoff=0;
	this.lockyoff=0;
	this.innerHTML=NL_html;
	this.draggable=false;
	this.dragnormal=true;
	this.dragvert=false;
	this.draghoriz=false;
	this.onmouseover="";
	this.onmouseout="";
	this.onclickup="";
	this.onclickdown="";
	this.opacity=100;
	this.clipleft=0;
	this.clipright=0;
	this.cliptop=0;
	this.clipbottom=0;
	this.clipped=false;
	this.following=null;
	this.followingx=0;
	this.followingy=0;
	this.followingox=-1000;
	this.followingoy=-1000;
	this.followedby=new Array();
	this.alive=true;
	this.index=G_Layer_index;
	if(isIE4)
	{
		if(G_firstlayer)
		{
			document.body.insertAdjacentHTML("BeforeEnd",'<iframe id="buffer" border=0 frameborder=no width=1 height=1 scrolling=no src="" style="position:absolute; left:0; top: -20; visibility:hidden"></iframe>');
			Gl_iebuffer=document.frames["buffer"];
			G_firstlayer=false
		}
		document.body.insertAdjacentHTML("BeforeEnd",'<div id="Adiv'+G_Layer_index+'" style="position: absolute; left:'+Xstart+'; top:'+Ystart+'; width:'+W+'; height: '+H+'; visibility: visible">'+NL_html+'</div>');
		this.ob=document.all["Adiv"+G_Layer_index].style;
		this.raw=document.all["Adiv"+G_Layer_index];
		this.ob.overflow='hidden';
		this.setBgcolor=G_setbgcolorIE;
		this.setBackground=G_setbackgroundIE;
		this.resize=G_layerresizeIE;
		this.width=this.raw.clientWidth;
		this.height=this.raw.clientHeight;
		this.setOpacity=setopacity;
		
		this.write=G_layerwriteIE;
		this.append=G_layerappendIE;
		this.load=G_loadIE;
		this.clip=G_obclipIE;
	}
	else if (isNav4)
	{
		this.ob=document.layers[G_Layer_index]=new Layer(W);
		document["Adiv"+G_Layer_index]=this.ob;
		this.ob.name=G_Layer_index;
		this.ob.height=0;
		this.ob.left=Xstart;
		this.ob.top=Ystart;
		this.ob.zIndex=G_Layer_index;
		this.ob.visibility="show";
		this.raw=this.ob;
		this.ob.document.open();
		this.ob.document.write(NL_html);
		this.ob.document.close();
		this.ob.width=this.ob.clip.width;
		this.ob.height=this.ob.clip.height;
		this.width=this.ob.width;
		this.height=this.ob.height;
		this.setOpacity=f_null;
		this.setBgcolor=G_setbgcolorNS;
		this.setBackground=G_setbackgroundNS;
		this.resize=G_layerresizeNS;
		this.resize(W,H);

		this.write=G_layerwriteNS;
		this.append=G_layerappendNS;
		this.load=G_loadNS;
		this.clip=G_obclipNS;
	}
	else if (isNav6)
	{
		if(G_firstlayer)
		{
			buffer = document.createElement('IFRAME');
			buffer.id = "buffer";
			buffer.border = 0;
			buffer.frameborder = "no";
			buffer.width = 1;
			buffer.height = 1;
			buffer.scrolling ="no";
			buffer.src = "";
			buffer.style.position = "absolute";
			buffer.style.left = 0;
			buffer.style.top = -20;
			buffer.style.visibility = "hidden";
			document.documentElement.appendChild(buffer);
			Gl_iebuffer=document.getElementById("buffer");
			G_firstlayer=false;
		}
		this.raw = document.createElement('DIV');
		this.raw.id = "Adiv"+G_Layer_index;
		this.raw.style.position = "absolute";
		this.raw.style.left = Xstart;
		this.raw.style.top = Ystart;
		this.raw.style.width = W;
		this.raw.style.height = H;
		this.raw.style.visibility = "visible";
		this.raw.innerHTML = NL_html;
		document.documentElement.appendChild(this.raw);
		this.raw = document.getElementById(this.raw.id);
		this.write=G_layerwriteIE;
		this.append=G_layerappendIE;
		this.load=G_loadIE;
		this.ob=this.raw.style;
		this.ob.overflow='hidden';
		this.setBgcolor=G_setbgcolorIE;
		this.setBackground=G_setbackgroundIE;
		this.resize=G_layerresizeIE;
		this.width=W;
		this.height=H;
		this.raw.innerHTML=NL_html;
		this.setOpacity=f_null;
		this.write=G_layerwriteIE;
		this.append=G_layerappendIE;
		this.load=G_loadN6;
		this.clip=G_obclipIE;
		this.resize(W,H);
	}
	else Alert ('Neznámý Prohlížeč!');
	
	this.moveTo=moveTo;
	this.moveBy=moveBy;
	this.lockLayer=G_locklayer;
	this.unlockLayer=G_unlocklayer;
	this.dragType=G_dragtype;
	this.setXlimits=G_setxlimits;
	this.setYlimits=G_setylimits;
	this.setZ=setz;
	this.hide=hide;
	this.show=show;
	this.follow=G_layerfollow;
	this.stopFollowing=G_stoplayerfollow;
	this.G_index=G_Layer_index;
	G_Layers[G_Layer_index]=this;
	G_Layer_index++;
	this.moveTo(Xstart,Ystart);
	return this;
}

function G_setbackgroundNS(Gl_i)
{
	this.ob.background.src=Gl_i
}

function G_setbackgroundIE(Gl_i)
{
	this.ob.backgroundImage="url("+Gl_i+")"
}

function G_setbgcolorIE(Gl_a)
{
	this.ob.backgroundColor=Gl_a
}

function G_setbgcolorNS(Gl_a)
{
	this.ob.bgColor=Gl_a
}

function G_layerresizeIE(Gl_w,Gl_h)
{
	this.ob.width=Gl_w;
	this.ob.height=Gl_h;
	this.ob.clip="rect(0 "+Gl_w+" "+Gl_h+" 0)";
	this.width=Gl_w;
	this.height=Gl_h;
	this.clipped=true;
	this.cliptop=0;
	this.clipbottom=Gl_h;
	this.clipleft=0;
	this.clipright=Gl_w
}

function G_layerresizeNS(Gl_w,Gl_h)
{
	this.ob.width=Gl_w;
	this.ob.height=Gl_h;
	this.ob.clip.left=0;
	this.ob.clip.right=Gl_w;
	this.ob.clip.top=0;
	this.ob.clip.bottom=Gl_h;
	this.width=Gl_w;
	this.height=Gl_h;
	this.clipped=true;
	this.cliptop=0;
	this.clipbottom=Gl_h;
	this.clipleft=0;
	this.clipright=Gl_w
}

function G_dragtype(Gl_a)
{
	this.dragnormal=false;
	this.dragvert=false;
	this.draghoriz=false;
	if(Gl_a==0)
		this.dragnormal=true;
	else if(Gl_a==1)
		this.dragvert=true;
	else if(Gl_a==2)
		this.draghoriz=true
}

function G_setxlimits(x1,x2)
{
	this.xmin=x1;
	this.xmax=x2;
	if(this.x<this.xmin)
		this.x=this.xmin;
	else if(this.x+this.width>this.xmax)
		this.x=this.xmax-this.width;
	this.moveTo(this.x,this.y)
}

function G_setylimits(y1,y2)
{
	this.ymin=y1;
	this.ymax=y2;
	if(this.y<this.ymin)
		this.y=this.ymin;
	else if(this.y+this.height>this.ymax)
		this.y=this.ymax-this.height;
	this.moveTo(this.x,this.y)
}

function G_locklayer(Gl_a)
{
	this.lock=Gl_a;
	this.lockxoff=Gl_a.x-this.x;
	this.lockyoff=Gl_a.y-this.y
}

function G_unlocklayer()
{
	this.lock=null
}

function moveTo( x, y )
{
	if (x>this.xmax-this.width)  x=this.xmax-this.width;
	if (y>this.ymax-this.height) y=this.ymax-this.height;
	if (x<this.xmin) x=this.xmin;
	if (y<this.ymin) y=this.ymin;

	this.x = x;
	this.y = y;
    generic_move( 'Adiv'+this.index , x, y, false );
	if(this.lock!=null)
		this.lock.moveTo(this.x+this.lockxoff,this.y+this.lockyoff)
}

function moveBy( nx, ny)
{
	if ( (this.x + nx) >= this.xmax-this.width ) nx = (this.xmax-this.width) - this.x;
	if ( (this.y + ny) >= this.ymax-this.height) ny = (this.ymax-this.height)- this.y;
	if ( (this.x + nx) < this.xmin) nx = this.xmin - this.x;
	if ( (this.y + ny) < this.ymin) ny = this.ymin - this.y;

	this.x = this.x + nx;
	this.y = this.y + ny;
    generic_move( 'Adiv'+this.index , this.x, this.y, false );
	if(this.lock!=null)
		this.lock.moveTo(this.x+this.lockxoff,this.y+this.lockyoff)
}

function show()
{
	this.ob.visibility="visible"
}

function hide()
{
	this.ob.visibility="hidden"
}

function setopacity(Gl_a)
{
	if(Gl_a<100)
	{
		this.ob.filter="alpha(opacity="+Gl_a+")";
		this.opacity=Gl_a
	}
	else
	{
		this.ob.filter="";
		this.opacity=100
	}
}

function setz(ZZ)
{
	this.ob.zIndex=ZZ;
	this.z=ZZ;
}

function G_layerfollow(Gl_ob,Gl_x,Gl_y)
{
	this.followingx=Gl_x;
	this.followingy=Gl_y;
	if(Gl_ob.alive)
	{
		this.following=Gl_ob;
		this.draggable=false;
		Gl_ob.followedby[Gl_ob.followedby.length]=this;
		Gl_ob.beingfollowed=true
	}
	else alert("Error!\nLayer cannot follow object (not a sprite or mouse!)")
}

function G_stoplayerfollow()
{
	var f=false;
	for(var n=0;n<this.following.followedby.length;n++)
	{
		if(this.following.followedby[n]==this)
		{
			f=true;break
		}
	}
	if(f)
	{
		for(var n2=n;n2<this.following.followedby.length-1;n2++)
			this.following.followedby[n2]=this.following.followedby[n2+1];
		if(--this.following.followedby.length==0)
			this.following.beingfollowed=false
	}
}

function G_layerwriteIE(Gl_txt)
{
	this.raw.innerHTML=Gl_txt;
	this.innerHTML=Gl_txt;
	if(!this.clipped)
	{
		this.width=this.raw.clientWidth;
		this.height=this.raw.clientHeight;
		this.moveTo(this.x,this.y)
	}
}

function G_layerappendIE(Gl_txt,Gl_pos)
{
	if(Gl_pos && Gl_pos<this.innerHTML.length)
	{
		var Gl_tmp=this.innerHTML.substring(0,Gl_pos)+Gl_txt+this.innerHTML.substring(Gl_pos,this.innerHTML.length);
		this.innerHTML=Gl_tmp;
		this.raw.innerHTML=Gl_tmp
	}
	else
	{
		this.innerHTML+=Gl_txt;
		this.raw.innerHTML=this.innerHTML
	}
	if(!this.clipped)
	{
		this.width=this.raw.clientWidth;
		this.height=this.raw.clientHeight;
		this.moveTo(this.x,this.y)
	}
}

function G_layerappendNS(Gl_txt,Gl_pos)
{
	if(Gl_pos && Gl_pos<this.innerHTML.length)
	{
		var Gl_tmp=this.innerHTML.substring(0,Gl_pos)+Gl_txt+this.innerHTML.substring(Gl_pos,this.innerHTML.length);
		this.innerHTML=Gl_tmp
	}
	else
		this.innerHTML+=Gl_txt;
	this.ob.document.open();
	this.ob.document.write(this.innerHTML);
	this.ob.document.close();
	if(!this.clipped)
	{
		this.width=this.raw.clip.width;
		this.height=this.raw.clip.height;
		this.moveTo(this.x,this.y)
	}
}

function G_layerwriteNS(Gl_txt)
{
	this.ob.width=0;
	this.ob.height=0;
	this.ob.document.open();
	this.ob.document.write(Gl_txt);
	this.ob.document.close();
	this.innerHTML=Gl_txt;
	if(!this.clipped)
	{
		this.width=this.raw.clip.width;
		this.height=this.raw.clip.height;
		this.moveTo(this.x,this.y)
	}
}

function G_loadIE(Gl_file,Gl_st)
{
	window.status="Loading layer, attempt "+(Gl_loadattempts+1)+"...";
	Gl_loadtimer=0;
	if(Gl_loadattempts==3)
	{
		Gl_loadwait=null;
		Gl_loadattempts=0;
		return
	}
	Gl_app="";
	if(Gl_loadwait==null)
	{
		this.URL=Gl_file;
		if(Gl_st)
			Gl_app="?"+(Math.floor(Math.random()*999999));
		if(Gl_iebuffer.document.body)
			Gl_iebuffer.document.body.innerHTML="";
		Gl_iebuffer.document.location.href=Gl_file+Gl_app;
		Gl_movedata=this;
		Gl_loadwait=setTimeout("G_wait_for_loadIE()",200)
	}
}

function G_loadNS(Gl_file,Gl_st)
{
	window.status="Loading layer, attempt "+(Gl_loadattempts+1)+"...";
	Gl_loadtimer=0;
	if(Gl_loadattempts==3)
	{
		Gl_loadwait=null;
		Gl_loadattempts=0;
		return
	}
	Gl_app="";
	if(Gl_loadwait==null)
	{
		this.ob.height=0;
		this.URL=Gl_file;
		Gl_movedata=this;
		if(Gl_st) Gl_app="?"+(Math.floor(Math.random()*999999));
		this.ob.load(Gl_file+Gl_app,this.width);
		Gl_loadwait=setTimeout("G_wait_for_loadNS()",200)
	}
}

function G_loadN6(Gl_file,Gl_st)
{
	window.status="Loading layer, attempt "+(Gl_loadattempts+1)+"...";
	Gl_loadtimer=0;
	if(Gl_loadattempts==3)
	{
		Gl_loadwait=null;
		Gl_loadattempts=0;
		return
	}
	Gl_app="";
	if(Gl_loadwait==null)
	{
		this.URL=Gl_file;
		if(Gl_st)
			Gl_app="?"+(Math.floor(Math.random()*999999));
		if(Gl_iebuffer)
			Gl_iebuffer.innerHTML="";
		Gl_iebuffer.src=Gl_file+Gl_app;
		Gl_movedata=this;
		Gl_loadwait=setTimeout("G_wait_for_loadN6()",200)
	}
}

function G_wait_for_loadIE()
{
	clearTimeout(Gl_loadwait);
	Gl_loadwait=null;
	var Gl_in=""+Gl_iebuffer.document.body.outerHTML.toLowerCase();
	if(Gl_in.length>100&&Gl_in.indexOf('</body>')!=-1)
	{
		var OldWidth = Gl_movedata.ob.width;
		var OldHeight = Gl_movedata.ob.height;

		Gl_loadattempts=0;
		Gl_movedata.raw.innerHTML="";
		Gl_movedata.ob.height=0;
		Gl_movedata.ob.clientHeight=0;
		Gl_movedata.ob.scrollHeight=0;
		Gl_movedata.raw.innerHTML=Gl_iebuffer.document.body.innerHTML;
		Gl_movedata.innerHTML=Gl_iebuffer.document.body.innerHTML;
		Gl_movedata.width=Gl_movedata.raw.clientWidth;
		Gl_movedata.height=Gl_movedata.raw.clientHeight;
		window.status="Done";
		Gl_movedata.resize(OldWidth,OldHeight);
//		Gl_movedata.ob.overflow='auto';
//     vyřazeno automatické skrolování obsahu kvůli NS4 a Mozille 0.8
	}
	else if(Gl_loadtimer++==100)
		Gl_loadIE(Gl_movedata.URL,true);
	else Gl_loadwait=setTimeout("Gl_wait_for_loadIE()",200)
}

function G_wait_for_loadNS()
{
	clearTimeout(Gl_loadwait);
	Gl_loadwait=null;
	if(Gl_movedata.ob.clip.height>0)
	{
		Gl_loadattempts=0;
		Gl_movedata.width=Gl_movedata.ob.clip.width;
		Gl_movedata.height=Gl_movedata.ob.clip.height;
		Gl_movedata.innerHTML="";
		Gl_movedata.moveTo(Gl_movedata.x,Gl_movedata.y);
		window.status="Done";
		this.clipped=false
	}
	else if(Gl_loadtimer++==100)
		G_loadNS(Gl_movedata.URL,true);
	else Gl_loadwait=setTimeout("G_wait_for_loadNS()",200)
}

function G_wait_for_loadN6()
{
	clearTimeout(Gl_loadwait);
	Gl_loadwait=null;
	var Gl_in=""+Gl_iebuffer.contentDocument.body.innerHTML;
	if(Gl_iebuffer.contentDocument.height>0)
	{
		var OldWidth = Gl_movedata.ob.width;
		var OldHeight = Gl_movedata.ob.height;
		
		Gl_loadattempts=0;
		Gl_movedata.raw.innerHTML="";
		Gl_movedata.ob.height=0;
		Gl_movedata.ob.clientHeight=0;
		Gl_movedata.ob.scrollHeight=0;
		Gl_movedata.raw.innerHTML=Gl_in;
		Gl_movedata.innerHTML=Gl_in;
//		Gl_movedata.ob.overflow='auto';
// Mozilla 0.8 má problémy s pořadím vrstev nad sebou, jestliže může vrstva skrolovat
		Gl_movedata.resize(OldWidth,OldHeight);
		window.status="Done";
	}
	else if(Gl_loadtimer++==100)
		G_loadN6(Gl_movedata.URL,true);
	else Gl_loadwait=setTimeout("G_wait_for_loadN6()",200)
}

function G_obclipNS(Gl_top,Gl_right,Gl_bottom,Gl_left)
{
	this.ob.clip.left=Gl_left;
	this.ob.clip.right=Gl_right;
	this.ob.clip.top=Gl_top;
	this.ob.clip.bottom=Gl_bottom;
	this.clipleft=Gl_left;
	this.cliptop=Gl_top;
	this.clipright=Gl_right;
	this.clipbottom=Gl_bottom;
	this.clipped=true
}

function G_obclipIE(Gl_top,Gl_right,Gl_bottom,Gl_left)
{
	this.ob.clip="rect("+Gl_top+","+Gl_right+","+Gl_bottom+","+Gl_left+")";
	this.clipleft=Gl_left;
	this.cliptop=Gl_top;
	this.clipright=Gl_right;
	this.clipbottom=Gl_bottom;
	this.clipped=true
}

/////////// start BAL - BrowserAbstractingLayer ////////////////////////
function getStyleBySelector( selector )
{
	if (!isNav6)
	{
		return null;
	}
	var sheetList = document.styleSheets;
	var ruleList;
	var i, j;

	for (i=sheetList.length-1; i >= 0; i--)
	{
		ruleList = sheetList[i].cssRules;
		for (j=0; j<ruleList.length; j++)
		{
			if (ruleList[j].type == CSSRule.STYLE_RULE &&
				ruleList[j].selectorText == selector)
			{
				return ruleList[j].style;
			}   
		}
	}
	return null;
}

function getIdProperty( id, property )
{
	if (isNav6)
	{
		var styleObject = document.getElementById( id );
		if (styleObject != null)
		{
			styleObject = styleObject.style;
			if (styleObject[property])
			{
				return styleObject[ property ];
			}
		}
		styleObject = getStyleBySelector( "#" + id );
		return (styleObject != null) ? styleObject[property] : null;
	}
	else if (isNav4)
	{
		return document[id][property];
	}
	else
	{
		return document.all[id].style[property];
	}
}

function setIdProperty( id, property, value )
{
	if (isNav6)
	{
		var styleObject = document.getElementById( id );
		if (styleObject != null)
		{
			styleObject = styleObject.style;
			styleObject[ property ] = value;
		}
	}
	else if (isNav4)
	{
		document[id][property] = value;
	}
	else if (isIE4)
	{
		document.all[id].style[property] = value;
	}
}

function generic_move( id, xValue, yValue, additive )
{
	var left = getIdProperty(id, "left");
	var top = getIdProperty(id, "top");
	var leftMatch, topMatch;

	if (isNav4)
	{
		leftMatch = new Array( 0, left, "");
		topMatch = new Array( 0, top, "");
	}
	else if (isNav6 || isIE4 )
	{
		var splitexp = /([-0-9.]+)(\w+)/;
		leftMatch = splitexp.exec( left );
		topMatch = splitexp.exec( top );
		if (leftMatch == null || topMatch == null)
		{
			leftMatch = new Array(0, 0, "px");
			topMatch = new Array(0, 0, "px");
		}
	}
	left = ((additive) ? parseFloat( leftMatch[1] ) : 0) + xValue;
	top = ((additive) ? parseFloat( topMatch[1] ) : 0) + yValue;
	setIdProperty( id, "left", left + leftMatch[2] );
	setIdProperty( id, "top", top + topMatch[2] );
}

function hex( n )
{
	var hexdigits = "0123456789abcdef";
	return ( hexdigits.charAt(n >> 4) + hexdigits.charAt(n & 0x0f) );
}

function getBackgroundColor( id )
{
	var color;

	if (isNav4)
	{
		color = document[id].bgColor;
	}
	else if (isNav6)
	{
		var parseExp = /rgb.(\d+),(\d+),(\d+)./;
		var rgbvals;
		color = getIdProperty( id, "backgroundColor" );
		if (color)
		{
			rgbvals = parseExp.exec( color );
			if (rgbvals)
			{
				color = "#" + hex( rgbvals[1] ) + hex( rgbvals[2] ) + hex( rgbvals[3] );
			}
		}
		return color;
	}
	else if (isIE4)
	{
		return document.all[id].backgroundColor;
	}
	return "";
}

function getDocument( divName )
{
	var doc;

	if (isNav4)
	{
		doc = window.document[divName].document;
	}
	else if (isNav6)
	{
		doc = document;
	}
	else if (isIE4)
	{
		doc = document;
	}
	return doc;
}

