function objText(o)
{
	var s = new Array();
	s.push('{<br>');
	for ( var i in o )
		s.push("&nbsp;&nbsp;&nbsp;" + i + ": " + o[i] + "<br>");
	s.push('}');
	return s.join('');
}
function debug(msg)
{
	document.getElementById('debug').innerHTML += msg + "<br>";
}

function JSWindow(title, oContent, x, y)
{
	// save arguments
	this.title = title;
	this.oContent = oContent;

	// initialization
	this.mx = 0;
	this.my = 0;

	// create table for window with title-bar and content
	this.oTable = document.createElement("table");
	this.oTable.id = "shopPopUpP2is";
	this.oTable.border = 0;

	// set the position of the window
	this.oTable.style.position = "absolute";
	this.oTable.style.left = x + "px";
	this.oTable.style.top = y + "px";
    this.oTable.cellPadding = 0;
    this.oTable.cellSpacing = 0;

	// set background to white (default is transparent)
	//this.oTable.style.backgroundColor = "white";

	// link from the table to the JSWindow object
	this.oTable.jsWindow = this;

	// if anywhere in the table are is clicked, bring the window to front.
	this.oTable.onmousedown = JSWindow.prototype.onBringToFront;

	// append to document body
	document.body.appendChild(this.oTable);

	// add row for title bar
	var oTR = this.oTable.insertRow(0);
    //oTR.bgColor = "#B3B3B3";
	//oTR.background = 'images/popupP2is.gif';

	// title
	var oTD = oTR.insertCell(0);
	oTD.innerHTML = title;
	oTD.innerHTML = '<table width="566" cellpadding="0" cellspacing="0"><tr><td colspan="9"	align="center" valign="middle" background ="images/popupP2is.gif" width="100%" height="23">'+title+'</td></tr></table>';
	oTD.jsWindow = this;
	oTD.onmousedown = JSWindow.prototype.tdOnMouseDown;

	// minimize
	//this.oMinTD = oTR.insertCell(1);
	//this.oMinTD.innerHTML = "_";
	//this.oMinTD.onmousedown = JSWindow.prototype.onMinimize;
	//this.oMinTD.jsWindow = this;

	// close
	oTD = oTR.insertCell(1);
	oTD.innerHTML = '<table width="25" cellpadding="0" cellspacing="0"><tr><td valign="middle" background ="images/popupP2is2.gif" width="23" height="23"></td></tr></table>';

	oTD.jsWindow = this;
	oTD.onmousedown = JSWindow.prototype.onClose;
	// add row for window content
	// a single cell the same width as the title bar row
	oTR = this.oTable.insertRow(1);
	oTD = oTR.insertCell(0);
    oTD.align = "right";
	oTD.colSpan = 3;
	oTD.appendChild(oContent);
	
	scrollTo(0,0);
}

JSWindow.prototype.onBringToFront = function()
{
	this.jsWindow.bringToFront();
}
JSWindow.prototype.bringToFront = function()
{
	// if not already the last child of the document.body, make it so
	if ( document.body.childNodes[document.body.childNodes.length-1] !== this.oTable )
	{
		// move to bottom of document
		document.body.appendChild(this.oTable);
	}
}

JSWindow.prototype.tdOnMouseDown = function()
{
	this.jsWindow.onMouseDown();
}
JSWindow.prototype.onMouseDown = function()
{
	// record that an onmousedown has just occurred
	this.bDown = true;

	// link from body to this JSWindow object
	document.body.jsWindow = this;

	// save body mouse handlers
	this.saveMouseMove = document.body.onmousemove;
	this.saveMouseUp = document.body.onmouseup;

	// set new handlers.
	document.body.onmousemove = JSWindow.prototype.bodyOnMouseMove;
	document.body.onmouseup = JSWindow.prototype.bodyOnMouseUp;
}
JSWindow.prototype.bodyOnMouseMove = function(evt)
{
	var e = window.event ? window.event : evt;
	this.jsWindow.onMouseMove(e);
}
JSWindow.prototype.onMouseMove = function(evt)
{
	// if mouse not down, stop the move (for IE only)
	if ( (document.all) && !(evt.button & 1) )
	{
		this.onMouseUp();
		return;
	}
	if ( this.bDown )
	{
		this.dx = parseInt(this.oTable.style.left, 10) - evt.clientX;
		this.dy = parseInt(this.oTable.style.top, 10) - evt.clientY;
		this.bDown = false;
	}
	else
	{
		this.oTable.style.left = Math.max((this.dx + evt.clientX),0) + "px";
		this.oTable.style.top = Math.max((this.dy + evt.clientY),0) + "px";
	}
}
JSWindow.prototype.bodyOnMouseUp = function()
{
	this.jsWindow.onMouseUp();
}
JSWindow.prototype.onMouseUp = function()
{
	document.body.onmouseup = this.saveMouseUp;
	document.body.onmousemove = this.saveMouseMove;
	document.body.jsWindow = null;
}
JSWindow.prototype.onMinimize = function()
{
	this.jsWindow.minimize();
}
JSWindow.prototype.hide = function()
{
	// hide the content
	this.oContent.style.visibility = "hidden";
}

