/*
function FadeEffect () {
  this.timer = 0;
  this.speed = 7;

  window.changeOpacity = function (opacity) {
    window.obj.opacity=(opacity/100);
    window.obj.MozOpacity=(opacity/100);
    window.obj.KhtmlOpacity=(opacity/100);
  }

  this.Execute = function (obj, change_cb) {
    window.obj = obj;
    this.Start();
    window.setTimeout(change_cb, this.timer++ * this.speed);
    this.Stop();
  }

  this.Start = function (obj) {
    for(i = 185; i > 0;  i-=2) {
      var call = 'window.changeOpacity('+i+')';
      window.setTimeout(call, this.timer++ * this.speed);
    }
  }

  this.Stop = function (obj) {
    for(i = 0; i <= 185; i+=2) {
      var call = 'window.changeOpacity('+i+')';
      window.setTimeout(this.changeOpacity, this.timer++ * this.speed, obj, i);
    }
  }
}
*/
function BannerPanel2 (id) {
  this.moduleId = '';
  this.container = null;
  this.images = null;
  this.current = 0;
  //this.Effect = new FadeEffect();

  this.initialize = function (id) {
	/*  	
    var divs = document.getElementsByTagName('div');

    for (var i=1; i<divs.length; i++) {
      if (divs[i].className.split(' ').contains('banner')) {
        this.container = divs[i];
        break;
      }
    }
    */
	
	this.container = document.getElementById(id);
	if (!this.container) return;
	
	this.moduleId = id.toString().replace('banner', '');

    this.loadImages();
  }

  this.setCurrentImage = function () {
    //FIX pra IE6
    if (!window.BannerPanel2.images.length) {
      //if (window.navigator.appVersion.split('; ')[1] == 'MSIE 60') window.BannerPanel2.container.innerHTML = 'nenhum banner selecionado';
      var r = new ajaxRequest();
      eval ('window.BannerPanel2.images = ' + r.get(baseURL + '/admin/banners' + window.BannerPanel2.moduleId + '/banners.php') + ';');
    }

    var img = document.createElement('img');
    img.setAttribute('src', window.BannerPanel2.images[this.current].src);
    //img.setAttribute('height', window.BannerPanel2.container.getAttribute('height'));
    //img.setAttribute('width',  window.BannerPanel2.container.getAttribute('width'));

    window.BannerPanel2.setImage(img);
  }

  this.ChangeImage = function () {
    window.BannerPanel2.current = ( window.BannerPanel2.current + 1 > window.BannerPanel2.images.length-1) ? 0 : window.BannerPanel2.current + 1;
    //window.BannerPanel2.Effect.Execute(window.BannerPanel2.container, window.BannerPanel2.setCurrentImage);
    //window.BannerPanel2.Effect.Start(window.BannerPanel2.container);
    window.BannerPanel2.setCurrentImage();
    //window.BannerPanel2.Effect.Stop(window.BannerPanel2.container);
  }

  this.setImage = function (img) {
    var lastImg = null;

    for (var i=0; i<window.BannerPanel2.container.childNodes.length; i++) {
      if (window.BannerPanel2.container.childNodes[i].nodeType == 1) {
        lastImg = window.BannerPanel2.container.childNodes[i];
      }
    }

    if (lastImg) {
      // troca a image
      //alert(lastImg); alert(img);
      window.BannerPanel2.container.replaceChild(img, lastImg);
    } else {
      // adiciona a imagem
      window.BannerPanel2.container.appendChild(img);
    }
  }

  this.loadImages = function () {
    this.images = new Array();
    var r = new ajaxRequest();
    r.get(baseURL + '/admin/banners' + this.moduleId + '/banners.xml', this.loadImages_Response);
  }

  this.loadImages_Response = function (doc) {
    var imgs = doc.getElementsByTagName('banner');

    for (var i=0; i<imgs.length; i++) {
      var im = new Image();
      im.src = imgs[i].getAttribute('src');

      //IE fix!
      if (window.BannerPanel2 && ! window.BannerPanel2.images) window.BannerPanel2.images = new Array();

      window.BannerPanel2.images.add(im);
    }

    window.BannerPanel2.setCurrentImage();
  }

  this.initialize(id);
}

function BannerPanel2_OnLoad() {
  if (! document.getElementById('banner2') ) return;
  
  window.BannerPanel2 = new BannerPanel2('banner2');

  window.setInterval(window.BannerPanel2.ChangeImage, 10000);
}

addEvent(window, 'load', BannerPanel2_OnLoad);

