
//======================== bloc wframe BEGIND================================================
function wFrame()
{
	this.blocs=null;
	this.blocSelected=-1;
	this.init();
}
wFrame.prototype.init=function()
{
	this.blocs=new Array();
	this.blocSelected=-1;
}
wFrame.prototype.newBloc=function(bl)
{
	this.blocs.push(bl);
	this.blocSelected=this.blocs.length;
}
var wframes=new wFrame();

//======================== bloc wframe END================================================

//======================================bloc manager=========================================

var curBlocId=0;
var highZ=100;
function bloc(Parent,w,h,x,y,id)
{
	this.blocFrame=null;
	this.maxBtn=null;
	this.maxEnabled=true;
	this.blocBar=null;
	this.blocContent=null;
	this.resizeBtn=null;
	this.resizeEnabled=null;
	this.blocParent=Parent;
	this.title='';
	this.resizeX=true;
	this.resizeY=true;
	var _this=this;

	this.init(Parent,w,h,x,y,id);
}
bloc.prototype.init=function(Parent,w,h,x,y,id)
{
	this.blocFrame=document.createElement("DIV");
	this.blocFrame.obj=this;
	//this.blocParent=Parent;
	if(this.blocParent==null) this.blocParent=document.body;
	if (this.blocFrame)
	{
		curBlocId++;
		this.blocFrame.id = "bloc"+id;
		this.blocParent.appendChild(this.blocFrame);
		//xAppendChild(this.blocParent,this.blocFrame);
		this.blocFrame.setAttribute("class",'blocCss');
		this.blocFrame.setAttribute("className",'blocCss');
		xResizeTo(this.blocFrame,w,h);
		xZIndex(this.blocFrame,highZ++);
		
		this.blocBar=document.createElement("DIV");
		if (this.blocBar)
		{
			this.blocBar.id = "blocBar"+id;
			this.blocFrame.appendChild(this.blocBar);
			//xAppendChild(this.blocFrame,this.blocBar);
			this.blocBar.setAttribute("class",'blocBarDefault');
			this.blocBar.setAttribute("className",'blocBarDefault');
			xMoveTo(this.blocBar,0,0);
		}
	/*
		this.maxBtn=document.createElement("DIV");
		if (this.maxBtn)
		{
			this.maxBtn.id = "maxBtn"+id;
			xAppendChild(this.blocFrame,this.maxBtn);
			this.maxBtn.setAttribute("class",'maxBtnDefault');
			this.maxBtn.setAttribute("className",'maxBtnDefault');
			xInnerHtml(this.maxBtn,'<div align="center">X</div>');
		}
	*/
		this.blocContent=document.createElement("DIV");
		if (this.blocContent)
		{
			this.blocContent.id = "blocContent"+id;
			xAppendChild(this.blocFrame,this.blocContent);
			this.blocContent.setAttribute("class",'blocContentCss');
			this.blocContent.setAttribute("className",'blocContentCss');
			this.blocContent.setAttribute("overflow","auto");

		}
		/*
		this.resizeBtn=document.createElement("DIV");
		if (this.resizeBtn)
		{
			this.resizeBtn.id = "resizeBtn"+id;
			xAppendChild(this.blocFrame,this.resizeBtn);
			xZIndex(this.resizeBtn,500);
			this.resizeBtn.setAttribute("class",'maxBtnDefault');
			this.resizeBtn.setAttribute("className",'maxBtnDefault');
		}
		*/
		xMoveTo(this.blocFrame, x, y);xResizeTo(this.blocFrame, w, h);
		this.blocFrame.onclick = this.onframeclick;
		this.Paint(this);
		xEnableDrag(this.blocBar, this.barOnDragStart, this.barOnDrag,this.barOnDragEnd);
		//xEnableDrag(this.resizeBtn, this.OnResizeStart, this.OnResize, this.OnResizeEnd);
		//this.maxBtn.onclick = this.maxOnClick;
		//this.loadParams();
	}
	return(this);
}
bloc.prototype.show=function()
{
	xShow(this.blocFrame);
}
bloc.prototype.hide=function()
{
	xHide(this.blocFrame);
}
bloc.prototype.maxEnable=function(enable)
{
	this.maxEnabled=enable;
	if(enable)
		xShow(this.maxBtn);
	else
		xHide(this.maxBtn);
	
}
bloc.prototype.resizeEnable=function(enable)
{
	this.resizeEnabled=enable;
	if(enable)
		xShow(this.resizeBtn);
	else
		xHide(this.resizeBtn);
}
bloc.prototype.moveEnable=function(enable)
{
	this.moveEnabled=enable;
	if(enable)
		xShow(this.blocBar);
	else
		xHide(this.blocBar);
}

bloc.prototype.setContent=function(str)
{
	xInnerHtml(this.blocContent,str);
}
bloc.prototype.appendContent=function(str)
{
	xInnerHtml(this.blocContent,xInnerHtml(this.blocContent)+str);
}

