var tpopup = new Object();
tpopup.offset = 15; // offset so that the popup doesn't cover the mouse
tpopup.scrollbarmargin = 16; // firefox doesn't deduct the scrollbar from window dimensions
tpopup.imagepath = 'http://cdn.travidia.com'; // base path for images
tpopup.divId = 'popup'; // id of popup div
tpopup.zoomMessage = '<b>Click to Zoom</b>'; // message displayed when popup doesn't fit into window

tpopup.p = null; // page element for popup
tpopup.image = null; // popup image element
tpopup.lastid = null; // id of last accessed image
tpopup.xmlhttp = null;

tpopup.sx = 0; //window min x
tpopup.sy = 0; //window min y
tpopup.mx = 0; //window max x
tpopup.my = 0; //window max y
tpopup.x = 0; // x mouse coordinate
tpopup.y = 0; // y mouse coordinate

//this addresses a bug in IE 6 - we have to hide all select boxes whenever the popup div shows
tpopup.s = new Array();
if (navigator.appVersion.indexOf("MSIE")!=-1) {
    temp = navigator.appVersion.split("MSIE")
    if (parseFloat(temp[1]) == 6)
      tpopup.s = document.getElementsByTagName('select'); // all select boxes on page
}
//end IE 6 hack

function getp(popupId) // get popup element from page
{
	var p = null;
	if(document.getElementById) // Firefox, Newer IE
	{
		p = document.getElementById(popupId);
	}
	else if(document.all) // Older IE
	{
		p = document.all[popupId];
	}
	else if(document.layers) // Older Netscape
	{
		p = document.layers[popupId];
	}
	return p;
}

function display(e)
{
	if (tpopup.image != null) // popup may need to be resized if it is an image-only popup
	{
		tpopup.p.style.width = tpopup.image.width + 'px';
		tpopup.p.style.height = tpopup.image.height + 'px';
	}

	if (tpopup.p.offsetWidth > (tpopup.mx - tpopup.sx) || tpopup.p.offsetHeight > (tpopup.my - tpopup.sy)) // if popup is too large to fit in the window, display the zoom message
	{
		tpopup.p.style.width = null;
		tpopup.p.style.height = null;
		tpopup.p.innerHTML = tpopup.zoomMessage;
	}

	movePopup(tpopup.x, tpopup.y);
	
	for( var i=0; i < tpopup.s.length; i++ ) //fix for IE6 select box glitch
	  tpopup.s[i].style.visibility='hidden';
	
	tpopup.p.style.visibility = 'visible';
	tpopup.p.style.display = 'block'; //left in for legacy
}

function tv_move(e)
{
	e = e || window.event;
	s = e.target || e.srcElement;

	if(e.pageX && e.pageY)
	{
		// Firefox
		tpopup.x = e.pageX;
		tpopup.y = e.pageY;
		tpopup.sy = window.pageYOffset;
		tpopup.sx = window.pageXOffset;
		tpopup.mx = window.innerWidth + window.pageXOffset - tpopup.scrollbarmargin;
		tpopup.my = window.innerHeight + window.pageYOffset - tpopup.scrollbarmargin;
	}
	else
	{
		// IE
		var d = (document.documentElement.clientWidth) ? document.documentElement : document.body;
		tpopup.x = e.clientX + d.scrollLeft;
		tpopup.y = e.clientY + d.scrollTop;
		tpopup.sy = d.scrollTop;
		tpopup.sx = d.scrollLeft;
		tpopup.mx = d.clientWidth + d.scrollLeft;
		tpopup.my = d.clientHeight + d.scrollTop;
	}

	// Safari bug - the event fires on the text node
	if (s.nodeType == 3) s = s.parentNode;
	
	// page element for popup
	if (tpopup.p == null) tpopup.p = getp(tpopup.divId);
	
	if( tpopup.p != null && s != null && s.tagName == 'IMG' )
	{
		s = s.parentNode; // if IMG get DIV

		if(s.id != tpopup.lastid) // Load up new popup
		{
			// pop is a url frag, ie advid=437724&adid=3252733
			// need to accomidate for complete http://url, done
			var pop = s.getAttribute('pop');
				if( pop != null)
			{
				tpopup.image = null;
				// allow for passing of (remote) http url (ie classifieds)
				if ( pop.substring(0,4) == 'http' && tpopup.lastid != pop )
				{
					tpopup.img = null;
					tpopup.image = new Image();
					tpopup.image.src = pop;
					tpopup.p.innerHTML = '';
					tpopup.image.onload = display;
					tpopup.p.appendChild(tpopup.image);
					tpopup.lastid = pop;

				}
				else
				{
					getdata('popup.aspx?' + pop);
				}
			}
			else
			{
				tpopup.image = new Image();
				tpopup.p.innerHTML = '';
				tpopup.p.appendChild(tpopup.image);
				tpopup.image.onload = display;
				tpopup.image.src = tpopup.imagepath + '/' + s.id;
			}
			tpopup.lastid = s.id;
		}

		movePopup(tpopup.x, tpopup.y);
				
		// Cancel event bubbling.
		if(e.cancelBubble) e.cancelBubble = true;
		if(e.stopPropagation) e.stopPropagation();
	}		
}

