// Code inspired by http://os.adamaltemus.com/social-actions/
// Many thanks to Adam Altemus for making this available
// The server URL.
var server = "http://charitymeter.goyaweb.nl/";

var isIE = navigator.userAgent.indexOf("MSIE") != -1;
var isWebKit = navigator.userAgent.indexOf("WebKit") != -1;
var isIEContinue = false;
var params = "";
var descLength = "";

// If JQuery is not already loaded or not at least version 1.2, we will add it.
if (typeof jQuery == 'undefined') 
{
	addJQuery();
	// Give IE some time to parse the script - seriously I am not joking...ugh...
	if (isIE) 
	{
		setTimeout('continueIE()',500);
	}
	
}	
else if (parseInt(jQuery.fn.jquery.substring(2,4)) < 2)
{
	addJQuery();
}

function continueIE() {
	isIEContinue = true;
	callSa(query,created,categories, height);
}
// Use this to append the JQuery script in. 
function addJQuery()
{
	if (isWebKit)
	{
		// Brute force it on WebKit.
		document.write('<script type="text/javascript" src="' + server + 'js/jquery-1.3.2.min.js" ></script>');
	}
	else
	{
		// Create the element.
		jScript = document.createElement("script");
		if (!isIE)
		{
			// IE cannot use this but, Chrome must have it. 
			jScript.setAttribute("type", "text/javascript");
		}
	
		jScript.setAttribute("src", server + "js/jquery-1.3.2.min.js");
		// Append it.
		document.getElementsByTagName('head')[0].appendChild(jScript);
	}

}


// Create the HTML to be displayed.
function createHtml(array,descLength)
{
	var htmlStr = "";
	for (var i = 0; i < array.length; i++)
	{
		// Used for truncating very long descriptions.
		var tStr = array[i].description;
		// Strip HTML chars.
		tStr = tStr.replace(/(<([^>]+)>)/ig,""); 
		if (tStr.length > descLength) 
		{
			tStr = tStr.substring(0,descLength);
			tStr += " ...";
		}
		htmlStr += "<p><a href='" + array[i].url + "' target='_blank'>" + array[i].title + "</a><br/>" + tStr + " (from " 
		    + "<a href='" + array[i].url + "' target='_blank'>" + array[i].site.name + "</a>"
            + ")</p>"
			+ "<hr style='width:85%' />";

	}
	htmlStr += "<p>Powered by:<br/><a href='http://www.socialactions.com' target='_blank' title='You make a difference, we make it easy'>"
	+ "<img src='" + server + "imgs/socialactions-logo-small.png' style='padding-bottom:5px;' border=0/></a></p>";
	return htmlStr;
}

// Make the call to the proxy that will query the API.
function callSa(params,descLength)
{
	// IE slow parsing stuff...grr...
	this.params = params;
	this.descLength = descLength;

	if (isIE && !isIEContinue) 
	{
		return;
	}
	// The server script.
	var serverUrl = server + "SAProxy.ashx?" + params;
	$.ajax(
		{
			dataType: 'jsonp',
			jsonp: 'callback',
			url:serverUrl,
			success: function (data) 
				{
					if (data !== null)
					{
					    var displayDiv = document.getElementById('saWidget');
					    displayDiv.innerHTML = createHtml(data, descLength);
					}
				}
		}
	);

}
