/*
	Last published on : 15-Dec-11 11:35
	Location          : \commercial\selfassist\cms\selfassist\static\js
	Filename          : overlay.js
*/
function Overlay(container, properties){
  if(container){
    if(typeof(container) == 'object')
      this.container = container;
    else if(typeof(container) == 'string')
      this.container = YAHOO.util.Dom.get(container);
    else
      this.container = document.getElementsByTagName('body')[0];
  }else{
    this.container = document.getElementsByTagName('body')[0];
  }
   
  this.properties = properties;

  this.properties['id'] = (this.properties['id'] ? this.properties['id'] : 'overlay');
  this.properties['iframe-id'] = (this.properties['iframe-id'] ? this.properties['iframe-id'] : this.properties['id'] + '-iframe');
  this.properties['className'] = (this.properties['className'] ? this.properties['className'] : 'overlay');
  
  if(this.properties['animate']){
    this.properties['opacity'] = this.properties['opacity'] ? this.properties['opacity'] : .5
    this.properties['speed'] = this.properties['speed'] ? this.properties['speed'] : .5
  }
}

Overlay.prototype.show = function(){
  var overlayExists = true;
  var overlayIFrameExists = true;
  var overlay = YAHOO.util.Dom.get(this.properties['id']);
  if(!overlay){
    overlay = document.createElement('div');
    overlay.id = this.properties['id'];
    overlayExists = false;
  }
  
  YAHOO.util.Dom.addClass(this.container, 'overlay-container');

  if(this.properties['className'] != null)
    overlay.className = this.properties['className'];


  if(document.all){ //IE6
    var iframe = YAHOO.util.Dom.get(this.properties['iframe-id']);
    if(!iframe){
      iframe = document.createElement('iframe');
      iframe.id = this.properties['iframe-id'];
      overlayIFrameExists = false;
    }
    iframe.frameBorder = 0;
    iframe.scrolling = 'no';
    iframe.src = 'about:blank';

    if(!overlayIFrameExists)
      this.container.appendChild(iframe);
    iframe = null;
  }

  if(!overlayExists)
    this.container.appendChild(overlay);

  overlay.style.display = 'block';

  var attributes = {
    opacity : { to: this.properties['opacity'] }
  }

  if(this.properties['animate']){
    anim = new YAHOO.util.Motion(overlay, attributes, this.properties['speed'], YAHOO.util.Easing.easeOut);
    anim.onComplete.subscribe(this.onShowComplete);
    anim.animate();
  }

  overlay = null;
}

Overlay.prototype.onShowComplete = function(){

}

Overlay.prototype.hide = function(){
  var overlay = YAHOO.util.Dom.get(this.properties['id']);
  if(this.properties['animate']){
    var attributes = {
      opacity : { to: 0 }
    }
    anim = new YAHOO.util.Motion(overlay, attributes, this.properties['speed'], YAHOO.util.Easing.easeOut);
    anim.controller = this;
    anim.onComplete.subscribe(this.onHideComplete);
    anim.animate();
  }else{
    YAHOO.util.Dom.removeClass(this.container, 'overlay-container');
    this.container.removeChild(overlay);
    var iframe = YAHOO.util.Dom.get(this.properties['iframe-id']);
    if(iframe)
      this.container.removeChild(iframe);
  }
}

Overlay.prototype.onHideComplete = function(){
  var overlay = YAHOO.util.Dom.get(this.controller.properties['id']);
  var iframe = YAHOO.util.Dom.get(this.controller.properties['iframe-id']);

  this.controller.container.removeChild(overlay);

  if(iframe)
    this.controller.container.removeChild(iframe);

  YAHOO.util.Dom.removeClass(this.controller.container, 'overlay-container');
} 

