var FRAMEWINDOW_FIXED = 0x1;
var FRAMEWINDOW_MODAL = 0x2;

var currentWindow;
var SCROLLBAR_WIDTH = 9;
var ADMINBAR_HEIGHT = 0;
var FRAME_EDGE_WIDTH = 30;
var BASE_ZINDEX = 200;
var rCount = 0;
var dimmerVisible = false;
var ignoreNextClose = false;

if (window.ie) {
  SCROLLBAR_WIDTH = 1;
} else if (window.safari) {
  SCROLLBAR_WIDTH = 1;
} else {
  SCROLLBAR_WIDTH = 11;
}
/* INICIO GERAL */
if (typeof aeOL == 'undefined')
{
 var aeOL = [];
 var addEvent = function(o, n, f, l)
 {
  var d = 'addEventListener', h = 'on' + n, t, a;
  if (o[d] && !l) return o[d](n, f, false);
  if (!o.aE) { o.aE = aeOL.length || 1; aeOL[o.aE] = { o:o } }
  t = aeOL[o.aE][n] || (aeOL[o.aE][n] = []);
  for (var i = 0; i < t.length; i++)
   for (var j = 0; j < t[i].length; j++)
    if (t[i][j] == f) return;
  if (o[h] && o[h]._ae)
  {
   a = t[t.length - 1];
   a[a.length] = f;
  }
  else
  {
   t[t.length] = o[h] ? [o[h], f] : [f];
   o[h] = new Function('e', 'var r = true, i = 0, o = aeOL[' + o.aE + '].o,' +
    'a = aeOL[' + o.aE + ']["' + n + '"][' + (t.length - 1) + '];' +
    'for (; i < a.length; i++) { ' +
   'o._f = a[i]; r = o._f(e||window.event) != false && r; o._f = null;' +
    '} return r');
   o[h]._ae = 1;
  }
 };
 var removeEvent = function(o, n, f, l)
 {
  var d = 'removeEventListener', t, a, i, j, s;
  if (o[d] && !l) return o[d](n, f, false);
  if (!o.aE || !aeOL[o.aE]) return;
  t = aeOL[o.aE][n];
  i = t.length;
  while (i--)
  {
   a = t[i];
   j = a.length;
   s = 0;
   while (j--)
   {
    if (a[j] == f) s = 1;
    if (s) a[j] = a[j + 1];
   }
   if (s) { a.length--; break }
  }
 };
}
function getViewportHeight() {
  if (self.innerHeight) // all except Explorer
  {
    y = self.innerHeight;
  }
  else if (document.documentElement && document.documentElement.clientHeight)
    // Explorer 6 Strict Mode
  {
    y = document.documentElement.clientHeight;
  }
  else if (document.body) // other Explorers
  {
    y = document.body.clientHeight;
  }
  return( y );
}

function getViewportWidth() {
  if (self.innerHeight) // all except Explorer
  {
    x = self.innerWidth;
  }
  else if (document.documentElement && document.documentElement.clientHeight)
    // Explorer 6 Strict Mode
  {
    x = document.documentElement.clientWidth;
  }
  else if (document.body) // other Explorers
  {
    x = document.body.clientWidth;
  }
  return( x );
}


function getViewportScrollX() {
  if (self.pageYOffset) // all except Explorer
  {
    x = self.pageXOffset;
  }
  else if (document.documentElement && document.documentElement.scrollTop)
    // Explorer 6 Strict
  {
    x = document.documentElement.scrollLeft;
  }
  else if (document.body) // all other Explorers
  {
    x = document.body.scrollLeft;
  }
  return( x );
}

function getViewportScrollY() {
  if (self.pageYOffset) // all except Explorer
  {
    y = self.pageYOffset;
  }
  else if (document.documentElement && document.documentElement.scrollTop)
    // Explorer 6 Strict
  {
    y = document.documentElement.scrollTop;
  }
  else if (document.body) // all other Explorers
  {
    y = document.body.scrollTop;
  }
  return( y );
}
/* FIM GERAL*/
var windowIndex = 0;
var windowStack = new Array();

