/* 
    Javscript Document
    ------------ 
    Creation date:  03/03/2008
    Last edit:      09/04/2008
    Company name:   Onlime s.n.c
*/

var att = new Attender();

// Funzione per impostare l'opacità di una immagine
function setOpacity(obj,opacity)
{
  opacity=(opacity==100) ? 99.999 : opacity;
  // IE/Win
  obj.style.filter = "alpha(opacity:"+opacity+")";
  // Safari<1.2, Konqueror
  obj.style.KHTMLOpacity = opacity/100;
  // Older Mozilla and Firefox
  obj.style.MozOpacity = opacity/100;
  // Safari 1.2, newer Firefox and Mozilla, CSS3
  obj.style.opacity = opacity/100;
}

// Attende il caricamento completo dell'immagine
function loaded_image(){
  att.stop_attender();
}

// Funzione per simulare l'effetto fade-in su un oggetto
function fadeIn(objId,opacity)
{
  if(!document.getElementById) return;
  obj = document.getElementById(objId);  
  if(opacity <= 100)
  {
    setOpacity(obj,opacity);
    opacity += 10;
    window.setTimeout("fadeIn('"+objId+"',"+opacity+")", 130);
  }
  else{att.stop_attender();}
}

// Funzione per simulare l'effetto fade-out su un oggetto
function fadeOut(objId,lim_opacity,opacity)
{
  if(!document.getElementById) return; 
  obj = document.getElementById(objId);
  if(opacity >= lim_opacity)
  {
    setOpacity(obj,opacity);
    opacity -= 10;
    window.setTimeout("fadeOut('"+objId+"',"+lim_opacity+","+opacity+")", 130);
  }
  else{att.stop_attender();}
}

/* Definizione oggetto Gallery_News */
var Gallery_News = function(space_id,arr_image){

  this.space_id_name = space_id;
  this.space_id = document.getElementById(space_id);
  this.arr_image = arr_image || null;
  this.current_image = "";
  this.position_array = 0;
  
  var _this = this;
  var loader;
  var syn = false;
  var timer;
  
  this.get_current_image = function ()
  {  
    return (this.current_image);
  },
  
  this.get_space_id = function ()
  {  
    return (this.current_image);
  },
    
  this.next_image = function()
  { 
    if( this.position_array < this.arr_image.length - 1 )
    {
      this.position_array++;
      this.current_image = this.arr_image[this.position_array];
    }
    else
    {
      this.position_array = 0;
      this.current_image = this.arr_image[this.position_array];
    }
    
    this.do_effect(space_id + '_splash', this.current_image);
    //alert(this.current_image);
  },
  
  this.prev_image = function()
  { 
    if( this.position_array > 0 )
    {
      this.position_array--;
      this.current_image = this.arr_image[this.position_array];
    }
    else
    {
      this.position_array = this.arr_image.length - 1;
      this.current_image = this.arr_image[this.position_array];
    }
    
    //alert(this.current_image);
    this.do_effect(space_id + '_splash', this.current_image);
  },
  
  this.do_effect = function (objId,new_src_image)
  { 
    if(!syn && !att.get_stato()){
      //var _this = this;
      timer = setInterval( function(){_this.do_effect(objId,new_src_image);} , 100);
      syn = true;
      att.set_step(0);
      att.set_nextstep(null);
    }
    else if(syn && !att.get_stato()){
      obj = document.getElementById(objId);
      if(att.get_step()==0){
        att.start_attender();
        //loader = document.getElementById('ultimi_eventi_spazio1_gallery');
        loader = document.getElementById(objId).parentNode;
        loader.style.backgroundImage = "url(http://www.onlime.it/encanthica/images/loading.gif)";
        fadeOut(objId,0,100);                   // Lancio il fadeOut
      } 
      else if(att.get_step()==1){
        att.start_attender();
        obj.src=new_src_image;                  // Eseguo lo swop
      }
      else if(att.get_step()==2){
        att.start_attender();
        fadeIn(objId,0);                        // Lancio il fadeIn
      }
      else if(att.get_step()==3){
        att.set_nextstep(0);
        loader = document.getElementById(objId).parentNode;
        //loader = document.getElementById('ultimi_eventi_spazio1_gallery');
        loader.style.backgroundImage = "url(inesistente)";
        clearInterval(timer);
        syn = false;
      }
    }
  },
  
  /* Attacco gestione eventi */
  document.getElementById(this.space_id_name + '_prev_image').onclick = function(){_this.prev_image();},
  document.getElementById(this.space_id_name + '_next_image').onclick = function(){_this.next_image();},
  document.getElementById(this.space_id_name + '_splash').onload =  function(){loaded_image();}
};

/* Definizione oggetto Gallery */
var Gallery = function(space_id,arr_image){

  this.space_id_name = space_id;
  this.space_id = document.getElementById(space_id);
  this.arr_image = arr_image || null;
  this.page_position = 0;
  this.current_images = Array();
  
  this.space_imgs = document.getElementById(this.space_id_name).getElementsByTagName('img'); 
  
  /* x ora nn utilizzato (riscontrati problemi con js legato e ie...)
  var spl;
  
  // Attacco gestione eventi 
  for(i=0;i<this.space_imgs.length;i++){
    //this.space_imgs[i].onload =  function(){loaded_image();}
    
    this.space_imgs[i].onclick =  function(){
      spl = document.getElementById('gallery_splash');
      spl.style.height = document.height; // nn funzia in ie....
      spl.style.display ='inline';
    }
  };
  */
  
  var _this = this;
  
  this.view_page = function(n_page)
  { 
    this.page_position = n_page
    this.current_images = this.arr_image[this.page_position];
    this.do_effect();
  },
   
  this.do_effect = function ()
  { 
    for(i=0;i<this.space_imgs.length;i++){
      if(this.current_images[i] == undefined)
      {
        this.space_imgs[i].style.display = 'none';
      }
      else
      {
        //alert(i + " -> " + this.current_images[i]);
        this.space_imgs[i].style.display = 'inline';
        this.space_imgs[i].src = this.current_images[i];
      }
       
    }
  }  
  
}