function movePopup(x, y)
{
	var t = y + tpopup.offset;
	var l = x + tpopup.offset;

	if(t + tpopup.p.offsetHeight > tpopup.my)
	{
		t = tpopup.my - tpopup.p.offsetHeight;
		if(l + tpopup.p.offsetWidth > tpopup.mx && tpopup.x - tpopup.sx > tpopup.p.offsetWidth) // trapped in bottom right && room on the left. flip to left
			l = x - tpopup.p.offsetWidth - tpopup.offset;
	}

	else if(l + tpopup.p.offsetWidth > tpopup.mx)
	{
		l = tpopup.mx - tpopup.p.offsetWidth;
	}

	tpopup.p.style.top = t + 'px';
	tpopup.p.style.left = l + 'px';
}

function tv_hide()
{
	if(tpopup.xmlhttp != null) tpopup.xmlhttp.onreadystatechange = function() {};
	if(tpopup.img != null) tpopup.img.onload = null;
	if(tpopup.image != null) tpopup.image.onload = null;
	tpopup.lastid = null;
	if(tpopup.p != null) tpopup.p.style.visibility = 'hidden';
	tpopup.p.style.width = null;
	tpopup.p.style.height = null;
	
	for(var i=0;i<tpopup.s.length;i++) //fix for IE6 select box glitch
		tpopup.s[i].style.visibility='visible';
}

function handle()
{
	if(tpopup.xmlhttp != null && tpopup.xmlhttp.readyState == 4 && tpopup.p != null && tpopup.xmlhttp.status == 200)
	{
		tpopup.p.innerHTML = tpopup.xmlhttp.responseText;
		var img = tpopup.p.getElementsByTagName('img');
		if( img )
		{
			tpopup.img = img[0]; // right now we just wait for the first image
			tpopup.img.onload = display;
		}
		else display();
	}
}

// assigns tpopup.xmlhttp, will call handle() which assigns tpopup.img
function getdata(url)
{
	if(tpopup.xmlhttp == null) tpopup.xmlhttp = gethttpobj();

	if(tpopup.xmlhttp != null)
	{
		tpopup.xmlhttp.abort();
		tpopup.xmlhttp.open('GET', url, true);
		tpopup.xmlhttp.onreadystatechange = handle;
		tpopup.xmlhttp.send(null);
	}
}

function gethttpobj()
{
	var http = null;
	// branch for native XMLHttpRequest object
	if(window.XMLHttpRequest)
	{
		http = new XMLHttpRequest();
	}
	else if(window.ActiveXObject)
	{
		try
		{
			http = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e)
		{
			try
			{
				http = new ActiveXObject("Microsoft.XMLHTTP");	
			}
			catch(e){}
		}
    }
	return http;
}