/*****************************
 Advanced AJAX Shoutbox
 Copyright (C) 2006 PiotrLegnica

 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; 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
******************************/

// TODO: innerHTML => w3c DOM
var errors = 0;
var tab = null;
var lines = 0;
var refresh_interval_handler = null;
// FIXME
var stop_refresh_link = '<a href="#sb_tab_0" id="sb_refresh_toggle" style="text-align: right" onclick="return refreshToggle();">Wyłącz automatyczne odświeżanie</a>';
var start_refresh_link = '<a href="#sb_tab_0" id="sb_refresh_toggle" style="text-align: right" onclick="return refreshToggle();">Włącz automatyczne odświeżanie</a>';
var refresh_link = stop_refresh_link;

function getShouts() {
 var params = 'tab='+tab+'&mode=getShouts';
 var oAjax = new Ajax.Request(url+'?sid='+sid,
                              {method: 'post',
                               onSuccess: insertShouts,
                               onFailure: insertError,
                               parameters: params
                              }
                             );
 if (errors > 10) {
  $('sb_shouts').innerHTML += '<div class="sb_error">Pobieranie przerwane - nie można uzyskać odpowiedzi od serwera.</div>';
  stopRefresh();
 }
}
function sendShout() {
 var params = 'tab='+tab+'&mode=sendShout&msg='+$F('sb_message');
 var oAjax = new Ajax.Request(url+'?sid='+sid,
                              {method: 'post',
                               onSuccess: insertShouts,
                               onFailure: insertError,
                               parameters: params
                              }
                             );
 $('sb_message').value = '';
 return false;
}
function deleteShout(id) {
 var params = 'tab='+tab+'&mode=delShout&id='+id;
 var oAjax = new Ajax.Request(url+'?sid='+sid,
                              {method: 'post',
                               onSuccess: insertShouts,
                               onFailure: insertError,
                               parameters: params
                              }
                             );
}
function sb_init() {
 tabs.each(function(value, index){
  $('sb_tab_'+value).onclick = function(e){changeTab(value,true);};
 });
 $('sb_form').onsubmit = function(e){return sendShout();};
 $('sb_refresh').onclick = function(e){getShouts();};
 if (window.location.hash !== '') {
  // bookmarked channel
  var savtab = window.location.hash.substring((window.location.hash.length-1), window.location.hash.length);
  changeTab(savtab,false);
 } else {
  changeTab(0,false);
 }
 getShouts(); // first refresh
 startRefresh();
 $('sb_refresh_link').innerHTML = refresh_link;
}
function changeTab(newTab, refr) {
 tabs.each(function(value, index){
  $('sb_tab_'+value).className = '';
 });
 $('sb_tab_'+newTab).className = 'sb_tab_current';
 tab = newTab;
 if (refr) {
  getShouts(); // need to refresh view if not in sb_init
 }
}
function insertShouts(oAjax) {
 $('sb_shouts_span').innerHTML = '';
 //$('sb_shouts_span').innerHTML = oAjax.responseText;
 var serverResp = oAjax.responseText;
 serverResp = serverResp.parseJSON();
 if (typeof(serverResp.returnType) === undefined) {
  insertError(oAjax);
 } else {
  switch (serverResp.returnType) {
   case 'miscError':
    $('sb_shouts_span').innerHTML = '<div class="sb_error">'+serverResp.error+'</div>';
    break;
   case 'shouts':
    if (serverResp.shouts[0] === "" && serverResp.shouts.length == 1) {
     break;
    }
    for (i = 0; i < serverResp.shouts.length; i++) {
     var shout = serverResp.shouts[i]; // [shout_id, show_X?, date (formatted), nick (formatted), msg (formatted), background color (hex)]
     if (shout[0] == 'shout') {
      $('sb_shouts_span').innerHTML += '<div class="sb_shout" style="background-color: '+shout[6]+'">'+(shout[2] ? '<a href="javascript:void(0)" onclick="deleteShout(\''+shout[1]+'\')" class="mainmenu"><b>x</b></a>&nbsp;' : '')+shout[3]+' '+shout[4]+':&nbsp;'+shout[5]+'</div>';
     } else if (shout[0] == 'genMsg') {
      $('sb_shouts_span').innerHTML += '<div class="sb_genmsg" style="background-color: '+shout[4]+'">'+(shout[2] ? '<a href="javascript:void(0)" onclick="deleteShout(\''+shout[1]+'\')" class="mainmenu"><b>x</b></a>&nbsp;' : '')+shout[3]+'</div>';
     }
     lines++;
    }
    break;
  }
  if (lines > 15) {
   $('sb_shouts').scrollTop = 99999;
  }
 }
}
function insertError(oAjax) {
 $('sb_shouts_span').innerHTML += '<div class="sb_error">[Błd serwera (kod '+oAjax.status+')] Nie można pobrać nowych shoutów.</div>';
 errors++;
}

function startRefresh() {
 refresh_interval_handler = setInterval(getShouts, refresh); // interval refreshes
 refresh_link = stop_refresh_link;
}
function stopRefresh() {
 clearInterval(refresh_interval_handler);
 refresh_link = start_refresh_link;
}
function refreshToggle() {
 if (refresh_link === start_refresh_link) {
  startRefresh();
  refresh_link = stop_refresh_link;
 } else {
  stopRefresh();
  refresh_link = start_refresh_link;
 }
 $('sb_refresh_link').innerHTML = refresh_link;
 return false;
}
