// libtime-bmt.js -- Biel Mean Time, Internet @time class
// $Id: libtime-bmt.js,v 1.7 2002/09/14 17:34:20 jaalto Exp $
//
//  File id
//
//      .Copyright: (C)  2001-2003 <jari.aalto@poboxes.com>
//      .Created:        2000-01-12
//      .Keywords:       www, html, javascript, object oriented class
//
//      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., 675 Mass Ave, Cambridge, MA 02139, USA.
//
//      http://www.gnu.org/copyleft/gpl.html
//
//  Install instructions
//
//      Include code via standard SCRIPT property:
//
//          src="libtime-bmt.js"
//
//       Here is an example to demontrate the class in action:
//
//          var bmt         = new LibTimeBMT(7000); // activate defaultStatus
//          var bmtNow      = bmt.time();           // time as decimal string
//          var bmtString   = bmt.timeString();     // time as string
//
//  Description
//
//      This class implements Biel Mean Time (BMT) Internet @time clock.
//      The Biel mean time is a universal way to indicate what is the
//      current time, for any citizen around the world. Instaed of using
//      and timezone + localtime, which causes one to compute the
//      local time (like UTC+2 and 17:00), the BMT simply gives a value
//      betbeen 0 .. 1000, which is the same for everyone.
//
//      Let's say, you have a internation web page (your project, homepage)
//      and you want to annoucne when you're available. You could say:
//
//          8.00 - 17.00 Pacific time
//
//      "What", wonders the person around the other side of atlantic, or
//      in the Japan. The issue is, that whaterver time you may annoucne,
//      it cannot be grapsed to any other that to your local people.
//      Others are forced to calculate what the time is on *their*
//      local timezone. On the other hand, if you announce:
//
//          I'm on-line usually @300 .. @500
//
//      That time is immediately clear to everyone who uses a BTM clock
//      on their computer. Have a look at resources:
//
//      o   http://www.ryanthiessen.com/swatch/resources.htm
//      o   http://www.mir.com.my/iTime/itime.htm
//      o   http://javaboutique.internet.com/InternetTime/
//
//      By loading this Javascrit class, the BMT time is autmatically
//      displayed at the bottom of the browser; at the status bar. To
//      incude the time to the HTML page the visitor entered, use this
//      code:
//
//          document.write( bmt.time() );
//
//  Available class attributes
//
//      None
//
//  Available methods
//
//      o   `activate(interval)' Start displaying BMT in status bar with
//          INTERVAL milliseconds. The minimum interval is 1000.
//      o   `deactivate' Stop displaying BMT time.
//      o   `time' Return BMT time with two decimals. Number string "nnnn".
//      o   `timeString', same as time(), but with leading "@" as Internet
//          time stamp "@nnnn.nn".
//
//  Change Log:

///////////////////////////////////////////////////////////////////////
//
// DESCRIPTION
//
//      Contructor
//
// INPUT PARAMETERS
//
//      timeout     [optional] Time in milliseconds. If given, activate the
//                  display with a timer process. The ACTIBATE must be
//                  more than 1000 milliseconsds
//
// RETURN VALUES
//
//      none
//
///////////////////////////////////////////////////////////////////////

function LibTimeBMT(timeout)
{
    var id = "LibTimeBMT";

    this.active   = null;       // Class private variable
    this.timeout  = null;       // Class private variable
    this.timer    = null;       // timer object

    if ( (LibTimeBMT.arguments.length > 0) && (timeout > 0) )
    {
        this.activate( timeout );
    }
}

if ( navigator.appName.indexOf("Netscape") != -1 )
{
    //  Netscape 3 Bug, create dummy to force making a prototype object
    new LibTimeBMT( -1 );
}


//  For the time being, the timeout is stored to the CLASS STATIC
//  variable, because it is unclear how to repeatedly call a
//  class form setTimeout() function and how to pass OBJECT instance

LibTimeBMT.obj     = null;

//  ........................ PUBLIC class functions ...................


///////////////////////////////////////////////////////////////////////
// DESCRIPTION
//
//      Activate printing time at status bar
//
// INPUT PARAMETERS
//
//      timeout     Time in milliseconds., which must be
//                  more than 1000 milliseconsds
//
// RETURN VALUES
//
//      boolean     true, if activated
//
///////////////////////////////////////////////////////////////////////

LibTimeBMT.prototype.activate = function(timeout)
{
    if ( this.SetTimeout(timeout) )
    {
        this.active     = true;
        LibTimeBMT.obj  = this;
        this.Show();

        return true;
    }

    return false;
}


