/*#ver:1.02 26/4/2007#*/
function Event(pageX,pageY,clientX,clientY,keyCode)
{
	this.pageX=pageX;
	this.pageY=pageY;
	this.clientX=clientX;
	this.clientY=clientY;
	this.keyCode=keyCode;
}

function wrapURL(e, url, w, h, wd, offx, offy, estilo) {
	writeDrag.dragId = "dragDiv";
	writeDrag.writeId = "cntnt";
	writeDrag.handleId = "handle";

	if (!window.opener && typeof(parent.writeDrag) != 'undefined' && parent != window)
	{
        //parent.writeDrag.hide();
        // setTimeout con argumentos (IE no lo soporta)
   	    var x = 0;
	    var y = 0;
     	if (parent.document.getElementById(window.name.substr(7)))
        {
	    	x = parseInt(parent.document.getElementById(window.name.substr(7)).style.left);
	    	y = parseInt(parent.document.getElementById(window.name.substr(7)).style.top);
		}
		if (window.name.substr(7) != writeDrag.dragId + '_' + estilo)
        {
        	x += e.clientX;
        	y += e.clientY;
		}
		else
		{
	    	x -= parent.writeDrag.offX;
	    	y -= parent.writeDrag.offY;
		}
		var callwrapper = new CCallWrapper(parent.wrapURL, 100, new Event(e.pageX, e.pageY, x, y, e.keyCode), url, w, h, wd, offx, offy, estilo);
		CCallWrapper.asyncExecute(callwrapper);
		return true;
	}
	if (document.getElementById(writeDrag.dragId + '_' + estilo))
	{
		writeDrag.dragId = writeDrag.dragId + '_' + estilo;
		writeDrag.writeId = writeDrag.writeId + '_' + estilo;
		writeDrag.handleId = writeDrag.handleId + '_' + estilo;
	}
	if (!document.getElementById(writeDrag.writeId)) writeDrag.writeId='';
	if (!document.getElementById(writeDrag.handleId)) writeDrag.handleId='';

	var ob = 'onblur="writeDrag.hide()"';
	var ob = '';
	switch(estilo)
	{
		case 'url':
		case 'unomismo':
		case 'pagina':
		case 'glosario':
		case 'seccion':
			break;
		case 'objeto':
			ob = '';
			break;
  		default:
	}

	if ( dragObj.supported && typeof document.body.innerHTML != "undefined" )
	{
		if (!wd) wd = w;
		var cntnt = '<div class="iframe"><iframe name="IFRAME_'+writeDrag.dragId+'" '+ob+' FRAMEBORDER=0 src="'+url+'" width="'+w+'" height="'+h+'"></iframe></div>';
		writeDrag.set(e, cntnt, wd, offx, offy);
		return true;
	}
	else
		return false;
}

