/* LibJS Ilunabar 1.1.1 */

var ilunabar = {
  "loadEvent":function (fnc) {
    // Incorpora a window funciones para ejecutar con window.onload
    if (typeof window.addEventListener != "undefined") {
      window.addEventListener("load", fnc, false);
    } else {
      if (typeof window.attachEvent != "undefined") {
        window.attachEvent("onload", fnc);
      }
    }
  },

  "redirect":function (url) {
    // Redirigimos a la URL indicada
    document.location = url;
  },

  "getXY":function (id) {
    // Obtenemos la posición X e Y de un elemento
    var obj = document.getElementById(id);
    var curleft = curtop = 0;
    if (obj.offsetParent) {
      do {
        curleft += obj.offsetLeft;
        curtop += obj.offsetTop;
      } while (obj = obj.offsetParent);
    }
    return [curleft,curtop];
  },

  "getWindowSize":function () {
    var width, height;
    this.width = window.innerWidth || (window.document.documentElement.clientWidth || window.document.body.clientWidth);
    this.height = window.innerHeight || (window.document.documentElement.clientHeight || window.document.body.clientHeight);
    return this;
  },

  "slideShow":function (params) {
    // ANIMACION: slideShow básico
    if (!params) {alert("slideShow: faltan parametros"); return;}

    // Elemento nodo, debe tener una posicion relative, absolute o fixed y width y height
    if (params.nodo) {var nodo = params.nodo;} else {alert("slideShow: falta nodo de insercción"); return;}

    // Array de imagenes y sus datos
    if (params.imagenes && params.imagenes.length > 1) {var imgFiles = params.imagenes;} else {alert("slideShow: se necesitan al menos dos imágenes"); return;}

    // Tiempo en segundos entre imagenes
    if (params.tiempo) {var tiempo = params.tiempo * 1000;} else {var tiempo = 5000;}

    // Duración del fade
    if (params.fade) {var fade = params.fade;} else {var fade = 2;}

    // Raiz de la web, para encontrar los botones del slide
    if (params.raizhtm) {var raizhtm = params.raizhtm;} else {var raizhtm = "/";}

    // Control de botones de play
    if (params.controls) {var controls = params.controls;} else {var controls = false;}

    // Control de inicio del slide, por omisión el slide esta en play
    if (params.play) {var play = params.play;} else {var play = false;}

    nodo = $(nodo);
    var hide = 0; // Posicion en el array de la imagen a ocultar
    var show = 1; // Posicion en el array de la imagen a mostrar
    var _ancho = nodo.getWidth(); // Ancho del nodo
    var _alto = nodo.getHeight(); // Alto del nodo

    // Creamos un contenedor para el slide
    var contenedor = new Element("div");
    contenedor.setStyle({"width":_ancho+"px", "height":_alto+"px", "position":"relative"});
    nodo.insert({"bottom":contenedor});

    // Creamos las capas a mostrar
    var imagenes = [];
    var enlaces = [];

    imagenes[0] = new Element("img", {"src":imgFiles[0].file, "alt":""});
    imagenes[0].setStyle({"position":"absolute", "left":"0px", "top":"0px"});
    contenedor.insert({"bottom":imagenes[0]});

    for (var i = 1; i < imgFiles.length; i++) {
      imagenes[i] = new Element("img", {"src":imgFiles[i].file, "alt":""});
      imagenes[i].setStyle({"position":"absolute", "left":"0px", "top":"0px", "opacity":0});
      contenedor.insert({"bottom":imagenes[i]});
    }

    if (imgFiles[0].url.length > 0) {
      var clase = (imgFiles[0].lightview)? "slideLink lightview":"slideLink";
    } else {
      var clase = "slideNoLink";
    }
    enlaces[0] = new Element("a", {"href":imgFiles[0].url, "class":clase, "title":imgFiles[0].title});
    enlaces[0].setStyle({"position":"absolute"});
    enlaces[0].innerHTML = imgFiles[0].text;
    contenedor.insert({"bottom":enlaces[0]});

    for (var i = 1; i < imgFiles.length; i++) {
      prueba = imgFiles[i].url.length;
      if (imgFiles[i].url.length > 0) {
        clase = (imgFiles[i].lightview)? "slideLink lightview":"slideLink";
      } else {
        clase = "slideNoLink";
      }
      enlaces[i] = new Element("a", {"href":imgFiles[i].url, "class":clase, "title":imgFiles[i].title});
      enlaces[i].setStyle({"position":"absolute", "display":"none"});
      enlaces[i].innerHTML = imgFiles[i].text;
      contenedor.insert({"bottom":enlaces[i]});
    }

    if (controls) {
      // Ponemos la capa final para controlar el slide
      _ancho = 64; // Ancho de la zona de botones
      _alto = 64; // Alto de la zona de botones
      var _left = (nodo.getWidth() - _ancho) / 2;
      var _top = (nodo.getHeight() - _alto) / 2;
      var zonaBotones = new Element("div");
      zonaBotones.setStyle({"position":"absolute", "left":_left+"px", "top":_top+"px", "width":_ancho+"px", "height":_alto+"px", "background":"url("+raizhtm+"lib-js/ilunabar/img/slide/fondo-vacio) repeat"});
      contenedor.insert({"bottom":zonaBotones});

      // Hacemos play o stop
      zonaBotones.observe("click", function () {
        if (play) {
          play = false;
          zonaBotones.setStyle({"background":"url("+raizhtm+"lib-js/ilunabar/img/slide/play) no-repeat center"});
        } else {
          play = true;
          zonaBotones.setStyle({"background":"url("+raizhtm+"lib-js/ilunabar/img/slide/stop) no-repeat center"});
        }
      });

      // Aparecen play o stop
      zonaBotones.observe("mouseover", function () {
        if (play) {
          zonaBotones.setStyle({"background":"url("+raizhtm+"lib-js/ilunabar/img/slide/stop) no-repeat center"});
        } else {
          zonaBotones.setStyle({"background":"url("+raizhtm+"lib-js/ilunabar/img/slide/play) no-repeat center"});
        }
      });

      // Desaparecen play y stop
      zonaBotones.observe("mouseout", function () {
        zonaBotones.setStyle({"background":"url("+raizhtm+"lib-js/ilunabar/img/slide/fondo-vacio) repeat"});
      });
    }

    // Slide
    var animacion = setInterval(function () {
      if (play) {
        new Effect.Opacity(imagenes[hide], {from: 1.0, to: 0.0, duration: fade});
        new Effect.Opacity(imagenes[show], {from: 0.0, to: 1.0, duration: fade});
        enlaces[hide].fade({duration: fade});
        enlaces[show].appear({duration: fade});
        hide++;
        show++;
        if (hide >= imagenes.length) {hide = 0;}
        if (show >= imagenes.length) {show = 0;}
      }
    }, tiempo);
  },

  "roll":function (imagen, tiempo) {
    // Rollover con fade, si todo sale bien con el evento onmouseover deberia bastar
    // Ejemplo:
    // <span style="position: relative;">
    //   <img src="pao2.jpg" alt="" style="position: absolute;" />
    //   <img src="pao1.jpg" alt="" onmouseover="ilunabar.roll(this, 1.0);" style="position: absolute;" />
    // </span>
    if (!tiempo) {var tiempo = 0.5;}

    imagen.onmouseout = function () {
      new Effect.Opacity(imagen, {from: 0.0, to: 1.0, duration: tiempo});
    }
    new Effect.Opacity(imagen, {from: 1.0, to: 0.0, duration: tiempo});
  }

};

