/* these calls are just the plumbing */
pipesrpc = {};
pipesrpc._timeoutlength = 30000;  /* 30 seconds by default */
pipesrpc._running = [];

pipesrpc._timeout = function(id,url) {
    var cbo = pipesrpc._running[id];
    pipesrpc._running[id]=null;
    if (!cbo.callbackErr) return;
    cbo.callbackErr("Timeout",-1,cbo.self);
}

pipesrpc._buildurl = function(pipeid,params) {
	var url = "http://pipes.yahoo.com/pipes/pipe.run?_id="+pipeid+"&_render=json";
	if (params) {
    	for (var key in params) {
            if (params[key]===null) continue;
    		url+="&"+encodeURIComponent(key)+"="+encodeURIComponent(params[key])
    	}
	}
    return url;
}

pipesrpc._callbackhandler = function(o) {
	var cbo = pipesrpc._running[callbackIndex];
    if (!cbo) return;
    pipesrpc._running[callbackIndex]=null;
   	window.clearTimeout(cbo.timeout);
   	if (!o || !o.count) {
   	    if (!cbo.callbackErr) return;
        cbo.callbackErr("Bad response",-2,cbo.self);
        return;
   	}
    if (!cbo.callbackOk) return;
    cbo.callbackOk(o,cbo.self);
}

pipesrpc._execute = function(url,callbackOk,callbackErr,timeoutlength) {
    if (!timeoutlength) timeoutlength = pipesrpc._timeoutlength;
    var id = pipesrpc._running.length;
  	url+="&_callback=pipesrpc._callbackhandler_"+id;
    var s=document.createElement("script");
    s.setAttribute("src",url);
    var fn = ""+pipesrpc._callbackhandler;
    fn = fn.replace(/callbackIndex/g,id);
    eval("pipesrpc._callbackhandler_"+id+"="+fn);
    pipesrpc._running.push({self:this,callbackOk:callbackOk,callbackErr:callbackErr,timeout:window.setTimeout(function() { pipesrpc._timeout(id,url); },timeoutlength)});    
    document.getElementsByTagName("head")[0].appendChild(s);
    return id;
}

/* use these three calls to run and cancel Pipes calls */
pipesrpc.cancelrequest = function(id) {
	var cbo = pipesrpc._running[i];
    window.clearTimeout(cbo.timeout);
    pipesrpc._running[i]=null;
}

pipesrpc.cancelallrequests = function() {
    for (var i=0; i<pipesrpc._running.length; i++) {
        pipesrpc._cancelrequest(i);
    }	
}

pipesrpc.run = function(pipeid,params,callbackOk,callbackErr,timeoutLength) {
    return pipesrpc._execute(pipesrpc._buildurl(pipeid,params),callbackOk,callbackErr,timeoutLength);
}

/* This is the example */
function init() {
	var requestId = pipesrpc.run("CjVPbZr43RGacFn51ZzWFw",null,
	    function(data) {
	    	//loop and create a dl dt/dd list.
	    	//Note: its much quicker to create using an HTML string but this looks nicer in an example
	        var feeddiv = document.getElementById("blogfeed");
	        //var dl = document.createElement("dl");
	       // for (var i=0; i<1; i++) {
	        	//var item = data.value.items[0];
	        	//var dt = document.createElement("dt");
				
				//var cutOutput = item.description.slice(0,140);
				//var outputArray = cutOutput.split(" ");
				
				//var finalOutput = "";
				
				//for(var i=0; i<outputArray.length-1; i++) {
					//var space = i < outputArray.length-2 ? " " : "...";
					//finalOutput += outputArray[i]+space;
				//}
				var html = "";
				
				for(var i=0; i<3; i++) {
					var item = data.value.items[i];
					var cutOutput = item.description.slice(0,140);
					var outputArray = cutOutput.split(" ");
				
					var finalOutput = "";
				
					for(var n=0; n<outputArray.length-1; n++) {
						var space = n < outputArray.length-2 ? " " : "...";
						finalOutput += outputArray[n]+space;
					}
					var published = new Date(item.pubDate);
					published = published.toLocaleString();
					published = published.substring(published.search(" ")+1,published.length-11);
					if (published.slice(-1)==" ")published=published.substr(0,published.length-1);
					html += "<h3>"+ item.title +"</h3><p>"+ finalOutput +"</p><span class=\"date\">"+published+"</span> | <a name='Nordstrom Blog More' href='"+item.link+"'>Read the story &gt;</a>";
					if (i !=2) html += "<div class=\"horiz\"></div>";
				}
				
	        	feeddiv.innerHTML = html;
	        	//dl.appendChild(dt);
				
				//alert("item.description: "+item.description);
				
	        	//var dd = document.createElement("dd");
	        	//dd.innerHTML = item.description.slice(0,100) +"... <a href='"+item.link+"'>MORE &gt;</a>";
	        	//dl.appendChild(dd);
	      	//}
		    //feeddiv.appendChild(dl);
		}
	);
	var flickrBadge = document.getElementById("flickr").innerHTML.toLowerCase();
	flickrBadge = flickrBadge.substr(flickrBadge.search("</script>")+9);
	var html='';
	for(var i=0;i<3;i++){
		flickrBadge=flickrBadge.replace("@n","@N");
	}
	if(flickrBadge.search("<tr>")!=-1){
		for(var i=0;i<3;i++){
			html+=flickrBadge.substring(flickrBadge.search("<a"),flickrBadge.search("</a>")+4);
			flickrBadge=flickrBadge.substr(flickrBadge.search("</tr>")+4);
		}
		html+=flickrBadge.substr(flickrBadge.search("<span"));
		flickrBadge=html;
	}
	var flickrDest = document.getElementById("flickrDest");
	flickrDest.innerHTML=flickrBadge;
	
	document.getElementById("dlp_contentBody").style.marginLeft=(document.body.offsetWidth-808)/2+"px";
	document.getElementById("dlp_contentBody").style.visibility="visible";
}

function addToOnload(func) {
   var oldonload = window.onload;
   if (typeof window.onload != 'function') {
      window.onload = func;
   }
   else {
      window.onload = function() {
      oldonload();
      func();
    }
   }
}

addToOnload(init)