var capas = new Array();
var writeDrag = {
  // offX and offY can be numbers or "c"
  offX: 20,
  offY: 20,
  dragId:   "",  // id of positioned div to be dragged
  handleId: "",         // optional, place null or "" if no handle
  writeId:  "",    // optional, will write to dragId if null or empty string
  zIndex: 1000,
  // end of settings area - no need to edit below this line

  draggable: false,   // set true once dragObj.init called
  hideFlag: true,     // used in checkHide (document click)

  init: function() {   // initialize for dragging
    if (this.handleId) dragObj.init(this.handleId, this.dragId);
    else dragObj.init(this.dragId);
    this.draggable = true;
    // add handlers for hiding layer (esc key and doc click)
    dw_event.add( document, "click",   writeDrag.checkHide, false );
    dw_event.add( document, "keydown", writeDrag.checkKey,  true );
  },

  // called onclick of links (from wrapContent)
  set: function(e, cntnt, wd, offx, offy) {
	capas[capas.length]=this.dragId;
	
    this.hideFlag = false;  // click on link to show layer is also document click, which would hide it
    var wobj = this.writeId? document.getElementById( this.writeId ): document.getElementById( this.dragId );
    var dobj = document.getElementById( this.dragId );
    if ( !this.draggable ) this.init();
    this.hide();
    wobj.innerHTML = cntnt;
    if (wd) {
      // wd might be width of image, so add border and padding
      // rely on styles set inline (or lengthy code needed)
      var bw = dobj.style.borderWidth? parseInt(dobj.style.borderWidth): 0;
      var pw = wobj.style.padding? parseInt(wobj.style.padding): 0;
      wd += 2 * bw + 2 * pw;
      dobj.style.width = wd + "px";
    }
    this.positionIt(e, dobj, offx, offy);
  },

  positionIt: function(e, o, offx, offy) {
    var x=0, y=0; viewport.getAll();
    // check positioning choices
    if ( this.offX == "c" ) {
      x = Math.round( (viewport.width - o.offsetWidth)/2 ) + viewport.scrollX;
    } else {  // use mouse location onclick to position
      x = e.pageX? e.pageX: e.clientX + viewport.scrollX;
      offx = offx || this.offX;  // check for passed offsets
      if ( x + o.offsetWidth + offx > viewport.width + viewport.scrollX )
      {
        x = viewport.width + viewport.scrollX - o.offsetWidth;
		}
      else x = x + offx;
    }
    if ( this.offY == "c" ) {
      y = Math.round( (viewport.height - o.offsetHeight)/2 ) + viewport.scrollY;
    } else {
      y = e.pageY? e.pageY: e.clientY + viewport.scrollY;
      offy = offy || this.offY;
      if ( y + o.offsetHeight + offy > viewport.height + viewport.scrollY )
        y = viewport.height + viewport.scrollY - o.offsetHeight;
      else y = y + offy;
    }
    o.style.left = x + "px"; o.style.top = y + "px";
    document.getElementById(this.dragId).style.visibility = "visible";
    document.getElementById(this.dragId).style.zIndex = this.zIndex++;
    setTimeout("writeDrag.hideFlag = true",200);  // delayed until after checkHide
  },

  checkKey: function(e) { // check for esc key
    e = e? e: window.event;  if ( e.keyCode == 27 ) writeDrag.hide();
  },

  // doc click hides
  checkHide: function(e) {
// Elimininamos el cerrado autom tico al hacer clic en el docuemnto
return;

    dw_event.DOMit(e);
    // hide the layer if you click anywhere in the document
    // except a link that displays the layer (hideFlag), or on the layer itself,
    // unless that click on the layer is on the layer's close box
    if (e.tgt.nodeType && e.tgt.nodeType == 3) e.tgt = e.tgt.parentNode;  // text node?
    if ( contained( e.tgt, document.getElementById(writeDrag.dragId) ) ) {
      if ( e.tgt.tagName && e.tgt.tagName == "IMG" ) e.tgt = e.tgt.parentNode;
      if ( e.tgt.tagName == "A" && e.tgt.href.indexOf("writeDrag.hide") != -1 )
	  {
//		for (var i=0;i<capas.length;i++){ alert(capas[i]);writeDrag.hide(capas[i]);}
	  	writeDrag.hide();
	  }
      else return;
    }
    if (writeDrag.hideFlag) {/*for (var i=0;i<capas.length;i++){ writeDrag.hide(capas[i]);}*/ writeDrag.hide()};
  },

  hide: function(id) { if(typeof(id)=='undefined') id = writeDrag.dragId; document.getElementById(id).style.visibility = "hidden";}
}

// returns true of oNode is contained by oCont (container)
function contained(oNode, oCont) {
  while ( oNode.parentNode ) {
    oNode = oNode.parentNode;
    if ( oNode == oCont ) return true;
  }
  return false;
}

