
var map = null;
var Cur_Point = 1;
var x=0;            
var Max_Dist2 = 0.0065 * 0.0065;
var Dist2 = 0.0;
var pan = 0;
var Delay = 2000;
// Slider script by Ronald H. Jankowsky (http://rj-edv-beratung.de ), parts of code (draglayer, movelayer) by DynamicDrive.com
// This script is free for use, please leave this notice intact
var sPosition;
var showPerc=100;
var newPos = -1;
document.onmousedown=dragLayer;
var helpScreen;
var i;
var j;
var Pics = [ "",
"LockPlan.gif", 
"LockPlan1.gif", 
"Aqueduct.gif", 
"Bridge.gif", 
"Tunnel.gif" ];

function AddLine()
{
var poly = null;
var tenPoints = new Array();
var tPoint;
var y;

	j=0;
    i=0;
	for ( j=0, i=-4 ; i<=4 ; i++)
	{
		if ( Cur_Point + i >= 0 && Cur_Point + i < (points.length) )
	  	{
		  	y = i+Cur_Point;		
		  	tenPoints[j++] = points[y].LatLong;
		}		
    }	
	
	poly = new VEPolyline("Line 1",tenPoints);
   	poly.SetWidth(4);
    poly.SetColor(new VEColor(255,0,255,0.25));

    map.AddPolyline(poly);
}
function AddPin(i)
      {
		var x="Pin "+i;
      	var pin = new VEPushpin(x, 
                        points[i].LatLong,
                        "../MapCode/" + Pics[points[i].LockFlag],
                        Towns[points[i].TownIdx],
                        null,
                        'iconStyle',
		                'titleSytle',
        		        'detailsStyle' );

        map.AddPushpin(pin);
     }

function Speed_Up()
{
	if ( Delay > 300 )
	{
		Delay = Delay - 300;
	}
	else
	{
		Delay = 0;
	}
}
function Slow_Down()
{
	Delay = Delay + 300;
}
function StartPan()
{
	pan = 1;
	PanToNext()
}
function StopPan()
{
	pan = 0;
}

function PanToNext()
{
	if ( newPos != -1 )
	{
		Cur_Point = newPos;
		newPos = -1;
		map.DeleteAllPolylines();
		map.PanToLatLong(points[Cur_Point].LatLong);
		AddLine();
 		window.setTimeout(PanToNext, Delay );

	}
	else
	{
		Dist2 = 0.0;
		y = points[Cur_Point].LatLong.Latitude;
		x = points[Cur_Point].LatLong.Longitude;
		while ( Dist2 < Max_Dist2 & Cur_Point < points.length )
		{
			Cur_Point = Cur_Point+1;
			
			if ( points[Cur_Point].LockFlag > 0 ) break;

			y = y - points[Cur_Point].LatLong.Latitude;
			x = x - points[Cur_Point].LatLong.Longitude;

			Dist2 = Dist2 + x*x + y*y;


		}
	map.DeleteAllPolylines();
	map.SetCenter(points[Cur_Point].LatLong);
	AddLine();
	document.getElementById("place_name").value = Towns[points[Cur_Point].TownIdx];

	document.getElementById("knobImg").style.pixelTop = 2 + (580-2)*((Cur_Point/(points.length-1))   ) ;	
	}
} 	  
function GetMap()
{
	Cur_Point = 0;

	helpScreen = document.getElementById("maphelp");
//	helpScreen.style.visibility="visible";

	map = new VEMap('myMap');

	start = new VELatLong();
	start = points[0].LatLong;

	map.LoadMap(start, 16 ,'h' , false);
	map.HideDashboard();
	AddLine();
	for ( i=0 ; i<points.length-1 ; i++ ) 
		if ( points[i].LockFlag > 0 ) AddPin(i);
	
	Cur_Point = 0;
	document.getElementById("place_name").value =  Towns[points[Cur_Point].TownIdx];

	map.AttachEvent("onendcontinuouspan", function(e) 
	{

		if ( pan > 0 )
		{
	 		window.setTimeout(PanToNext, Delay );
		}
	}
	)
}   



// Drag and move engine (original code by DynamicDrive.com ), don't change unless explicitely indicated
var dragMe=false, kObj, yPos, dragPos;
function moveLayer(mEvent) {
	var element;

	if (document.all)
	{
		mEvent = event;
		element = event.srcElement;
	}
// Netscape and Firefox
	else if (mEvent.target)
	{
		element = mEvent.target;
	}
	if ( element.className == "drag" )
	{
		if (dragMe) 
		{
			var oldY = parseInt(kObj.style.top); 
			newY=temp2+mEvent.clientY-yPos; 
		
// Limit movement of knob to stay inside layer

			if (newY < 5 ) {newY=5; }
			if (newY > 582 ) {newY=582; }

			kObj.style.top=newY; 

// Set working variable 'showPerc' depending on 100 or 250 scaling
			sPosition=newY; showPerc = showPerc = (sPosition-5)/(580); 

			newPos = Math.ceil(showPerc * (points.length-1));
			if ( newPos != dragPos )
			{
				document.getElementById("place_name").value =  Towns[points[newPos].TownIdx];
				dragPos = newPos;
			}
			return false; 
		}
	}
	else
	{
		resetPos();
		dragMe = false;
		document.onmousemove = null;
	}
}



function dragLayer(mEvent) 
{
	if ( dragMe == true )
	{
		resetPos();
		dragMe=false;
		document.onmousemove = null;
	}
	else
	{	
// Internet Explorer
		mEvent = mEvent || window.event;

		if (document.all)
		{
		    if (event.button==1 && event.srcElement.className=="drag")	{dragMe=true; kObj=event.srcElement; temp2=kObj.style.pixelTop; 
		    yPos=event.clientY; document.onmousemove=moveLayer; }
		}
// Netscape and Firefox
		else if (mEvent.target)
		{
    		if (mEvent.button==0 && mEvent.target.className=="drag") {dragMe=true; kObj=mEvent.target; temp2=parseInt(kObj.style.top); 
	    	yPos=mEvent.clientY; document.onmousemove=moveLayer; }
	    }
	    dragPos = Cur_Point;
	}
}
function resetPos() {

	if (dragMe)	
	{
//        newPos = Math.ceil(showPerc * (points.length-1));
//		document.getElementById("place_name").value =  Towns[points[newPos].TownIdx];
		newPos = dragPos;
		if ( pan == 0 )
	    {
		    Cur_Point = newPos;
			newPos = -1;
			map.DeleteAllPolylines();
			map.PanToLatLong(points[Cur_Point].LatLong);
			AddLine();
		}
	}
	dragMe=false;
}
