/* functions for spawning and manipulating resizable windows */

function getnumber(string)
{
	var num = string.match(/\d+/)
	return parseInt(num[0]);
}

var windows = { };
var d_w = 200;
var d_h = 400;

var topz = 1;

ccsettings = {
  tl: { radius: 6 },
  tr: { radius: 6 },
  bl: { radius: 6 },
  br: { radius: 6 },
  antiAlias: true,
  autoPad: true,
  validTags: ["div"]
}

function drwindow(name)
{
	// basic window creation..
	this.wname = name
	this.hasfocus = true;
	windows[name] = Array(d_w,d_h);
	this.rootwin = document.createElement("div");
	this.rootwin.setAttribute("id", name + "_r");

	// window look
	this.rootwin.style.position = "absolute";
	this.rootwin.style.width = d_w + "px";
	this.rootwin.style.height = d_h + "px";
	this.rootwin.style.border = "1px solid black";
	this.rootwin.style.backgroundColor = "#ddd";
	this.rootwin.style.zIndex = topz++;
	//var rw_boxcc = new curvyCorners(ccsettings, this.rootwin);

	// window contents
	// title bar 
	titlebar = '<div style="position: relative; top: 2px; left: 1%; right: 1%; width: 98%; background-color: #77f; height: 49px" id="' + name + '_h"></div>';
	this.rootwin.innerHTML = titlebar;

	// main window area
	this.rootwin.innerHTML += '<div style="position: absolute; left: 1%; width: 98%; height: 93%; top: 20px; overflow: hidden; background-color: #fff" id="' + name + '_main"></div>';
	// resize area
	this.rootwin.innerHTML += '<div style="position: absolute; width: 10px; height: 10px; background-image: url(' + cp_basepath + '/jwc/icon_resize.gif)" id="' + name + '_resize"></div>';

	// add window to DOM HTML tree..
	myBody = document.getElementsByTagName("body")[0];
	myBody.appendChild(this.rootwin);

	// create member variables which point to the layers of matter..
	this.handle = document.getElementById(name + '_h');
	this.main = document.getElementById(name + '_main');
	this.resize  = document.getElementById(name + '_resize');
	//var h_boxcc = new curvyCorners(ccsettings, this.handle);

	// define methods
	this.setbody = function (buf) { this.main.innerHTML = buf; }
	this.settitlebar = function (buf) {
		titlebar = '<div style="float: left; width: 80%"><a href="javascript:void(0)" onclick="close_window(\'' + this.wname + '\')" tityle="Close window" style="text-decoration: none"><img src="' + cp_basepath + '/jwc/lame_close.png" border="0" /></a> ';
		// minimize button
		titlebar += '<a href="javascript:void(0)" onclick="minimize_window(\'' + this.wname + '\')" title="Minimize window" style="text-decoration: none"><img src="'+ cp_basepath +'/jwc/lame_minimize.png" border="0" /></a>';
		// window name, and title bar ends
		this.handle.innerHTML = titlebar +' ' + this.wname + '</div>' + buf;
	}

	this.settitlebar('<div style="float: right"><a href="javascript:void(0)" onclick="save_conv(\'' + this.wname + '\')" title="Save conversation" style="text-decoration: none; text-align: right"><img src="'+ cp_basepath + '/lullacons/folder-option-insert.png" border="0" alt="Save conversation image" /></a></div>');

	Drag.init(this.handle, this.rootwin, null, null, null, null, false, false);
	Drag.init(this.resize, null, null, null, null, null, true, true);

	this.resize.onDrag = function(x, y)
	{
		var win = document.getElementById(name + "_r");
		var main = document.getElementById(name + "_main");
		win.style.width = windows[name][0]-x + "px";
		win.style.height = windows[name][1]-y + "px";
		//main.innerHTML = "width=" + win.style.width;
		//main.innerHTML += "<br>height=" + win.style.height;
		//main.innerHTML += "<br>x=" + x;
		//main.innerHTML += "<br>y=" + y;
	}

	this.resize.onDragEnd = function(x,y)
	{
		var win = document.getElementById(name + "_r");
		windows[name][0] = getnumber(win.style.width);
		windows[name][1] = getnumber(win.style.height);
	}

	// bring window to the top
	this.rootwin.onmousedown = function() { document.getElementById(name + "_r").style.zIndex = topz++; }
	//rw_boxcc.applyCornersToAll();
	//h_boxcc.applyCornersToAll();
}

function destroy_window(wname)
{
	myBody = document.getElementsByTagName("body")[0];
	myBody.removeChild(document.getElementById(wname + '_r'));
}

function position_window(name,x,y)
{
	var win = document.getElementById(name + '_r');
	win.style.left = x + 'px';
	win.style.top = y + 'px';
}

function window_position(name)
{
	var win = document.getElementById(name + '_r');
	return new Array(getnumber(win.style.left),getnumber(win.style.top));
}

function window_size(name)
{
	return windows[name];
}

function resize_window(name,w,h)
{
	var win = document.getElementById(name + '_r');
	win.style.width = w + 'px';
	win.style.height = h + 'px';
	windows[name][0] = w;
	windows[name][1] = h;
}

