function preloadImages() {
  arImageSrc = new Array (
  '../images/verificando.gif',
  '../images/top_peque.gif',
  '../images/gris.gif',
  '../images/verde.gif',
  '../images/amarillo.gif',
  '../images/rojo.gif',
  '../images/cross_g.gif',
  '../images/tick_g.gif',
  '../images/cross.gif',
  '../images/tick.gif',
  '../images/b_verificar1.gif',
  '../images/b_verificar2.gif',
  '../images/b_comentar1.gif',
  '../images/b_comentar2.gif',
  '../images/importante.gif'
  )
  arImageList = new Array ();
  for (counter in arImageSrc) {
    arImageList[counter] = new Image();
    arImageList[counter].src = arImageSrc[counter];
  }
}

function showBox(){
  var arrayPageSize = getPageSize();
  $('overlay').setStyle({ width: arrayPageSize[0] + 'px', height: arrayPageSize[1] + 'px' });
  $('overlay').show();
  center('box');
  return false;
}

function hideBox(){
  $('box').hide();
  $('overlay').hide();
  return false;
}

function center(element){
  try{
    element = $(element);
  }catch(e){
    return;
  }
  var my_width = 0;
  var my_height = 0;
  if ( typeof( window.innerWidth ) == 'number' ){
    my_width = window.innerWidth;
    my_height = window.innerHeight;
  }else if ( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ){
    my_width = document.documentElement.clientWidth;
    my_height = document.documentElement.clientHeight;
  }else if ( document.body && ( document.body.clientWidth || document.body.clientHeight ) ){
    my_width = document.body.clientWidth;
    my_height = document.body.clientHeight;
  }
  element.style.position = 'absolute';
  element.style.zIndex = 99;
  var scrollY = 0;
  if ( document.documentElement && document.documentElement.scrollTop ){
    scrollY = document.documentElement.scrollTop;
  }else if ( document.body && document.body.scrollTop ){
    scrollY = document.body.scrollTop;
  }else if ( window.pageYOffset ){
    scrollY = window.pageYOffset;
  }else if ( window.scrollY ){
    scrollY = window.scrollY;
  }
  var elementDimensions = Element.getDimensions(element);
  var setX = ( my_width - elementDimensions.width ) / 2;
  var setY = ( my_height - elementDimensions.height ) / 2 + scrollY;
  setX = ( setX < 0 ) ? 0 : setX;
  setY = ( setY > 0 ) ? 450 : setY;
  element.style.left = setX + "px";
  element.style.top = setY + "px";
  element.style.display = 'block';
}

function drags_init() {
  Drags.destroy();
  Drops.destroy();
  new Drag('windowlabel', {caption: 'Opinión', self:true});
}

function getKey(e) {
  if (e == null) { // ie
    keycode = event.keyCode;
    escapeKey = 27;
  } else { // mozilla
    keycode = e.keyCode;
    escapeKey = e.DOM_VK_ESCAPE;
  }
  key = String.fromCharCode(keycode).toLowerCase();

  if(keycode == escapeKey){	// close lightbox
    hideBox();
  }
}

function getPageSize() {
  var xScroll, yScroll;

  if (window.innerHeight && window.scrollMaxY) {
    xScroll = window.innerWidth + window.scrollMaxX;
    yScroll = window.innerHeight + window.scrollMaxY;
  } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
    xScroll = document.body.scrollWidth;
    yScroll = document.body.scrollHeight;
  } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
    xScroll = document.body.offsetWidth;
    yScroll = document.body.offsetHeight;
  }

  var windowWidth, windowHeight;

  if (self.innerHeight) {	// all except Explorer
    if(document.documentElement.clientWidth){
      windowWidth = document.documentElement.clientWidth;
    } else {
      windowWidth = self.innerWidth;
    }
    windowHeight = self.innerHeight;
  } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
    windowWidth = document.documentElement.clientWidth;
    windowHeight = document.documentElement.clientHeight;
  } else if (document.body) { // other Explorers
    windowWidth = document.body.clientWidth;
    windowHeight = document.body.clientHeight;
  }

  // for small pages with total height less then height of the viewport
  if(yScroll < windowHeight){
    pageHeight = windowHeight;
  } else {
    pageHeight = yScroll;
  }

  // for small pages with total width less then width of the viewport
  if(xScroll < windowWidth){
    pageWidth = xScroll;
  } else {
    pageWidth = windowWidth;
  }

  return [pageWidth,pageHeight];
}

function showhideDiv(thediv, idimg) {
  thediv = $(thediv);
  theimg = $(idimg);
  if (Element.visible(thediv)) { 
    new Effect.SlideUp(thediv, { duration: 0.25 });
    /*theimg.src = '../images/x-button.gif';*/
  } else { 
    new Effect.SlideDown(thediv, { duration: 0.25 });
    /*theimg.src = '../images/m-button.gif';*/
  }
}

function hideDiv(thediv){
  thediv = $(thediv);
  if (!Element.visible(thediv)) { 
    new Effect.SlideDown(thediv, { duration: 0.25 });
  }
}

function displayDiv(thediv){
  thediv = $(thediv);
  if (Element.visible(thediv)) { 
    new Effect.SlideUp(thediv, { duration: 0.25 });
  }
}

window.onload = function() {
  drags_init();
  preloadImages();
  document.onkeypress = getKey;
}