function init() {
  // quit if this function has already been called
  if (arguments.callee.done) return;

  // flag this function so we don't do the same thing twice
  arguments.callee.done = true;

  // kill the timer
  if (_timer) clearInterval(_timer);

  // do stuff
  readCookie( );
}

/* for Mozilla/Opera9 */
if (document.addEventListener) {
    document.addEventListener("DOMContentLoaded", init, false);
}

/* for Internet Explorer */
/*@cc_on @*/
/*@if (@_win32)
    document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
    var script = document.getElementById("__ie_onload");
    script.onreadystatechange = function() {
        if (this.readyState == "complete") {
            init(); // call the onload handler
        }
    };
/*@end @*/

/* for Safari */
if (/WebKit/i.test(navigator.userAgent)) { // sniff
    var _timer = setInterval(function() {
        if (/loaded|complete/.test(document.readyState)) {
            init(); // call the onload handler
        }
    }, 10);
}

/* for other browsers */

function addLoadEvent(func) {
  // http://simonwillison.net/2004/May/26/addLoadEvent/
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

addLoadEvent( init );

soundManager.debugMode = false; // disable debug output

soundManager.url = 'soundmanager/soundmanager2.swf'; // path to movie

soundManager.onload = function() {
  soundManager.createSound( 'startingBell', 'starting-meditation-bell.mp3' );
  soundManager.createSound( 'endingBell', 'ending-meditation-bell.mp3' );
}

var counter = 0;
var timerInterval;
var wakeupInterval;
var timerTimeout;
var paused = false;
var started = false;
var lastPeriod = "";

function meditate( ) {
    if( !paused ||  parseInt( document.getElementById('time').value ) != counter) {
	document.getElementById('starting').style.visibility = 'visible';
	clearTimeout( timerTimeout );
	timerTimeout = setTimeout( meditateHelper, 15000 );
    }
    else {
	paused = false;
	document.getElementById('pause').disabled = false;
    }
}

function meditateHelper( ) {
    counter = parseInt( document.getElementById('time').value ); // assume minutes
    if( isNaN( counter) ) counter = 1;
    if( document.getElementById('time').value.indexOf( 'h' ) != -1 ) { // hours
      counter = parseFloat( document.getElementById('time').value );
      if( isNaN( counter) ) counter = 1;
      counter = Math.floor( counter * 60 );
    }
    if( document.getElementById('time').value.indexOf( 'm' ) == -1 ) { // seconds 
      counter /= 60;
    }
    if( counter < 1 ) counter = 1;

    document.getElementById('starting').style.visibility = 'hidden';
    document.getElementById('time').value = counter + ((counter == 1) ? " minute" : " minutes");
    lastPeriod = document.getElementById('time').value;
    writeCookie( lastPeriod );

    paused = false;
    document.getElementById('pause').disabled = false;
    started = true;
    soundManager.stopAll();

    soundManager.play( 'startingBell' );
    clearInterval( timerInterval );
    timerInterval = setInterval( countDown, 60000 );
    clearInterval( wakeupInterval );
    wakeupInterval = setInterval( wakeup, 300000 );
}


function countDown( ) {
  if( !paused && counter < 2 ) { // it will take at least 1 minute for this function to be called
    soundManager.play( 'endingBell' );
    clearInterval( timerInterval );
    clearInterval( wakeupInterval );
    started = false;
    document.getElementById('pause').disabled = true;
    document.getElementById('time').value = lastPeriod;
  }
  else if( !paused ) {
    --counter;
    document.getElementById('time').value = counter + ((counter == 1) ? " minute" : " minutes");
  }
  else;
}

function wakeup( ) {
  // The wakeup bell will only happen if the timer has been set to
  // more than 7 minutes.
  if( !paused && counter > 1 ) { // it will take at least 1 minute for this function to be called
    soundManager.play( 'startingBell' );
  }
  else;
}

function pauseCount( ) {
    if( started ) {
	paused = true;
	document.getElementById('pause').disabled = true;
    }
}

function writeCookie( value ) {
    var date = new Date();
    date.setTime(date.getTime()+(365*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
    document.cookie = "timecookie=" + value + expires;
}

function readCookie( ) {
  var ca = document.cookie.split(';');
  for( var i=0; i < ca.length; ++i ) {
    var c = ca[i];
    var name = 'timecookie='
    while( c.charAt(0) == ' ' ) c = c.substring( 1, c.length );
    if( c.indexOf( name ) == 0 ) {
      document.getElementById('time').value = c.substring( name.length, c.length );
    }
  }
}

function soundCheck( ) {
  soundManager.play( 'startingBell' );
}
