var waterfalls = new Array;
var waterfallover="";
var currentfall=0;
var cellx=57;
var celly=34;
var mapx=0;
var mapy=0;
var initx=0;
var inity=0;
var pointx;
var pointy;
var state;
var debug=0;
var is_ns=0;
var lasttime=0;
var lastfall=-1;
var ulx;
var uly;
var thediv;
var scrolltimer;
var scrollcount;
var scrolling;
var explodecount;
var mousetype=false;
var currentdir=0;
var cellprefix;
var mapwidth;
var mapheight;
var tourtime;
var tourspeed=200;
var zoomlevel=0;
var offsetx=350;
var offsety=350;

function zoom(z)
{
//	alert(z);
	if (isNaN(z))
	{
		z=0;
	}
	if (z!=zoomlevel && !isNaN(z))
	{
		l=waterfalls[currentfall].left*(1<<z)/(1<<zoomlevel);
		t=waterfalls[currentfall].top*(1<<z)/(1<<zoomlevel);
		n=waterfalls[currentfall].page;
		clearInterval(scrolltimer);
		thediv.style.visibility="hidden";
		document['zoom'+zoomlevel].src='zbutton.jpg';
		document['zoom'+z].src='zredbutton.jpg';
		mapheight=Math.floor(mapheight*(1<<z)/(1<<zoomlevel));
		mapwidth=Math.floor(mapwidth*(1<<z)/(1<<zoomlevel));
		cx=cellx*100-mapx+offsetx;
		cy=celly*100-mapy+offsety;
		cx=cx*(1<<z)/(1<<zoomlevel);
		cy=cy*(1<<z)/(1<<zoomlevel);
		cellx=Math.floor((cx-offsetx)/100);
		celly=Math.floor((cy-offsety)/100);
		mapx=-((cx-offsetx)-100*cellx);
		mapy=-((cy-offsety)-100*celly);
		thediv.style.left=Math.round(mapx);
		thediv.style.top=Math.round(mapy);
		zoomlevel=z;
		var obj=document.getElementById("mainmap");
		cellprefix=("scrolling/"+obj.getAttribute("mapname"))+zoomlevel+"/";
//		alert(cellprefix);
		load_data();
		updatecells();
		thediv.style.visibility="visible";
		m=0;
		for (i=0; i<waterfalls.length; ++i)
		{
			if (waterfalls[i].page == n)
			{			
				currentfall=i;
				break;
			}
			else if (i==0 || (waterfalls[i].left-l)*(waterfalls[i].left-l)+
				(waterfalls[i].top-t)*(waterfalls[i].top-t)< d)
			{
				m=i;
				d= (waterfalls[i].left-l)*(waterfalls[i].left-l)+
				(waterfalls[i].top-t)*(waterfalls[i].top-t);
			}
		}
		if (i>=waterfalls.length)
		{
			currentfall=m;
		}
	}
}

function waterfall(left,right,top,bottom,name,page,image,next,prev)
{
	this.left=left;
	this.right=right;
	this.top=top;
	this.bottom=bottom;
	this.name=name;
	this.page=page;
	this.image=image;
	this.next=next;
	this.prev=prev;
}

function updatefall(n)
{
	document.body.style.cursor = "pointer";
	waterfallover=waterfalls[n].page;
	if (n!=lastfall)
	{
		lastfall=n;
		currentfall=n;
		document['currentwaterfall'].src="../images/thumbs/"
			+waterfalls[n].image;
		document.getElementById('fallimage').border="2";
		obj = document.getElementById('waterfallname'); 
		obj.innerHTML="<b>"+waterfalls[n].name+"</b>";
	}
}

function explode()
{
	if (explodecount)
	{
		document.getElementById('ex'+explodecount).style.visibility='hidden';
	}
	++explodecount;
	if (explodecount==6)
	{
		clearInterval(scrolltimer);
		updatefall(currentfall);
		scrolling=0;
		nextwaterfall(currentdir);
	}
	else
	{
		document.getElementById('ex'+explodecount).style.visibility='visible';
	}
}

