/************************************************************************
Script: ChaseIt
Author: Caveman
Copyright: © 2008 Caveman (www.cavemanconclusion.com)
URL: http://www.cavemanconclusion.com

Usage: ChaseIt(this,event,[count of times to move],[width boundary],[height boundary], 'your msg in quotes')

Image useage: <img src="image.jpg" onmouseover="ChaseIt(this,event,40,1000,700,'Haha, you missed it')">

DIV useage: <div style="width:100px;height:100px;" onmouseover="ChaseIt(this,event,40,1000,700,'fooled ya!')"><img src="image.jpg"></div>

The count, width, height, and msg are not required and have defaults of:

count = 10
width = 1000
height = 700
msg = "Ok, I guess I'll let ya click it."

So, to use defaults simply don't supply those parameters... 
	
	ChaseIt(this,event)

Note: This script automatically gets the width and height if using an image. If using a div, you must set the width and height using css.

Disclaimer:  Use at your own risk.  I take no responsibility for anything.
*************************************************************************/

/* Globals, do not edit */
var up = 0;
var down = 0;
var left = 0;
var right = 0;
var allowclick = 0;
var count = 0;
var offset = 20;

function ChaseIt(o,e,i,maxLeft,maxTop,msg)
{
  e.cancelBubble = true;

  i = isNaN(i) ? 10 : i;
  maxLeft = isNaN(maxLeft) ? 1000 : maxLeft;
  maxTop = isNaN(maxTop) ? 700 : maxTop;

  if(count < i)
  {
	var oWidth;
	var oHeight;
	//Get ready to move it
  	o.style.position = 'absolute';

	//Get width of element
	if(o.width != undefined)
	{
		oWidth = o.width;
	}
	else if(isNaN(parseInt(o.style.width)) == false)
	{
		oWidth = parseInt(o.style.width);
	}
	else
	{
		alert('You gotta add the width dude!');
		return false;
	}

	//Get height of element
	if(o.height != undefined)
	{
		oHeight = o.width;
	}
	else if(isNaN(parseInt(o.style.height)) == false)
	{
		oHeight = parseInt(o.style.height);
	}
	else
	{
		alert('You gotta add the height dude!');
		return false;
	}
	
	//Get cursor pos
  	var x = 0;
  	var y = 0;
	if (document.all) 
	{
    		x = event.clientX + document.body.scrollLeft;
    		y = event.clientY + document.body.scrollTop;
	} 
	else 
	{
    		x = e.pageX;
    		y = e.pageY;
	}
	
	//Move it to rand loc
	var randLeft = Math.round((maxLeft - oWidth) * Math.random())
	var randTop = Math.round((maxTop - oHeight) * Math.random())
	o.style.left = randLeft + 'px';
	o.style.top = randTop + 'px';

	//Adjust for boundaries			
	if((parseInt(o.style.left) + oWidth) > maxLeft)
	{
		o.style.left = maxLeft - oWidth - offset + 'px';
	}
	else if(parseInt(o.style.left) < 0)
	{
		o.style.left = oWidth + offset + 'px';
	}

	if((parseInt(o.style.top) + oHeight) > maxTop)
	{
		o.style.top = maxTop - oHeight - offset + 'px';
	}
	else if(parseInt(o.style.top) < 0)
	{
		o.style.top = oHeight + offset + 'px';
	}
	count++;
  }
  else if(allowclick == 0)
  {
	allowclick = 1;
	if(msg == undefined)
		alert("Ok, I guess I'll let ya click it.");
	else
		alert(msg);
  }

}