var tables=new Array(); // oTable objects
var tableHili=-1;
var pLevels=new Array();
var planLevel=1; // current selection value of pLevels
var lpath='.';
var xmlhttp=null;
var interval2=0;
function resetLevButVisibility() {
   var o=document.all.LevelDiv;
   document.all.ulb.style.visibility=(o.scrollLeft>0?'visible':'hidden');
   document.all.lrb.style.visibility=(o.scrollWidth - o.scrollLeft > o.clientWidth?'visible':'hidden');
}
function levelMoveUp()
{
	var o=document.all.LevelDiv;
   o.scrollLeft-=80;
   resetLevButVisibility();
}
function levelMoveDown()
{
	var o=document.all.LevelDiv;
   o.scrollLeft+=80;
   resetLevButVisibility();
}
function getNextTableNR()
{
	var n=tables.length;
	var maxNR=0;
	for (i=0;i<n;i++) {
		if (tables[i].NR > maxNR)
			maxNR=tables[i].NR;
	}
	return maxNR+1;
}
function add_c1()
{
	a1='';

	n=this.c1;
	if (n==0)
		return a1;

   if (this.tt==0 || this.tt==2)
   	region=this.vert;
   else // circle
		region=Math.PI*this.vert/4;

	if (n>region/5) {
		n=parseInt(region/5);
		this.c1=n;
	}

	dy=(region - n*5)/(n+1);
	for (i=0;i<n;i++) {
		if (this.tt==0) { // square
			x=0;
			y=parseInt( (dy+5)*(i+1) );
		}
		else if (this.tt==1) { // circle
			fx=dy+(dy+5)*i+2.5;
			f=fx*2/this.vert;
			xx=Math.cos(Math.PI/4-f)*(this.horz/2+2.5);
			x=parseInt( this.horz/2+5-xx-2.5 );
			yy=Math.sin(Math.PI/4-f)*(this.horz/2+2.5);
			y=parseInt( this.horz/2+5+yy-2.5 );
		}
		else { // tt=2: rhomb
			fy=dy+(5+dy)*i;
			dx=fy * Math.cos(Math.PI/4);
			x=parseInt( 5 + dx - 3.5 );
			y=parseInt( 5 + this.horz * Math.cos(Math.PI/4) - dx - 4.5 );
		}
		a1 += '<v:oval style="position:absolute;left:' + x + ';top:' + y + ';width:5;height:5;"/>';
	}
	return a1;
}
function add_c2()
{
	a1='';

	n=this.c2;
	if (n==0)
		return a1;

	if (this.tt==0 || this.tt==2)
		region=this.horz;
	else // circle
		region=Math.PI*this.horz/4;

	if (n>region/5) {
		n=parseInt(region/5);
		this.c2=n;
	}

	dx=(region-n*5)/(n+1);
	for (i=0;i<n;i++) {
		if (this.tt==0) { // square
			x=parseInt( (dx+5)*(i+1) );
			y=0;
		}
		else if (this.tt==1) { // circle
			fx=dx+(dx+5)*i+2.5;
			f=fx*2/this.horz;
			xx=Math.cos(f+Math.PI/4)*(this.horz/2+2.5);
			x=parseInt( this.horz/2+5-xx-2.5 );
			yy=Math.sin(f+Math.PI/4)*(this.horz/2+2.5);
			y=parseInt( this.horz/2+5-yy-2.5 );
		}
		else { // tt=2: rhomb
			fx=dx+(5+dx)*i;
			dy=fx * Math.cos(Math.PI/4);
			x=parseInt( 5 + this.horz * Math.cos(Math.PI/4) + dy + 3 );
			y=parseInt( 5 + dy - 2 );
		}
		a1 += '<v:oval style="position:absolute;left:' + x +';top:' + y +';width:5;height:5;"/>';
	}
	return a1;
}
function add_c3()
{
	a1='';

	n=this.c3;
	if (n==0)
		return a1;

	if (this.tt==0 || this.tt==2)
   	region=this.vert;
   else // circle
		region=Math.PI*this.vert/4;

	if (n>region/5) {
		n=parseInt(region/5);
		this.c3=n;
	}

	dy=(region-n*5)/(n+1);
	for (i=0;i<n;i++) {
		if (this.tt==0) { // square
			x=this.horz+5;
			y=parseInt( (dy+5)*(i+1) );
		}
		else if (this.tt==1) { // circle
			fx=dy+(dy+5)*i+2.5;
			f=fx*2/this.vert;
			xx=Math.cos(Math.PI/4-f)*(this.horz/2+2.5);
			x=parseInt( this.horz/2+5+xx-2.5 );
			yy=Math.sin(Math.PI/4-f)*(this.horz/2+2.5);
			y=parseInt( this.horz/2+5+yy-2.5 );
		}
		else { // tt=2: rhomb
			fy=dy+(5+dy)*i;
			dx=fy * Math.cos(Math.PI/4);
			x=parseInt( 5 + this.horz * Math.cos(Math.PI/4) + dx + 2 );
			y=parseInt( 5 + 2* this.horz * Math.cos(Math.PI/4) - dx - 3 );
		}
		a1 += '<v:oval style="position:absolute;left:' + x + ';top:' + y + ';width:5;height:5;"/>';
	}
	return a1;
}
function add_c4()
{
	a1='';

	n=this.c4;
	if (n==0)
		return a1;

	if (this.tt==0 || this.tt==2)
   	region=this.horz;
   else // circle
		region=Math.PI*this.horz/4;

	if (n>region/5) {
		n=parseInt(region/5);
		this.c4=n;
	}	

	dx=(region-n*5)/(n+1);
	for (i=0;i<n;i++) {
		if (this.tt==0) { // square
			x=parseInt( (dx+5)*(i+1) );
			y=this.vert+5;
		}
		else if (this.tt==1) { // circle
			fx=dx+(dx+5)*i+2.5;
			f=fx*2/this.horz;
			xx=Math.cos(f+Math.PI/4)*(this.horz/2+2.5);
			x=parseInt( this.horz/2+5-xx-2.5 );
			yy=Math.sin(f+Math.PI/4)*(this.horz/2+2.5);
			y=parseInt( this.horz/2+5+yy-2.5 );
		}
		else { // tt=2: rhomb
			fx=dx+(5+dx)*i;
			dy=fx * Math.cos(Math.PI/4);
			x=parseInt( 5 + dy - 3.5 );
			y=parseInt( 5 + this.horz * Math.cos(Math.PI/4) + dy + 1.5 );
		}
		a1 += '<v:oval style="position:absolute;left:' + x + ';top:' + y + ';width:5;height:5;"/>';
	}
	return a1;
}
function tableRedraw(odiv)
{
	if (this.tt<2) { // square or circle
		gWidth=this.horz+10;
		gHeight=this.vert+10;
	}
	else { // rhomb
		dx=parseInt( 2 * Math.sqrt( Math.pow(this.horz,2)/2 ) );
		gWidth=dx+10;
		gHeight=gWidth;
	}

	odiv.style.width=gWidth;
	odiv.style.height=gHeight;

   a1='<v:group\
		style="width:' + gWidth + ';height:' + gHeight + ';position:relative;top:0;left:0;" coordsize="' +
		gWidth + ',' + gHeight + '">';

	if (this.tt==0) // square
		a1 += '<v:roundrect fillcolor="' + this.color + '" style="position:absolute;left:5;top:5;width:' + this.horz + ';height:' + this.vert + ';" arcsize="0.1"/>';
	else if (this.tt==1) // circle
		a1 += '<v:oval fillcolor="' + this.color + '" style="position:absolute;left:5;top:5;width:' + this.horz + ';height:' + this.vert + ';"/>';
	else { // tt=2: rhomb
		x2=this.horz * Math.cos(Math.PI/4) / (2 * (1+Math.cos(Math.PI/4)));
		x2i=parseInt(x2)+5;
		a1 += '<v:roundrect fillcolor="' + this.color + '" style="position:absolute;left:' + x2i + ';top:' + x2i + ';width:' + this.horz + ';height:' + this.vert + ';rotation:45;" arcsize="0.1"/>';
	}

	a1 += this.add_c1();
	a1 += this.add_c2();
	a1 += this.add_c3();
	a1 += this.add_c4();

	a1 += '</v:group><div style="font-size:10pt;font-weight:bold;text-align:center;font-fmaily:arial;">' + this.NR + '</div>';
	odiv.innerHTML=a1;
}
function tableHit(x)
{
	var f1=document.forms[0];
	f1.LOGIN.value=x;
	f1.submit();
}
function oTable(nr, pl, x, y, tt, ho, ve, c1, c2, c3, c4, rgb)
{
	this.NR=nr;
	this.plevel=pl;
	this.tt=tt;
	this.horz=ho;
	this.vert=ve;
	this.c1=c1; // chairs left
	this.c2=c2; // chairs top
	this.c3=c3; // chairs right
	this.c4=c4; // chairs bottom
	this.color=rgb;
	this.active=1;
	this.redraw=tableRedraw;
	this.add_c1=add_c1;
	this.add_c2=add_c2;
	this.add_c3=add_c3;
	this.add_c4=add_c4;

	odiv=document.createElement('DIV');
	odiv.id='otable'+nr;
	odiv.style.position='absolute';
	odiv.style.top=y.toString();
	odiv.style.left=x.toString();
	odiv.style.backgroundColor='';
	this.redraw(odiv);
	if (document.all.tcpanel==undefined)
	  odiv.onclick=tableHit;

	document.body.appendChild(odiv);

	if (document.all.tcpanel==undefined)
		ADD_DHTML( odiv.id+NO_DRAG);
	else // ok
		ADD_DHTML( odiv.id );

	if (pl!=planLevel)
		dd.elements[odiv.id].hide();
}
function tableAdd(tt)
{
	nr=getNextTableNR();
	var o1=new oTable(nr, planLevel, 350, 250, tt, 20, 20, 1, 1, 1, 1, 'white');
	var n=tables.length;
	tables[n]=o1;

	if (tableHili>=0) {
		var id='otable'+tableHili;
		document.all[id].style.backgroundColor='';
	}

	id='otable'+nr;
	document.all[id].style.backgroundColor='#000080';
	tableHili=nr;
}
function tableMod(x)
{
	if (tableHili<0)
		return;

	// Find element
	fi=-1;
	n=tables.length;
	for (i=0;i<n;i++) {
		if (tables[i].NR==tableHili) {
			fi=i;
			break;
		}
	}
	if (fi<0) // not found
		return;

   o1=tables[fi];
	switch (x)
	{
	case 0:
		o1.vert+=5;
		if (o1.tt>0)
			o1.horz+=5;
		break;
	case 1:
		if (o1.vert>=25) {
			o1.vert-=5;
			maxc=o1.vert/5;
			if (o1.c1>maxc)
				o1.c1=maxc;
			if (o1.c3>maxc)
				o1.c3=maxc;
		}
		if (o1.tt>0) {
			o1.horz=o1.vert;
			maxc=o1.horz/5;
			if (o1.c2>maxc)
				o1.c2=maxc;
			if (o1.c4>maxc)
				o1.c4=maxc;
		}
		break;
	case 2:
		o1.horz+=5;
		if (o1.tt>0)
			o1.vert+=5;
		break;
	case 3:
		if (o1.horz>=25) {
			o1.horz-=5;
			maxc=o1.horz/5;
			if (o1.c2>maxc)
				o1.c2=maxc;
			if (o1.c4>maxc)
				o1.c4=maxc;
		}
		if (o1.tt>0) {
			o1.vert=o1.horz;
			maxc=o1.vert/5;
			if (o1.c1>maxc)
				o1.c1=maxc;
			if (o1.c3>maxc)
				o1.c3=maxc;
		}
		break;
	case 4:
		if (o1.c1<o1.vert/5)
			o1.c1++;
		break;
	case 5:
		if (o1.c1>0) {
			o1.c1--;
		}
		break;
	case 6:
		if (o1.c2<o1.horz/5)
			o1.c2++;
		break;
	case 7:
		if (o1.c2>0) {
			o1.c2--;
		}
		break;
	case 8:
		if (o1.c3<o1.vert/5)
			o1.c3++;
		break;
	case 9:
		if (o1.c3>0) {
			o1.c3--;
		}
		break;
	case 10:
		if (o1.c4<o1.horz/5)
			o1.c4++;
		break;
	case 11:
		if (o1.c4>0) {
			o1.c4--;
		}
		break;
	}
	id='otable'+tableHili;
   o1.redraw(document.all[id]);
}
function setPLevel(x)
{
	planLevel=x;
	planRedrawAll();
}
function planRedrawAll()
{
   document.body.style.backgroundImage='url(/ishop/'+ lpath + '/plan' + planLevel + '.png)';

	var n=tables.length;
	for (var i=0;i<n;i++) {
		var id='otable'+tables[i].NR;
		if (document.all[id]!=undefined) {
			if (tables[i].plevel==planLevel)
				dd.elements[id].show();
			else
				dd.elements[id].hide();
		}
	}
}
function saveTables()
{
	var f1=document.forms[0];

	n=tables.length;
	for (i=0;i<n;i++) {
		t1=tables[i];
		var id='otable'+t1.NR;
		a=new Array(t1.active, t1.NR, t1.plevel, dd.elements[id].x, dd.elements[id].y, t1.tt, t1.horz, t1.vert, t1.c1, t1.c2, t1.c3, t1.c4);

		var o1=document.createElement('INPUT');
  		o1.type='hidden';
	  	o1.name='TABLE[]';
  		o1.value=a.join(':');
  		f1.appendChild(o1);
 	}
 	f1.submit();
}
function wzLevPick() {
	if (dd.obj.name.substr(0,6)!='otable')
		return;

	x=dd.obj.name.substr(6);
	if (x==tableHili)
		return;

	if (tableHili>=0) {
		var id='otable'+tableHili;
		document.all[id].style.backgroundColor='';
	}
	tableHili=x;
	var id='otable'+tableHili;
	document.all[id].style.backgroundColor='#0000C0';
}
function wzLevDrop() {
	if (dd.obj.name.substr(0,6)!='otable')
		return;

	ey=dd.obj.y + dd.obj.h;
	if (dd.obj.x<560 && ((dd.obj.y >= 520 && dd.obj.y <= 570) || (ey >= 520 && ey <= 570)))
   	; // ok, continue
   else {
		if (ey>=520)
			dd.obj.moveTo(dd.obj.defx, dd.obj.defy);
		else {
			dd.obj.defx=dd.obj.x;
			dd.obj.defy=dd.obj.y;
		}
		return;
	}

   var o=document.all.LevelDiv;
   i=o.scrollLeft/80; // leftmost
   i2=parseInt( dd.obj.x/80 ) + i;
   // After calculations have been done, reset position
   dd.obj.moveTo(dd.obj.defx, dd.obj.defy);
   if (i2 >= pLevels.length)
   	return;

   j=pLevels[i2];
   if (j==planLevel)
   	return;

	// find object
	nr=dd.obj.name.substr(6);
	n=tables.length;
	for (i=0;i<n;i++) {
		if (tables[i].NR==nr) {
			tables[i].plevel=j;
			dd.obj.hide();
			return;
		}
	}
}
function goLevel(f,p)
{
   location.href='/ishop/restau_plan.php?FUNCTION='+f+'&PLANID='+p;
}
function refreshPlan()
{
   location.href='/ishop/restau_plan.php?FUNCTION=4';
}
function show_obj(q1)
{
   if (q1.error.length)
      return;

   n=q1.data.length;
   for (i=0;i<n;i++) {
      id='T'+q1.data[i].TABLE;
      try {
         document.all[id].fillcolor=q1.data[i].COLOR;
      }
      catch (e) {
         //alert(e);
      }
   }
}
function refreshPlan2()
{
   if (xmlhttp==null) {
		try {
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			xmlhttp=null;
			return;
		}
	}

	px=0;
	try {
      xmlhttp.open("POST", "/ishop/restau_pjson.php?PLANID="+planLevel, true);
   }
   catch (e) {
      return;
   }
	xmlhttp.onreadystatechange=function() {
  		if (xmlhttp.readyState==4) {
  			if (xmlhttp.status==200) {
	   		var local=new Function("return "+xmlhttp.responseText)();
            var n=local.length;
            for (i=0;i<n;i++)
               show_obj(local[i]);
         }

	      window.setTimeout('refreshPlan2()',interval2);
  		}
 	}
 	xmlhttp.send(null);
}
function planTimers(sec)
{
	timer1=window.setInterval('TA_test()',1000);

	if (sec>0) {
	   interval2=sec*1000;
	   window.setTimeout('refreshPlan2()',interval2);
	}
}
