
// SunBox - An AJAX Comment Box - SunSean
// 0.5 - Basic Features
// 0.6 - Textile / Thumbnail
// 0.7 - Administrator Features
// 0.8 - Time / Post Order
// 0.9 - Pagination
// 1.0 - Release!

SunBox = Class.create();
SunBox.prototype = {

  initialize: function(title){
    this.title = title;
    $(document).ready(this.init.bind(this));
  },

  init: function(){
    $("body").append('<div id="comments"></div>');
    this.e = $('#comments');//.hide();
    this.ld = true;
    this.start = 0;
    this.limit = 0;
    this.animate = 0;
    this.updateCall();
    this.e.html('<div class="title">Comments</div><div id="commentarea"></div>');
    var str = '<label for="author">Name: <input type="text" id="author" name="author" maxlength=256 /></label>';
    str += '<label for="web">Website: <input type="text" id="web" name="web" maxlength=256 /> <em>optional</em></label>';
    str += '<a id="help"><span>Textile Code:<br /><em>_italic_</em><br /><strong>*bold*</strong><br />!image!<br />##<pre>code</pre>##</span></a>';
    str += '<label for="newcomment">Comment: <textarea name="newcomment"></textarea></label>';
    str += '<div class="right"><input type="submit" value="Submit" class="button" /></div>';
    this.e.append('<form name="sunbox"></form>').find('form').submit(this.submit.bind(this)).html(str).end();
//    this.e.fadeIn('crawl');
  },

  updateCall: function() {
    new $.post('SunBox.php', {title: this.title,start:this.start} , this.update.bind(this));
  },

  update: function(response) {
    var r = response.split("\n<");
    for(i=1; i<r.length; ++i) this.addComment(r[i].split(">"),200*i);
    if(!this.timer) this.timer = setInterval (this.updateCall.bind(this), 5000);
    if(!this.animate) this.animate = 1;
  },
  
  addComment: function(list,i) {
    str = '<div class="commentbox '+((this.ld)?'light':'dark')+'"><div class="author">';
    str += (list.length>5)?'<a href="'+list[5]+'">'+list[0]+'</a>':list[0];
    str += '</div><div class="time">'+list[2]+'</div>';
    str += '<div class="text">'+this.parseText(list[4])+'</div></div>';
    $("#commentarea").append('<div class="comment" id='+list[1]+'></div>');
    if(this.animate) {
      $("#"+list[1]).hide().html(str);
      setTimeout('$("#'+list[1]+'").slideDown(600)', i); }
    else $("#"+list[1]).html(str);

    $('#'+list[1]).find('a.img img').each(function(i){this.it="img"+list[1]+i;$(this).load(function(){
      var i=this.it; if(this.className=="hidden"){
      if(this.width<=190)
        $(this).parent().after('<img src="'+this.parentNode.href+'" />').remove();
      else
        {this.width="190"; this.src='http://www.sunsean.com/tn/'+this.parentNode.href+'\|190';}
      $(this).removeClass('hidden'); }
      l=this.offsetLeft; t=this.offsetTop;
      w=this.scrollWidth; h=this.scrollHeight;
      $('#comments').append('<a href="'+this.parentNode.href+'"><img id="'+i+'" class="full" src="http://www.sunsean.com/tn/'+this.parentNode.href+'\|500" /></a>')
      .find('#'+i).each(function(){ h=(this.height/this.width)*w;
        $(this).css('left',l-(this.width-w)/2+'px').css('top',t-((this.height-h)/2)+'px')
        .hover(function(){},function(){ $('#'+i).hide(); }); });
      $(this).hover(function(){ $('#'+i).show(); });
    }); });
    
    this.ld = !this.ld;
    this.start = list[1];
  },

  submit: function() {
    var f = document.forms['sunbox'];
    if(f.author.value=="") { alert('Please enter a name'); f.author.focus(); return false; }
    if(f.newcomment.value=="") { alert('Please enter a comment'); f.newcomment.focus(); return false; }
    var $text = new String(f.newcomment.value);
    $.post('SunBox.php', {title:this.title,author:f.author.value,id:0,text:$text,web:f.web.value}, this.submitSuccess.bind(this));
    return false;
  },
  
  submitSuccess: function(request) {
//    this.updateCall();
    var f = document.forms['sunbox'];
    f.newcomment.value="";
    f.newcomment.focus();
  },
  
  parseText: function($str,o) {
    o = o || 0;
    if(o==1){
      re = new RegExp('\\b_(.+?)_\\b','g');
      $str = $str.replace(re,'<em>$1</em>');
      re = new RegExp('\\*\\b(.+?)\\b\\*','g');
      $str = $str.replace(re,'<strong>$1</strong>');
      re = new RegExp('!\\b(.+?)\\b!','g');
      $str = $str.replace(re,'##$1##');
      str3 = $str.split("##"); $str = "";
      for(var i=0; i<str3.length; ++i)
        if((i%2)==1 && str2[i]!="")
          $str += '<a href='+str3[i]+' class=img><img class=hidden src=http://www.sunsean.com/tn/'+str3[i]+'\|500></a>';
        else $str += this.parseText(str3[i],2);
      $str = $str.replace(/"/g,'&quot;');
    }else if(o==2){
      re = new RegExp('\\b(.+?)\\b://([^\\s]+)','g');
      $str = $str.replace(re,'<a href=$1://$2>$1://$2</a>');
    }else{
      $str = $str.replace(/\n/g,"<br/>\n");
      str2 = $str.split("##"); $str = "";
      for(var i=0; i<str2.length; ++i)
        if((i%2)==1) $str += "<code>"+str2[i].replace(/\t/g,'  ').replace(/ /g,'&nbsp;')+"</code>";
        else $str += this.parseText(str2[i],1);
    }
    return $str;
  }
};

