// ================================================================
//  ajaxtb.js ---- Ajax comment component
//  Copyright 2005-2006 Kawasaki Yusuke <u-suke [at] kawa.net>
//  http://www.kawa.net/works/ajax/ajaxtb/ajaxtb.html
// ================================================================

AjaxTB = function ( area, path ) {
    this.area = area;
    if ( typeof(path) == "undefined" ) {
        path = window.location.pathname;
        path = path.replace( /\/\/\/*/g, "/" );     // multiple /
        path = path.replace( /\/((index|default).(s?html?|asp|cgi|php))?$/, "" );
        path = path.replace( /^\//, "" );           // first /
        path = path.replace( /\/$/, "" );           // last /
        if ( path == "" ) path = "/";               // root /
    }
    this.path = path.replace( /[^A-Za-z0-9\_\.\-]/g, "_" );
    return this;
};

AjaxTB.prototype.class_root     = 'ajaxtb';
AjaxTB.prototype.class_title    = 'ajaxtb_title';
AjaxTB.prototype.class_date     = 'ajaxtb_date';
AjaxTB.prototype.class_blogname = 'ajaxtb_blog_name';
AjaxTB.prototype.class_excerpt  = 'ajaxtb_excerpt';
AjaxTB.prototype.class_notice   = 'ajaxtb_notice';
AjaxTB.prototype.text_max_length = 100;
AjaxTB.prototype.mess_loading   = 'Now loading...';
AjaxTB.prototype.use_simpleapi  = true;

////
AjaxTB.prototype.url_receive   = '/c/tb/ajaxtb-data';
////

AjaxTB.prototype.load = function () {
    var url = this.url_receive + "/" + this.path + ".txt";
//  url += "?t=" + Math.floor(Math.random()*9000+1000);
    var req = this.get_http_request( "GET", url );
    var copythis = this;
    var loaded = 0;
    var noticed = 0;
    var func = function () {
        if ( req.readyState == 3 && ! noticed ) {
            copythis.disp_notice( copythis.mess_loading );
            noticed ++;
            return;
        }
        if ( req.readyState != 4 ) return;
        if ( loaded ++ ) return;
        if ( req.status == 404 ) {
            copythis.clear_tb_area();
        } else {
            copythis.disp_content( req );
        }
    }
    req.onreadystatechange = func;
    req.send("");
}

AjaxTB.prototype.clear_tb_area = function () {
    var ediv = document.getElementById( this.area );
    while ( ediv.childNodes.length ) {
        ediv.removeChild( ediv.firstChild );
    }
    return ediv;
}

AjaxTB.prototype.init_tb_area = function () {
    var ediv = this.clear_tb_area();
    var eul = document.createElement( "ul" );
    eul.className = this.class_root;
    ediv.appendChild( eul );
    return eul;
}

AjaxTB.prototype.disp_content = function ( req ) {
    var text = this.get_response_text( req );
    if ( typeof(text) != "string" ) return;
    var eul = this.init_tb_area();

    var lines = text.split(/[\r\n][\r\n]*/);
    var scroll;
    for( var i=lines.length; i>=0; i-- ) {
        var aline = lines[i];
        if ( typeof(aline) != "string" ) continue;
        var cols = lines[i].split("\t");
        if ( cols.length < 3 ) continue;
        var dd = new Date();
        dd.setW3CDTF( cols[0] );
        var idate = dd.toLocaleString()

        var fli = document.createElement( "li" );
        var fspan1 = document.createElement( "span" );
        var fspan2 = document.createElement( "span" );
        var fspan3 = document.createElement( "span" );
        var fspan4 = document.createElement( "span" );
        fspan1.className = this.class_title;
        fspan2.className = this.class_date;
        fspan3.className = this.class_blogname;
        fspan4.className = this.class_excerpt;

        var fa1 = document.createElement( "a" );
        fa1.name = "tb-"+cols[0];
        fa1.href = cols[4];
        fa1.target = "_blank";

        var title    = this.check_length( cols[5] );
        var blogname = this.check_length( cols[7] );
        var excerpt  = this.check_length( cols[6] );

        var ftext1 = document.createTextNode( title );
        var ftext2 = document.createTextNode( idate );
        var ftext3 = document.createTextNode( blogname );
        var ftext4 = document.createTextNode( excerpt );

        fa1.appendChild( ftext1 );
        fspan1.appendChild( fa1 );
        fspan2.appendChild( ftext2 );
        fspan3.appendChild( ftext3 );
        fspan4.appendChild( ftext4 );

        if ( this.use_simpleapi ) {
            var fimg1 = document.createElement( "img" );
            fimg1.src = 'http://img.simpleapi.net/small/'+cols[4];
            var fa2 = document.createElement( "a" );
            fa2.href = cols[4];
            fa2.target = "_blank";
            fa2.appendChild( fimg1 );
            fli.appendChild( fa2 );
	        var fbr1 = document.createElement( "br" );
			fbr1.style.clear = 'both';
	        fspan4.appendChild( fbr1 );
        }

        fli.appendChild( fspan1 );
        fli.appendChild( fspan2 );
        fli.appendChild( fspan3 );
        fli.appendChild( fspan4 );
        eul.appendChild( fli );

        if ( location.hash == "#tb-"+cols[0] ) {
            scroll = fli;
        }
    }
    if ( scroll ) {
        var y = scroll.offsetTop;
        if ( y ) window.scrollTo(0,y);
    }
}

AjaxTB.prototype.disp_notice = function ( mess ) {
    var eul = this.init_tb_area();
    var fli = document.createElement( "li" );
    var fspan1 = document.createElement( "span" );
    fspan1.className = this.class_excerpt;
    var ftext1 = document.createTextNode( mess );
    fspan1.appendChild( ftext1 );
    fli.appendChild( fspan1 );
    eul.appendChild( fli );
}

AjaxTB.prototype.check_length = function ( text ) {
    if ( text.length < this.text_max_length ) return text;
    return text.substr(0,this.text_max_length)+" ... ";
}

AjaxTB.prototype.get_http_request = function ( method, url ) {
    var req;
    if ( window.XMLHttpRequest ) {
        req = new XMLHttpRequest();
    } else if ( window.ActiveXObject ) {
        req = new ActiveXObject( "Microsoft.XMLHTTP" );
    } else {
        return;
    }
    req.open( method, url, true );
    if ( typeof(req.setRequestHeader) != "undefined" ) {
        req.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded" );
    }
    return req;
}

AjaxTB.prototype.get_response_text = function ( req ) {
    var text = req.responseText;
    if ( navigator.appVersion.indexOf( "KHTML" ) > -1 ) {
        var esc = escape( text );
        esc = esc.replace( /^(%[89ABab][0-9A-Fa-f])+/, "?" );
        if ( esc.indexOf("%u") < 0 && esc.indexOf("%") > -1 ) {
            text = decodeURIComponent( esc );
        }
    }
    return text;
}

Date.prototype.setW3CDTF = function( dtf ) {
    var sp = dtf.split( /[^0-9]/ );
    if ( sp.length < 6 || sp.length > 8 ) return;

    if ( sp.length == 7 ) {
        if ( dtf.charAt( dtf.length-1 ) != "Z" ) return;
    }

    for( var i=0; i<sp.length; i++ ) sp[i] = sp[i]-0;    // to numeric

    if ( sp[0] < 1970 ||                // year
         sp[1] < 1 || sp[1] > 12 ||     // month
         sp[2] < 1 || sp[2] > 31 ||     // day
         sp[3] < 0 || sp[3] > 23 ||     // hour
         sp[4] < 0 || sp[4] > 59 ||     // min
         sp[5] < 0 || sp[5] > 60 ) {    // sec
        return;                         // invalid date 
    }

    // get UTC milli seconds
    var msec = Date.UTC( sp[0], sp[1]-1, sp[2], sp[3], sp[4], sp[5] );

    // time zene offset
    if ( sp.length == 8 ) {
        if ( dtf.indexOf("+") < 0 ) sp[6] *= -1;
        if ( sp[6] < -12 || sp[6] > 13 ) return;    // time zone offset hour
        if ( sp[7] < 0 || sp[7] > 59 ) return;      // time zone offset min
        msec -= (sp[6]*60+sp[7]) * 60000;
    }

    // set by milli second;
    return this.setTime( msec );
}

// ****

