var xMove = Speed;
var yMove = Speed;

var xStart = xPos;
var yStart = yPos;

var xEnd = xPosEnd;
var yEnd = yPosEnd;

var Running = BounceAdRunning; 

var moveToEnd = false;



function calculatePosition(moveToEnd) {

	if(xDir == "right") {
	
		if(xPos > (xMax - Width - xMove)) {
		
			xDir = "left";
		
		}
	
	} else if(xDir == "left") {
	
		if(xPos < (0 + xMin + xMove)) {
		
			xDir = "right";
		
		}
	
	}
	
	
	
	if(yDir == "down") {
	
		if(yPos > (yMax - Height - yMove)) {
		
			yDir = "up";
		
		}
	
	} else if(yDir == "up") {
	
		if(yPos < (0 + yMin + yMove)) {
		
			yDir = "down";
		
		}
	
	}
	
	
	
	if(moveToEnd == true) {
	
		if(xEnd > xPos) {
		
			xDir = "right";
		
		} else {
		
			xMove = 0;
		
		}
		
		if(yEnd < yPos) {
		
			yDir = "up";
		
		} else {
		
			yMove = 0;
		
		}
		
	
	}
	
		
	
	if(xDir == "right") {
	
		xPos = xPos + xMove;
	
	} else if(xDir == "left") {
	
		xPos = xPos - xMove;
	
	}
	
	if(yDir == "down") {
	
		yPos = yPos + yMove;
	
	} else if(yDir == "up") {
	
		yPos = yPos - yMove;
	
	}

}



function BounceAd() {

	if(Running == true) {
	
		counter++;
	
		if(counter == maxCounter) {
	
			moveToEnd = true;
	
		}

		calculatePosition(moveToEnd);
		
		if(document.all) {
		
			document.all("bouncead").style.left = xPos + document.body.scrollLeft;
			document.all("bouncead").style.top = yPos + document.body.scrollTop;
		
		} else if(document.layers) {
		
			document.layers["bouncead"].left = xPos + pageXOffset;
			document.layers["bouncead"].top = yPos + pageYOffset;
		
		} else if(document.getElementById) {
		
			document.getElementById("bouncead").style.left = xPos + pageXOffset;
			document.getElementById("bouncead").style.top = yPos + pageYOffset;
		
		}
		
		setTimeout("BounceAd()", 30);		
	
	}

}



function initializeBounceAd() {

	if(document.all) {
	
	
		if(xMax == 0) {
		
			xMax = document.body.clientWidth;
		
		}
		
		if(yMax == 0) {
		
			yMax = document.body.clientHeight;
		
		}
		
		document.all("bouncead").style.visibility = "visible";
	
	} else if(document.layers || document.getElementById) {
	
		if(xMax == 0) {
		
			xMax = window.innerWidth - 14;
		
		}
		
		if(yMax == 0) {
		
			yMax = window.innerHeight;
		
		}
		
		if(document.getElementById) {
		
			document.getElementById("bouncead").style.visibility = "visible";
		
		} else {
		
			document.layers["bouncead"].visibility = "show";
		
		}
		
	}
	
	BounceAd();	

}


function hidead() {

 if (document.all) { 
 
   document.all("bouncead").style.visibility = "hidden";
 
 } else if (document.layers||document.getElementById) {

  if (document.getElementById) {
  
   document.getElementById("bouncead").style.visibility="hidden"
   
  } else {
  
   document.layers["bouncead"].visibility = "hide";
   
  }
  
  }

}
