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

//////////////////////////////////////////////////////////////////////////
//
// Function Equivalent to URLEncoder.encode(String, "UTF-8")
//
//
// Import from
// http://www.cresc.co.jp/tech/java/URLencoding/JavaScript_URLEncoding.htm
//
// Original Copyrights:
// Copyright (C) 2002 Cresc Corp.
// Version: 1.0
//
//
function encodeURL(str){
    var s0,i,s,u;
    
    s0=""; // encoded str
    
    // scan the source
    for (i=0;i<str.length;i++){
        s=str.charAt(i);
        u=str.charCodeAt(i); // get unicode of the char
        if(s==" "){
            // SP should be converted to "+"
            s0+="+";
        }
        else {
            // check for escape
            if(u==0x2a||
               u==0x2d||
               u==0x2e||
               u==0x5f||
               ((u>=0x30)&&(u<=0x39))||
               ((u>=0x41)&&(u<=0x5a))||
               ((u>=0x61)&&(u<=0x7a))){
                s0=s0+s; // don't escape
            }
            else {
                // escape
                if((u>=0x0)&&(u<= 0x7f)){
                    // single byte format
                    s="0"+u.toString(16);
                    s0+="%"+s.substr(s.length-2);
                }
                else if(u>0x1fffff){
                    // quaternary byte format (extended)
                    s0+="%"+(oxf0+((u&0x1c0000)>>18)).toString(16);
                    s0+="%"+(0x80+((u&0x3f000)>>12)).toString(16);
                    s0+="%"+(0x80+((u&0xfc0)>>6)).toString(16);
                    s0+="%"+(0x80+(u&0x3f)).toString(16);
                }
                else if(u > 0x7ff){
                    // triple byte format
                    s0+="%"+(0xe0+((u&0xf000)>>12)).toString(16);
                    s0+="%"+(0x80+((u&0xfc0)>>6)).toString(16);
                    s0+="%"+(0x80+(u&0x3f)).toString(16);
                }
                else{
                    // double byte format
                    s0+="%"+(0xc0+((u&0x7c0) >> 6)).toString(16);
                    s0+="%"+(0x80+(u&0x3f)).toString(16);
                }
            }
        }
    }
    return s0;
}


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


RecentTrack.prototype.show=function(xml){
    if(!xml){
        return null;
    }
    var buf=xml.getElementsByTagName('track');
    if(!buf||!buf[0]){
        return null;
    }

    var artist_name='';
    var artist_link='';
    var track_title='';
    var track_link='';
    var album_title=''
    var album_link;
    var album_img='/%7Ezophos/img/lastfm-track.png';
    var album_img_width='48';
    var album_img_height='48';
    var album_img_padding='0px';

    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);
    }

    buf=buf[0];
    try{
        artist=buf.getElementsByTagName('artist')[0];
        artist_name=_get_inner_text(artist.getElementsByTagName('name')[0]);
        artist_link=_get_inner_text(artist.getElementsByTagName('uri')[0]);
        track_title=_get_inner_text(buf.getElementsByTagName('title')[0]);
        track_link=_get_inner_text(buf.getElementsByTagName('uri')[0]);
        
        var album=buf.getElementsByTagName('album');
        if(album&&album[0]){
            album=album[0];
            album_title=_get_inner_text(
                album.getElementsByTagName('title')[0]);
            album_title=artist_name+' - '+album_title;
            var uri=album.getElementsByTagName('uri');
            var asin=album.getElementsByTagName('asin');
            var coverart=album.getElementsByTagName('img');
            if(uri&&uri[0])
                album_link=_get_inner_text(uri[0]);
            if(asin&&asin[0]){
                asin=_get_inner_text(asin[0]);
                album_link='http://www.amazon.co.jp/exec/obidos/ASIN/'+
                    asin+'/daytriponkoka-22';
                album_img='http://images.amazon.com/images/P/'+
                    asin+'.01._AA48_SCMZZZZZZZ_.jpg';

                var img=new Image();
                img.src=album_img;
                if((img.width==1)&&(img.height==1)){
                    album_img='http://images.amazon.com/images/P/'+
                        asin+'.01._AA48_SCLZZZZZZZ_.jpg';
                    img=new Image();
                    img.src=album_img;
                    if((img.width==1)&&(img.height==1))
                        album_img=
                            '/%7Ezophos/img/lastfm-track.png';
                }
            }
            else if(coverart&&coverart[0]){
                album_img=_get_inner_text(coverart[0]);
                //album_link='http://www.amazon.co.jp/gp/search?'+
                //    'ie=UTF8&keywords='+
                //    encodeURL(album_title)+
                //    '&tag=daytriponkoka-22&index=music&linkCode=ur2'+
                //    '&camp=247&creative=1211';
                var tmp=new Image();
                tmp.src=album_img;
                var w=tmp.width;
                var h=tmp.height;
                if((tmp.width!=48)||(tmp.height!=48)){
                    if((tmp.width>=tmp.height)&&(tmp.width>48)){
                        h=h*48/w;
                        w=48;
                    }
                    else if((tmp.height>=tmp.width)&&(tmp.height>48)){
                        w=w*48/h;
                        h=48;
                    }
                }
                if(w<=1||h<=1){
                    album_img='/%7Ezophos/img/lastfm-track.png';
                    w=48;
                    h=48;
                }
                album_img_width=''+w;
                album_img_height=''+h;
                album_img_padding=''+(48-h)/2+'px '+(48-w)/2+'px';
            }
        }
    }
    catch(e){}

    var HTMLescape=function(str){
        str=str.replace('&','&amp;');
        str=str.replace('<','&lt;');
        str=str.replace('>','&gt;');
        str=str.replace('"','&quot;');
        return str.replace("'",'&#39;');
    }

    var str="<p class='album'>";
    if(album_link)
        str+="<a href='"+album_link+"'>";
    str+="<img src='"+album_img+"' width='"+album_img_width+
    "' height='"+album_img_height+"' style='padding:"+album_img_padding+
    "' alt='"+HTMLescape(album_title)+
    "' title='"+HTMLescape(album_title)+"' />";
    if(album_link)
        str+="</a>";
    str+="</p>";
    str+="<p class='track'><a href='"+track_link+"'>"+
    HTMLescape(track_title)+"</a></p>";
    str+="<p class='artist'><a href='"+artist_link+"'>"+
    HTMLescape(artist_name)+"</a></p>";

    if(this.dst)
        this.dst.innerHTML=str;
}

onload.queue.push(function(){
    var lastfm=new RecentTrack('/%7Ezophos/cgi-bin/lastfm.mrb',
                               90000,
                               document.getElementById('navi-left-lastfm-track'));
    lastfm.fetch();
})