function movemap(dx , dy, l)
{
	t=new Date;
	scrolling=1;
	mapx+=dx;
	mapy+=dy;
	updatemap();
	if (++scrollcount>=l)
	{
		currentfall=currentdir ? waterfalls[currentfall].prev : 
			waterfalls[currentfall].next;
		clearInterval(scrolltimer);
		if (waterfalls[currentfall].page=="")
		{
			nextwaterfall(currentdir);
		}
		else
		{
			explodecount=0;	
			for (i=1; i<=5; ++i)
				document['ex'+i].src='../images/thumbs/'+waterfalls[currentfall].image;
	
			scrolltimer=setInterval('explode()',50);
		}
	}
	else 
	{
		if (t.getTime()-tourtime>100)
		{
			tourspeed=Math.round(tourspeed/2)+2;
			clearInterval(scrolltimer);
			scrolltimer=setInterval('movemap('+dx+','+dy+','+l+')',tourspeed);
		}
		else if (t.getTime()-tourtime<80)
		{
			tourspeed=Math.round(tourspeed*1.5);
			clearInterval(scrolltimer);
			scrolltimer=setInterval('movemap('+dx+','+dy+','+l+')',tourspeed);
		}
		tourtime= t.getTime();
	}
}

function nextwaterfall(dir)
{
	if (dir!=currentdir)
	{
		currentdir=dir;
		if (currentdir)
		{
			document['cwbutton'].src='cwbutton2.jpg';
			document['ccwbutton'].src='ccwbutton.jpg';
		}
		else
		{
			document['cwbutton'].src='cwbutton.jpg';
			document['ccwbutton'].src='ccwbutton2.jpg';
		}
	}
	x=cellx*100-mapx+offsetx;		// mapwidth/2+100
	y=celly*100-mapy+offsety;		// mapwidth/2+100
	nextfall=currentdir ? waterfalls[currentfall].prev : waterfalls[currentfall].next;
	nextx=(waterfalls[nextfall].left
			+waterfalls[nextfall].right)/2;
	nexty=(waterfalls[nextfall].top
			+waterfalls[nextfall].bottom)/2-50;
	distance=Math.sqrt((nextx-x)*(nextx-x)+(nexty-y)*(nexty-y));
	numsteps=Math.floor(distance/5)+1;
	if (numsteps<50)
		numsteps=50;

	dx=-(nextx-x)/numsteps;
	dy=-(nexty-y)/numsteps;
	speed2=200;
	scrollcount=0;
	scrolling=0;
	var p=new Image(300,200);
	p.src='../images/thumbs/'+waterfalls[nextfall].image;
	t=new Date;
	tourtime= t.getTime();
	clearInterval(scrolltimer);
	scrolltimer=setInterval('movemap('+dx+','+dy+','+numsteps+')',tourspeed);
}

function updatecells()
{
	for (j=0; j<8; ++j)
	{
		for (i=0; i<8; ++i)
		{
			var cell="cell"+((j+inity)%8)+""+((i+initx)%8);
			img=cellprefix+(celly+j)+"/"+(cellx+i)+".jpg";
			if (celly+j < 0 || cellx+i <0 || cellx+i>=mapwidth
					|| celly+j >=mapheight)	
				img="transparent.gif";
			document[cell].src=img;
// 			var obj = document.getElementById(cell); 
		}
	}
}


