// globales
var tamanoTextoNoticia = 12;
var tamanoTextoComentarios = 12;

var enviarCorreoVisible = false;

// default text
var busquedaDefText = 'Buscas algo?';
var emailDefText = 'E-mail';

function init() {
  initCssHacks();
  var objMedia = $('media');
  if (!enviarCorreoVisible) {
    var obj = $('enviar_correo');
    if (obj) {
      obj.style.display = 'none';
    }
  }
  var nuBody = $('nu_body');
  if (nuBody) {
     nuBody.style.display = 'none';
  }
  var neNoticiasRecientes = $('ne_noticias_recientes');
  if (neNoticiasRecientes) {
    neNoticiasRecientes.style.display = 'none';
  }
  updatePageSize();
}

function initCssHacks() {
  if (is_ie6) {
    var pageOffsetHeight = $('page').offsetHeight;
    $('sombra_left').style.height = pageOffsetHeight + 'px';
    $('sombra_right').style.height = pageOffsetHeight + 'px';
    $('busqueda_right_border').style.right = '-1px';
    var btnReducirTexto = $('btnReducirTexto');
    if (btnReducirTexto) {
      btnReducirTexto.onmouseover = function() {
        btnReducirTexto.style.backgroundImage = 'url(img/reducir_texto_btn_hover.jpg)';
      }
      btnReducirTexto.onmouseout = function() {
        btnReducirTexto.style.backgroundImage = 'url(img/reducir_texto_btn.jpg)';
      }
    }
    var btnAumentarTexto = $('btnAumentarTexto');
    if (btnAumentarTexto) {
      btnAumentarTexto.onmouseover = function() {
        btnAumentarTexto.style.backgroundImage = 'url(img/aumentar_texto_btn_hover.jpg)';
      }
      btnAumentarTexto.onmouseout = function() {
        btnAumentarTexto.style.backgroundImage = 'url(img/aumentar_texto_btn.jpg)';
      }
    }
    var btnEstadisticas = $('btnEstadisticas');
    if (btnEstadisticas) {
      btnEstadisticas.onmouseover = function() {
        btnEstadisticas.style.backgroundImage = 'url(img/estadisticas_btn_hover.jpg)';
      }
      btnEstadisticas.onmouseout = function() {
        btnEstadisticas.style.backgroundImage = 'url(img/estadisticas_btn.jpg)';
      }
    }
    var btnImprimir = $('btnImprimir');
    if (btnImprimir) {
      btnImprimir.onmouseover = function() {
        btnImprimir.style.backgroundImage = 'url(img/imprimir_btn_hover.jpg)';
      }
      btnImprimir.onmouseout = function() {
        btnImprimir.style.backgroundImage = 'url(img/imprimir_btn.jpg)';
      }
    }
    var btnEmviarEmail = $('btnEmviarEmail');
    if (btnEmviarEmail) {
      btnEmviarEmail.onmouseover = function() {
        btnEmviarEmail.style.backgroundImage = 'url(img/enviar_email_btn_hover.jpg)';
      }
      btnEmviarEmail.onmouseout = function() {
        btnEmviarEmail.style.backgroundImage = 'url(img/enviar_email_btn.jpg)';
      }
    }
    var total_comentarios = $('total_comentarios');
    if (total_comentarios) {
      total_comentarios.onmouseover = function() {
        total_comentarios.style.backgroundImage = 'url(img/comentarios_btn_hover.jpg)';
        total_comentarios.style.color = '#CE0000';
      }
      total_comentarios.onmouseout = function() {
        total_comentarios.style.backgroundImage = 'url(img/comentarios_btn.jpg)';
        total_comentarios.style.color = '#666666';
      }
    }
    var btnCerrarEnviarCorreo = $('btnCerrarEnviarCorreo');
    if (btnCerrarEnviarCorreo) {
      btnCerrarEnviarCorreo.onmouseover = function() {
        btnCerrarEnviarCorreo.style.backgroundImage = 'url(img/cerrar_btn_hover.gif)';
      }
      btnCerrarEnviarCorreo.onmouseout = function() {
        btnCerrarEnviarCorreo.style.backgroundImage = 'url(img/cerrar_btn.gif)';
      }
    }
    var btnBuscar = $('btnBuscar');
    if (btnBuscar) {
      btnBuscar.onmouseover = function() {
        btnBuscar.style.backgroundImage = 'url(img/busqueda_btn_hover.jpg)';
      }
      btnBuscar.onmouseout = function() {
        btnBuscar.style.backgroundImage = 'url(img/busqueda_btn.jpg)';
      }
    }
    var btnPublicar = $('btnPublicar');
    if (btnPublicar) {
      btnPublicar.onmouseover = function() {
        btnPublicar.style.backgroundImage = 'url(img/publicar_btn_hover.jpg)';
      }
      btnPublicar.onmouseout = function() {
        btnPublicar.style.backgroundImage = 'url(img/publicar_btn.jpg)';
      }
    }
    var btnReportes = $('btnReportes');
    if (btnReportes) {
      btnReportes.onmouseover = function() {
        btnReportes.style.backgroundImage = 'url(img/reportes_btn_hover.jpg)';
      }
      btnReportes.onmouseout = function() {
        btnReportes.style.backgroundImage = 'url(img/reportes_btn.jpg)';
      }
    }
    var btnReporteros = $('btnReporteros');
    if (btnReporteros) {
      btnReporteros.onmouseover = function() {
        btnReporteros.style.backgroundImage = 'url(img/reporteros_btn_hover.jpg)';
      }
      btnReporteros.onmouseout = function() {
        btnReporteros.style.backgroundImage = 'url(img/reporteros_btn.jpg)';
      }
    }
    var btnRegistrate = $('btnRegistrate');
    if (btnRegistrate) {
      btnRegistrate.onmouseover = function() {
        btnRegistrate.style.backgroundImage = 'url(img/registrate_btn_hover.jpg)';
      }
      btnRegistrate.onmouseout = function() {
        btnRegistrate.style.backgroundImage = 'url(img/registrate_btn.jpg)';
      }
    }
  }
}