function FrameWindow() {

}

FrameWindow.prototype.reposition = function () {

  var c = document.getElementById( this.id + "Container" );
  var b = document.getElementById( this.id + "Body" );
  var width, height;

  if (this.maximized) {
    width = getViewportWidth() - (SCROLLBAR_WIDTH * 2);
    height = getViewportHeight();
  } else {
    width = this.width + (FRAME_EDGE_WIDTH * 2);
    height = this.height + (FRAME_EDGE_WIDTH * 2);
  }

  if (!this.maximized) {

    var heightCandidate = height;
    if (height > (getViewportHeight() - ADMINBAR_HEIGHT)) {
      height = getViewportHeight() - ADMINBAR_HEIGHT;
    }

  }

  c.style.height = (height) + "px";
  c.style.width = (width) + "px";

  if (!this.maximized) {

    var topCandidate = (((getViewportHeight() - ADMINBAR_HEIGHT - height) / 2) + getViewportScrollY() + ADMINBAR_HEIGHT);
    if (topCandidate < (ADMINBAR_HEIGHT)) {
      topCandidate = ADMINBAR_HEIGHT;
    }

  } else {

    topCandidate = getViewportScrollY();

  }

  c.style.top = topCandidate + "px";
  c.style.left = (((getViewportWidth() - width) / 2) - SCROLLBAR_WIDTH) + "px";

  this.bodyHeight = (height - (FRAME_EDGE_WIDTH * 2));
  this.bodyWidth = (width - (FRAME_EDGE_WIDTH * 2));

  // reposition long edges

  document.getElementById( this.id + "h" ).style.height = (this.bodyHeight - (FRAME_EDGE_WIDTH * 2)) + "px";
  document.getElementById( this.id + "i" ).style.height = (this.bodyHeight - (FRAME_EDGE_WIDTH * 2)) + "px";

  document.getElementById( this.id + "c" ).style.width = (this.bodyWidth - (FRAME_EDGE_WIDTH * 2)) + "px";
  document.getElementById( this.id + "n" ).style.width = (this.bodyWidth - (FRAME_EDGE_WIDTH * 2)) + "px";

  // reposition full body

  b.style.height = this.bodyHeight + "px";
  b.style.width = this.bodyWidth + "px";

};

FrameWindow.prototype.show = function () {

  frames[this.id + "Body"].location.href = this.url;
  document.getElementById( this.id + "Container" ).style.display = "";
  document.getElementById( this.id + "Container" ).style.visibility = "visible";
  document.getElementById( this.id + "Container" ).style.zIndex = BASE_ZINDEX + this.windowIndex;

};

FrameWindow.prototype.hide = function () {

  document.getElementById( this.id + "Container" ).style.visibility = "hidden";

  frames[this.id + "Body"].tinyMCE = null;
  frames[this.id + "Body"].location.href = "blank.html";

};

FrameWindow.prototype.toggleMaximized = function () {

  this.maximized = !this.maximized;
  this.reposition();
  if (window.ie) { this.reposition(); } // you have got to be kidding.

};

function openWindow(url, width, height, options) {

  ++windowIndex;

  nWindow = new FrameWindow();
  nWindow.id = "window" + windowIndex;
  nWindow.contentWindow = document.getElementById("window" + windowIndex + "Body").contentWindow;
  nWindow.parentWindow = currentWindow;
  nWindow.url = url;
  nWindow.width = width;
  nWindow.height = height;
  nWindow.windowIndex = windowIndex;
  nWindow.resizable = !(options & FRAMEWINDOW_FIXED);
  nWindow.maximized = false;

  nWindow.reposition();
  nWindow.show();

  showDimmer();

  currentWindow = nWindow;
  windowStack.push( nWindow );

}

