var offsetxpoint=20 //Customize x offset of tooltip
var offsetypoint=20 //Customize y offset of tooltip
var ie=document.all
var ns6=document.getElementById && !document.all
var enabletip=false
if (ie||ns6)
var tipobj=document.all? document.all["dhtmltooltip"] : document.getElementById? document.getElementById("dhtmltooltip") : ""

function ietruebody(){
return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function ddrivetip(thetext, thecolor, thewidth){
if (ns6||ie){
if (typeof thewidth!="undefined") tipobj.style.width=thewidth+"px"
if (typeof thecolor!="undefined" && thecolor!="") tipobj.style.backgroundColor=thecolor
tipobj.innerHTML=thetext
enabletip=true
return false
}
}

function positiontip(e){
if (enabletip){
var curX=(ns6)?e.pageX : event.clientX+ietruebody().scrollLeft;
var curY=(ns6)?e.pageY : event.clientY+ietruebody().scrollTop;
//Find out how close the mouse is to the corner of the window
var rightedge=ie&&!window.opera? ietruebody().clientWidth-event.clientX-offsetxpoint : window.innerWidth-e.clientX-offsetxpoint-20
var bottomedge=ie&&!window.opera? ietruebody().clientHeight-event.clientY-offsetypoint : window.innerHeight-e.clientY-offsetypoint-20
var eX=ie&&!window.opera? event.clientX : e.clientX
var eY=ie&&!window.opera? event.clientY : e.clientY
var glY=ie&&!window.opera? ietruebody().scrollTop : window.pageYOffset

var leftedge=(offsetxpoint<0)? offsetxpoint*(-1) : -1000

//if the horizontal distance isn't enough to accomodate the width of the context menu
if (rightedge<tipobj.offsetWidth)
//move the horizontal position of the menu to the left by it's width
if (eX<tipobj.offsetWidth) {
tipobj.style.left=eX+20+"px"
}
else {
tipobj.style.left=ie? ietruebody().scrollLeft+event.clientX-tipobj.offsetWidth+"px" : window.pageXOffset+e.clientX-tipobj.offsetWidth+"px"
}
else if (curX<leftedge)
tipobj.style.left="5px"
else
//position the horizontal position of the menu where the mouse is positioned
tipobj.style.left=curX+offsetxpoint+"px"

//same concept with the vertical position
//alert(eY+" - "+window.innerHeight+" - "+window.pageYOffset);
if (bottomedge<tipobj.offsetHeight)
if (eY<tipobj.offsetHeight) {
tipobj.style.top=glY+20+"px"
}
else {
tipobj.style.top=ie? ietruebody().scrollTop+event.clientY-tipobj.offsetHeight-offsetypoint+"px" : window.pageYOffset+e.clientY-tipobj.offsetHeight-offsetypoint+"px"
}
else
tipobj.style.top=curY+offsetypoint+"px"
tipobj.style.visibility="visible"
}
}

function hideddrivetip(){
if (ns6||ie){
enabletip=false
tipobj.style.visibility="hidden"
tipobj.style.left="-2000px"
tipobj.style.backgroundColor=''
tipobj.style.width=''
}
}

function gt(x) 
{ 
	var f = location.search.substr(1,location.search.length).split('&'); 
	
	t=''; 
	
	for(i=0;i<f.length;i++) 
	{ 
		r=f[i].split('='); 
		if(r[0]==x)
			t = r[1];      
	} 
	
	return t; 
} 

var x;
var y;

var IE = document.all?true:false;

if (!IE) 
	document.captureEvents(Event.MOUSEMOVE)

function getMouseXY(e) 
{
	if (IE) 
	{ // grab the x-y pos.s if browser is IE
		
		x = window.event.x;
		y = window.event.y;
		
	}
	else 
	{  // grab the x-y pos.s if browser is NS
		x = e.pageX - document.getElementById('grid').offsetLeft;
		y = e.pageY - document.getElementById('grid').offsetTop;
	}
	//alert(x);
	//alert(y);
	
	selectblock(x,y);
} 

function selectblock(x,y)
{
	if(gt('action') != "selectblock")
		return;
	
	if(x >= 0 && y >= 0)
    {
    	x = transformPixel(x);
    	y = transformPixel(y);
    }
    else
    {
    	x = -1;
    	y = -1;
    }
    
	var width = 0;
	var height = 0;
	
	array = new Array();
	
	var value = false;
	
	if(y >= 0 || x >= 0)
	{
		if(array.length > 0)
		{
			for(var j = 0; j < i; j+=4)
			{
				array[j+2] -= 5; array[j+2] += array[j];// array[j] += 5;
				array[j+3] -= 5; array[j+3] += array[j+1];// array[j+1] += 5;
				
				var imageLeft = transformPixel(array[j]);
				var imageTop = transformPixel(array[j+1]);
				var imageRight = transformPixel(array[j+2]);
				var imageBottom = transformPixel(array[j+3]);
				
				if(!verifyImposition(imageLeft, imageTop, imageRight, imageBottom, x, y, x+width, y+height))
				{
					value = false;
					break;
				}
				else
				{
					value = true;
				}
			}
		}
		else
		{
			value = true;
		}
	}
	
	if(value)
	{
		document.getElementById('x').value = x;
		document.getElementById('y').value = y;
		
		var con = confirm("Would you like to buy a Pixel Ad at this location?");
		
		if(con)
			document.forms['paypal'].submit();
	}
	else
		alert("Error: image is overlapping");
}

function transformPixel(pixel)
{
	var result = pixel/5;
	result = Math.abs(result);
	result = Math.floor(result);
	//alert (result);
	return result;
}

function verifyImposition(imageLeft, imageTop, imageRight, imageBottom, x, y, width, height)
{
	//alert("imageLeft: "+imageLeft+" imageTop: "+imageTop+" imageRight: "+imageRight+" imageBottom: "+imageBottom);
	//alert("x: "+x+" y: "+y+" width: "+width+" height: "+height);
	
	//return false;
	
	//alert("asfsaf");
	
	if(width > 199 || height > 199)
		return false;
		
	if(width < imageLeft)
		return true;
		
	if(height < imageTop)
		return true;
	
	if(x > imageRight)
		return true;
		
	if(y > imageBottom)
		return true;
		
	return false;
}

document.onmousemove=positiontip;
document.getElementById('grid').onclick = getMouseXY;