function updatemap()
{
	var i;
	var j;

	k=0;
	while (mapx>0 && cellx>=-2 && k<10)
	{
		++k;
		if (cellx>=-2)
		{
			mapx-=100;
			for (i=0;i<8;++i)
			{
				for (j=initx;((j+1)%8)!=(initx%8);j=j+1)
				{
					var cell="cell"+i+""+(j%8);
					var obj = document.getElementById(cell);
					obj.style.left=100*(j-initx)+"px";			
				}
				var cell="cell"+((i+inity)%8)+""+((initx+7)%8);
				var obj = document.getElementById(cell);
				if ((cellx-1>=mapwidth || cellx-1<0 || celly+i<0 || 
						celly+i>=mapheight))
					img="transparent.gif";
				else
					img=cellprefix+(celly+i)+"/"+(cellx-1)+".jpg";
				document[cell].src=img;
				obj.style.left="-100";
			}
			--cellx;
			initx=(initx+7)%8;
		}
	}
	k=0;
	while (mapx<-100 && cellx<mapwidth-6 && k<10)
	{
		++k;
		if (cellx<mapwidth-6)
		{
			var i;

			for (i=0;i<8;++i)
			{
				for (j=initx+1;(j%8)!=(initx%8);j=j+1)
				{
					var cell="cell"+i+""+(j%8);
					var obj = document.getElementById(cell);
					obj.style.left=100*(j-initx-2);			
				}
				var cell="cell"+((i+inity)%8)+""+((initx)%8);
				var obj = document.getElementById(cell);

				obj.style.left=800;
				img=cellprefix+(celly+i)+"/"+(cellx+8)+".jpg";
				if (cellx+8>=mapwidth || celly+i>=mapheight || cellx+8<0
					|| celly+i<0)
					img="transparent.gif";
				document[cell].src=img;
			}

			mapx+=100;
			++cellx;
			++initx;
		}
	}
	if (mapx>0)
		mapx=0;
	if (cellx==mapwidth-6)
		mapx=0;
	k=0;
	while (mapy>0 && celly && k<10)
	{
		++k;
		if (celly)
		{
			mapy-=100;
			for (i=0;i<8;++i)
			{
				for (j=inity;((j+1)%8)!=(inity%8);j=j+1)
				{
					var cell="cell"+(j%8)+""+i;
					var obj = document.getElementById(cell);
					obj.style.top=100*(j-inity)+"";			
				}
				var cell="cell"+((inity+7)%8)+""+((i+initx)%8);
				var obj = document.getElementById(cell);
				img=cellprefix+(celly-1)+"/"+(cellx+i)+".jpg";
				if (cellx+i>=mapwidth || celly-1>=mapheight 
						|| celly-1<0 || cellx+i<0)
					img="transparent.gif";
				document[cell].src=img;
				obj.style.top="-100";
			}
			--celly;
			inity=(inity+7)%8;
		}
		else
		{
			mapy=0;
		}
	}
	k=0;
	while (mapy<-100 && celly<mapheight-3 && k<10)
	{
		++k;
		if (celly<mapheight-3)
		{	
			var i;

			for (i=0;i<8;++i)
			{
				for (j=inity+1;(j%8)!=(inity%8);j=j+1)
				{
					var cell="cell"+(j%8)+""+i;
					var obj = document.getElementById(cell);
					obj.style.top=100*(j-inity-2);			
				}
				var cell="cell"+((inity)%8)+""+((i+initx)%8);
				var obj = document.getElementById(cell);

				obj.style.top=800;
				var img=cellprefix+(celly+8)+"/"+(cellx+i)+".jpg";
				if (celly+8>=mapheight || cellx+i>=mapwidth || cellx+i
					>=mapwidth || cellx+i<0)
					img="transparent.gif";
				document[cell].src=img;
			}

			mapy+=100;
			++celly;
			++inity;
		}
		else
			mapy=-100;
	}
	if (celly>=mapheight-3)
		mapy=0;

	thediv.style.left=Math.round(mapx);
	thediv.style.top=Math.round(mapy);
//	thediv.style.width=500;
}

function kdown(e)
{
    d=0;
    if ( window.event )
    {
    k=event.keyCode&0xff;
    if (event.keyCode==39 || k==3)
        d=1;
    else if (event.keyCode==37 || k==2)
        d=2;
    else if (event.keyCode==38 || k==0)
        d=3;
    else if (event.keyCode==40 || k ==1)
        d=4;
    }
    else
   {
    k=e.keyCode&0xff;
    if (e.keyCode==39 || k==3)
        d=1;
    else if (e.keyCode==37 || k==2)
        d=2;
    else if (e.keyCode==38 || k==0)
        d=3;
    else if (e.keyCode==40 || k ==1)
        d=4;
    }
    if ( d== 1 )
    {
        mapx=mapx-4;
        updatemap();
        return false;
    }
    else if ( d==2 )
    {
        mapx=mapx+4;
        updatemap();
        return false;
    }
    else if ( d==3 )
    {
        mapy=mapy+4;
        updatemap();
        return false;
    }
    else if ( d== 4)
   {
        mapy=mapy-4;
        updatemap();
        return false;
    }
 
    return false;
}

