/* cbe_fade.js
 * Extention to CBE v4.19, Cross-Browser DHTML API from Cross-Browser.com (mike@cross-browser.com)
 * Only works with browsers that support alpha blending.
 * @author: Cory Simpson (csimpson@fulcrm.com)
 */

CrossBrowserElement.prototype.setOpacity = function(percent) {
  if(is.ie5up && !is.mac) this.ele.filters.alpha.opacity = percent;
  else if (is.gecko) {this.ele.style.MozOpacity= percent/100;}
  else return;
}

CrossBrowserElement.prototype.getOpacity = function() {
  if(is.ie5up && !is.mac) return this.ele.filters.alpha.opacity;
  else if (is.gecko) return this.ele.style.MozOpacity*100;
  else return null;
}

//only works if you have first done a fadeIn or a fadeOut.
CrossBrowserElement.prototype.resetOpacity = function() {
  if(is.ie5up && !is.mac) this.ele.filters.alpha.opacity = this.oldOpacity;
  else if (is.nav6up) {this.ele.style.MozOpacity = this.oldOpacity/100}
  else return null;
}

CrossBrowserElement.prototype.fadeIn = function(fadeSpeed, fadePercent, endListener, startOpacity, endOpacity) {
  //Set the default for what you want the opacity to end up as
  if(endOpacity == null) this.endOpacity=100;
  else this.endOpacity=endOpacity;

  //Add an end listener.
  if (endListener && window.cbeEventJsLoaded) {
    this.autoRemoveListener = true;
    this.addEventListener('fadeend', endListener);
  }

  this.oldOpacity = this.getOpacity();

  //Set defaults for initial opacity level
  if((is.ie5up && !is.mac) || is.nav6up){
    if(!startOpacity) this.setOpacity(0);
    else this.setOpacity(startOpacity);
  }
  else {this.show(); this.execFadeend(); return;}

  //Set the defaults for the speed of the fade and the amount that the fade percentage will jump each iteration.
  if(fadeSpeed && fadePercent) {
    this.fadeSpeed=fadeSpeed/(100/fadePercent);
    this.fadePercent=fadePercent;
  }
  else if(fadeSpeed && !fadePercent) {
    this.fadeSpeed = fadeSpeed/10;
    this.fadePercent=10;
  }
  else if(!fadeSpeed && fadePercent) {
    this.fadeSpeed=300/(100/fadePercent);
    this.fadePercent = fadePercent;
  }
  else {
    this.fadeSpeed=30;
    this.fadePercent = 10;
  }

  //Make sure the element is visible
  this.show();

  this.direction = 'in';
  this.fade();
}

CrossBrowserElement.prototype.fadeOut = function(fadeSpeed, fadePercent, endListener, endOpacity) {
  //Set the default for what you want the opacity to end up as
  if(endOpacity == null) this.endOpacity=0;
  else this.endOpacity=endOpacity;

  //Add an end listener that will reset the opacity to the original level and hide the element.
  //if a end listener is added append it to the rest.
  if (window.cbeEventJsLoaded) {
    this.autoRemoveListener = true;
    var listener = '';
    if(this.endOpacity == 0) {
      listener = 'window.cbeAll['+this.index+'].hide(); window.cbeAll['+this.index+'].resetOpacity();'
    }
    if(endListener) listener += endListener;
    this.addEventListener('fadeend', listener);
  }

  //Set defaults for initial opacity level
  if((is.ie5up && !is.mac) || is.gecko){
    var opacity = this.getOpacity();
    if(opacity == null || opacity == '' || opacity == 0) this.setOpacity(100);
  }
  else {this.execFadeend(); return;}

  this.oldOpacity = this.getOpacity();

  //Set the defaults for the speed of the fade and the amount that the fade percentage will jump each iteration.
  if(fadeSpeed && fadePercent) {
    this.fadeSpeed=fadeSpeed/(100/fadePercent);
    this.fadePercent=fadePercent;
  }
  else if(fadeSpeed && !fadePercent) {
    this.fadeSpeed = fadeSpeed/10;
    this.fadePercent=10;
  }
  else if(!fadeSpeed && fadePercent) {
    this.fadeSpeed=300/(100/fadePercent);
    this.fadePercent = fadePercent;
  }
  else {
    this.fadeSpeed=30;
    this.fadePercent = 10;
  }

  this.direction = 'out';
  this.fade();
}

CrossBrowserElement.prototype.fade = function() {
  var currentOpacity = this.getOpacity();
  if(this.direction == 'in' && currentOpacity < this.endOpacity) {
    this.setOpacity(currentOpacity+this.fadePercent);
    setTimeout('window.cbeAll['+this.index+'].fade()', this.fadeSpeed);
  }
  else if(this.direction == 'out' && currentOpacity > this.endOpacity) {
    this.setOpacity(currentOpacity-this.fadePercent);
    setTimeout('window.cbeAll['+this.index+'].fade()', this.fadeSpeed);
  }
  else {
    this.execFadeend();
  }
}

CrossBrowserElement.prototype.execFadeend = function() {
  if (this.onfadeend) {
    var exp = this.onfadeend;
    if (this.autoRemoveListener && window.cbeEventJsLoaded) {
      this.autoRemoveListener = false;
      this.removeEventListener('fadeend');
    }
    cbeEval(exp, this);
  }
}