JSWindow.prototype.minimize = function()
{
	// hide the content
	this.oContent.style.visibility = "hidden";
	this.oContent.style.position = "absolute";
	document.body.appendChild(this.oContent);

	this.oTable.deleteRow(1);

	// save current position
	this.saveX = this.oTable.style.left;
	this.saveY = this.oTable.style.top;

	// get the "window bar"
	if ( !window.jsWindowBar )
	{
		window.jsWindowBar = document.createElement("span");
		document.body.appendChild(window.jsWindowBar);
	}

	window.jsWindowBar.appendChild(this.oTable);
	this.oTable.style.position = "static";
	this.oTable.style.left = "0px";
	this.oTable.style.top = "0px";

	this.oMinTD.innerHTML = "#";
	this.oMinTD.onmousedown = JSWindow.prototype.onMaximize;
}
JSWindow.prototype.onMaximize = function()
{
	this.jsWindow.maximize();
}
JSWindow.prototype.maximize = function()
{
	document.body.appendChild(this.oTable);
	this.oTable.style.position = "absolute";

	this.oTable.style.left = this.saveX;
	this.oTable.style.top = this.saveY;

	// add the content again.
	oTR = this.oTable.insertRow(1);
	oTD = oTR.insertCell(0);
	oTD.colSpan = 3;

	oTD.appendChild(this.oContent);
	this.oContent.style.position = "static";
	this.oContent.style.visibility = "visible";

	this.oMinTD.innerHTML = "_";
	this.oMinTD.onmousedown = JSWindow.prototype.onMinimize;
}
JSWindow.prototype.onMaximize = function()
{
	this.jsWindow.maximize();
}
JSWindow.prototype.close = function()
{
	// remove content from browser document
	this.oContent.parentNode.removeChild(this.oContent);

	// remove from browser document
	this.oTable.parentNode.removeChild(this.oTable);
}
JSWindow.prototype.onClose = function()
{
	if(document.getElementById("filtrid"))
	{
		document.getElementById("filtrid").style.display='inline';
	}
	this.jsWindow.close();
}

function closeWindow()
{
	this.jsWindow.close();
	return false;
}

function newShopPopUP(output)
{
	if(document.getElementById("filtrid"))
	{
		document.getElementById("filtrid").style.display='none';
	}
	window.scrollBy(0,-100);
	var oDiv = document.createElement("div");
	oDiv.innerHTML = output;
	oDiv.id = "shopPopUp";
	oDiv.style.zIndex=44444;
	new JSWindow("Kaupluse Link", oDiv, 185, 145);
}

function newKuulutusPopUP(output)
{
	if(document.getElementById("filtrid"))
	{
		document.getElementById("filtrid").style.display='none';
	}
	window.scrollBy(0,-100);
	var oDiv = document.createElement("div");
	oDiv.innerHTML = output;
	oDiv.id = "shopPopUp";
	new JSWindow("Kontaktandmed", oDiv, 185, 145);
}

function newFavoritesPopUP(output)
{
	if(document.getElementById("filtrid"))
	{
		document.getElementById("filtrid").style.display='none';
	}
	window.scrollBy(0,-100);
	var oDiv = document.createElement("div");
	oDiv.innerHTML = output;
	new JSWindow("Lemmikud", oDiv, 195, 160);
}

function newSendToFriendPopUP(output)
{
	if(document.getElementById("filtrid"))
	{
		document.getElementById("filtrid").style.display='none';
	}
	window.scrollBy(0,-100);
	var oDiv = document.createElement("div");
	oDiv.innerHTML = output;
	new JSWindow("Saada s&otilde;brale", oDiv, 215, 180);
}

function newSendToKallimalePopUP(output)
{
	if(document.getElementById("filtrid"))
	{
		document.getElementById("filtrid").style.display='none';
	}
	window.scrollBy(0,-100);
	var oDiv = document.createElement("div");
	oDiv.innerHTML = output;
	new JSWindow("Saada kallimale", oDiv, 225, 190);
}

function allahindlusPopUP(output)
{
	if(document.getElementById("filtrid"))
	{
		document.getElementById("filtrid").style.display='none';
	}
	window.scrollBy(0,-100);
	var oDiv = document.createElement("div");
	oDiv.innerHTML = output;
	oDiv.id = "allahindlusPopUp";
	new JSWindow("Kontaktandmed", oDiv, 185, 145);
}

function erilinePopUP(output)
{
	if(document.getElementById("filtrid"))
	{
		document.getElementById("filtrid").style.display='none';
	}
	window.scrollBy(0,-100);
	var oDiv = document.createElement("div");
	oDiv.innerHTML = output;
	new JSWindow("Sisustus.ee on eriline", oDiv, 185, 145);
}

function disainerPopUP(output)
{
	if(document.getElementById("filtrid"))
	{
		document.getElementById("filtrid").style.display='none';
	}
	window.scrollBy(0,-100);
	var oDiv = document.createElement("div");
	oDiv.innerHTML = output;
	new JSWindow("Sisekujundaja", oDiv, 185, 145);
}

function taaskasutusPopup(output)
{
	if(document.getElementById("filtrid"))
	{
		document.getElementById("filtrid").style.display='none';
	}
	window.scrollBy(0,-100);
	var oDiv = document.createElement("div");
	oDiv.innerHTML = output;
	new JSWindow("Vii oma kasutatud asjad Tallinna Taaskasutuskeskusesse", oDiv, 185, 145);
}

function n6uandePopup(output)
{
	if(document.getElementById("filtrid"))
	{
		document.getElementById("filtrid").style.display='none';
	}
	window.scrollBy(0,-100);
	var oDiv = document.createElement("div");
	oDiv.style.position=absolute;
	new JSWindow("N&otilde;uandeid", oDiv, 185, 145);
}

function lemmikulistPopup(output)
{
	if(document.getElementById("filtrid"))
	{
		document.getElementById("filtrid").style.display='none';
	}
	window.scrollBy(0,-100);
	var oDiv = document.createElement("div");
	oDiv.innerHTML = output;
	new JSWindow("E-maili eelvaade", oDiv, 185, 145);
}


