/*
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 BannerPanel4 (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.BannerPanel4.images.length) {
      //if (window.navigator.appVersion.split('; ')[1] == 'MSIE 60') window.BannerPanel4.container.innerHTML = 'nenhum banner selecionado';
      var r = new ajaxRequest();
      eval ('window.BannerPanel4.images = ' + r.get(baseURL + '/admin/banners' + window.BannerPanel4.moduleId + '/banners.php') + ';');
    }

    var img = document.createElement('img');
    img.setAttribute('src', window.BannerPanel4.images[this.current].src);
    //img.setAttribute('height', window.BannerPanel4.container.getAttribute('height'));
    //img.setAttribute('width',  window.BannerPanel4.container.getAttribute('width'));

    window.BannerPanel4.setImage(img);
  }

  this.ChangeImage = function () {
    window.BannerPanel4.current = ( window.BannerPanel4.current + 1 > window.BannerPanel4.images.length-1) ? 0 : window.BannerPanel4.current + 1;
    //window.BannerPanel4.Effect.Execute(window.BannerPanel4.container, window.BannerPanel4.setCurrentImage);
    //window.BannerPanel4.Effect.Start(window.BannerPanel4.container);
    window.BannerPanel4.setCurrentImage();
    //window.BannerPanel4.Effect.Stop(window.BannerPanel4.container);
  }

  this.setImage = function (img) {
    var lastImg = null;

    for (var i=0; i<window.BannerPanel4.container.childNodes.length; i++) {
      if (window.BannerPanel4.container.childNodes[i].nodeType == 1) {
        lastImg = window.BannerPanel4.container.childNodes[i];
      }
    }

    if (lastImg) {
      // troca a image
      //alert(lastImg); alert(img);
      window.BannerPanel4.container.replaceChild(img, lastImg);
    } else {
      // adiciona a imagem
      window.BannerPanel4.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.BannerPanel4 && ! window.BannerPanel4.images) window.BannerPanel4.images = new Array();

      window.BannerPanel4.images.add(im);
    }

    window.BannerPanel4.setCurrentImage();
  }

  this.initialize(id);
}

function BannerPanel4_OnLoad() {
  if (! document.getElementById('banner4') ) return;
  
  window.BannerPanel4 = new BannerPanel4('banner4');

  window.setInterval(window.BannerPanel4.ChangeImage, 10000);
}

addEvent(window, 'load', BannerPanel4_OnLoad);

