// Virtual exhibition JavaScript.
// Version ITE
// Copyright by Kruglov S.A. 2000, 2001 (http://www.kruglov.ru)
// Do not use this software without author agreement!

// This in NOT a free software!

var ready=false
var loadedAll=false
var zoom=1
var border=1

var cssBuffer=""
var htmlBuffer=""
var cssSubBuffer=""
var htmlSubBuffer=""

var len=data.length

var j=0
var images="images/" // path to image directory

var spacer="images/spacer.gif"

preloadImage(spacer)

var id=new Array(len)
var descr=new Array(len)
var color=new Array(len)
var notEmpty=new Array(len)
var type=new Array(len)
var coords=new Array(len)
var logo=new Array(len)
var standText=new Array(len)

var visited=new Array(len)
var info=new Array(len)

function makeArrays(){
  var i,j,k
  var string
  var string2
  var Cookie=getCookie("VISIT-STATUS-"+expocodename+"-"+pavilion())
  //var Zoom=getCookie("ZOOM")
  
  var Zoom=getVariable("zoom")
  
  //alert(Zoom)
  
  if(Zoom!=0 && Zoom!=""){
	zoom=parseFloat(Zoom)
  }

  for(i=0;i<data.length;i++){
     data[i]=myunescape(""+data[i]);
     
     info[i]=false
     id[i]=""
     descr[i]=""
     notEmpty[i]=""
     type[i]=""
     coords[i]=new Array()
     logo[i]=""

     if(Cookie==0 || Cookie=="" || Cookie.indexOf("none")==0){
        visited[i]=false
     }else{
		if(Cookie.charAt(i)=='0')
	        visited[i]=false
		else
	        visited[i]=true

     }

     if(!visited[i])
	     color[i]=2
	else
	     color[i]=3

     string=data[i].split("|")

     for(j=0;j<string.length;j++){
        if(j==0)
	    id[i]=string[j]
	if(j==1)
	    descr[i]=string[j]
	if(j==2)
	    notEmpty[i]=string[j]
	if(j==3)
	    type[i]=string[j]
	if(j==4){
	    string2=string[j].split(",")
	    for(k=0;k<string2.length;k++){
			coords[i][k]=parseInt(string2[k])*zoom
	    }
	    for(k=0;k<coords[i].length;k+=4){
	    	coords[i][k]+=xOffset*zoom;
	    	coords[i][k+1]+=yOffset*zoom;
	    }
	}
	if(j==5)
	    logo[i]=string[j]
      }
   }

   if(single){
	setCookie("CURRENT",pavilionId,0)
   }else{
	setCookie("CURRENT",pavilionsQuantity+1,0)
   }

   pageLeft=-(width*zoom)/2+getWindowWidth()/2
   pageTop=-(height*zoom)/2+getWindowHeight()/2

   if(pageLeft<0)pageLeft=0
   if(pageTop<0)pageTop=0

}

function cssWrite(s){
	cssSubBuffer+=s
	l=cssSubBuffer.length
	if(l>2048){
		cssBuffer+=cssSubBuffer
		cssSubBuffer=""
	}
}

function cssWriteln(s){
	cssWrite(s+'\n');
}

function cssFlush(){
	cssBuffer+=cssSubBuffer
	cssSubBuffer=""
}

function htmlWrite(s){
	htmlSubBuffer+=s
	l=htmlSubBuffer.length
	if(l>2048){
		htmlBuffer+=htmlSubBuffer
		htmlSubBuffer=""
	}
}

function htmlWriteln(s){
	htmlWrite(s+'\n')
}

function htmlFlush(){
	htmlBuffer+=htmlSubBuffer
	htmlSubBuffer=""
}

function printCSS(Id,X,Y,Dx,Dy,Class,Number){
	cssWrite("#s"+Id+Class+Number+" {")
	cssWrite("left:"+(X+pageLeft)+";")
	cssWrite("top:"+(Y+pageTop)+";")
	cssWrite("width:"+Dx+";")
	cssWrite("height:"+Dy+"")

	cssWriteln("}")
}

