// Windows
function openWindow(caller, title, content, style) {
	style		 = (typeof(top)=="undefined")?'':style;
	var styles	 = new Array();
	
	var get_params = style.split('&');
	for(i = 0; i < get_params.length; i++) {
		var key_value = get_params[i].split('=');
		if(key_value.length == 2) {
			styles[key_value[0]] = key_value[1];
		}
	}
	
	styles['top']	 = (typeof(styles['top'])=="undefined")?0:parseInt(styles['top']);
	styles['left']	 = (typeof(styles['left'])=="undefined")?0:parseInt(styles['left']);
	
	var tpl		 = false;
	var id		 = "window"+caller;
	try { closeWindow(id); } catch(e) {};
	
	if(content == 'tpl') {
		content	 = '';
		tpl		 = caller;
	}
	
	/*	create window	*/
	var window_id		 = document.createAttribute('id');
	window_id.nodeValue	 = id;

	var window_class		 = document.createAttribute('class');
	window_class.nodeValue	 = 'window';

	var node	 = document.createElement('div');
	node.setAttributeNode(window_id);
	node.setAttributeNode(window_class);

	/*	create closebtn	*/
	var close_class			 = document.createAttribute('class');
	close_class.nodeValue	 = 'right';

	var close_node			 = document.createElement('span');
	close_node.setAttributeNode(close_class);
	close_node.innerHTML	 = '<a href="#" onclick="closeWindow(\'' + id + '\'); return false;"><span>x</span></a>';

	/*	create title	*/
	var title_node			 = document.createElement('h2');
	title_node.innerHTML	 = title;
	
	title_node.appendChild(close_node);

	/*	create inner	*/
	var inner_class			 = document.createAttribute('class');
	inner_class.nodeValue	 = 'inner';

	var inner_id			 = document.createAttribute('id');
	inner_id.nodeValue	 = 'inner'+caller;

	var inner_node			 = document.createElement('div');
	inner_node.setAttributeNode(inner_class);
	inner_node.setAttributeNode(inner_id);
	inner_node.innerHTML	 = content;

	/*	create content box	*/
	var box_class		 = document.createAttribute('class');
	box_class.nodeValue	 = 'box';

	var box_node		 = document.createElement('div');
	box_node.setAttributeNode(box_class);
	box_node.appendChild(inner_node)
	
	/*	create clearer	*/
	var clear_class			 = document.createAttribute('class');
	clear_class.nodeValue	 = 'clear';

	var clear_node			 = document.createElement('div');
	clear_node.setAttributeNode(clear_class);
	clear_node.innerHTML	 = '&nbsp;';

	/*	fuse everything	*/
	node.appendChild(title_node);
	node.appendChild(box_node);
	node.appendChild(clear_node);

	/*	add to helper	*/
	document.getElementById('helpers').appendChild(node);

	var offset	 = getOffset(caller);
	offset[0]	 = offset[0]+styles['top'];
	offset[3]	 = offset[3]+styles['left'];
	
	document.getElementById(id).style.top	 = offset[0]+'px';
	document.getElementById(id).style.left	 = offset[3]+'px';
	document.getElementById(id).style.display	 = 'block';
	
	if(typeof(styles['width'])!="undefined")
	{
		document.getElementById(id).style.width		 = styles['width'];
	}
	if(typeof(styles['height'])!="undefined")
	{
		document.getElementById(id).style.height	 = styles['height'];
	}
	
	if(tpl != false) { 
		var template	 = ajaxGetTpl(tpl, 'return');
		document.getElementById('inner'+caller).innerHTML	 = template;
		
		var innerScript	 = document.getElementById('inner'+caller).getElementsByTagName('script');
		for(var i=0;i<innerScript.length;i++){
			eval(innerScript[i].text);
		}
	}
}

function closeWindow(id) {
	document.getElementById('helpers').removeChild(document.getElementById(id));
}

function getOffset(id) {
	var parents	 = document.getElementById(id);

	var position	 = new Array(0,0,0,0);

	while (parents) {
		position[0]	+= parents.offsetTop;
		position[3]	+= parents.offsetLeft;

		parents	 = parents.offsetParent;
	}

	return position;
}
