/*
Moving icons plugin for C00L GuestMap 2.0

makes icons move around on the map

INSTALLATION

1. Embed in the <head> tag below the other JavaScript on page with the guestmap
2. Add initMove(); to the onload attribute in the <body> tag
3. Change the values of the variables to your how you like it

*/

//Setting this too low might cause performance problems (depending on the number of icons)
//for faster movement, change the direction.
var interval = 200; //milliseconds between each movement. 

//mimimum pixels in direction X, 
//both POSITIVE: icons move RIGHT
//both NEGATIVE: icons move LEFT
//difference between numbers: random speed
//same numbers: no random speed
var minDirX = -3; 
var maxDirX = 3; 

//direction on Y axis, same as for X, only with UP and DOWN.
var minDirY = -3; 
var maxDirY = 3;

var changeInterval = 12; //set lower for more changes in direction. Set to 0 for no changes

var dirX=[];
var dirY=[];
var moveIcon;

function initMove() {

	setSpeeds();
	startIcons();

	document.getElementById('mapButtons').innertHTML += '<li><a href="">Movement</a></li>';
}

function setSpeeds() {
	for(var i = 0; i < icon.length; i++) {
		dirX[i] = Math.round(Math.random() * (maxDirX - minDirX))+minDirX;
		dirY[i] = Math.round(Math.random() * (maxDirY - minDirY))+minDirY;
	}
}

function startIcons() {

	setTimeout("shiftIcons()", interval);
}

function shiftIcons() {
	
	var oldPosX;
	var oldPosY;
	var newPosX;
	var newPosY;

	for(var i = 0; i < icon.length; i++) {
	
		moveIcon = document.getElementById('emo'+i);

		oldPosX = getPixels(moveIcon.style.left);
		oldPosY = getPixels(moveIcon.style.top);

		newPosX = oldPosX+dirX[i];
		newPosY = oldPosY+dirY[i];

		if(dirX[i] < 0 && newPosX < 0) {
			newPosX = map.width - moveIcon.scrollWidth;
		} else if(dirX[i] > 0 && newPosX > map.width) {
			newPosX = 0;
		}

		if(dirY[i] < 0 && newPosY < 0) {
			newPosY = map.height - moveIcon.scrollHeight;
		} else if(dirY[i] > 0 && newPosY > map.height) {
			newPosY = 0;
		}

		moveIcon.style.left = newPosX+'px';
		moveIcon.style.top = newPosY+'px';

		if(Math.round(Math.random() * changeInterval) == 1) {
			dirX[i] = Math.round(Math.random() * (maxDirX - minDirX))+minDirX;
			dirY[i] = Math.round(Math.random() * (maxDirY - minDirY))+minDirY;
		}
	}
	startIcons();
}