function printRect(N,X,Y,Dx,Dy,Class,Number,IsId){

	var Id=id[N]

	printCSS(Id,X,Y,Dx,Dy,Class,Number)

	htmlWrite('<div class="'+Class+'" id="s'+Id+Class+Number+'">')
	if(Class=="stand"){
		htmlWrite('<img src='+spacer+' width='+Dx+' height='+Dy+' name="c'+Id+Number+'">')
	}else if(Class=="under"){
		if(isNC4){
		htmlWrite('<table cellspacing=0 cellpadding=0 border=0 bgcolor="'+colors[0]+'"><tr><td><img src='+spacer+' width='+Dx+' height='+Dy+'></td></tr></table>')
		}else{
		htmlWrite('<div style="background-color:'+colors[0]+'"><img src='+spacer+' width='+Dx+' height='+Dy+'></div>')
		}
	}else if(Class=="id" && IsId){
		if(!single){
			Id=descr[N]
		}else{

			if(logo[N]!=""){
			  // privileged user with logotype on stand.
			  if(Dx<22*zoom || Dy<14*zoom){
			    Id="*"
			  }else{
			    Id=logo[N]
			  }

			}else{
			  if(Dx<Id.length*3.5*zoom || Dy<8*zoom){
			    Id='*'
			  }
			}

		        if(isNC4){
		        	if(notEmpty[N]!=""){
			        	Id='<font color="'+privileged+'"><b>'+Id+'</b></font>'
        			}else{
			        	Id='<font color="'+fgcolors[2]+'">'+Id+'</font>'
        			}
        		}
			
                }
                
		if(zoom>=4){
			standText[N]='<small class=big>'+Id+'</small>'
		}else if(zoom>=2){
			standText[N]='<small>'+Id+'</small>'
		}else{
			standText[N]='<small class=verysmall>'+Id+'</small>'
		}
		
		htmlWrite(standText[N])

	}else if(Class=="spacer"){
	   var href
	   var target
 	   if(single){
		// what to do if stand clicked...
			href=''+standFile+'?lan='+lan+'&exhibition='+expocodename+'&pavilion='+pavilionId+'&stand='+Id+''
			if(infoTarget.charAt(0)!='_'){
				target="search"
			}else if(infoTarget=="_blank"){
		 		target="_blank"
			}else
				target="_top"
	    }else{
			href=("plan.html?lan="+lan+"&exhibition="+expocodename+"&pavilion="+Id)
			target=""
    	   }
	   htmlWrite('<a href="'+href+'" '+((target=='')?(''):('target="'+target+'"'))+' onclick="if(isIE){this.blur()}; return press(\''+Id+'\')"><img src='+spacer+' width='+Dx+' height='+Dy+' border=0 alt=""></a>')

	}
	htmlWrite('</div>')
}

