/*
<script>
friendfeed0 = "http://chwong1.mocasting.com/feed/0/0/0";
friendfeed1 = "http://bigcoin.mocasting.com/feed/0/0/0";
</script>
<script src="http://www.willflow.net/webservice/friendsposts.js"></script>
*/
  /* by Peter-Paul Koch & Alex Tingle. */
  function findPosX(obj)
  {
    var curleft = 0;
    if(obj.offsetParent)
        while(1) 
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
  }

  function findPosY(obj)
  {
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
  }

// Generate random ID
genGUID= function()
{
    var guid="";
    for (var i=0; i<16; i++)
        guid += Math.floor(Math.random()*16).toString(16);
    return guid;
};

fixE = function (e)
{
    if (typeof e == 'undefined') e = window.event;
    if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;
    if (typeof e.layerY == 'undefined') e.layerY = e.offsetY;
    if (typeof e.pageX == 'undefined') e.pageX = e.clientX + document.body.scrollLeft;
    if (typeof e.pageY == 'undefined') e.pageY = e.clientY + document.body.scrollTop;
    if (typeof e.target == 'undefined') e.target = e.srcElement;
    if (typeof e.which == "number") e.keyNum = e.which;
    else e.keyNum = e.keyCode;
    if (typeof e.relatedTarget == 'undefined') {
    	if (e.type=="mouseover")
    		e.relatedTarget = e.fromElement;
    	else if (e.type=="mouseout")
    		e.relatedTarget = e.toElement;
    }
    return e;
}

// to Human Date
Date.prototype.toHumanString = function() {
	var now = new Date();
	var timediffms = now.getTime()-this.getTime();
	var timediffs = timediffms/1000;
	if (timediffs<60) {
		return timediffs + " secs ago";
	}
	else if (timediffs<3600) {
		return parseInt(timediffs/60) + " mins ago";
	}
	else if (timediffs<86400) {
		return parseInt(timediffs/3600) + " hrs ago";
	}
	else if (timediffs<2592000) {
		return parseInt(timediffs/86400) + " days ago";
	}
	else if (timediffs<31536000) {
		return parseInt(timediffs/2592000) + " mths ago";
	}
	else {
		return parseInt(timediffs/31536000) + " yrs ago";
	}
}

guidguidguidguidguid = genGUID();

document.write('<div id="'+ guidguidguidguidguid + '">&nbsp;</div>');

friendFeedsLoaded = function(rs)
{
    var divtag = document.getElementById(guidguidguidguidguid);
    if (divtag===null||rs.count==0) {
        divtag.parentNode.removeChild(divtag);
        return;
    }

    var items = rs.value.items;
    divtag.style.fontSize = "12px";
    for (var i=0; i<items.length; i++) {
        var localscope = function(item) {
            var itemdiv = document.createElement("div");
            var atag = document.createElement("a");
            atag.href = item.link;
            atag.appendChild(document.createTextNode(item.title.substr(0,15)+"..."));
            atag.alt = item.title;
            atag.title = item.title;
            // items[i]["content:encoded"]
            itemdiv.appendChild(atag);
            var datespan = document.createElement("span");
            datespan.style.fontSize = "10px";
            var isDate = !isNaN(Date.parse(item.pubDate));
            var str = "";
            if (typeof item["dc:creator"] != "undefined") {
                str += " by " + item["dc:creator"];
            }
            else if (typeof item.author != "undefined") {
                str += " by " + item.author.name;
            }

            if (isDate) {
                str += " " + (new Date(item.pubDate)).toHumanString();
            }
            datespan.appendChild(document.createTextNode(str));
            itemdiv.appendChild(datespan);
            divtag.appendChild(itemdiv);

            atag.onmouseover = function(e) {
                e = fixE(e);
                var contentDiv = createContentOverlay();
                contentDiv.style.left = findPosX(atag)+"px";
                contentDiv.style.top = (findPosY(atag)+atag.offsetHeight)+"px"
                if (typeof item.description!="undefined" && item.description!==null) {
                    contentDiv.innerHTML = item["description"].substr(0,250)+"...";
                }
                else if (typeof item.summary!="undefined" && typeof item.summary.content!="undefined") {
                    contentDiv.innerHTML = item.summary.content.substr(0,250)+"...";
                }
                document.body.appendChild(contentDiv);
                return false;
            }
    
            atag.onmouseout = function() {
                var contentDiv = document.getElementById(guidguidguidguidguid+"content");
                if (contentDiv===null) {
                     return false;
                }
                contentDiv.parentNode.removeChild(contentDiv);
                return false;
            }
        }
        localscope(items[i]);
    }
}

createContentOverlay = function() {
    var contentDiv = document.getElementById(guidguidguidguidguid+"content");
    if (contentDiv!==null) {
         return contentDiv;
    }
    contentDiv = document.createElement("div");
    contentDiv.id = guidguidguidguidguid+"content";
    contentDiv.style.position = "absolute";
    contentDiv.style.border = "1px solid black";
    contentDiv.style.backgroundColor = "white";
    contentDiv.style.width = "150px";
    contentDiv.style.fontSize = "12px";
    contentDiv.style.padding = "5px";
    return contentDiv;
}

// load the feeds
var scripturl = "http://pipes.yahoo.com/pipes/wEQuuknD2xGghuLgJZhxuA/run?" +
                    "&_render=json" +
                    "&_callback=friendFeedsLoaded";
if (typeof friendfeed0!="undefined" && friendfeed0.indexOf("http://")==0) {
    scripturl += "&feed0=" + encodeURI(friendfeed0);
}
if (typeof friendfeed1!="undefined" && friendfeed1.indexOf("http://")==0) {
    scripturl += "&feed1=" + encodeURI(friendfeed1);
}
if (typeof friendfeed2!="undefined" && friendfeed2.indexOf("http://")==0) {
    scripturl += "&feed2=" + encodeURI(friendfeed2);
}
if (typeof friendfeed3!="undefined" && friendfeed3.indexOf("http://")==0) {
    scripturl += "&feed3=" + encodeURI(friendfeed3);
}
if (typeof friendfeed4!="undefined" && friendfeed4.indexOf("http://")==0) {
    scripturl += "&feed4=" + encodeURI(friendfeed4);
}
var headTag = document.getElementsByTagName('head')[0];
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = scripturl;
headTag.appendChild(script);