var imagesRootPath = 'img/';
function preloadImages(param) {
  var img = new Image();
  if (isArray(param)) {
    for (var i = 0; i < param.length; ++i)
      img.src = imagesRootPath + param[i];
  } else {
    img.src = imagesRootPath + param;
  }
}

function aumentarTexto() {
  if (tamanoTextoNoticia < 18) {
    tamanoTextoNoticia++;
    tamanoTextoComentarios++;
    $('noticia_contenido').style.fontSize = tamanoTextoNoticia + 'px';
    $('comentarios').style.fontSize = tamanoTextoComentarios + 'px';
    updatePageSize();
  }
}

function reducirTexto() {
  if (tamanoTextoNoticia > 8) {
    tamanoTextoNoticia--;
    tamanoTextoComentarios--;
    $('noticia_contenido').style.fontSize = tamanoTextoNoticia + 'px';
    $('comentarios').style.fontSize = tamanoTextoComentarios + 'px';
    updatePageSize();
  }
}

// param = field : field to clear
// param = def : default value
// desc : clears the field if it has a default value
function clearText(field, def) {
  var elem = $(field);
  if (elem.value == def) {
    elem.value = '';
  }
}

// param = field : field to fill
// param = def : default value
// desc : sets the default value of a field
function setDefText(field, def) {
  var elem = $(field);
  if (elem.value == '') {
    elem.value = def;
  }
}

function updatePageSize() {
  if (is_ie6) {
    var page = $('page');
    $('sombra_left').style.height = page.offsetHeight + 'px';
    $('sombra_right').style.height = page.offsetHeight + 'px';
  }
}