var dragObj = {
  supported: document.getElementById && (document.addEventListener || document.attachEvent),
  obj: null,
  zOrder: 1000,
  // a class can be attached to close box (or other elements) so mousedown on it won't trigger drag
  skipClass: "xBox",

  // id is that of object you mousedown on when you want to drag,
  // which may or may not be inside another element (rootID) which gets dragged
  init: function(id, rootID, x, y, minX, maxX, minY, maxY) {
    if (this.supported) {
      var o = document.getElementById(id);
      o.root = rootID? document.getElementById(rootID): o;
      o.idx = id; // used for checking in start
      //  pass x/y, set left/top inline or via script, or it gets set to 0,0
      if ( isNaN( parseInt(o.root.style.left) ) ) o.root.style.left = x? x + "px": 0 + "px";
      if ( isNaN( parseInt(o.root.style.top) ) )  o.root.style.top =  y? y + "px": 0 + "px";
      o.minX = minX; o.maxX = maxX; o.minY = minY; o.maxY = maxY;
      o.root.on_drag_start = function() {}
      o.root.on_drag = function() {}
      o.root.on_drag_end = function() {}
      dw_event.add( o, "mousedown", dragObj.start, false );
    }
  },

  start: function(e) {
    var o;
    e = dw_event.DOMit(e);

    // Check if moused down on an object that shouldn't trigger drag (close box, for example)
    if (e.tgt.nodeType && e.tgt.nodeType == 3) e.tgt = e.tgt.parentNode;  // text node?
    if (e.tgt.className && e.tgt.className == dragObj.skipClass ) return;

    if (this.idx) o = dragObj.obj = this;
    else {  // o != this for ie when using attachEvent
     while (!e.tgt.idx) e.tgt = e.tgt.parentNode;
     o = dragObj.obj = e.tgt;
    }
    o.root.style.zIndex = dragObj.zOrder++;
    o.downX = e.clientX; o.downY = e.clientY;
    o.startX = parseInt(o.root.style.left);
    o.startY = parseInt(o.root.style.top);
    o.root.on_drag_start(o.startX, o.startY);
    dw_event.add( document, "mousemove", dragObj.drag, true );
    dw_event.add( document, "mouseup",   dragObj.end,  true );
    e.preventDefault();
  },

  drag: function(e) {
    e = e? e: window.event;
    var o = dragObj.obj;
    // calculate new x/y values
    var nx = o.startX + e.clientX - o.downX;
    var ny = o.startY + e.clientY - o.downY;
    if ( o.minX != null ) nx = Math.max( o.minX, nx );
    if ( o.maxX != null ) nx = Math.min( o.maxX, nx );
    if ( o.minY != null ) ny = Math.max( o.minY, ny );
    if ( o.maxY != null ) ny = Math.min( o.maxY, ny );
    o.root.style.left = nx + "px"; o.root.style.top  = ny + "px";
    o.root.on_drag(nx,ny);
    return false;
  },

  end: function() {
    dw_event.remove( document, "mousemove", dragObj.drag, true );
    dw_event.remove( document, "mouseup",   dragObj.end,  true );
    if ( !dragObj.obj ) return; // avoid errors in ie if inappropriate selections
    dragObj.obj.root.on_drag_end( parseInt(dragObj.obj.root.style.left), parseInt(dragObj.obj.root.style.top) );
    dragObj.obj = null;
  }

}

var dw_event = {

  add: function(obj, etype, fp, cap) {
    cap = cap || false;
    if (obj.addEventListener) obj.addEventListener(etype, fp, cap);
    else if (obj.attachEvent) obj.attachEvent("on" + etype, fp);
  },

  remove: function(obj, etype, fp, cap) {
    cap = cap || false;
    if (obj.removeEventListener) obj.removeEventListener(etype, fp, cap);
    else if (obj.detachEvent) obj.detachEvent("on" + etype, fp);
  },

  DOMit: function(e) {
    e = e? e: window.event;
    e.tgt = e.srcElement? e.srcElement: e.target;

    if (!e.preventDefault) e.preventDefault = function () { return false; }
    if (!e.stopPropagation) e.stopPropagation = function () { if (window.event) window.event.cancelBubble = true; }

    return e;
  }

}