function printStand(n){
	if(type[n]=='R'){ // if plain rectangle
	    if(single){
		printRect(n,coords[n][0]+border,coords[n][1]+border,coords[n][2]-border,coords[n][3]-border,"stand",0,true)
		printRect(n,coords[n][0],coords[n][1],coords[n][2]+border,coords[n][3]+border,"under",0,true)
		printRect(n,coords[n][0]+border,coords[n][1]+border,coords[n][2]-border,coords[n][3]-border,"id",0,true)
            }
	    printRect(n,coords[n][0],coords[n][1],coords[n][2]+border,coords[n][3]+border,"spacer",0,true)

	}else if(type[n]=='P'){ // if polyrectangle

		var leftIndent
		var topIndent
		var rightIndent
		var bottomIndent
		var i,j

		var BL=0; // most bottom left rectangle

		for(i=4;i<coords[n].length;i+=4){
			if(coords[n][i+1]>coords[n][BL+1] ||
			   (coords[n][i+1]==coords[n][BL+1] &&
			    coords[n][i]<coords[n][BL])){
			    BL=i;
			}
		}

		for(i=0;i<coords[n].length;i+=4){
		    leftIndent=1
		    topIndent=1
		    rightIndent=1
		    bottomIndent=1

		    for(j=0;j<coords[n].length;j+=4){
			if(i==j)continue // sam s soboj?
			if(coords[n][i+0]==coords[n][j+0]+coords[n][j+2] &&
			   coords[n][i+1]==coords[n][j+1]){
				// esli slewa wprityk estx blok
				// (schitaem, chto bloki wpisany w tablicu).
				leftIndent=0
			}

			if(coords[n][i+0]+coords[n][i+2]==coords[n][j+0] &&
			   coords[n][i+1]==coords[n][j+1]){
				// esli sprawa wprityk estx blok
				rightIndent=0
			}

			if(coords[n][i+1]==coords[n][j+1]+coords[n][j+3] &&
			   coords[n][i+0]==coords[n][j+0]){
				// esli swerhu wprityk estx blok
				topIndent=0
			}

			if(coords[n][i+1]+coords[n][i+3]==coords[n][j+1] &&
			   coords[n][i+0]==coords[n][j+0]){
				// esli snizu wprityk estx blok
				bottomIndent=0
			}

		    }
		if(single){
		    printRect(n,coords[n][0+i]+border*leftIndent,coords[n][1+i]+border*topIndent,coords[n][2+i]-border*leftIndent-border*rightIndent+1,coords[n][3+i]-border*topIndent-border*bottomIndent+1,"stand",i/4,true)

		    printRect(n,coords[n][0+i],coords[n][1+i],coords[n][2+i]+border,coords[n][3+i]+border,"under",i/4,true)
		    if(i==BL){
			printRect(n,coords[n][0+i]+border,coords[n][1+i]+border,coords[n][2+i]-border,coords[n][3+i]-border,"id",i/4,true)
		    }
                 }
		 printRect(n,coords[n][0+i],coords[n][1+i],coords[n][2+i]+border,coords[n][3+i]+border,"spacer",i/4,true)
		}
	}
}

function getInfo(n){
    var infobuffer=""
	infobuffer+=('<table border=0 cellspacing=0 cellpadding=0 width=150><tr><td bgcolor="'+infofgcolor+'">')
	infobuffer+=('<table border=0 cellspacing=1 cellpadding=3 width="100%"><tr><td bgcolor="'+infocolor+'">')

	var Id=id[n]
	if(notEmpty[n]!=""){
		Id='<font color="'+privileged+'"><b>'+id[n]+'</b></font>'
	}else{
		Id='<font color="'+infofgcolor+'">'+id[n]+'</font>'
	}

		if(zoom>=4){
			infobuffer+=('<span class=big>')
		}else{
			infobuffer+=('<span>')
		}

	var description='<font color="'+infofgcolor+'">'+descr[n]+'</font>'
	
	if(single && descr[n]!=""){
		infobuffer+=(Id+'&nbsp;<font color="'+infofgcolor+'">-</font> '+description)
	}else{
		infobuffer+=description
	}
	infobuffer+="</span>";
	
	infobuffer+=('</td></tr></table>')
	infobuffer+=('</td></tr></table>')
	return infobuffer
}

function printInfo(n){
    //html:
	htmlWrite('<div class="descr" id="s'+id[n]+'info">')
	htmlWrite(getInfo(n))
	htmlWriteln('</div>')
    //css:
   	cssWrite("#s"+id[n]+"info {")
	cssWrite("width:"+150)
	cssWriteln("}")
}