bloc.prototype.setTitle=function(str)
{
	this.title=str;
	if(xParent(this) && xParent(this).obj)
		p=xParent(this).obj;
	else
		p=this;
	xInnerHtml(p.blocBar,str);
}
bloc.prototype.Paint=function()
{
	if(this.resizeBtn!=null)
		xMoveTo(this.resizeBtn, xWidth(this.blocFrame) - xWidth(this.resizeBtn), xHeight(this.blocFrame) - xHeight(this.resizeBtn));
	if(this.maxBtn!=null)
	{
		xMoveTo(this.maxBtn, xWidth(this.blocFrame) - xWidth(this.maxBtn), 0);
	}
	
	//xResizeTo(bl.blocContent,xWidth(bl.blocFrame),xHeight(bl.blocFrame)-xHeight(bl.blocBar)
}

bloc.prototype.foreground=function()
{
	p=this;
	if(xZIndex(p.blocFrame)<highZ)
		xZIndex(p.blocFrame, highZ++);
}
bloc.prototype.onframeclick=function(ele)
{
	p=this.obj;
	if(xZIndex(p.blocFrame)<highZ)
		xZIndex(p.blocFrame, highZ++);
}
bloc.prototype.barOnDragStart=function(ele, mx, my)
{
	p=xParent(this).obj;
	//alert(p+" "+p.blocFrame+" "+xZIndex(p.blocFrame)+" "+highZ);
	if(xZIndex(p.blocFrame)<highZ)
		xZIndex(p.blocFrame, highZ++);
}
bloc.prototype.barOnDrag=function(ele, mdx, mdy)
{
	p=xParent(this).obj;
	xMoveTo(p.blocFrame, xLeft(p.blocFrame) + mdx, xTop(p.blocFrame) + mdy);
}

bloc.prototype.barOnDragEnd=function(ele, mdx, mdy)
{
	p=xParent(this).obj;
	p.saveParams();
}

bloc.prototype.OnResizeStart=function(ele, mx, my)
{
	p=xParent(this).obj;
	xZIndex(p.blocFrame, highZ++);
}
bloc.prototype.resizeBloc=function(mdx, mdy)
{
	p=this;
	//alert(p.blocFrame.className+" "+xWidth(p.blocFrame,mdx)+" "+mdx);
	
	xResizeTo(p.blocFrame,mdx, mdy);
	xResizeTo(p.blocContent,mdx, mdy);
	this.Paint();
}
bloc.prototype.OnResizeEnd=function(ele, mx, my)
{
	p=xParent(this).obj;
	p.saveParams();
}
bloc.prototype.OnResize=function(ele, mdx, mdy)
{
	p=xParent(this).obj;
	if(p.resizeX) newX=mdx;else newX=0;
	if(p.resizeY) newY=mdy;else newY=0;
	xResizeTo(p.blocFrame, xWidth(p.blocFrame) + newX, xHeight(p.blocFrame) + newY);
	p.Paint(p);
}
bloc.prototype.maxOnClick=function()
{
	p=xParent(this).obj;
	if (p.blocFrame.maximized)
	{
		p.blocFrame.maximized = false;
		xResizeTo(p.blocFrame, p.blocFrame.prevW, p.blocFrame.prevH);
		xMoveTo(p.blocFrame, p.blocFrame.prevX, p.blocFrame.prevY);
		p.Paint(p);
	}
	else
	{
		p.blocFrame.prevW = xWidth(p.blocFrame);
		p.blocFrame.prevH = xHeight(p.blocFrame);
		p.blocFrame.prevX = xLeft(p.blocFrame);
		p.blocFrame.prevY = xTop(p.blocFrame);
		xMoveTo(p.blocFrame, xScrollLeft(), xScrollTop());
		p.blocFrame.maximized = p;
		xResizeTo(p.blocFrame, xClientWidth(), xClientHeight());
		p.Paint(p);
	}
}
bloc.prototype.saveParams=function()
{
	//alert(this.blocFrame.id);
	w=xWidth(this.blocFrame);h=xHeight(this.blocFrame);x=xLeft(this.blocFrame);y=xTop(this.blocFrame);
	bk=xBackground(this.blocFrame);col=xColor(this.blocFrame);zi=xZIndex(this.blocFrame);vis=xVisibility(this.blocFrame);
	op=xOpacity(this.blocFrame);
	cook=w+"|"+h+"|"+x+"|"+y+"|"+bk+"|"+col+"|"+zi+"|"+vis+"|"+op;
	d=new Date();//d.setYear(d.getYear()+1);
	d.setFullYear(d.getFullYear()+10);
	xSetCookie(this.blocFrame.id,cook,d,"/");
}

bloc.prototype.loadParams=function()
{
	ck=xGetCookie(this.blocFrame.id);
	if(ck==null) return;
	parms=ck.split("|");
	if(this.resizeX) xWidth(this.blocFrame,parseInt(parms[0],10));
	if(this.resizeY)xHeight(this.blocFrame,parseInt(parms[1],10));
	xMoveTo(this.blocFrame,parseInt(parms[2],10),parseInt(parms[3],10));
	xBackground(this.blocFrame,parms[4]);xColor(this.blocFrame,parms[5]);
	xZIndex(this.blocFrame,parseInt(parms[6],10));
	if((parms[7]=="visible")||(parms[7]=="show"))xShow(this.blocFrame); else xHide(this.blocFrame);
	xOpacity(this.blocFrame,parseInt(parms[6],10));
	this.Paint(this);
	if(xZIndex(this.blocFrame)>highZ)highZ=xZIndex(this.blocFrame);
}

//======================== bloc manager END================================================