// JavaScript Document

// can define any functions used by this js here

// find position of window on the physical screen
function FindWindowPos()
{
	// get actual position of current window relative to screen
	var winX = (document.all)?window.screenLeft:window.screenX;
	var winY = (document.all)?window.screenTop:window.screenY;

	return [winX, winY];
}	

// find position of an object on the browser screen
function FindObjPos(obj)
{
	var curleft = curtop = 0;
	
	// If the browser supports offsetParent we proceed
	if (obj.offsetParent)
	{
		// iterate through the lineage of container objects
		// Every time we find a new object, we add its offsetLeft and offsetTop to curleft and curtop
		do
		{
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);	// assignment operator
	}
	return [curleft,curtop];
}

function RedeemPopup(targetobj, partnerid)
{ 
	var winpos;
	var objpos;
	var ScrollLeft = document.body.scrollLeft;
	var ScrollTop = document.body.scrollTop;
	var x = 0;		// pseudos for array elements
	var y = 1;

	// figure out current scroll position
	if (ScrollTop == 0)
	{
		if (window.pageYOffset)
			ScrollTop = window.pageYOffset;
		else
			ScrollTop = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
	}

	if (ScrollLeft == 0)
	{
		if (window.pageXOffset)
			ScrollLeft = window.pageXOffset;
		else
			ScrollLeft = (document.body.parentElement) ? document.body.parentElement.scrollLeft : 0;
	}
	
	winpos = FindWindowPos();
	objpos = FindObjPos(targetobj);

	// insert logic here to dynamically set the popup based on the relative placement of banner on page
	
//	OpenWindow=window.open("http://linkserv.mytownie.com/popupcollectpoint.php?linkpartid=" + partnerid, "newwin", "height=250, width=300,titlebar=1,toolbar=0,status=0,location=0,directories=0,resizable=0,scrollbars=0,menubar=0,left="+(winpos[x]+objpos[x]-ScrollLeft)+",top="+(winpos[y]+objpos[y]-ScrollTop-250));
	OpenWindow=window.open("http://linkserv.mytownie.com/popupcollectpoint.php?linkpartid=" + partnerid, "newwin", "height=250, width=300,titlebar=1,left="+(winpos[x]+objpos[x]-ScrollLeft)+",top="+(winpos[y]+objpos[y]-ScrollTop-250));
	self.name="main";
}

// sample functional call
// document.write(test());

// GetLinkPartnerId() is dynamically defined in netlink.php

document.write('<img src="http://linkserv.mytownie.com/images/banner150x100.gif" border="0" style="border:none;" onclick="javascript:RedeemPopup(this, ' + "'" + GetLinkPartnerId() + "'" + ')" style="cursor:hand">');
