// $Rev: 22538 $ $URL: svn://subversion.gepro/kokes/_verze_branch/9.60/Kokes/web/files/grfcore.js $ 
// Obecný javascript pro práci s grafikou
// ----------------------------------------------------------------------------------------------
function GrfConst() {
 this.SHOW_VYKRESY_VRSTVY = 0; 
 this.SHOW_TEMATA_OBLASTI = 1; 
 this.SHOW_SKUPINY = 2;        
 this.OPTION_SCREEN_WHITE      = 0x0; 
 this.OPTION_SCREEN_BLACK      = 0x1; 
 this.OPTION_PRINTER           = 0x2; 
 this.OPTION_BY_KEY            = 0x04; 
 this.OPTION_BY_LAY            = 0x08; 
 this.OPTION_SS                = 0x10; 
 this.OPTION_SS_RESET          = 0x20; 
 this.OPTION_VECTOR            = 0x40; 
 this.OPTION_VECTOR_RESET      = 0x80; 
 this.OPTION_RASTER            = 0x100; 
 this.OPTION_RASTER_RESET      = 0x200; 
 this.OPTION_COLRAS            = 0x400; 
 this.OPTION_COLRAS_RESET      = 0x800; 
 this.OPTION_AREA              = 0x1000; 
 this.OPTION_AREA_RESET        = 0x2000; 
 this.OPTION_POINTS            = 0x10000; 
 this.OPTION_POINTS_RESET      = 0x20000; 
 this.OPTION_INFO_SS           = 0x40000; 
 this.OPTION_INFO_SS_RESET     = 0x80000; 
 this.OPTION_INFO_POINTS       = 0x100000; 
 this.OPTION_INFO_POINTS_RESET = 0x200000; 
 this.OPTION_INFO_TEXTS        = 0x400000; 
 this.OPTION_INFO_TEXTS_RESET  = 0x800000; 
 this.OPTION_NETCROSS          = 0x1000000; 
 this.OPTION_NETCROSS_RESET    = 0x2000000; 
 this.OPTION_TEXTS             = 0x4000000; 
 this.OPTION_TEXTS_RESET       = 0x8000000; 
 this.OPTION_PX_WIDTH          = 0x10000000; 
 this.OPTION_SKUPINY           = 0x20000000; 
 this.OPTION_TEMATA_OBLASTI    = 0x40000000; 
 this.SEL_NULL_NODES = 1;
 this.SEL_DEFAULT_NODES = 2; 
 this.SEL_ADD_NODE_ID = 3; 
 this.SEL_ADD_NODE_ALIAS = 4; 
 this.SEL_ADD_NODE_NAME = 5; 
 this.SEL_REMOVE_NODE_ID = 6; 
 this.SEL_REMOVE_NODE_ALIAS = 7; 
 this.SEL_REMOVE_NODE_NAME = 8; 
 this.SEL_ADD_NODE_ALIAS2 = 9; 
 this.SEL_NULL_SOURCES = 21; 
 this.SEL_ADD_SOURCE_ID = 22; 
 this.SEL_ADD_SOURCE_NAME = 23; 
 this.SEL_REMOVE_SOURCE_ID = 24; 
 this.SEL_REMOVE_SOURCE_NAME = 25; 
 this.SEL_NULL_LAYERS = 41; 
 this.SEL_ADD_LAYER_ID = 42; 
 this.SEL_ADD_LAYER_NAME = 43; 
 this.SEL_REMOVE_LAYER_ID = 44; 
 this.SEL_REMOVE_LAYER_NAME = 45; 
 this.SEL_NULL_FILES = 61; 
 this.SEL_ADD_FILE_ID = 62; 
 this.SEL_ADD_FILE_NAME = 63; 
 this.SEL_REMOVE_FILE_ID = 64; 
 this.SEL_REMOVE_FILE_NAME = 65; 
 this.MOUSE_DETAIL = 1;         
 this.MOUSE_VECTOR = 2;         
 this.MOUSE_DETAIL_VIRTUAL = 3; 
 this.MOUSE_CLICK = 4;          
 this.MOUSE_CLICK_VIRTUAL = 5;  
 this.MOUSE_RECT_VIRTUAL = 6;   
 this.MOUSE_POINT_POINT = 7;    
 this.MOUSE_AREA = 8;           
 this.MOUSE_LINIE = 9;          
 this.MOUSE_IDENTIFY = 10;      
 this.NO_CLICK = 0;     
 this.FIRST_CLICK = 1;  
 this.SECOND_CLICK = 2; 
 this.SET_HLETEXT=1;  
 this.SET_SHOWPOI=2;  
 this.SET_PASPORT=3;  
 this.SET_IDENTIFY=4;  
 this.SET_IDENTIFY2=5;  
}
var GCon=new GrfConst();
function Click(Flag, pScale) { 
 this.Events.MasterAction=((Flag==0 || Flag==2) ? GCon.MOUSE_CLICK : GCon.MOUSE_CLICK_VIRTUAL);
 this.Events.MasterOrder = GCon.NO_CLICK;   
 this.Events.MasterFlag = Flag;
 this.Events.MasterScale = pScale;
 this.Doc.getElementById(this.Name).style.cursor='pointer';
}  
function Detail(Flag) { 
 if( Flag != -1) 
 {
  switch(Flag) {
   case 0: this.Events.MasterAction=GCon.MOUSE_DETAIL; break;
   case 1: this.Events.MasterAction=GCon.MOUSE_DETAIL_VIRTUAL; break;
   case 2: this.Events.MasterAction=GCon.MOUSE_RECT_VIRTUAL; break;
  }
  this.Events.MasterOrder == GCon.NO_CLICK
  this.Doc.getElementById(this.Name).style.cursor='crosshair';
 }
 else {
  this.Events.MasterAction=this.Events.MasterOrder=0;
  this.Doc.getElementById(this.Name).style.cursor='default';
 }
}  
function SetPaintDetail( dUrl, Barva) { 
 if( dUrl=="" || this.cx<(this.scar*2) || this.cy<(this.scar*2)) return "";
 var x1o,y1o,x2o,y2o;
 var pole=dUrl.split(",");
 var centerxo = pole[0];
 var centeryo = pole[1];
 var scaleo = pole[2];
 var cxo = pole[3];
 var cyo = pole[4];
 var dpio = pole[5];
 x1o = 1.0*(centerxo) - (1.0*(cyo/2*scaleo*0.0254/dpio));  
 y1o = 1.0*(centeryo) + (1.0*(cxo/2*scaleo*0.0254/dpio));  
 x2o = 1.0*(centerxo) + (1.0*(cyo/2*scaleo*0.0254/dpio));  
 y2o = 1.0*(centeryo) - (1.0*(cxo/2*scaleo*0.0254/dpio));  
 if( this.gauss==1) {
  x1o = 1.0*(centerxo) + (1.0*(cyo/2*scaleo*0.0254/dpio));  
  y1o = 1.0*(centeryo) - (1.0*(cxo/2*scaleo*0.0254/dpio));  
  x2o = 1.0*(centerxo) - (1.0*(cyo/2*scaleo*0.0254/dpio));  
  y2o = 1.0*(centeryo) + (1.0*(cxo/2*scaleo*0.0254/dpio));  
 }
	 
 this.dpi=dpio;
 var x1paint = this.ZpetnyPrepocetBodu( x1o, y1o, false);
 var y1paint = this.ZpetnyPrepocetBodu( x1o, y1o, true);
 var x2paint = this.ZpetnyPrepocetBodu( x2o, y2o, false);
 var y2paint = this.ZpetnyPrepocetBodu( x2o, y2o, true);

 if( x1paint < -10) x1paint = -10;
 if( y1paint < -10) y1paint = -10;
 if( x2paint < -10) x2paint = -10;
 if( y2paint < -10) y2paint = -10;
 if( x1paint > this.cx+10) x1paint = this.cx+10;
 if( y1paint > this.cy+10) y1paint = this.cy+10;
 if( x2paint > this.cx+10) x2paint = this.cx+10;
 if( y2paint > this.cy+10) y2paint = this.cy+10;
 if( Barva!=this.PaintColor)
  this.SetPaintColor(Barva);
 var sirka_lemu=this.scar;
 if( prohlizec.IE!=1) sirka_lemu=-1*this.scar;
 this.fillRect("OPA",x1paint,y1paint,x2paint-x1paint+sirka_lemu,y2paint-y1paint+sirka_lemu); 		   
 if( prohlizec.IE!=1) sirka_lemu=-2*this.scar;
 this.drawRect("POS",x1paint,y1paint,x2paint-x1paint+sirka_lemu,y2paint-y1paint+sirka_lemu); 		   
 return ""; 
}  
function SetPaintColor(Barva) {
 this.PaintColor=Barva;
 if( this.Doc.getElementById("POS").style.border!="")
  this.Doc.getElementById("POS").style.border=(this.scar+"px solid "+this.PaintColor);
 else
  this.Doc.getElementById("POS").style.color=this.PaintColor;
}
function SetAppSize(a,b) {return ""; }  
function Pozadi( backgr, Reload) {	
 if( backgr < -1) backgr = -1;		 
 if( backgr == -1)
  this.option = this.option==GCon.OPTION_SCREEN_WHITE ? (this.option |= GCon.OPTION_SCREEN_BLACK) : (this.option &= (~GCon.OPTION_SCREEN_BLACK));
 else
  this.option = backgr;
 this.CreateStrUrl();
 if( Reload == true)
 {
  this.NavalObraz();
  this.onResize();
 }
}
function FLoaded() {return this.Fload;}
function CreateStrUrl()
{
 this.option &= (~GCon.OPTION_SKUPINY);
 this.option &= (~GCon.OPTION_TEMATA_OBLASTI);
 if( (this.TypZobrazeni&GCon.SHOW_TEMATA_OBLASTI) == GCon.SHOW_TEMATA_OBLASTI)
  this.option |= GCon.OPTION_TEMATA_OBLASTI;	 
 if( (this.TypZobrazeni&GCon.SHOW_SKUPINY) == GCon.SHOW_SKUPINY)
  this.option |= GCon.OPTION_SKUPINY;	 
 this.StrDim=this.cx+","+ this.cy+","+this.dpi+","+this.option;
} 
// !!! Dodelat honorovani parametru - kresby+idbox
function GetActURL(Flg,Newcx,Newcy,NewDpi) {
 var Source="", ObjPar="", Store="", postdata="";

 if((Flg&0x02)!=0x02)
  Source = "/OUT/"+this.xUrl+"/WIN?"+this.centerx+","+this.centery+",";
 else {
  Source="/MACRO/"+this.xUrl+"/WIN/SYSPAINT?";
  if( this.IsServerConditions()!=0) Source+=("&CON="+escape(this.ServerConditions));
  if(this.Export!=""&&this.Export!="*") Source+=("&PAR="+this.Export)
  Source+="&WIN="+this.centerx+","+this.centery+",";
  Store="/STORE/"+this.xUrl+"/WIN/"+"SYSPAINT?&WIN="+this.centerx+","+this.centery+",";
  postdata=(this.IsServerConditions()!=0?("&CON="+escape(this.ServerConditions)):"");
 } // URL s tiskem značek      
switch( Flg&0x01)
{
 case 0:    
 default:		  
  ObjPar+=this.scale;
  //!!! idbox zatim ne: if(idbox!=null) ObjPar += new Double(idbox.fscale).toString(); else
  ObjPar+=(","+this.angle+","+this.cx+","+this.cy+","+this.dpi+","+this.option);
  break;																				
 case 1: 
  var xscale=this.scale;
  if((Flg&0x04)==0x04) {
   m1=this.scale*this.cx/(Newcx/25.4*this.dpi);
   m2=this.scale*this.cy/(Newcy/25.4*this.dpi);
   xscale=m1>m2?m1:m2;
  }
  ObjPar+=(xscale+","+this.angle+","+(Newcx/25.4*NewDpi)+","+(Newcy/25.4*NewDpi)+","+NewDpi+","+(this.option&(~GCon.OPTION_SCREEN_BLACK)|GCon.OPTION_PRINTER));
  break;
 } 
 if((this.TypZobrazeni&GCon.SHOW_TEMATA_OBLASTI)==GCon.SHOW_TEMATA_OBLASTI)
  ObjPar+=(","+escape(this.Temata));  
 if((this.TypZobrazeni&GCon.SHOW_SKUPINY)==GCon.SHOW_SKUPINY) 
  ObjPar+=(","+escape(this.Groups));  
 ObjPar+=(","+escape(this.Vrstvy));
 Source+=ObjPar;
 if((Flg&0x08)==0x08 && !Store=="" && !postdata=="") {
  Store+=ObjPar;
  var id=RunURL(Store,postdata);
  if( id=="0"||id=="-1")
   Source="";
  else
   Source="/STORE?"+id;	  
 }
 return Source;
}
function NavalObrazUnDo(flag) {
 var flg=0;
 if( this.IsOrient==false) {
  if(null!=this.Objects) 
   if( this.Export!="") flg=2;
  if( this.IsServerConditions()!=0) flg=2; 
 }
 this.urldata=this.GetActURL(flg,0,0,0);
 if( flag==true && this.IsOrient==false && null!=this.UnDoBuff)
  this.UnDoBuff.Insert(this.centerx,this.centery,this.scale,this.angle);
}
function NavalObraz() {
 this.NavalObrazUnDo(true);
}
function NavalPrehledku() {
 var dimx = this.WndSize(1);
 var dimy = this.WndSize(0);
 if( (dimx != this.cx || dimy != this.cy) && dimx!=0 && dimy!=0)
 {
  this.cx = dimx;   
  this.cy = dimy;
  this.CreateStrUrl();
 }
 var Source="/OUT/TXT/OVR?"+this.StrDim;
 if((this.TypZobrazeni&GCon.SHOW_TEMATA_OBLASTI)==GCon.SHOW_TEMATA_OBLASTI) 
  Source += (","+this.Temata);	  
 if((this.TypZobrazeni&GCon.SHOW_SKUPINY)==GCon.SHOW_SKUPINY)
  Source += (","+this.Groups);	  
 var InputStream=RunURL(Source,"");
 this.angle=0;
 var strarr = InputStream.split(",");
 this.centerx = strarr[0];
 this.centery = strarr[1];
 this.scale = strarr[2];
 this.gauss=0;
 this.digits=2;
 if( strarr.length>3) this.gauss=strarr[3];
 if( this.centerx == 0 && this.centery == 0 && this.scale == 0) {
  alert("Nejsou k dispozici zadna data"); // !!! dodelat poradne
 }
}
function onResize()
{
 var dimx = this.WndSize(1);
 var dimy = this.WndSize(0);
 if( (dimx != this.cx || dimy != this.cy) && dimx!=0 && dimy!=0)
 {
  this.cx = dimx;   
  this.cy = dimy;
  this.CreateStrUrl();
  if( this.IsOrient == true) 
   this.NavalPrehledku();	 
  this.NavalObraz();	  
 }
 // Timhle se nejdrive nastretchuje stary obraz
 this.Doc.getElementById(this.Name).style.height=this.WndSize(0);
 this.Doc.getElementById(this.Name).style.width=this.WndSize(1);
 // a tady se dotahne novy
 this.Doc.getElementById(this.Name).src=this.urldata;
}
function SetGrfFormat(fmt)
{
 this.GrfFormat=1*fmt;	  
 if( fmt > 2 || fmt<0)
  this.GrfFormat=0;	  
 switch( this.GrfFormat) {
 case 0:
 default:this.xUrl="GIF"; break; 	  
 case 1: this.xUrl="JPG"; break; 	  
 case 2: this.xUrl="PNG"; break; 	  
 } 
}
function WndSize(flg) {
 var docwidth=640,docheight=480;
 try {
  if (parent[this.Fram].innerWidth || parent[this.Fram].innerHeight) {
   docwidth = parent[this.Fram].innerWidth; 
   docheight = parent[this.Fram].innerHeight;
  }
  if (this.Doc.body.clientWidth || this.Doc.body.clientHeight) {
   docwidth = this.Doc.body.clientWidth; 
   docheight = this.Doc.body.clientHeight;
  }
 } catch(e){}
 if( !flg) return docheight;
 else return docwidth;
}
function PrepocetBodu(x, y, flg) {
 var vx=x-(this.cx*0.5);
 var vy=y-(this.cy*0.5);
 var VX,VY;
 if(this.gauss==1) {
  VX=((-1*vy)/this.dpi)*0.0254*this.scale;
  VY=(vx/this.dpi)*0.0254*this.scale;
  } else {
   VX=(vy/this.dpi)*0.0254*this.scale;
   VY=((-1*vx)/this.dpi)*0.0254*this.scale;
  }
  var s=Math.sin(this.angle);
  var c=Math.cos(this.angle);
  var X=((1*this.centerx)+((VX*c)+(VY*s)));
  var Y=((1*this.centery)+((-1*VX*s)+(VY*c)));
  return((flg==false)?X:Y);
}
function ZpetnyPrepocetBodu(X, Y, Flg) 
{
 var VX,VY, vx,vy;
 var s=Math.sin(this.angle);
 var c=Math.cos(this.angle);
 VX=((X-(1*this.centerx))*c)-((Y-(1*this.centery))*s);
 VY=((X-(1*this.centerx))*s)+((Y-(1*this.centery))*c); 
 if (this.gauss==1) {
  vy=(-1*VX*this.dpi)/0.0254/(this.scale>0?this.scale:0.1);
  vx=(VY*this.dpi)/0.0254/(this.scale>0?this.scale:0.1);
 } else {
  vy=(VX*this.dpi)/0.0254/(this.scale>0?this.scale:0.1);
  vx=(-1*VY*this.dpi)/0.0254/(this.scale>0?this.scale:0.1);
 }
 return ((Flg==false)?(vx+this.cx*0.5):(vy+this.cy*0.5));
}
function drawRect(id,x,y,w,h) {
 //if( x<0 || y<0 || w<0 || h<0) return;
 if( w<0) w=0; if(h<0) h=0;
 this.mkDiv(id,x,y,w,h);
 this.Doc.getElementById(id).style.backgroundColor="";
 this.Doc.getElementById(id).style.border=(this.scar+"px solid "+((id=="POS")?this.PaintColor:"cyan"));
}
function fillRect(id,x, y, w, h) {
 //if( x<0 || y<0 || w<0 || h<0) return;
 if( w<0) w=0; if(h<0) h=0;
 this.mkDiv(id,x,y,w,h);
 this.Doc.getElementById(id).style.border=0;
 this.Doc.getElementById(id).style.backgroundColor="gray";
}
function clearRect(id) {
 this.mkDiv(id,0,0,0,0);
 this.Doc.getElementById(id).style.border=0;
 this.Doc.getElementById(id).style.backgroundColor="";
}
function mkDiv(id,x,y,w,h) {
 this.Doc.getElementById(id).style.left=x;
 this.Doc.getElementById(id).style.top=y;
 this.Doc.getElementById(id).style.width=w;
 this.Doc.getElementById(id).style.height=h;
}
function IsServerConditions() { 
 if( !this.ServerConditions==("")) return 1;		 
 return 0; 
}
function AddServerCondition(Condition)
{
 if( !this.ServerConditions=="") 
  this.ServerConditions+="\x1e";		 
 this.ServerConditions+=Condition;
 return;	
} 
function DeleteServerConditions() { this.ServerConditions=""; } 
function GetServerConditions() { return this.ServerConditions; } 
function mm2Px(mm) { return mm/25.4*((null!=screen.deviceXDPI)?screen.deviceXDPI:120.0); } 
function cnstr_events(_parent) {
 this.MasterAction=0;
 this.MasterFlag=0;
 this.MasterScale=0;
 this.MasterOrder=0;
 this.SlaveAction=0;    
 this.SlaveOrder=0;
 this.InfoName="";
 this.IdeLights="";
 this.Hexlist="";
 this.Attribs="";
 this.IdePrep="";
 this.TypIdent=0;
 this.refresh=0;
 this.mp1X=0;
 this.mp1Y=0;
 this.mp2X=0;
 this.mp2Y=0;
 this.mpsX=0;
 this.mpsY=0;
 this.sp1X=0;
 this.sp1Y=0;
 this.IdentPasport=0;
 this.ResetSlaveAction=function ResetSlaveAction(){
  this.SlaveAction=0;
  this.SlaveOrder=0;
  this.sp1X=this.sp1Y=0; 
  _parent.Doc.getElementById("GRF").style.cursor='auto';
 }
}
// Konstruktor objektu Grafika
function cnstr_grafika(item_name,frame_name,dokument) {
 // Globální proměnné a funkce
 this.Fload=0;
 this.Name=item_name;
 this.Doc=dokument;
 this.IsOrient=false;
 this.Fram=frame_name;
 this.GrfFormat=-1;     // !!! Později bude objekt Cookie
 this.FLoaded=FLoaded;
 this.Click=Click;
 this.Detail=Detail;
 this.SetPaintDetail=SetPaintDetail;
 this.SetAppSize=SetAppSize;
 this.Pozadi=Pozadi;
 // Vnitřní proměnné a funkce
 this.cx=0;
 this.cy=0;
 this.centerx=0;
 this.centery=0;
 this.scale=0;
 this.angle=0;
 this.gauss=0;
 this.digits=2;
 this.dpi=(null!=screen.deviceXDPI)?screen.deviceXDPI:120.0;
 this.option=0; 
 this.StrDim=""; 
 this.TypZobrazeni=GCon.SHOW_TEMATA_OBLASTI; 
 this.Temata=""; 
 this.DefTemata=""; 
 this.Groups=""; // !!! podle pravidel se skupinami
 this.Vrstvy=""; // !!! dodelat podle vrstev, zatim vsechny
 this.xUrl="GIF";
 this.urldata="/OUT/"+this.xUrl+ "/LOGO";
 this.scar=3;
 this.PaintColor="red";
 this.ServerConditions="";
 this.Events=new cnstr_events(this);
 this.IsServerConditions=IsServerConditions;
 this.AddServerCondition=AddServerCondition;
 this.DeleteServerConditions=DeleteServerConditions;
 this.GetServerConditions=GetServerConditions;
 this.CreateStrUrl=CreateStrUrl;
 this.NavalObraz=NavalObraz;
 this.NavalObrazUnDo=NavalObrazUnDo;
 this.NavalPrehledku=NavalPrehledku;
 this.onResize=onResize;
 this.WndSize=WndSize;
 this.SetGrfFormat=SetGrfFormat;
 this.PrepocetBodu=PrepocetBodu;
 this.drawRect=drawRect;
 this.fillRect=fillRect;
 this.clearRect=clearRect;
 this.mkDiv=mkDiv;
 this.SetPaintColor=SetPaintColor;
 this.ZpetnyPrepocetBodu=ZpetnyPrepocetBodu;
 this.GetActURL=GetActURL;
 this.mm2Px=mm2Px;
 this.Objects=null;
}
