/**
 * @author krzysztof
 */

var SliderShow = {
  initialize: function() {
    this.images = new Array(
      { src : 'facebook',   href : 'http://facebook.com',   title : 'Facebook'   },
      { src : 'orkut',      href : 'http://orkut.com',      title : 'Orkut'      },
      { src : 'hi5',        href : 'http://hi5.com',        title : 'Hi5'        },
      { src : 'myspace',    href : 'http://myspace.com',    title : 'MySpace'    },
      { src : 'yahoo',      href : 'http://yahoo.com',      title : 'Yahoo'      },
      { src : 'sonico',     href : 'http://sonico.com',     title : 'Sonico'     },
      { src : 'bebo',       href : 'http://bebo.pl',        title : 'Bebo'       },
      { src : 'ning',       href : 'http://ning.com',       title : 'Ning'       },
      { src : 'yonja',      href : 'http://yonja.com',      title : 'Yonja'      },
      { src : 'friendster', href : 'http://friendster.com', title : 'Friendster' },
      { src : 'dernek',     href : 'http://dernek.ba',      title : 'Dernek'     },
      { src : 'iwiw',       href : 'http://iwiw.hu',        title : 'Iwiw'       },
      { src : 'jesper',     href : 'http://jesper.nu',      title : 'Jesper'     }
    );

//       {src : 'gamedesire' , href : 'http://www.gamedesire.com' , title : 'GameDesire.com'} ,
//       {src : 'myspace', href : 'http://myspace.com', title : 'myspace.com'},
//       {src : 'friendster', href : 'http://friendster.com', title : 'Friendster.com'},
//       {src : 'facebook', href : 'http://apps.facebook.com/gdgames/', title : 'Facebook.com'},
//       {src : 'bebo', href : 'http://bebo.com', title : 'Bebo.com'},
//       {src : 'poker4chips' , href : 'http://www.poker4chips.com' , title : 'Poker4Chips.com'} ,
//       {src : 'wp' , href : 'http://gryonline.wp.pl' , title : 'GryOnline WP'} ,
//       {src : 'gazeta' , href : 'http://gryonline.gazeta.pl' , title : 'Gryonline Gazeta.pl'} ,
//       {src : 'interia' , href : 'http://gramy.interia.pl' , title : 'Gramy Interia.pl'} ,
//       {src : 'atrativa' , href : 'http://atrativa.com.br' , title : 'Atrativa.com.br'} ,
//       {src : 'univision' , href : 'http://juegosenlinea.univision.com' , title : 'Univision.com'} ,
//       {src : 'fueps' , href : 'http://multi.fueps.com' , title : 'Fueps.com'} ,
//       {src : 'popyeah' , href : 'http://www.popyeah.com' , title : 'Popyeah.com'} ,
//       {src : 'portalro' , href : 'http://jocuri.portal.ro' , title : 'Portal.ro'} ,
//       {src : 'hispavista' , href : 'http://juegos-online.hispavista.com' , title : 'Hispavista.com'} ,
//       {src : 'jiggy' , href : 'http://www.jiggy.it' , title : 'Jiggy.it'},
//       {src : 'takplayer', href : 'http://takplayer.com', title : 'takplayer.com'},
//       {src : 'juegostv', href : 'http://multiplayer.juegos.tv', title : 'multiplayer.juegos.tv'},
//       {src : 'hi5', href : 'http://hi5.com', title : 'hi5.com'}
//       /*{src : 'messaggero' , href : 'http://giochi.caltanet.it/' , title : 'Caltanet.it'}*/

    this.currentlyShown = new Array();
    this.oImages = new Array();
    this.div = $('links2');
    this.divs = this.div.getElementsByTagName('div');
    this.lastRandCol = -1;
    this.lastRandSite = -1;
    this.images_number = 7;
//     this.addr = 'http://www.ganymede.eu';
    
    for(var i = 0; i < this.images.length; i++) {
      var a = document.createElement('a');
//       a.target = '_blank';
      a.href = '/links.html';//this.images[i].href;
      var img = new Image();
      img.title = this.images[i].title;
      img.src = './images/logos2/' + this.images[i].src + '.png';
      a.appendChild(img);
      this.oImages.push(a);
    }
    
    for(var i = 0 ; i < this.images_number; i++) {
      var div = document.createElement('div');
      Element.addClassName(div , 'links_img');
      div.appendChild(this.oImages[i]);
      this.div.appendChild(div);
      this.currentlyShown.push(this.images[i].title);
    }
      this.timer = setInterval(this.fadeRandom.bind(this), 3000);
  },
    
  fadeRandom: function() {
      //rand = obrazek do wstawienia
      var rand = Math.round(Math.random() * 100);
      while(rand > (this.images.length - 1) || this.currentlyShown.indexOf(this.images[rand].title) > -1 || rand == this.lastRandSite) {
        rand = Math.round(Math.random() * 100);
      }
      
      this.lastRandSite = rand;
      
      //rand2 kolumna do zastapienia
      var rand2 = Math.round(Math.random() * 10);
      while(rand2 >= this.images_number || parseInt(this.lastRandCol) == rand2) {
        rand2 = Math.round(Math.random() * 10);
      }
      
      this.lastRandCol = rand2;
      
      var a = this.divs[rand2].down();
      new Effect.Opacity(a, {duration:1, from: 1.0, to: 0});
      var tmp = a.down().title;
      Element.remove(a);
      
      var a = this.oImages[rand];
      
      if(/MSIE/.test(navigator.userAgent) && (!a.hasLayout)) {
        Element.setStyle(a, {zoom: 1});
        Element.setStyle(a ,{opacity : 0});
      }
      else {
        Element.setStyle(a , {opacity : 0});  
      }
      
      this.divs[rand2].appendChild(a);
      
      this.currentlyShown = this.currentlyShown.without(tmp);
      this.currentlyShown.push(this.images[rand].title);
      new Effect.Opacity(a, {duration:1, from: 0.0, to: 1.0});
  }
}

window.onload = function() {
  if($('links2')) {
    SliderShow.initialize();
  }
}