function JSONscriptRequest(fullUrl) { 
	this.fullUrl = fullUrl;
	this.noCacheIE = '&noCacheIE='+new Date().getTime();
	this.headLoc = document.getElementsByTagName("head").item(0);
	this.scriptId = 'JscriptId'+JSONscriptRequest.scriptCounter++;
}

JSONscriptRequest.scriptCounter=1;

JSONscriptRequest.prototype.buildScriptTag=function() {
	this.scriptObj = document.createElement("script");
	this.scriptObj.setAttribute("type","text/javascript");
	this.scriptObj.setAttribute("charset","utf-8");
	this.scriptObj.setAttribute("src",this.fullUrl+this.noCacheIE);
	this.scriptObj.setAttribute("id",this.scriptId);
};

JSONscriptRequest.prototype.removeScriptTag=function() {
	this.headLoc.removeChild(this.scriptObj);
};

JSONscriptRequest.prototype.addScriptTag=function() { 
	this.headLoc.appendChild(this.scriptObj);
};

function addContainer() {
	/*
	if (window.ff_placeholder_text === undefined) {
		ff_placeholder_text = "We're loading your local content now.";
	}
	var container_div=document.createElement('div');
	container_div.setAttribute('id',container_id);
	container_div.setAttribute('class','ff_container');
	container_div.innerHTML="<div style=\"padding:10px; font-size:11px; color:#555;\">"+ff_placeholder_text+"<br /><strong>This will take only a few moments...</strong>"+"<img src=\"http://dev.francisfrith.com/fpn/assets/images/spinner.gif\" style=\"margin: 3px 5px -3px 5px; border: 0px;\" />"+"</div>";
	var caller=document.getElementById('ff_script_'+ff_apikey);
	caller.parentNode.insertBefore(container_div,caller);
	return container_div;
	*/
}

function addStylesheetTag() {
	var css;
	if(ff_apikey) {
		css=document.createElement('link');
		css.setAttribute('rel','stylesheet');
		css.setAttribute('href','http://dev.francisfrith.com/fpn/assets/css/api_css.asp?api_key='+ff_apikey);
		css.setAttribute('type','text/css');
		css.setAttribute('media','all');
		css.setAttribute('charset','utf-8');
		document.getElementsByTagName('head')[0].appendChild(css);
	}
}

function writeContent(jsonData) {
	if(container === null) {
		alert("Unable to write content: no container div with an id of "+container_id+" exists.");
	} else {
		var container_div = document.createElement('div');
		container_div.setAttribute('id',container_id);
		container_div.setAttribute('name',container_id);	
		container_div.setAttribute('class','ff_container');
		if (jsonData.responseText.match(/Error:\s.*?/i) !== null) {
			container_div.innerHTML = "The Frith API key is not valid for this domain.  If this is your website, you should add it to your Frith Account."
		} else {
			var content_error_text = '<strong>No local content was found.<\/strong><br \/><br /><br />Technical note: It could be '+'that either the location information was not passed to Frith in the expected format, '+'or this location is outside the bounds of Frith\'s coverage. You should also check that '+'the Frith JavaScript is intact.';
			//container.innerHTML = (jsonData.responseText === '' || jsonData.responseText === 'No local content found')?content_error_text:jsonData.responseText;
			
			if  (jsonData.responseText === '' || jsonData.responseText === 'No local content found') {
				container_div.innerHTML = content_error_text;
			} else {
				if(window.ff_placeholder_text === undefined) {
					ff_placeholder_text = "We're loading your local content now.";
				}
				container_div.innerHTML = jsonData.responseText;
			}
		}
		var caller=document.getElementById('ff_script_'+ff_apikey);
		caller.parentNode.insertBefore(container_div,caller);
	}
}

var ff_url = "http://content.francisfrith.com/jsonwrapper.aspx?service=getfrithcontent.aspx&callback=writeContent";
var ff_jsr;
var container_id = 'ff_container_'+ff_apikey;
var container = addContainer();
addStylesheetTag();
ff_url = ff_url+"&apikey="+ff_apikey+"&searchstring="+ff_search;
ff_jsr = new JSONscriptRequest(ff_url);
ff_jsr.buildScriptTag();
ff_jsr.addScriptTag();