//variables
var ie = navigator.appName.indexOf('Explorer')>-1;
var es_seccion_agenda=false;
var window_handler_name = "div_window_handler";

function HideDiv(nm) {
	nm.style.visibility="hidden";
	nm.style.display='none';
}

function GetMouseX(ev) {
	return (ie) ? (event.clientX +document.body.scrollLeft) : ev.pageX;
}

function GetMouseY(ev) {
	return (ie) ? (event.clientY +document.body.scrollTop) : ev.pageY;
}

function MoveLayer(ev) {	
	
	var x = GetMouseX(ev);
	var y = GetMouseY(ev);
	
	//datos ventana
	xpos=x+initial_width_drag;
	ypos=y+initial_height_drag;
	
	//if (minlimx<=xpos && xpos<maxlimx)
	initial_drag_layer.style.left = xpos;
	
	//if (minlimy<=ypos && ypos<maxlimy) 
	initial_drag_layer.style.top = ypos;
}


function StartDrag(ev, ly) {
	// Obtenemos la capa a mover
	initial_drag_layer = document.getElementById(ly);

	// Calculamos la posicion inicial, y su defase.
	initial_x_drag = GetMouseX(ev);
	initial_y_drag = GetMouseY(ev);

	initial_width_drag = parseInt(initial_drag_layer.style.left) - initial_x_drag;
	initial_height_drag = parseInt(initial_drag_layer.style.top) - initial_y_drag;
	
	// Asignamos la función de desplazamiento
	document.body.onmousemove=MoveLayer;
}

function EndDrag() {
	document.body.onmousemove=null;

	initial_drag_layer = null;
	initial_x_drag = 0;
	initial_y_drag = 0;
	initial_width_drag = 0;
	initial_height_drag = 0;
}


function CloseWindow(wnd)
{
	ly = document.getElementById(wnd);
	HideDiv(ly);
}

function CenterWindow(wnd)
{
	if (ie) {
		wwidth = document.body.clientWidth;
		wheight = document.body.clientHeight; 
	}		
	else if (document.getElementById && !document.all) {
		wwidth = window.innerWidth;
		wheight = window.innerHeight; 
	} 
	
	y=(wheight-parseInt(wnd.style.height))/4;
	x=(wwidth-parseInt(wnd.style.width))/2;

	wnd.style.left = x + 'px';
	wnd.style.top = y + 'px';
}


function DialogWindow()
{
	this.whandle = document.getElementById(window_handler_name);
	this.wndname = "v_" + String(Math.round(Math.random()*1000));
	this.ajax = new XHConn();

	this.setContent = function (oXML) {
		document.getElementById(wndname+'_document').innerHTML = oXML.responseText;
		CenterWindow(document.getElementById(wndname));
	}

	this.getContent = function () {
		return document.getElementById(wndname+'_document');
	}
	
	this.show = function (title, width, height, url) {
		wndname = this.wndname;
		name = wndname;
		
		x=(640 - width)/2;
		y=(480-height)/2;

		if (ie) {
			wwidth = document.body.clientWidth;
			wheight = document.body.clientHeight; 
		}		
		else if (document.getElementById && !document.all) {
			wwidth = window.innerWidth;
			wheight = window.innerHeight; 
		} 
	
		y=(wwidth-height)/2;
		x=(wwidth-width)/2;

		// Cabecera		
		this.whandle.innerHTML = '<div id="' + name + '" style="position:absolute; display:block; visibility: visible; left:' + x + 'px; top:' + y + 'px; width:' + width + 'px; height: ' + height + 'px;">' +
		'<table border=0 width=100% height=100% cellspacing=0 cellpadding=0>' +
		'<tr onmouseover="this.style.cursor=\'pointer\'" onMouseDown="StartDrag(event, \'' + name + '\')" onMouseUp="EndDrag()"><td><div class="wnd_title_bar"><div class="wnd_left_top"></div><div class="wnd_title">' + title + '</div><div style="float:right"><div class="wnd_right_top_close"><a href="javascript: CloseWindow(\'' + name + '\')"><input type="button" class="wnd_close_icon" onClick="CloseWindow(\'' + name + '\')"></a></div><div class="wnd_right_top">&nbsp;</div></div></div></td></tr>' +
		'<tr><td height="100%" bgcolor="#FFFFFF"><div class="wnd_left">&nbsp;</div><div class="wnd_content" style="width: 95%; height: 100%; display:block; float:left"><div id="' + name + '_document"></div></div><div class="wnd_right">&nbsp;</div></td></tr>' +
		'<tr><td><div class="wnd_bottom"><div class="wnd_left_bottom">&nbsp;</div><div class="wnd_right_bottom">&nbsp;</div></div></td></tr></table></div>';

		this.ajax.connect(url, 'GET', '', this.setContent);
	}
	
	this.hide = function () {
		doc = document.getElementById(this.wndname);
		doc.style.visible = false;
	}
}

function close_layer(nlayer){
	document.getElementById(nlayer).style.visibility = "hidden";
	window.onresize = null;
}

document.write ('<div id="' + window_handler_name + '"></div>');
