// JavaScript Document

//Display Mouse coordinates- By Kous (kous@ihateclowns.com)
//Visit http://javascriptkit.com for this script and more

/*var where = ""; // which link
var xCoord, yCoord;
function checkwhere(e) {
        if (document.layers){
        xCoord = e.x;
        yCoord = e.y;
}
        else if (document.all){
        xCoord = event.clientX;
        yCoord = event.clientY;
}
        else if (document.getElementById){
        xCoord = e.clientX;
        yCoord = e.clientY;
}
        self.status = "X= "+ xCoord + "  Y= " + yCoord; 
        }


document.onmousemove = checkwhere;
if ( document.captureEvents ) 
     document.captureEvents(Event.MOUSEMOVE);

*/

function getScrollXY()
{ 
  var scrOfX = 0, scrOfY = 0; 
  if( typeof( window.pageYOffset ) == 'number' ) 
    { //Netscape compliant 
	  scrOfY = window.pageYOffset; 
	  scrOfX = window.pageXOffset; 
	} 
  else 
      if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) 
	    { //DOM compliant 
	      scrOfY = document.body.scrollTop; scrOfX = document.body.scrollLeft; 
		} 
	  else 
	      if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) 
		    { //IE6 standards compliant mode 
			  scrOfY = document.documentElement.scrollTop; 
			  scrOfX = document.documentElement.scrollLeft; 
			} 
  return [ scrOfX, scrOfY ]; 
}

var xCoord, yCoord;

function mouseMove(e) 
{
  var scrOfXfY = getScrollXY();
  
  xCoord = (document.all) ? event.x + scrOfXfY[ 0 ] : e.pageX;
  yCoord = (document.all) ? event.y + scrOfXfY[ 1 ] : e.pageY;
  self.status = "X= "+ xCoord + "  Y= " + yCoord;
  /*xCoord = (document.all)? event.x+document.body.scrollLeft : e.pageX
  yCoord = (document.layers)? event.y+document.body.scrollTop : event.y+document.body.scrollTop
  self.status = "X= "+ xCoord + "  Y= " + yCoord; */
}

document.onmousemove = mouseMove;
if (document.captureEvents) document.captureEvents(Event.MOUSEMOVE);


//Funciones par la ventanas con DIV

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)
{

	    // alert( this.oContent == oContent );
	
	// 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.border = 1;

	// set the position of the window
	this.oTable.style.position = "absolute";
	this.oTable.style.left = x + "px";
	this.oTable.style.top = y + "px";

	// 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.className = "JSWindowTitleStyle";

	// title	
	var oTD = oTR.insertCell(0);
	//oTD.innerHTML = title;
	oTD.innerHTML = "";
	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 = "X";
	oTD.align='right';
	oTD.innerHTML = "<img src='img/x.gif'>";
	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.colSpan = 3;
	oTD.appendChild(oContent);
}

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)
{
	//alert( "bodyOnMouseMove..." );
	var e = window.event ? window.event : evt;
	this.jsWindow.onMouseMove(e);
}
JSWindow.prototype.onMouseMove = function(evt)
{
	//alert( "onMouseMove..." );
	// 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.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()
{
  this.jsWindow.close();
}

/* createNewWindow()
{
	if ( document.getElementById( 'Hola' ) == null )
	   {  
		 
	var oDiv = document.createElement("div");
	oDiv.id = "Hola";
	oDiv.style.width = "250px";
	oDiv.style.height = "80px";
	oDiv.innerHTML = "This is a new window<br>blah blah blah blah blah blah...";
	new JSWindow( document.getElementById( "wName" ).value, oDiv, 10, 10 );
	}
}*/


function createNewWindow( sIdWindow, sNombreWindow, nX, nY )
{
	if ( document.getElementById( sIdWindow ) == null )
	   {  
		 
	var oDiv = document.createElement("div");
	oDiv.id = sIdWindow;
	oDiv.style.width = "250px";
	oDiv.style.height = "80px";
	//oDiv.innerHTML = "This is a new window<br>blah blah blah blah blah blah...";
	oDiv.innerHTML = sNombreWindow;
	new JSWindow( sNombreWindow, oDiv, xCoord, yCoord );
	}
}