function getUsingFrameWindows() {
  return true;
}


function getOpenerWindow() {

  if (currentWindow.parentWindow) {
    return( currentWindow.parentWindow.contentWindow );
  } else {
    return( window );
  }

}

function closeSpecificWindow(id) {
  if (id == windowIndex) {
    closeWindow();
  }
}

function closeWindow(reloadLocation) {

  if (windowIndex > 0) {

    currentWindow.hide();
    windowStack.pop();

    --windowIndex;

    if (windowStack.length > 0) {
      currentWindow = windowStack[windowStack.length - 1];
    } else {
      hideDimmer();
      currentWindow = null;
    }

  }

  if (reloadLocation) {
    window.location.href = reloadLocation;
  }

}

function showDimmer() {

  if (!dimmerVisible) {

    document.getElementById("windowDimmer").style.height = (getViewportScrollY() + getViewportHeight()) + "px";
    document.getElementById("windowDimmer").style.width = "100%";
    document.getElementById("windowDimmer").style.display = "block";

    // hide all embedded objects

    /*var os = document.getElementsByTagName("object");
    for(i = 0; i < os.length; ++i) {
      os[i].style.visibility = "hidden";
    }

    var os = document.getElementsByTagName("embed");
    for(i = 0; i < os.length; ++i) {
      os[i].style.visibility = "hidden";
    }*/
	var os = document.getElementsByTagName("select");
    for(i = 0; i < os.length; ++i) {
      os[i].style.visibility = "hidden";
    }
	

    dimmerVisible = true;

  }

}


function hideDimmer() {

  if (dimmerVisible) {

    document.getElementById("windowDimmer").style.display = "none";

    // show all embedded objects

    /*var os = document.getElementsByTagName("object");
    for(i = 0; i < os.length; ++i) {
      os[i].style.visibility = "visible";
    }

    var os = document.getElementsByTagName("embed");
    for(i = 0; i < os.length; ++i) {
      os[i].style.visibility = "visible";
    }*/
	var os = document.getElementsByTagName("select");
    for(i = 0; i < os.length; ++i) {
      os[i].style.visibility = "visible";
    }

    dimmerVisible = false;

  }

}

addEvent(window, "resize", function() {

  document.getElementById("windowDimmer").style.height = (getViewportScrollY() + getViewportHeight()) + "px";
  for (var i = 0; i < windowStack.length; ++i) {
    windowStack[i].reposition();
  }

});


// IE 5/6 won't recognize PNG transparency properly -- fix (detection taken from start.com)
// IE 7 actually works properly

var badPNG = (/MSIE ((5\.5)|[6])/.test(navigator.userAgent) && navigator.platform == "Win32");
 if (badPNG) {
   // congratulations IE.  AlphaImageLoader doesn't work using SSL
   if (document.location.href.indexOf("https") == 0) {
     document.write('<link rel="stylesheet" type="text/css" media="screen" href="c/badpng-noshadow.css"/>');
   } else {
     document.write('<link rel="stylesheet" type="text/css" media="screen" href="c/badpng.css"/>');
   }
}

// check for window scrolling

function framewindowScrollAdjust() {

  document.getElementById("windowDimmer").style.height = (getViewportScrollY() + getViewportHeight()) + "px";
  for (var i = 0; i < windowStack.length; ++i) {
    windowStack[i].reposition();
  }

}

addEvent(window, "scroll", framewindowScrollAdjust);

// fix frame initial src in safari

if (window.safari) {
  addEvent(window, "load", function() {
    if (frames["window1Body"]) { frames["window1Body"].location.href = "blank.html"; }
    if (frames["window2Body"]) { frames["window2Body"].location.href = "blank.html"; }
    if (frames["window3Body"]) { frames["window3Body"].location.href = "blank.html"; }
  }); // and this will only work in safari, since we have explicitly set onload for other browsers (and thus this is ignored?)
}