function putInHTML()
{
	this.wnd.document.write(this.htm);
	this.htm = '';
}

function putInContainer()//za ralika tova v canvasa
{
	var x = document.createRange();//creates range
	x.setStartBefore(this.container);//Sets the start position of a range relative to another
	x = x.createContextualFragment(this.htm);//returns document fragment
	this.container.appendChild(x); 
	//this.cnv.removeChild(x);
	this.htm = '';
}
//////////////////////
function pntDoc()
{
	this.wnd.write(this.htmRpc());
	this.htm = '';
}
function mkDivIe(x, y, w, h)
{
	this.htm += '%%'+this.color+';'+x+';'+y+';'+w+';'+h+';';
}

var regex =  /%%([^;]+);([^;]+);([^;]+);([^;]+);([^;]+);/g;

function htmRpc()
{
	return this.htm.replace(
		regex,
		'<div style="overflow:hidden;position:absolute;background-color:'+
		'$1;left:$2;top:$3;width:$4;height:$5"></div>\n');		
}

function pntCnvIe()
{
	this.container.insertAdjacentHTML("BeforeEnd", this.htmRpc());
	this.htm = '';
}
/////////////////////
function putInInnerContainerHTML()//printCanvasInnerHtml()
{
	this.container.innerHTML += this.htm;
	this.htm = '';
}


function clearHtml()
{
	this.htm = '';
}

function createLayer(x, y, w, h)// create-va standarten div left, top, height, width
{
	this.htm += '<div id=drawDIV'+this.endIndex+' style="position:absolute;'+
		'left:' + x + 'px;'+
		'top:' + y + 'px;'+
		'width:' + w + 'px;'+
		'height:' + h + 'px;'+
		'clip:rect(0,'+w+'px,'+h+'px,0);'+//tova ne mie qsno kvo pravi
		'background-color:' + this.color +
		';overflow:hidden' + //jg_moz = jg_dom && typeof x.style.MozOpacity != "undefined"
		';"><\/div>';//jg_dom proverqva vsi4ki neshtadeto gi gledah appendChild(), setNeZnamSiKvo() 
		
	++this.endIndex;
}

function createSimpleLine(x1, y1, x2, y2)
{
	if (x1 > x2)
	{
		var tempX2 = x2;
		var tempY2 = y2;
		x2 = x1;
		y2 = y1;
		x1 = tempX2;
		y1 = tempY2;
	}
	var dx = x2 - x1, 
	    dy = Math.abs(y2 - y1),
	    x = x1, 
	    y = y1,
	    yIncr = (y1 > y2)? -1 : 1;

	if (dx >= dy)
	{
		var pr = dy<<1,      //pr = dy*2
		pru = pr - (dx<<1),  //pru = dy*2 - dx*2
		p = pr - dx,         // p = dy*2 - dx
		ox = x;              //ox e oldX
		while ((dx--) > 0)
		{
			++x;
			if (p > 0)
			{
				this.createLayer(ox, y, x-ox, 1);
				y += yIncr;
				p += pru;//p = p + (dy*2 - dx*2);    p = dy*2 - dx;    (dy*2 - dx*2) < 0
				ox = x;
			}
			else p += pr;//p = p + dy*2;   p = dy*2 - dx
		}
		this.createLayer(ox, y, x2-ox+1, 1);
	}

	else
	{
		var pr = dx<<1,
		pru = pr - (dy<<1),
		p = pr-dy,
		oy = y;
		if (y2 <= y1)
		{
			while ((dy--) > 0)
			{
				if (p > 0)
				{
					this.createLayer(x++, y, 1, oy-y+1);
					y += yIncr;
					p += pru;
					oy = y;
				}
				else
				{
					y += yIncr;
					p += pr;
				}
			}
			this.createLayer(x2, y2, 1, oy-y2+1);
		}
		else
		{
			while ((dy--) > 0)
			{
				y += yIncr;
				if (p > 0)
				{
					this.createLayer(x++, oy, 1, y-oy);
					p += pru;
					oy = y;
				}
				else p += pr;
			}
			this.createLayer(x2, oy, 1, y2-oy+1);
		}
	}
}

function simpleGraphics(containerID)
{
	this.setColor = new Function('arg', 'this.color = arg.toLowerCase();');
    
    this.startIndex = 0;
    this.endIndex = 0;
	
	this.setStroke = function(x)
	{
		this.drawLine = createSimpleLine;
	};
	this.clear = function()
	{
		this.htm = "";
		if (this.container) 
			this.container.innerHTML = this.defhtm;
	}
	this.setStroke(1);
	this.color = '#000000';
	this.htm = '';
	this.wnd = null || window;
    
    this.createLayer = createLayer;
    this.htmRpc = htmRpc;
    
	if (typeof containerID != 'string' || !containerID) 
		this.paint = pntDoc;
	else
	{   
		//tova e za containera
		this.container = document.all? (this.wnd.document.all[containerID] || null)
			: document.getElementById? (this.wnd.document.getElementById(containerID) || null)
			: null;
		this.defhtm = (this.container && this.container.innerHTML)? this.container.innerHTML : '';
		this.paint =  pntCnvIe;// da probvam i s dvete  putInInnerContainerHTML;
	}
	return this;
}