function printNavigation(){
	var current=parseInt(0+""+getVariable("pavilion"))
	
	var buffer=""
	buffer+='<table border=0><tr><td nowrap><small><font color="'+fgcolor+'">Павильоны:</font> &nbsp;</small></td>'

	var iii,jjj
	var fileName
	var linkName
	
	// bublsort!!!!
	var xxx
	for(iii=0;iii<pavilionsQuantity-1;iii++){
		for(jjj=iii;jjj<pavilionsQuantity;jjj++){
			if(parseInt(navdata[iii].split("|")[0]) > parseInt(navdata[jjj].split("|")[0])){
				xxx=navdata[iii];
				navdata[iii]=navdata[jjj];
				navdata[jjj]=xxx;
			}
		}
	}
	
	for(iii=0;iii<=pavilionsQuantity;iii++){
	    buffer+="<td nowrap><small>"
	    if(iii==0){
		fileName="plan.html?lan="+lan+"&exhibition="+expocodename
		linkName="<font color=\""+fgcolor+"\">Все</font>"
	    }else{
		fileName="plan.html?lan="+lan+"&exhibition="+expocodename+"&pavilion="+iii
		linkName="<font color=\""+fgcolor+"\">"+myunescape(navdata[iii-1].split("|")[1])+"</font>"
	    }
	    if(iii!=current){
		buffer+='<a class=menu href="'+fileName+'">'
	    }else{
	        buffer+="<b>"
	    }

    	buffer+=linkName

    	if(iii!=current){
		buffer+="</a>"
    	}else{
	        buffer+="</b>"
    	}
    
    	buffer+="&nbsp;</small></td>"
    }
    
    buffer+='<td nowrap><small>&nbsp; &nbsp; <font color="'+fgcolor+'">Zoom:</font> </td>'

	var zooms=new Array();
	zooms[0]=1;
	zooms[1]=2;
	zooms[2]=4;
	for(iii=0;iii<zooms.length;iii++){

		buffer+='<td nowrap><small>'+((zoom!=zooms[iii])?'<a href=\"'+(location.href.split("?")[0])+"?lan="+lan+"&exhibition="+expocodename+((pavilionId<=pavilionsQuantity)?"&pavilion="+pavilionId:"")+"&zoom="+zooms[iii]+'\">':'')+'<font color="'+fgcolor+'">'+Math.floor(100*zooms[iii])+'%</font>'+((zoom!=zooms[iii])?'</a>':'')+'</small></td>'
	}
    
	buffer+='</tr></table>'
	
/*
if(isLive){
	buffer+='<td><a href="javascript:routeStart()">&gt;&gt;</a></td>'
	buffer+='<td><a href="javascript:routeStop()">[]</a></td>'
}
*/

	buffer+="</tr></table>"
	return buffer
}

function printPlan(){

    htmlWrite("<title>"+myunescape(exponame)+"</title>")

    if(getCookie("isLive")!="true"){
		layer("wait").show()
	}

	document.bgColor=bgcolor

	htmlWrite('<div id=pointer class=pointer><img src="/images/pointer.gif"></div>')

	for(i=0;i<len;i++){
		printStand(i)
	    if(isNC4){
	        printInfo(i)
	    }
	}

	htmlWrite('<img src="'+spacer+'" width="'+(parseInt(width*zoom))+'" height="'+(parseInt(height*zoom)+70)+'">')
	
	if(bgimage!=""){
		htmlWrite('<div id=sbg class=bg><img src="expo/'+bgimage+'" width="'+parseInt(width*zoom)+'" height="'+parseInt(height*zoom)+'"></div>')
	}
	
        htmlWrite('<div class=loading id=bottom style="visibility: hidden">'+printNavigation()+'</div>')

	printCSS("bg",0,0,width,height,"","");


	cssFlush()
	document.writeln("<style type=text/css>")
	document.write(cssBuffer)
	document.writeln("</style>")
	
	htmlFlush()
	layer("wait").hide()
	
	if(getCookie("isLive")!="true"){
		layer("wait2").show()
	}

	document.write(htmlBuffer)
	
	layer("wait2").hide()

	var Lbottom=layer("bottom")
	Lbottom.move(width/2*zoom-Lbottom.getWidth()/2,height*zoom+16);
	Lbottom.show()
	colorize()
	filter()
	
	ready=true
}

