/*--------------------------------------------------------------------------------------------------
# The Buddha online
#
# Author: Martin Albrecht <martin.albrecht@javacoffee.de>
# Homepage: http://code.javacoffee.de
#
# Copyright (C) 2008 Martin Albrecht <martin.albrecht@javacoffee.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 3 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, see http://www.gnu.org/licenses/.
--------------------------------------------------------------------------------------------------*/

/*
	Globals
*/
var msg;
var buddha_main;
var interf;
var msg_area;
var msg_out;
var tinyTxt;
var server;


/*
    Functions
*/

/* Initialize the buddha */
function init() {
  msg = document.getElementById('message');
  msg.value = "";
  msg.focus();
  
  getDayX();
  
  document.getElementById('btnSend').style.visibility = "visible";

  /* Style the main area of the buddha and show the picture */
  buddha_main = document.getElementById('buddha_main');
  buddha_main.style.height = "180px";
  buddha_main.style.width = "119px";
  buddha_main.style.backgroundImage = 'url(gfx/Thai_buddha02-kl.jpg)';
  

  /* Style of input interface */
  interf = document.getElementById('interface');
  interf.style.width = "240px";

  msg_area = document.getElementById('msg_area');
  msg_area.style.position = "relative";
  msg_area.style.height = "140px";
  
  msg_out = document.getElementById('msg_out');
  msg_out.style.position = "absolute";
  msg_out.style.visibility = "hidden";
  msg_out.style.width = "240px";
  msg_out.style.height = "0px";  
  
  // Style text
  tinyTxt = document.getElementById('tiny');
  tinyTxt.style.fontSize = "10pt";
  tinyTxt.style.fontWeight = "bold";
  tinyTxt.style.background = "#ccc";
  tinyTxt.style.width = "80%";   

}

/* Count how many messages the buddha already holds */
function countMsg(request){

  // Style text
  tinyTxt = document.getElementById('msg');
  tinyTxt.style.fontSize = "10pt";
  tinyTxt.style.fontWeight = "bold";
  tinyTxt.style.background = "#ccc";
  tinyTxt.style.width = "60%";
  
  // Style text
  tinyTxt = document.getElementById('lastMsg');
  tinyTxt.style.fontSize = "11pt";
  tinyTxt.style.background = "#ccc";
  tinyTxt.style.width = "80%";
  
  document.getElementById('lastMsg').innerHTML = request.responseText+" messages for now...";
  return
}


/* Check formular for errors before sending it */
function checkForm() {
  msg = document.getElementById('message');
  
  if( msg.value == "" || !msg.value ) {
    alert("Please enter a message!");
    msg.focus();
    return;
  }
  
  if( msg.value.length >= 160 ) {
    alert("Message is too long!\nMessages can only be 160 characters!");
    msg.focus();
    return;
  }
  
  sendMessage();
}


/* Send a message */
function sendMessage() {
  server = "buddha_server.php";
  var args = "action=send&msg="+msg.value;
  var do_send = new Ajax.Request(server, {method: 'get', parameters: args, onComplete: sendDone});
}


/* Sending is done...*/
function sendDone(request) {
  // Style tiny text
  tinyTxt = document.getElementById('tiny');
  tinyTxt.style.fontSize = "8pt";
  tinyTxt.style.fontWeight = "bold";
  tinyTxt.style.background = "#ccc";
  tinyTxt.style.width = "100px";
  
  document.getElementById('message').value = request.responseText;
  document.getElementById('btnSend').style.visibility = "hidden";
  getDayX();
  return;
}


/* Click on buddha */
function buddhaClick() {
  msg_area = document.getElementById('msg_area');

  if( msg_area.style.visibility == "hidden" && msg_out.style.visibility == "visible" ) {
    msg_area.style.visibility = "visible";
    msg_out.style.visibility = "hidden";
    msg_out.style.height = "140px";
    msg_area.style.height = "140px";
	document.getElementById('btnSend').style.visibility = "visible";
    msg.value = "";
    msg.focus();
    return;
  }
  // Style and show messages
  else {
    msg_area.style.visibility = "hidden";
    msg_out.style.visibility = "visible";
    msg_area.style.height = "140px";
    msg_out.style.height = "140px";
    document.getElementById('btnSend').style.visibility = "hidden";
    var server  = "buddha_server.php";
    var Args = "action=count";
    var do_ajax = new Ajax.Request(server, {method: 'get', parameters: Args, onComplete: countMsg});
    return;
  }
}


/* Get time till day of enlightenment  without calculation*/
function getDayX() {
	var server  = "buddha_server.php";
    var Args = "action=gettime";
    var do_ajax = new Ajax.Request(server, {method: 'get', parameters: Args, onComplete: printTimeX});
    return;
}

/* Print out the time */
function printTimeX(request) {
	document.getElementById("txt_day").style.textAlign = "center";
	document.getElementById("txt_day").value = request.responseText;
	return;
}


/* START OF SCRIPT */
window.onload = init();
