//
//  ICG Javascript Library
//  ----------------------
//

var myMovingMenu;
var f0Id = 'f0';

// constructor for class MovingMenu 
function MovingMenu()
{
	// private members (can only be accessed by private and privileged methods)
	
	var test = 0;
	
	var N = 6; // N = number of layers
	var dragging;
	var fps = 25;
	var dx, dy, dr;
	
	var f = new Array( 'f0', 'f1', 'f2', 'f3', 'f4', 'f5', 'f6' );
	var px = new Array( 0, 0, 0, 0, 0, 0, 0 );
	var py = new Array( 0, 0, 0, 0, 0, 0, 0 );
	var vx = new Array( 0, 0, 0, 0, 0, 0, 0 );
	var vy = new Array( 0, 0, 0, 0, 0, 0, 0 );
	var ax = new Array( 0, 0, 0, 0, 0, 0, 0 );
	var ay = new Array( 0, 0, 0, 0, 0, 0, 0 );

	var vmax = 20;
	var rmin = 40;
	var k = 20;
	var j = 10000;
	var d = 0.98;
	var j = 80000;
	var d = 0.96;
	
	var mouseX, mouseY, offsetX, offsetY, obj;
	
	// public members
	/*
	this.xyz = 4711;
	*/
	
	/* no - start with fix initial position 
	px[ 0 ] = Math.random() *300 + 150;
	py[ 0 ] = Math.random() *200 + 100;
	*/
	px[ 0 ] = 500;
	py[ 0 ] = 400;
	
	for ( i = 1; i <= N; i++ )
	{
		px[ i ] = Math.random() * 600;
		py[ i ] = Math.random() * 400;
	}
	
	// private methods (can only be accessed by privileged methods)
	/*
	function xyz()
	{
	}
	*/
	
	// privileged methods
	this.positionsInit = function()
	{
		if ( document.all )
		{
			xmax = window.width;
			ymax = window.height;
		}
		else
		{
			if ( document.layers || document.getElementById )
			{
				xmax = window.innerWidth;
				ymax = window.innerHeight;
			}
		}
		
		for ( n = 0; n <= N; n++ )
		{
			this.moveIt( f[ n ], px[ n ], py[ n ] );
		}
	}

	this.movefs = function()
	{
		dt = 1 / fps; // dt is in seconds!
	
		// find f0 position
		if ( document.layers )
		{
			px[ 0 ] = document.layers.f0.left;
			py[ 0 ] = document.layers.f0.top;
		}
		else
			if ( document.all )
			{
				// need parseInt to remove 'px'
				px[ 0 ] = parseInt( document.all.f0.style.left );
				py[ 0 ] = parseInt( document.all.f0.style.top );
			}
			else
				if ( document.getElementById )
				{
					px[ 0 ] = parseInt( document.getElementById( f0Id ).style.left );
					py[ 0 ] = parseInt( document.getElementById( f0Id ).style.top );
				}
		// rule one - follow the f0
		for ( n = 1; n <= N; n++ )
		{
			dx = px[ 0 ] - px[ n ];
			dy = py[ 0 ] - py[ n ];
			dr = Math.sqrt( dx * dx + dy * dy );
			if ( ( dr < 1 ) && ( dr >= 0 ) )
				dr = 1;
			if ( ( dr > -1 ) && ( dr <= 0 ) )
				dr = -1;
			vx[ n ] += k * dx / dr;
			vy[ n ] += k * dy / dr;
			vx[ n ] *= d;
			vy[ n ] *= d;
		}
		// rule two - avoid each other
		for ( n = 0; n <= N; n++ )
		{
			for ( m = n + 1; m <= N; m++ )
			{
				dx = px[ m ] - px[ n ];
				dy = py[ m ] - py[ n ];
				dr = Math.sqrt( dx * dx + dy * dy );
				if ( ( dr < 1 ) && ( dr >= 0 ) )
					dr = 1;
				else
					if ( ( dr > -1 ) && ( dr <= 0 ) )
						dr = -1;
				if ( ( dx < 1 ) && ( dx >= 0 ) )
					dx = 1;
				else
					if ( ( dx > -1 ) && ( dx <= 0 ) )
						dx = -1;
				if ( ( dy < 1 ) && ( dy >= 0 ) )
					dy = 1;
				else
					if ( ( dy > -1 ) && ( dy <= 0 ) )
						dy = -1;
				//vx[n]=50*k*dx/dr;
				//vy[n]=50*k*dy/dr;
				vx[ n ] += -j * dx / ( dr * dr * dr );
				vy[ n ] += -j * dy / ( dr * dr * dr );
				vx[ m ] += j * dx / ( dr * dr * dr );
				vy[ m ] += j * dy / ( dr * dr * dr );
			}
		}
		// plot positions
		for ( n = 1; n <= N; n++ )
		{
			px[ n ] += vx[ n ] * dt;
			py[ n ] += vy[ n ] * dt;
			
			this.moveIt( f[ n ], px[ n ], py[ n ] );
		}
		next = setTimeout( "myMovingMenu.movefs()", 1000 / fps ); // timeout in millisecs
	}
	
	// ---------------------------------------------------
	
	this.moveIt = function( id, x, y )
	{
		if ( document.getElementById )
		{	// IE5, NS6, DOM
			var d = document.getElementById( id );
			d.style.left = x + 'px';
			d.style.top = y + 'px';
		}
		else
		{
			if ( document.all )
			{		// IE4
				var d = document.all[ id ];
				d.style.left = x;
				d.style.top = y;
			}
			else
			{
				if ( document.layers )
				{	// NS4
					document.layers[ id ].moveTo( x, y );
				}
			}
		}
	}
	
	this.getLeft = function( id )
	{
		if ( document.getElementById )
			return document.getElementById( id ).style.left;
		else
			if ( document.all )
				return document.all[ id ].style.left;
			else
				if ( document.layers )
					return document.layers[ id ].left;
	}
	
	this.getTop = function( id )
	{
		if ( document.getElementById )
			return document.getElementById( id ).style.top;
		else
			if ( document.all )
				return document.all[ id ].style.top;
			else
				if ( document.layers )
					return document.layers[ id ].top;
	}
	
	// -------------------------------------------
	
	this.mouseDown = function( e )
	{
		// new: move f0 to mouse cursor
		if ( dragging == false )
		{
			px[ 0 ] = mouseX;
			py[ 0 ] = mouseY;
		}
		
		offsetX = mouseX - px[ 0 ];
		offsetY = mouseY - py[ 0 ];
		// dragging = true;
		dragging = !dragging;
		
		return true;
	}
	
	this.mouseUp = function( e )
	{
		// dragging = false;
		return true;
	}
	
	this.mouseMove = function( e )
	{
				
		if ( document.all )
		{
			mouseX = event.x;
			mouseY = event.y + document.body.scrollTop;
		}
		else
		{
			if ( document.layers || document.getElementById )
			{
				mouseX = e.pageX;
				mouseY = e.pageY;
			}
		}
		
		
		if ( dragging )
		{
			if ( !document.all && document.layers )   
			{
				// netscape spinnt? Wenn obj verschoben wird, wird mousedown nicht erkannt?
				// Daher der bloede workaraound hier, dass alle 5 mousemoves kein Verschieben erfolgt
				// Dann geht oefter mal auch das Beenden des Verschiebens mit einem Klick...
			
				test++;
				
				if ( test < 5 )
				{
					obj.left = mouseX - offsetX;
					obj.top = mouseY - offsetY;
				}
				else
				{
					if ( test > 10 )
					{
						test = 0;
					}
				}
			}
			else
			{
				obj.left = mouseX - offsetX;
				obj.top = mouseY - offsetY;
			}
			
			return false;
		}
		return true;
	}
	
	this.dragInit = function()
	{
		if ( document.all )
		{
			// nothing to do
		}
		else
		{
			if ( document.layers || document.getElementById )
			{
				document.captureEvents( Event.MOUSEDOWN | Event.MOUSEMOVE | Event.MOUSEUP );
			}
		}
		
		document.onmousedown = this.mouseDown;
		document.onmousemove = this.mouseMove;
		document.onmouseup = this.mouseUp;
	
		if ( document.layers )
			obj = document.layers.f0;
		else
			if ( document.all )
				obj = document.all.f0.style;
			else
				if ( document.getElementById )
					obj = document.getElementById( f0Id ).style;
	
		dragging = false;
	}
	
	return this;
}

// public methods  (none used here)
/* 
MovingMenu.prototype.xyz = function (string) 
{
	return this.member + string;
}
*/




// ----------------------------------------------------------

function movingMenuExists()
{
	var activateMovingMenu = false;
	
	if ( document.getElementById ) // IE5, NS6, DOM
	{	
		if ( document.getElementById( f0Id ) )
		{
			activateMovingMenu = true;
		}
	}
	else
	{
		if ( document.all ) // IE4
		{		
			if ( document.all[ f0Id ] )
			{
				activateMovingMenu = true;
			}
		}
		else
		{
			if ( document.layers ) // NS4
			{	
				if ( document.layers[ f0Id ] )
				{
					activateMovingMenu = true;
				}
			}
		}
	}
	
	return activateMovingMenu;
}

// ----------------------------------------------------------

function init()
{
	if ( movingMenuExists() )
	{
		myMovingMenu = new MovingMenu();
	
		myMovingMenu.positionsInit();
		myMovingMenu.dragInit();
		myMovingMenu.movefs();
		
		window.status = "Klicken schaltet Verschiebung ein bzw. aus...";
	}
}