function setColor(m,Color){
    var k
    for(k=0;k<coords[m].length;k+=4){
    	var L=layer("s"+id[m]+"stand"+(k/4))
    	if(L.isExist()) {
		L.setBgColor(colors[Color])
		if(isIE || isNC6){
			var I=layer("s"+id[m]+"id"+(k/4))
			if (I.isExist()){
				I.write('<font color="'+fgcolors[Color]+'">'+standText[m]+'</font>') // fgcolors
			}
		}
    	}
	color[m]=Color
    }
}

function colorize(){
    for(i=0;i<len;i++){
	setColor(i,color[i])
    }
}

function highLight(m,channel){
    if(visited[m]){
	setColor(m,6+(channel-1)*3)
    }else{
	setColor(m,5+(channel-1)*3)
    }
}

function lowLight(m){
    if(color[m]==5 || color[m]==6 || color[m]==7){
	setColor(m,color[m]-3)
    }
    if(color[m]==8 || color[m]==9 || color[m]==10){
	setColor(m,color[m]-6)
    }
}

function showInfo(k){
    var name
    
    if(isNC4){
      for(i=0;i<len;i++){
	    if(i!=k)hideInfo(k)
      }
      name="s"+id[k]+"info"
    }
    
    if(isIE || isNC6){
      name="info"
    }
  
  	var L=layer(name)
  
  	if(!L.isExist()) {
  		alert("WRONG LAYER: "+L.path)
  		return
  	}
  
    var newx=mousex-L.getWidth()/2
    var newy=mousey+16

/* это нам пока не нужно, мы пока маленькие :)
    if(getCookie("isLive")=="true"){
      if(coords[k]){
      	newx=coords[k][0]+5
      	newy=coords[k][1]+45
      	var X=layer("pointer")
      	X.move(newx-5,newy-40)
      	X.show()
      }
    }
*/
    if(newx<getScrollX()){
	  newx=getScrollX()
    }

    if(newx+L.getWidth()>getScrollX()+getWindowWidth()){
	  newx=getScrollX()+getWindowWidth()-L.getWidth()
    }

    if(newy<getScrollY()){
	  newy=getScrollY() // :))))))
    }

    if(newy+L.getHeight()>getScrollY()+getWindowHeight()){
	  newy=getScrollY()+getWindowHeight()-L.getHeight()
    }
 
 
    if(getCookie("isLive")=="true"){
/* это нам пока не нужно, мы пока маленькие :)
    	L.move(newx-5,newy-19)
*/
    }else{
    	L.move(newx-pageLeft,newy-pageTop)
    }

    
    if(isIE || isNC6){
    	L.write(getInfo(k))
    }
    
    setTimeout("layer('"+name+"').show()",10)
    info[k]=true
}

function hideInfo(k){
    if(!info[k])return
    forceHideInfo(k)
}

function forceHideInfo(k){
    if(isNC4){
      var name="s"+id[k]+"info"
    }else{
      var name="info"
    }
    
    layer(name).hide()
    layer("pointer").hide()
    info[k]=false
}

function mouseHighLighted(k){
    return (color[k]==4 || color[k]==7 || color[k]==10)
}

function mouseLowLighted(k){
    return (color[k]==2 || color[k]==3 || color[k]==5 || color[k]==6 || color[k]==8 || color[k]==9)
}

function isRed(k){
    return (color[k]==8 || color[k]==9 || color[k]==10)
}

function mouseHighLight(k){
    if(mouseHighLighted(k))return
    if(!visited[k]){
		color[k]+=2
    }else{
		color[k]+=1
    }
    setColor(k,color[k])
    showInfo(k)
}

function mouseLowLight(k){
    if(mouseLowLighted(k))return
    if(visited[k]){
		color[k]-=1
    }else{
		color[k]-=2
    }
    setColor(k,color[k])
    hideInfo(k)
}

