/**
 * BWin 
 * @version	1.3
 * @author	André Mendonça
 * @update	27/10/2005
 * @link	http://corp2.grupos.com.br/~andre/lib/js/bwin/
 */
function BWin(id, title, closeDestroy) {

    this.win = document.createElement('div');
    this.win.className = 'bwin';
    this.win.id = id;
    this.win.style.visibility = 'hidden';
    this.win.obj = this;

    this.setWidth(300);

    if (closeDestroy) {
        this.closeDestroy = true;
    }
    else {
        this.closeDestroy = false;
    }

    if (title) {
        this.createTitle(title);
    }
    
    this.moveTo(10,10);

    this.content = document.createElement('div');
    this.content.className = 'content';
    this.win.appendChild(this.content);



    document.body.appendChild(this.win);    
    
    /**
     * Eventos
     */
    this.onclose = null;
    this.ondestroy = null;
    this.onhide = null;
    this.onshow = null;
    this.onchangecontent = null;
}


BWin.prototype.call_onchangecontent = function() {
	if (this.onchangecontent != null) {
		this.onchangecontent();
	}
}

BWin.prototype.call_onclose = function() {
	if (this.onclose != null) {
		this.onclose();
	}
}

BWin.prototype.call_ondestroy = function() {
	if (this.ondestroy != null) {
		this.ondestroy();
	}
}

BWin.prototype.call_onhide = function() {
	if (this.onhide != null) {
		this.onhide();
	}
}

BWin.prototype.call_onshow = function() {
	if (this.onshow != null) {
		this.onshow();
	}
}

/**
 * @access	protected
 * @return	void
 */
BWin.prototype.createTitle = function(title) {
    // div titlebar
    var header = document.createElement('div');
    header.className = 'titlebar';
    this.win.appendChild(header);

    // h3
    var h = document.createElement('h3');
    h.className = 'title';
    header.appendChild(h);
    h.appendChild(document.createTextNode(title));

    // button close
    var close = document.createElement('span');
    close.obj = this;
    close.onclick = function() {
        this.obj.close();
    }
    close.appendChild(document.createTextNode('close'));
    header.appendChild(close);
    
    header.win = this;
    header.onmousedown = BWin_startDrag;
}

function BWin_startDrag(e) {
    this.relativeX = this.win.mouseX(e) - this.win.getX();
    this.relativeY = this.win.mouseY(e) - this.win.getY();
    this.drag = true;
    
    var body = document.getElementsByTagName('body');
    
    document.drag = this;
    document.onmousemove = BWin_moveDrag;
    document.onmouseup = BWin_endDrag;
}

function BWin_moveDrag(e) {
    this.drag.win.moveTo(this.drag.win.mouseX(e) - this.drag.relativeX, this.drag.win.mouseY(e) - this.drag.relativeY);
}

function BWin_endDrag(e) {
    this.drag.relativeX = 0;
    this.drag.relativeY = 0;
    document.onmousemove = null;
    document.onmouseup = null;
    this.drag = null;
}

BWin.prototype.mouseX = function(e) {
    if (!e) var e = window.event;
    if (e.pageX)
    {
        return e.pageX;
    }
    else if (e.clientX)
    {
        return e.clientX + document.body.scrollLeft;
    }
    return false;
}

BWin.prototype.mouseY = function(e) {
    if (!e) var e = window.event;
    if (e.pageY)
    {
        return e.pageY;
    }
    else if (e.clientY)
    {
        return e.clientY + document.body.scrollTop;
    }
    return false;
}

BWin.prototype.setContent = function(content) {
    this.content.innerHTML = content;
    this.call_onchangecontent();
}

BWin.prototype.show = function() {
    this.win.style.visibility = 'visible';
    this.call_onshow();
}

BWin.prototype.setOpacity = function(opacity) {
   if (opacity < 100) {
      var moz = new String(opacity / 100);
   }
   else {
      var moz = new String(0.99);
   }
	this.win.style.filter = "alpha(opacity="+opacity+")";
	this.win.style.MozOpacity = moz;
	this.win.style.opacity = moz;
   this.opacity = opacity;  
}

BWin.prototype.showOpacity = function(delay) {
   this.setOpacity(0);
   this.show();
   window.setTimeout("_BWin_stepShowOpacity('" + this.win.id + "'," + delay +")" , delay);
}

function _BWin_stepShowOpacity(id, delay) {
   var win = document.getElementById(id);
   if (!win) {
      return false;
   }
      var step = 100 / delay;      
      var next = Math.ceil( win.obj.opacity + step);
//      win.obj.setContent(next);      
   if (next >= 100) {
      win.obj.setOpacity(100);
   }   
     else {
      win.obj.setOpacity(next);  
      window.setTimeout("_BWin_stepShowOpacity('" + win.id + "'," + delay +")" , delay);
   }   
}



BWin.prototype.hide = function() {
    this.win.style.visibility = 'hidden';
    this.call_onhide();

}

BWin.prototype.destroy = function() {
	this.call_ondestroy();
    this.win.parentNode.removeChild(this.win);    
}

BWin.prototype.close = function() {
    if (this.closeDestroy) {
        this.destroy();
    }
    else {
        this.hide();
    }
    this.call_onclose();
}

BWin.prototype.moveTo = function(x, y) {
    this.setX(x);
    this.setY(y);
}

BWin.prototype.setX = function(x) {
    this.win.style.left = x + 'px';
    this.win.x = x;
}

BWin.prototype.setY = function(y) {
    this.win.style.top = y + 'px';
    this.win.y = y;
}

BWin.prototype.getX = function() {
    return this.win.x;
}
BWin.prototype.getY = function() {
    return this.win.y;
}

BWin.prototype.setSize = function(w, h) {
    this.setWidth(w);
    this.setHeight(h);
}

BWin.prototype.setWidth = function(w) {
    if (w) {
        this.win.style.width = w + 'px';
    }
    else {
        this.win.style.width = 'auto';
    }
}

BWin.prototype.getWidth = function() {
	return this.win.clientWidth;
}
BWin.prototype.getHeight = function() {
	return this.win.clientHeight;
}



BWin.prototype.setHeight = function(h) {
    if (h) {
        this.win.style.height = h + 'px';
    }
    else {
        this.win.style.height = 'auto';
    }
}


/**
 * Centraliza a janela na tela
 * @access	public
 * @since	1.2
 * @return	void
 */
BWin.prototype.center = function() {
	var de 		= document.documentElement;
	var left 	= (de.clientWidth - this.getWidth()) / 2 + de.scrollLeft;
	var top 	= (de.clientHeight - this.getHeight()) / 2 + de.scrollTop;
	this.moveTo(left, top);
}


/**
 * Centraliza a janela no cursor do mouse
 * @since 	1.2
 * @param 	Event	event	Evento "e"
 * @param	int		offsetX	Opcional, deslocar X pixels 
 * @param	int		offsetY	Opcional, deslocar Y pixels
 * @return 	void
 */
BWin.prototype.centerMouse = function(event, offsetX, offsetY) {
	
	if (!offsetX) {
		offsetX = 0;
	}
	if (!offsetY) {
		offsetY = 0;
	}
	
	var x = this.mouseX(event) - (this.getWidth() / 2) + offsetX;
	var y = this.mouseY(event)- (this.getHeight() / 2) + offsetY;
	this.moveTo(x, y);
}