var ns_resizing = false;
function ns_header_Click() {
  if (ns_resizing == false) {
    ns_resizing = true;
    var nsBody = $('ns_body');
    var nsHeaderImg = $('ns_header_img');
    if (nsBody.style.display == 'none') {
      nsHeaderImg.src = '/img/menu_abierto.jpg';
      nsHeaderImg.style.marginRight = '2px';
      Effect.BlindDown(nsBody, {
        afterFinish: function() {
          updatePageSize();
          ns_resizing = false;
        }
      });
      nsBody.style.overflow = 'auto';
    } else if (nsBody.style.display == '') {
      nsHeaderImg.src = '/img/menu_cerrado.jpg';
      nsHeaderImg.style.marginRight = '5px';
      Effect.BlindUp(nsBody, {
        afterFinish: function() {
          updatePageSize();
          ns_resizing = false;
        }
      });
    }
  }
}

var nu_resizing = false;
function nu_header_Click() {
  if (nu_resizing == false) {
    nu_resizing = true;
    var nuBody = $('nu_body');
    var nuHeaderImg = $('nu_header_img');
    if (nuBody.style.display == 'none') {
      nuHeaderImg.src = '/img/menu_abierto.jpg';
      nuHeaderImg.style.marginRight = '2px';
      Effect.BlindDown(nuBody, {
        afterFinish: function() {
          updatePageSize();
          nu_resizing = false;
        }
      });
      nuBody.style.overflow = 'auto';
    } else if (nuBody.style.display == '') {
      nuHeaderImg.src = '/img/menu_cerrado.jpg';
      nuHeaderImg.style.marginRight = '5px';
      Effect.BlindUp(nuBody, {
        afterFinish: function() {
          updatePageSize();
          nu_resizing = false;
        }
      });
    }
  }
}

function btnEnviarEmail_Click() {
  var enviarCorreo = $('enviar_correo');
  enviarCorreoVisible = !enviarCorreoVisible;
  if (enviarCorreoVisible && enviarCorreo.style.display == 'none' && enviarCorreo.offsetHeight == 0) {
    Effect.BlindDown(enviarCorreo, { afterFinish: updatePageSize });
  } else if (!enviarCorreoVisible && enviarCorreo.style.display == '' && enviarCorreo.offsetHeight == 323) {
    Effect.BlindUp(enviarCorreo, { afterFinish: updatePageSize });
  }
}

function btnLista_Click() {
  updatePageSize();
  var lista = $('ne_noticias_recientes');
  var grid = $('noticias_etiqueta');
  grid.style.display = 'none';
  lista.style.display = '';
  updatePageSize();
  // setTimeOut('updatePageSize', 1000);
  // Effect.BlindUp(grid, { afterFinish: function() { Effect.BlindDown(lista); } });
}

function btnGrid_Click() {
  updatePageSize();
  var lista = $('ne_noticias_recientes');
  var grid = $('noticias_etiqueta');
  lista.style.display = 'none';
  grid.style.display = '';
  updatePageSize();
  // setTimeOut('updatePageSize', 1000);
  // Effect.BlindUp(lista, { afterFinish: function() { Effect.BlindDown(grid); } });
}

var resizingNoticiaComponerComentario = false;
function btnCerrarComponerComentario_Click() {
  if (resizingNoticiaComponerComentario == false) {
    resizingNoticiaComponerComentario = true;
    var noticiaNuevoComentario = $('noticia_nuevo_comentario');
    var agregarComentario = $('agregar_comentario');
    if (noticiaNuevoComentario.style.display == '') {
      agregarComentario.innerHTML = '[+] Comentar ';
      noticiaNuevoComentario.style.display = 'none';
    } else {
      agregarComentario.innerHTML = '[-] Comentar ';
      noticiaNuevoComentario.style.display = '';
    }
    agregarComentario.innerHTML += '<img src="../../img/bubble_icon.gif" alt="" />';
    resizingNoticiaComponerComentario = false;
    updatePageSize();
  }
}

function isArray(obj) {
 if (obj.constructor.toString().indexOf("Array") == -1)
    return false;
 else
    return true;
}

