// http://goessner.net/2006/01/remote-json.html
// http://www.xml.com/pub/a/2005/12/21/json-dynamic-script-tag.html?CMP=OTC-TY3388567169&ATT=JSON+and+the+Dynamic+Script+Tag:+Easy+XML-less+Web+Services+for+JavaScript
// 

/*


   2. var x={"a":"b"}
      Here the data is accessible through variable x, which is visible in the client's global scope. However the exact time of the data availability is not known, as it is delivered by an asynchron process.
   3. f({"a":"b"});
      This is the best way, as the function f will get called automatically, when the data is available. But how do remote web service and web client agree upon the function name?
         1. The web service rigidly dictates the name of the callback function.
         2. The web client sends the callback name as part of its request.

I wrote a javascript function remoteJson that handles incoming Json data according to cases 2. + 3. from static as well as dynamic <script> elements.

That function remoteJson(listener) expects a single object as argument. The optional data members of this object are:

"uri":
    The uri to get the Json data from, which is equivalent to the src-attribute of the dynamic <script> element. When omitted, a corresponding static <script> element is expected in the same document.
"callback":
    A reference to the callback function with no arguments. When omitted, an automatic callback from the web service according to case 3. will occur.
"condition":
    A boolean javascript expression as a string, which will be evaluated to decide, if the Json data has been completely loaded. Only needed in combination with callback.

An example call to del.icio.us with a static <script> element might be:

remoteJson({"callback": function(){alert(Delicious.posts.length);},
            "condition": "typeof(Delicious) != 'undefined'"});

with the corresponding <script> element somewhere on the same page. The alternative call creating a dynamic <script> element looks like so:

remoteJson({"uri": "http://del.icio.us/feeds/json/yourname/",
            "callback": function(){alert(Delicious.posts.length);},
            "condition": "typeof(Delicious) != 'undefined'"});

And making a call relying on the server calling back into a provided function might look like:

remoteJson({"uri": "http://del.icio.us/feeds/json/yourname?callback=f"});

Finally here is the implementation of remoteJson: 

*/

function remoteJson(listener) 
{
	if (listener && listener.uri) 
	{ 
		// create dynamic script element. 
		script = document.getElementById("remotejson");
		
		// script element may exist from previous call, so ..
		if (script) {
			// .. delete it.
			script.parentNode.removeChild(script);  
		}
		
		// new script element.
		script = document.createElement("script"); 
		script.setAttribute("type", "text/javascript");
		script.setAttribute("id", "remotejson");
		script.setAttribute("src", listener.uri);
		document.getElementsByTagName("head")[0].appendChild(script);
	}
	
	// if there is condition and callback 
	// check every 0.5 sec if condition true.
	if (listener && listener.condition && listener.callback)
	{	
		var timer = setInterval(function() {
								if (eval(listener.condition)) {
									clearInterval(timer);
									listener.callback(listener.target);
								}
					}, 500);		
	}
}