///////////////////////////////////////////////////////////////////////
// DESCRIPTION
//
//      Deactivate printing time at status bar
//
// INPUT PARAMETERS
//
//      none
//
// RETURN VALUES
//
//      none
//
///////////////////////////////////////////////////////////////////////

LibTimeBMT.prototype.deactivate = function()
{
    this.active = false;

    if ( this.timer != null )
    {
        window.clearTimeout( this.timer );
    }
}


///////////////////////////////////////////////////////////////////////
// DESCRIPTION
//
//      Return Swatch Biel Mean Time (BMT), World tme, Internet time
//      with two decimal accuracy. "NNN.NN"
//
// INPUT PARAMETERS
//
//      none
//
// RETURN VALUES
//
//      string      Number cut to 2 decimals
//
///////////////////////////////////////////////////////////////////////

LibTimeBMT.prototype.time = function()
{
    var id = "LibTimeBMT.prototype.time";

    // "Biel Mean Time" - Swatch headquarter in Switzerland

    var beat = this.Beat();
    beat = this.CutDecimals( beat, 2);

    return beat;
}

///////////////////////////////////////////////////////////////////////
// DESCRIPTION
//
//      Return Swatch Biel Mean Time (BMT), as string "@NNN.NN"
//
// INPUT PARAMETERS
//
//      none
//
// RETURN VALUES
//
//      string      time with leading "@"
//
///////////////////////////////////////////////////////////////////////

LibTimeBMT.prototype.timeString = function()
{
    var id = "LibTimeBMT.prototype.timeString";

    return "@" + this.time();
}



//  ........................ PRIVATE class functions ..................


///////////////////////////////////////////////////////////////////////
// DESCRIPTION
//
//      Deactivate printing time at status bar
//
// INPUT PARAMETERS
//
//      timeout     Interval in milliseconds. Must be > 1000
//
// RETURN VALUES
//
//      boolean
//
///////////////////////////////////////////////////////////////////////

LibTimeBMT.prototype.SetTimeout = function(timeout)
{
    if ( timeout > 1000 )
    {
        this.timeout = timeout;

        return true;
    }

    return false;
}

///////////////////////////////////////////////////////////////////////
//
// DESCRIPTION
//
//      Take UTC time, and do the following: [(Hours + 1 {Make sure
//      that the # is not more than 23} *3600*1000) +
//      (Minutes *60*1000) + (Seconds * 1000)] / 86400
//
// INPUT PARAMETERS
//
//      None
//
// RETURN VALUES
//
//      float
//
///////////////////////////////////////////////////////////////////////

LibTimeBMT.prototype.Beat = function()
{
    var id = "LibTimeBMT.prototype.Beat";

    // Calculate middle European time, i.e. UTC + 1

    var bmt = new Date();

    bmt.setTime( bmt.getTime()
                 + (bmt.getTimezoneOffset() + 60) * 60 * 1000 );

    var beat =
    (
        bmt.getHours()     * 3600
        + bmt.getMinutes() * 60
        + bmt.getSeconds()
    )
    /  86.4
    ;

    return beat;
}


///////////////////////////////////////////////////////////////////////
//
// DESCRIPTION
//
//          Convert floating point number to N number of decimels.
//
// INPUT PARAMETERS
//
//          float
//
// RETURN VALUES
//
//          string
//
///////////////////////////////////////////////////////////////////////

LibTimeBMT.prototype.CutDecimals = function(n, count)
{
    var id  = "LibTimeBMT.prototype.CutDecimals";
    var str = n.toString();
    var pos = str.indexOf(".");

    if ( pos > 0 )
    {
        if ( count < 1 )
        {
            str = str.substr(0, pos -1 )
        }
        else
        {
            str = str.substr(0, pos + count + 1);
        }
    }

    return str;
}


///////////////////////////////////////////////////////////////////////
//
// DESCRIPTION
//
//      Display the BMT to the target source
//
// INPUT PARAMETERS
//
//      obj     Class object
//
// RETURN VALUES
//
//      boolean true, if activated
//
///////////////////////////////////////////////////////////////////////

LibTimeBMT.prototype.Show = function()
{
    var id  = "LibTimeBMT.prototype.Show";
    var obj = LibTimeBMT.obj;

    if ( obj  &&  obj.active )
    {
        var timeout     = obj.timeout;
        var time        = obj.time();
        var str         = "Swatch Internet time is now " + time;

        defaultStatus   = str;                  // Print to the status bar

        // Repeat ourself
        // TODO: This should really call the class instance OBJ,
        // but it is unclear how it could be done.
        //
        // At this time call the SUPERCLASS

        var code =  "LibTimeBMT.prototype.Show();"
        this.timer = window.setTimeout( code, timeout );

        return true;
    }

    return false;
}


// End of file