function mdown(e)
{
	if (window.event)
	{
		theX=event.x;
		theY=event.y;
	}
	else
	{
		theX=e.pageX;
		theY=e.pageY;
	}

	var currentTime= new Date;
	if (theX>500+ulx || theX<ulx || theY>500+uly || theY<uly)
		return true;
	if (scrolling)
	{
		clearInterval(scrolltimer);
		if (explodecount && explodecount<6)
			updatefall(currentfall);
		for (i=1; i<=5; ++i)
			document.getElementById('ex'+i).style.visibility='hidden';
		scrolling=0;
	}
	if (waterfallover!="" && (currentTime.getTime()-lasttime)<500)
	{
		lasttime=currentTime.getTime();
		document.location.href="../"+waterfallover;
	}
	else
	{
		lasttime=currentTime.getTime();
		pointx=theX;
		pointy=theY;
		mapx=Math.floor(mapx);
		mapy=Math.floor(mapy);
		document.body.style.cursor = "move"
		state=1;
		return false;
	}
}
/*
function mresize(e)
{
	var obj=document.getElementById("mainmap");
	m=Math.floor((document.body.clientWidth-290)/20)*20;
	n=Math.floor((document.body.clientHeight-50)/20)*20;
	if (m>500)
		m=500;
	if (m<300)
		m=300;
	if (n>500)
		n=500;
	if (n<300)
		n=300;
	offsetx=(m/2)+100;
	offsety=(n/2)+100;
	obj.style.clip="rect(0,"+m+","+n+",0)";
	document.getElementById("text").style.left=m+15;
	document.getElementById("frame").style.width=m+8;
	document.getElementById("frame").style.height=n+8;
	n=document.body.clientWidth-(m+15);
	document.getElementById("text").style.width=n>300 ? n : 300;
	alert(e);
}
*/

function mup(e)
{
	if (window.event)
	{
		theX=event.x;
		theY=event.y;
	}
	else
	{
		theX=e.pageX;
		theY=e.pageY;
	}
	if (state)
	{
		state=0;
		document.body.style.cursor = "auto"
		mapx=mapx+(theX-pointx);
		mapy=mapy+(theY-pointy);
		updatemap();
	}
}

function mmove(e)
{
        if (window.event)
	{
		theX=event.x;
		theY=event.y;
	}
	else
	{
		theX=e.pageX;
		theY=e.pageY;
	}
	if (state)
	{
		mapx=mapx+(theX-pointx);
		mapy=mapy+(theY-pointy);
		updatemap();
		pointx=theX;
		pointy=theY;
//		document.debugform.x.value=mapx+" "+mapy;
	
		return false;
	}
	else if (!scrolling && theX>=+ulx && theX<=500+ulx && 
			theY>=uly && theY<=500+uly)
	{
		var realx=cellx*100-mapx+theX+100-ulx;
		var realy=celly*100-mapy+theY+100+50-uly;
		found=0;
		f=0;
		l=waterfalls.length-1;
//		document.debugform.x.value=mapx+" "+mapy;
//		document.debugform.x.value=theX+" "+theY+" "+realx+" "+realy+" "+cellx+" "+celly +" "+mapx+" "+mapy;
		while (f<=l && found==0)
		{
			mid=Math.floor((f+l)/2);
			if (debug)
				alert("Mid ="+mid+" Left="+waterfalls[mid].left+" X="
						+realx);

//			if (realx>6380 && realx<6390)
//				alert(mid);

//			document.debugform.y.value=mid;
			if (realx<waterfalls[mid].left)
			{
				// check falls to left
				for (j=mid-1; j>=0 && !found && realx<waterfalls[j].right; 
						--j)
				{
					if (realx>=waterfalls[j].left && realy>=waterfalls[j].top && 
						realy<=waterfalls[j].bottom)
					{
						updatefall(j);	
						found=1;
					}
				}
				l=mid-1;
			}
			else
			{
				if (realx<=waterfalls[mid].right && 
					realy>=waterfalls[mid].top && realy<=waterfalls[mid].bottom)
				{
					updatefall(mid);	
					found=1;
				}
				f=mid+1;
			}
		}
		debug=0;
		if (!found && f>=waterfalls.length-1)
		{
//			alert(realx+","+f+","+(waterfalls.length-1));
			var j=waterfalls.length-1;
//			alert(waterfalls[j].right+","+j);
			for (; j>=0 && !found && 
					realx<waterfalls[j].right; --j)
			{
//				alert(j);
				if (realx>=waterfalls[j].left && realy>=waterfalls[j].top && 
					realy<=waterfalls[j].bottom)
				{
					updatefall(j);	
					found=1;
				}
			}
		}
		if (!found && waterfallover !="")
		{
			document.body.style.cursor = "auto";
			waterfallover="";
//			document['currentwaterfall'].src="next.gif";
		}
	}
	return true;
}

function load_data()
{
	var obj=document.getElementById("mainmap");
	var xml;

	xml=getRequest();

	xml.open("GET","scrolling/"+obj.getAttribute("mapname")+zoomlevel+"/data",false);
	xml.send(null);
	lines=xml.responseText.split('\n');

	i=0;
	waterfalls.length=0;
 	for (i=0; i<lines.length-1; ++i)
	{
		data=lines[i].split(';');
		waterfalls[i]=new waterfall(parseInt(data[0]),parseInt(data[1]),
			parseInt(data[2]),parseInt(data[3]),data[4],data[5],
			data[6],parseInt(data[7]),parseInt(data[8]));
    }
}

