/***
 * 
 * ActiveBar - A simple crossbrowser messagebar                             
 * Copyright (C) 2006 Jakob Westhoff <jakob@westhoffswelt.de>              
 * 
 * This program is free software; you can redistribute it and/or modify 
 * it under the terms of the GNU General Public License as published by 
 * the Free Software Foundation; either version 2 of the License, or (at 
 * your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License 
 * along with this program; if not, write to the Free Software Foundation, 
 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. 
 * 
 * You may contact the author by mail:
 * jakob@westhoffswelt.de
 * 
 * Or write to:
 * Jakob Westhoff
 * Kleiner Floraweg 35
 * 44229 Dortmund
 * Germany
 *
 * The latest version of ActiveBar can be obtained from:
 * http://www.westhoffswelt.de/
 *
 **/

//
// Constructor
//
function ActiveBar( text )
{
    this._text = text;

    // Do we have an IE as a client?
    this._isIE = ( ( document.all ) && ( navigator.userAgent.indexOf( 'Opera' ) == -1 ) );

    // Retrieve the body node
    var o = document.getElementsByTagName( "body" );
    this._bodyNode = o[0];

    // Create the Activebar div area
    var activebar = document.createElement( "div" );
    this._activebarNode = activebar;
    activebar.style.display = "none";
    activebar.className = "activebar";
    activebar.style.height = this._barHeight + "px";
    activebar.style.top = "0px";
    activebar.style.left = "0px";
    activebar.style.width = "100%";
    if ( this._isIE )
    {
        activebar.style.position = "absolute";
    }
    this._bodyNode.appendChild( activebar );

    // Create the text area inside the Activebar
    var textArea = document.createElement( "div" );
    this._textAreaNode = textArea;
    textArea.className = "messageText";
    this._activebarNode.appendChild( textArea );

    // Set the appropriate text.
    // InnerHTML is used to support html tags in the text.
    this._textAreaNode.innerHTML = text;

    //Create the close node
    var close = document.createElement( "div" );
    this._closeNode = close;
    close.className = "closeButton";
    this._activebarNode.appendChild( close );
    
    var self = this;
    // We only need to handle the scrollposition for the ie, because every other browser supports "position:fixed"
    if ( this._isIE ) 
    {
        this._registerEvent( window, "scroll", function(event) { self._onScroll(event, self) } );
    }

    this._registerEvent( this._closeNode, "click", function(event) { self.hide(); } );
}

ActiveBar.prototype = {

    //
    // Private attributes
    //
    _text:                  false,
    _bodyNode:              false,
    _activebarNode:         false,
    _textAreaNode:          false,
    _textNode:              false,
    _closeNode:             false,
    _slideIn:               false,
    _slideDelay:            30,
    _currentTop:            0,
    _barHeight:             24,
    _topBorder:             0,
    _animation_in_progress: false,
    _animationTimer:        false,
    _isIE:                  false,
    
    
    //
    // Private functions
    //
    _getScrollOffsetY: function() 
    {
        if( typeof( window.pageYOffset ) == "number" ) 
        {
            //Netscape compliant
            return window.pageYOffset;
        }
        else if( document.body != undefined && ( document.body.scrollLeft != undefined || document.body.scrollTop != undefined ) ) 
        {
            //DOM compliant
            return document.body.scrollTop;
        }
        else if( document.documentElement != undefined && ( document.documentElement.scrollLeft != undefinded || document.documentElement.scrollTop !=	undefined ) ) 
        {
            //IE6 standards compliant mode
            return document.documentElement.scrollTop;
        }
    },

	_registerEvent: function(element, event, handler) 
	{
		if (typeof element.addEventListener != "undefined") {   //Dom2
			element.addEventListener(event, handler, false);
		} else if (typeof element.attachEvent != "undefined") { //IE 5+
			element.attachEvent("on" + event, handler);
		} else {
			if (element["on" + event] != null) {
				var oldHandler = element["on" + event];
				element["on" + event] = function(e) {
					oldHander(e);
					handler(e);
				};
			} else {
				element["on" + event] = handler;
			}
		}
	},

    _slideAnimate: function() 
    {
        if ( this._slideIn  == true ) 
        {
            this._activebarNode.style.top = this._currentTop-- + "px";
            if ( this._currentTop ==  this._topBorder - this._barHeight - 1 ) 
            {
                this._activebarNode.style.display = "none";
                this._animation_in_progress = false;   
                return;
            }
        }
        else 
        {
            this._activebarNode.style.top = this._currentTop++ + "px";
            if ( this._currentTop == this._topBorder + 1 )
            {
                this._animation_in_progress = false;   
                return;
            }
        }
        // Animate the next step
        var self = this;
        this._animationTimer = window.setTimeout( function() { self._slideAnimate() }, this._slideDelay );
    },

    _slide: function( direction_in ) 
    {
        this._animation_in_progress = true;
		var self = this;
        if ( direction_in == true ) 
        {
            this._slideIn = true;
            this._currentTop = this._topBorder;
        } else 
        {
            this._slideIn = false;
            this._currentTop = this._topBorder - this._barHeight;
            this._activebarNode.style.top = this._currentTop + "px";
            this._activebarNode.style.display = "block";
        }
        this._slideAnimate();
    },

    //
    // Action Callbacks
    //
    _onScroll: function(event, o)
    {
        o._topBorder = o._getScrollOffsetY();
        if ( o._animation_in_progress ) 
        {
            // Stop the animation
            window.clearTimeout( o._animationTimer );
            o._activebarNode.style.display = o._slideIn ? "none" : "block";
        }

        o._activebarNode.style.top = o._topBorder + "px";		
    },

    //
    // Public functions
    //
    show: function() 
    {
        this._slide( false );
    },

    hide: function()
    {
        this._slide( true );
    }

}
