// -*- c++ -*-
// twitter.js
//
// require intervalrequest.js
// require onload.js
//

function TwitterStatus(uri,interval){
    this.uri=uri;
    this.interval=parseInt(interval);
    this.last_queried=new Date(1970,0,1);
    this.timer=null;
}
try{
    TwitterStatus.prototype=new IntervalRequest();
    TwitterStatus.prototype.constructor=TwitterStatus;
}
catch(e){}
new TwitterStatus();

TwitterStatus.prototype.show=function(xml){
    if(!xml){
        return null;
    }

    var buf=xml.getElementsByTagName('status');
    if(!buf||!buf[0]){
        return null;
    }
    buf=buf[0];

    var _get_inner_text=function(el){
        var x='';
        try{
            el=el.firstChild;
        }
        catch(e){
            return null;
        }
            
        while(el){
            try{
                x+=el.data;
            }
            catch(e){}
            el=el.nextSibling;
        }
        return(x);
    }

    var time=this.relative_time(
        _get_inner_text(buf.getElementsByTagName('created_at')[0]));
    var str=_get_inner_text(buf.getElementsByTagName('text')[0]);
    
    if(!document.getElementById('twitter-balloon')){
        var div=document.createElement('div');
        div.setAttribute('id','twitter-balloon');
        var h1=document.createElement('h1');
        h1.appendChild(document.createTextNode('What am I doing...'));
        div.appendChild(h1);
        var p=document.createElement('p');
        p.setAttribute('id','twitter-my-status');
        div.appendChild(p);
        p=document.createElement('p');
        p.setAttribute('id','twitter-my-status-time');
        div.appendChild(p);
        
        document.body.appendChild(div);
    }
    document.getElementById('twitter-my-status').innerHTML=str;
    document.getElementById('twitter-my-status-time').innerHTML=time+
    '<br /><a href="http://twitter.com/zophos">http://twitter.com/zophos</a>';
}

TwitterStatus.prototype.relative_time=function(time_value) {
    time_values=time_value.split(" ");
    time_value=time_values[1]+" "+
        time_values[2]+", "+
        time_values[5]+" "+
        time_values[3]; //IE Hack

    var parsed_date = Date.parse(time_value);

    var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
    var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
    delta=delta+(relative_to.getTimezoneOffset()*60); // IE Hack

    if(delta < 60) {
        return 'less than a minute ago';
    } else if(delta < 120) {
        return 'about a minute ago';
    } else if(delta < (45*60)) {
        return (parseInt(delta / 60)).toString() + ' minutes ago';
    } else if(delta < (90*60)) {
        return 'about an hour ago';
    } else if(delta < (24*60*60)) {
        return 'about ' + (parseInt(delta / 3600)).toString() + ' hours ago';
    } else if(delta < (48*60*60)) {
        return '1 day ago';
    } else {
        return (parseInt(delta / 86400)).toString() + ' days ago';
    }
}

onload.queue.push(function(){
    var twitter=new TwitterStatus('/%7Ezophos/cgi-bin/twitter.mrb',300000);
    twitter.fetch();
})