function filter(){
  var stands=new Array(3)
  stands[1]=getCookie("FILTER1-"+expocodename+"-"+pavilion())
  stands[2]=getCookie("FILTER2-"+expocodename+"-"+pavilion())
  var j,channel,i

  for(j=0;j<len;j++){
    if(isRed(j) && (""+stands[2]).charAt(0)!='-') lowLight(j)
    if((""+stands[2]).charAt(0)=='-')lowLight(j)
  }

  for(channel=1;channel<=2;channel++){

    if((""+stands[channel]).charAt(0)=='-' || stands[channel]==0 ){
	continue
    }

    var str=stands[channel].split(' ')
    var first=-1

    for(i=0;i<str.length;i++){
	for(j=0;j<len;j++){
	    if(id[j]==str[i]){
                if(i==0)first=j
		highLight(j,channel)
		if(channel==2){
			visited[j]=true
			saveVisitStatus()
		}
	    }
	}
    }
    if(first!=-1 && (channel==2 || (channel==1 && (""+stands[2]).charAt(0)=='-'))){
    	scrollTo(first)
    	if(getCookie("isLive")=="true"){
    		showInfo(first)
    	}	
    }
  }
}

function saveVisitStatus(){
    var Cookie=""
    for(k=0;k<len;k++){
		if(visited[k]){
	    	Cookie+='1'
		}else{
	    	Cookie+='0'
		}
    }
    setCookie("VISIT-STATUS-"+expocodename+"-"+pavilion(),Cookie,1000)
}

function press(Id){
    var k
    if(isNC4){
    	for(k=0;k<len;k++){
        	forceHideInfo(k)
     	}
    }
    
    if(isIE || isNC6){
    	forceHideInfo(Id)
    }
    
    for(k=0;k<len;k++){       
		if(Id==id[k]){
	    	visited[k]=true
	//	    mouseHighLight(k)
		}
    }
    saveVisitStatus()

    if(single){
	// what to do if stand clicked...
	if(k<len){
		showInfo(k)
	}
    }
    
    if(!isToolbar && single){
	   var href,nw1
	   href=''+standFile+'?lan='+lan+'&exhibition='+expocodename+'&pavilion='+pavilionId+'&stand='+Id+''
	   nw1=window.open(href,infoTarget,"menubar=false,width=400,height=400,resizable=yes,scrollbars=yes")
	   nw1.focus()
	   return false
    }
    return true
}

function clearHistory(Id){
    var Cookie=""
    var k
    for(k=0;k<len;k++){
        mouseLowLight(k)
        Cookie+='0'
		if(visited[k]){
	    	visited[k]=false;
	    	color[k]--;
        }
    }
    setCookie("VISIT-STATUS-"+expocodename+"-"+pavilion(),Cookie,0)
    colorize();
}

// on screen or out of screen
function isOutOfScreen(m){
    var k
    for(k=0;k<coords[m].length;k+=4){
	if(getScrollX()>coords[m][k+0] || getWindowWidth()+getScrollX()<coords[m][k+0]+coords[m][k+2] ||
	   getScrollY()>coords[m][k+1] || getWindowHeight()+getScrollY()<coords[m][k+1]+coords[m][k+3]){
	   return true
	}
    }
    return false
}

function scrollTo(m){
    if(!isOutOfScreen(m))return

    scroll(coords[m][0]-getWindowWidth()/3,coords[m][1]-getWindowHeight()/3)
}

function isMouseOver(m){
    var k
    for(k=0;k<coords[m].length;k+=4){
		if(mousex>coords[m][k+0]+pageLeft && mousex<coords[m][k+0]+coords[m][k+2]+pageLeft &&
	   		mousey>coords[m][k+1]+pageTop && mousey<coords[m][k+1]+coords[m][k+3]+pageTop){
	   		return true
		}
    }
    return false
}

function mouseOn(){
    var k
    for(k=0;k<len;k++){
		if(isMouseOver(k))return k
    }
    return -1
}

function run(){
  if(ready && loadedAll){
     var k
     for(k=0;k<len;k++){
	 if(isMouseOver(k)){
	      mouseHighLight(k)
        }else{
          mouseLowLight(k)
	   }
     }
  }
  setTimeout("run()",200)
}

run()