function getRequest()
{
	var request;
	if ( window.XMLHttpRequest )
	{
		request = new XMLHttpRequest(); 
	}
	else
	{
		try 
		{
			request = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e)  
		{
			try 
			{
				request = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch (E) 
			{
				document.location.href='scrollinglakesuperiormap2.shtml';
			}
		}            
	}
	return request;
}

function initMap()
{
//	alert(Math.floor((document.body.clientWidth-290)/100)*100);
	is_ns=(navigator.vendor=="Firefox" || navigator.vendor=="Netscape" || navigator.vendor =="Camino");
	
	var obj=document.getElementById("mainmap");
//	alert(obj.style.clip);
	if (obj.getAttribute("zoom"))
		zoomlevel=obj.getAttribute("zoom");
	mapwidth=parseInt(obj.getAttribute("mapwidth"));
	mapheight=parseInt(obj.getAttribute("mapheight"));
	if (location.search != "")
	{
		params=location.search.substr(1,location.search.length-1).split('&');
 		for (i=0; i<params.length; ++i)
		{
			data=params[i].split('=');
			if (data[0]=="fall")
			{
				name=data[1];
			}
			else if (data[0]=="zoom")
			{
				if (data[1]>=0 && data[1]<=3)
				{
					z=parseInt(data[1]);
					if (isNaN(z))
					{
						z=0;
					}
					mapheight=Math.floor(mapheight*(1<<z)/(1<<zoomlevel));
					mapwidth=Math.floor(mapwidth*(1<<z)/(1<<zoomlevel));
					document['zoom'+zoomlevel].src='zbutton.jpg';
					document['zoom'+z].src='zredbutton.jpg';
					zoomlevel=z;
				}
			}
			else
				name=data[0];	
		}
	}
	else
	{
		r=document.referrer.split("/waterfalls/");
		l=getRequest();

		l.open("GET","maplocation.php3?page="+r[1],false);
		l.send(null);
		data=l.responseText.split(",");
  //              alert(data);
data="*";
		if (data[0]=="*" || data[0]=="")
                {
 //                       alert("This area is outside the scope of the tour map");
			name=obj.getAttribute("start");
                }
		else
		{
			name=data[0];
			z=parseInt(data[1]);
			if (isNaN(z))
			{
				z=0;
			}
			if (z>=0 && z<=3)
			{
				mapheight=Math.floor(mapheight*(1<<z)/(1<<zoomlevel));
				mapwidth=Math.floor(mapwidth*(1<<z)/(1<<zoomlevel));
				document['zoom'+zoomlevel].src='zbutton.jpg';
				document['zoom'+z].src='zredbutton.jpg';
				zoomlevel=z;
			}
			else
				name=obj.getAttribute("start");
		}	
	}
	ulx=parseInt(document.getElementById("mainmap").style.left)+
		parseInt(document.getElementById("cc").style.left)+
		parseInt(document.getElementById("theBackground").style.left);
	uly=parseInt(document.getElementById("mainmap").style.top)+
		parseInt(document.getElementById("cc").style.top)+
		parseInt(document.getElementById("theBackground").style.top);
 	thediv = document.getElementById("map3"); 
	document.getElementById('text').style.width=
		document.body.clientWidth-520>300 ? 
		document.body.clientWidth-520 : 300;

	cellprefix=("scrolling/"+obj.getAttribute("mapname"))+zoomlevel+"/";
//	alert(cellprefix);

	load_data();

	if (name != "")
	{
		for (i=0; i<waterfalls.length && waterfalls[i].page != name+".shtml"; ++i);
//		alert(waterfalls[i].page);
		if (i<waterfalls.length)
		{
			cellx=Math.floor((waterfalls[i].left+waterfalls[i].right)/200)-3;
			celly=Math.floor(waterfalls[i].bottom/100)-3;
			mapx=(100*(cellx+3))-Math.floor((waterfalls[i].left+waterfalls[i].right)/2)+50;
			if (mapx>0) mapx=0;
			thediv.style.left=mapx;
			thediv.style.top=mapy;
			updatefall(i);
		}
	}
	updatecells();
}


document.onmousedown=mdown;
document.onmousemove=mmove;
document.onmouseup=mup;
document.onkeydown=kdown;
document.onkeypress=kdown;
//window.onresize=mresize;
onload=initMap;