var viewport = {
  getWinWidth: function () {
    this.width = 0;
    if (window.innerWidth) this.width = window.innerWidth - 18;
    else if (document.documentElement && document.documentElement.clientWidth)
  		this.width = document.documentElement.clientWidth;
    else if (document.body && document.body.clientWidth)
  		this.width = document.body.clientWidth;
  },

  getWinHeight: function () {
    this.height = 0;
    if (window.innerHeight) this.height = window.innerHeight - 18;
  	else if (document.documentElement && document.documentElement.clientHeight)
  		this.height = document.documentElement.clientHeight;
  	else if (document.body && document.body.clientHeight)
  		this.height = document.body.clientHeight;
  },

  getScrollX: function () {
    this.scrollX = 0;
  	if (typeof window.pageXOffset == "number") this.scrollX = window.pageXOffset;
  	else if (document.documentElement && document.documentElement.scrollLeft)
  		this.scrollX = document.documentElement.scrollLeft;
  	else if (document.body && document.body.scrollLeft)
  		this.scrollX = document.body.scrollLeft;
  	else if (window.scrollX) this.scrollX = window.scrollX;
  },

  getScrollY: function () {
    this.scrollY = 0;
    if (typeof window.pageYOffset == "number") this.scrollY = window.pageYOffset;
    else if (document.documentElement && document.documentElement.scrollTop)
  		this.scrollY = document.documentElement.scrollTop;
  	else if (document.body && document.body.scrollTop)
  		this.scrollY = document.body.scrollTop;
  	else if (window.scrollY) this.scrollY = window.scrollY;
  },

  getAll: function () {
    this.getWinWidth(); this.getWinHeight();
    this.getScrollX();  this.getScrollY();
  }

}

/*************************************************************************
  This code is from Dynamic Web Coding at http://www.dyn-web.com/
  Copyright 2003 by Sharon Paine
  See Terms of Use at http://www.dyn-web.com/bus/terms.html
  regarding conditions under which you may use this code.
  This notice must be retained in the code as is!
*************************************************************************/


///////////////////////////////////////////////////////////////////////////////////////////////
// CCallWrapper
// IE no soporta setTimeout con argumentos, tal como describe la especificación Javascript.
// Esta clase permite llamadas asíncronas a una función respetando la lista de argumentos.

function CCallWrapper(aObjectReference, aDelay, aArgument0, aArgument1, aArgument2, aArgument3, aArgument4, aArgument5, aArgument6, aArgument7, aArgument8, aArgument9)
{
  this.mId = 'CCallWrapper_' + (CCallWrapper.mCounter++);
  this.mObjectReference = aObjectReference;
  this.mDelay     = aDelay;
  this.mTimerId = 0;
  this.mArgument0 = aArgument0; this.mArgument1 = aArgument1; this.mArgument2 = aArgument2; this.mArgument3 = aArgument3; this.mArgument4 = aArgument4;
  this.mArgument5 = aArgument5; this.mArgument6 = aArgument6; this.mArgument7 = aArgument7; this.mArgument8 = aArgument8; this.mArgument9 = aArgument9;
  CCallWrapper.mPendingCalls[this.mId] = this;
}

CCallWrapper.prototype.execute = function()
{
  this.mObjectReference(this.mArgument0, this.mArgument1, this.mArgument2, this.mArgument3, this.mArgument4, this.mArgument5, this.mArgument6, this.mArgument7, this.mArgument8, this.mArgument9);
  delete CCallWrapper.mPendingCalls[this.mId];
};

CCallWrapper.prototype.cancel = function()
{
  clearTimeout(this.mTimerId);
  delete CCallWrapper.mPendingCalls[this.mId];
};

CCallWrapper.asyncExecute = function (/* CCallWrapper */ callwrapper)
{
  CCallWrapper.mPendingCalls[callwrapper.mId].mTimerId = setTimeout('CCallWrapper.mPendingCalls["' + callwrapper.mId + '"].execute()', callwrapper.mDelay);
};

CCallWrapper.mCounter = 0;
CCallWrapper.mPendingCalls = {};

// CCallWrapper
///////////////////////////////////////////////////////////////////////////////////////////////


