
// Loader

// http://www.nczonline.net/blog/2009/06/23/loading-javascript-without-blocking/
function loadScript(url, callback){
    var script = document.createElement("script")
    script.type = "text/javascript";

    if (script.readyState){  //IE
        script.onreadystatechange = function(){
            if (script.readyState == "loaded" ||
                    script.readyState == "complete"){
                script.onreadystatechange = null;
                callback();
            }
        };
    } else {  //Others
        script.onload = function(){
            callback();
        };
    }

    script.src = url;
    document.body.appendChild(script);
}

// Used for element counts
function min(a, b) {
    return (a < b) ? a : b;
}


// Services

// http://mlcastle.net/flickr-js-dom-json.html
//
// grab flickr images (via JSON -- what a total fscking hack) and
// display them
//

function flickrcb(aSrc, aUrl, aTitle) {
    //var listitem = document.createElement('li');
    //listitem.className = 'flickritem';

    var anchor = document.createElement('a');
    anchor.setAttribute('href', aUrl);
    anchor.className = 'flickra toggleopacity';

    var image = document.createElement('img');
    image.setAttribute('src', aSrc);
    image.setAttribute('title', aTitle);
    image.setAttribute('alt', 'Photograph of ' + aTitle);
    //image.setAttribute('width', 75);
    //image.setAttribute('height', 75);
    image.className = 'flickrphoto';

    anchor.appendChild(image);
    //listitem.appendChild(anchor);
    document.getElementById(options.flickr_element).appendChild(anchor);
}

// function MUST have this name, as it is what the flickr code will call
function jsonFlickrFeed(obj) {

    // make sure required DOM API is available
    if (!document.createElement)
        return false;
    if (!document.getElementById)
        return false;
    if (!document.getElementsByTagName)
        return false;
    
    var i;
    for (i=0; i < min(obj.items.length, options.flickr_count); ++i) {
        var item = obj.items[i];
        var url = item.media['m'];
        flickrcb(url.replace('_m', '_s'), item.link, item.title);
    }
    return true;
}

function niceDate(d) {
     isodate = prettyDate(Date.parse(d).toISOString().replace(/[""]/g,'').replace(".000Z", 'Z'));
     return (isodate == undefined) ? Date.parse(d).toString("MS MMM yyyy").replace(/[""]/g,'') : isodate;
}


/* http://www.bagofspanners.com/2009/02/09/using-the-twitter-json-api-to-suck-twitter-posts-into-your-blog-asynchronously/ */
function twitter(twitters) {
  var html = ['<h3>twitter</h3>','<ul>'];
  for(var i=0 ; i < min(twitters.length, options.twitter_count) ; i++){
     html.push("<li>"+twitters[i].text + " <a class=\"small quiet\" href='http://twitter.com/" + options.twitter_id + "/status/" + twitters[i].id + "'> " + niceDate(twitters[i].created_at) + "</a>"+"</li>");
  }
  html.push('</ul>');
  document.getElementById(options.twitter_element).innerHTML = html.join('');
}

function github(entries) {
  var html = ['<h3>github</h3>','<ul>'];
  for (var i = 0; i < min(entries.length, options.github_count); i++) {
    var entry = entries[i];
    if (entry['repository'] != undefined) {
        description = '';
        d = niceDate(entry['created_at']);
        //alert(entry['type']);
        switch(entry['type']) {
            case 'WatchEvent':
                pdesc = entry['repository']['description'].substr(0,45) + ' ...';
                project = "<a href=\""+entry['repository']['url']+"\">"+entry['repository']['name'] + "</a> -"+pdesc+" <span class=\"small quiet\">" + d + "</span>";
                description = "started watching " + project;
                break;
            case 'ForkEvent':
                pdesc = entry['repository']['description'].substr(0,45) + ' ...';
                project = "<a href=\""+entry['repository']['url']+"\">"+entry['repository']['name'] + "</a> -"+pdesc+" <span class=\"small quiet\">" + d + "</span>";
                description = "forked project " + project;
                break;
            case 'CreateEvent':
                pdesc = entry['repository']['description'].substr(0,45) + ' ...';
                project = "<a href=\""+entry['repository']['url']+"\">"+entry['repository']['name'] + "</a> -"+pdesc+" <span class=\"small quiet\">" + d + "</span>";
                description = "created project " + project;
                break;
            case 'PushEvent':
                pdesc = entry['repository']['description'].substr(0,45) + ' ...';
                project = "<a href=\""+entry['repository']['url']+"\">"+entry['repository']['name'] + "</a> -"+pdesc+" <span class=\"small quiet\">" + d + "</span>";
                description = "pushed changes to " + project;
                break;
            case 'IssuesEvent':
                pdesc = entry['repository']['description'].substr(0,45) + ' ...';
                project = "<a href=\""+entry['repository']['url']+"\">"+entry['repository']['name'] + "</a> -"+pdesc+" <span class=\"small quiet\">" + d + "</span>";
                description = "opened issue on " + project;
                break;
            default:
                description = "missing event "+entry['type'];
                break;

        }
        html.push('<li>'+description+'</li>');
    }
  }
  html.push('</ul>');
  document.getElementById(options.github_element).innerHTML = html.join('');
}

function delicious(bookmarks) {
  var html = ['<h3>del.icio.us</h3>','<ul>'];
  for (var i = 0; i<min(bookmarks.length, options.delicious_count); i++) {
    var entry = bookmarks[i];
      html.push('<li><a href="'+entry.u+'">'+entry.d+'</a> '+'<span class="small quiet">'+niceDate(entry.dt)+'</span>');
  }
  html.push('</ul>');
  document.getElementById(options.delicious_element).innerHTML = html.join('');
}
