var shout_cooldown = 10;
var shout_timer;

function shout_enableinput(enabled)
{
  if (enabled)
  {
    document.getElementById("messagebox").style.display = 'block';
    document.getElementById("wait").style.visibility = 'hidden';
  }
  else
  {
    document.getElementById("messagebox").style.display = 'none';
    document.getElementById("wait").style.visibility = 'visible';
  }
  //document.getElementById("messagebox").style.backgroundColor = 'black';
}
function shout_countdown()
{
  shout_cooldown -= 1;
  document.getElementById("wait").innerHTML = "You must wait for "+shout_cooldown+" sec.";
  if (shout_cooldown<=0)
  {
    shout_enableinput(1);
    clearInterval(shout_timer);
    document.getElementById("wait").innerHTML = "You must wait for 10 sec.";
  }else
     shout_enableinput(0);
}

function shout_send()
{
  if(document.getElementById("messagebox").value)
  {

    var message='message='+URLEncode(document.getElementById("messagebox").value);
    createRequestAndFill('shoutboard/shoutboard_send.php','POST',message,'shout-text');
    document.getElementById("messagebox").value="";

    shout_cooldown = 10;
    shout_timer = setInterval("shout_countdown();",1000);
    shout_enableinput(0);
  }
  // setTimeout("enableinput(1);", 10000);
}

function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}

function show_edit(id)
{
  var em = document.getElementById('editmessage');
  var text = document.getElementById('msg'+id).innerHTML;
  /*
  text = text.replace(/<a href=\"(.*?)\">(.*?)<\/a>/gi, "[url=$1]$2[/url]");
  text = text.replace(/<span style=[\"|']color: ?(.*?);?[\"|']>(.*?)<\/span>/gi, "[color=$1]$2[/color]");
  text = text.replace(/<b>(.*?)<\/b>/gi, "[b]$1[/b]");
  text = text.replace(/<i>(.*?)<\/i>/gi, "[i]$1[/i]");
  text = text.replace(/<br>/gi, "\n");*/

  text = text.replace(/<a href=\"(.*?)\">/gi, "[url=$1]");
  text = text.replace(/<span style=[\"|']color: ?(.*?);?[\"|']>/gi, "[color=$1]");
  text = text.replace(/<b>/gi, "[b]");
  text = text.replace(/<i>/gi, "[i]");
  text = text.replace(/<br>/gi, "\n");
  
  text = text.replace(/<\/a>/gi     , "[/url]"    );
  text = text.replace(/<\/span>/gi  , "[/color]"  );
  text = text.replace(/<\/b>/gi     , "[/b]"      );
  text = text.replace(/<\/i>/gi     , "[/i]"      );
  text = text.replace(/<br>/gi      , "\n"        );
  
  document.editmessageform.editmessagetext.value = text;
  document.editmessageform.id.value = id;

  var bodyWidth = 0, bodyHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    bodyWidth = window.innerWidth;
    bodyHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    bodyWidth = document.documentElement.clientWidth;
    bodyHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    bodyWidth = document.body.clientWidth;
    bodyHeight = document.body.clientHeight;
  }

  em.style.left = (bodyWidth/2 - parseInt(em.style.width)/2) + "px";
  em.style.top = (bodyHeight/2 + getScrollXY()[1] - parseInt(em.style.height)/2) + "px";
  em.style.display = 'block';
  
}

function shout_edit()
{
    var message='id='+document.editmessageform.id.value+'&editmessage='+URLEncode(document.editmessageform.editmessagetext.value);
    createRequestAndFill('shoutboard/shoutboard_send.php','POST',message,'shout-text');
    document.getElementById('editmessage').style.display = 'none';
}

function shout_delete(id)
{
  var sure = confirm("Delete post id="+id+".\nDo you want to continue?");
  if(sure)
  {
    var message='delete='+id;
    createRequestAndFill('shoutboard/shoutboard_send.php','POST',message,'shout-text');
  }
}

function editmessage_preview(tag)
{
  var text = document.editmessageform.editmessagetext.value;
  /*text = text.replace(/(\r\n)|(\n\r)|(\n)|(\r)/g         , "<br>"                           );
  text = text.replace(/\[url=(.+?)\](.+?)\[\/url\]/g     , "<a href=\"$1\">$2</a>"          );
  text = text.replace(/\[b\](.+?)\[\/b\]/g               , "<b>$1</b>"                      );
  text = text.replace(/\[i\](.+?)\[\/i\]/g               , "<i>$1</i>"                      );
  text = text.replace(/\[color=(.+?)\](.+?)\[\/color\]/g , "<span style=\"color:$1\">$2</span>");*/
  
  //oteviraci znacky nebo neparove znacky
  text = text.replace(/(\r\n)|(\n\r)|(\n)|(\r)/g  , "<br>"                      );
  text = text.replace(/\[url=(.+?)\]/g            , "<a href=\"$1\">"           );
  text = text.replace(/\[b\]/g                    , "<b>"                       );
  text = text.replace(/\[i\]/g                    , "<i>"                       );
  text = text.replace(/\[color=(.+?)\]/g          , "<span style=\"color:$1\">" );
  
  //uzaviraci znacky
  text = text.replace(/\[\/url\]/g      , "</a>"    );
  text = text.replace(/\[\/b\]/g        , "</b>"     );
  text = text.replace(/\[\/i\]/g        , "</i>"     );
  text = text.replace(/\[\/color\]/g    , "</span>"  );
    
  document.getElementById('msg'+document.editmessageform.id.value).innerHTML = text;
}

//setTimeout("enableinput()", 10000);
//setInterval('createRequestAndFill("shoutboard/shoutboard_send.php","POST","","text")',10000);
//createRequestAndFill('shoutboard/shoutboard_send.php','POST','message=AhojAjax','text');
//createRequestAndFill("shoutboard/shoutboard_send.php","GET","text=AhojAjax","text");
