
// The server URL.
var server = "http://os.adamaltemus.com/social-actions/";

var isIE = navigator.userAgent.indexOf("MSIE") != -1;
var isWebKit = navigator.userAgent.indexOf("WebKit") != -1;
var isIEContinue = false;
var query = "";
var created = "";
var categories = "";
var height = "";
var numResults = 2;

// 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, numResults);
}
// 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.
// Currently it is just using an HTML string since we are returning ~10 actions.
// It could create elements and append later if this size increases.
function createHtml(array, height)
{
	height += "px";
	var htmlStr = "<div style='width:100%; height:38px;'><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></div>"
	+"<div style='overflow:auto;overflow-x:hidden;height:" + height + ";padding-bottom:3px;'>";
	//array.length
	maxResults = parseInt(numResults);
	returnedResults = array.length;
	results = Math.min(maxResults, returnedResults);
	for (var i = 0; i <= results - 1; i++)
	{
		// Used for truncating very long descriptions.
		var tStr = array[i].description;
		// String HTML chars.
		tStr = tStr.replace(/(<([^>]+)>)/ig,""); 
		if (tStr.length > 50) 
		{
			tStr = tStr.substring(0,50);
			tStr += " [...]";
		}
		
		// Only give the first part of the date.
		//var tCr = array[i].created_at;
		//tCr = tCr.split(" ");
		htmlStr += "<div style='padding-bottom: 5px;'>"
			+ "<div style='font-size:13px; border-bottom: 1px solid rgb(12, 190, 243);padding-bottom: 5px;'>"
			+ "<a href='" + array[i].url  + "' target='_blank' title='"+ tStr + "' style='color:black'>" + array[i].site.name + "</a>"
			+ "<div style='font-size:13px;'>" + array[i].title + "</div><div style='font-size:12px;padding-top:2px;color:rgb(93, 133, 48);'>" + array[i].action_type.name  + "</div>"
			+ "</div>"
			+ "</div>";
	}
	htmlStr += "</div>";
	return htmlStr;
}

// Make the call to the PHP script that will query the API.
function callSa(query,created,categories,height,numResults)
{
	// IE slow parsing stuff...grr...
	this.query = query;
	this.created = created;
	this.categories = categories;
	this.height = height;
	this.numResults = numResults;
	if (isIE && !isIEContinue) 
	{
		return;
	}
	// The server script.
	var serverUrl = server + "php/sa.php?query=" + query + "&created=" + created + "&categories=" + categories;
	$.ajax(
		{
			dataType: 'jsonp',
			jsonp: 'jsonp_callback',
			url:serverUrl,
			success: function (data) 
				{
					// debug.
					//alert(data);
					if (data !== null)
					{
						var displayDiv = document.getElementById('saDisplay');
						html = createHtml(data, height);
						displayDiv.innerHTML = html;
					}
				}
		}
	);

}


// Show a preview of the widget. 
function getPreview(query, created, actions, height)
{
	// Make the JSON Call.
	// The server script.
	var serverUrl = server + "php/sa.php?query=" + query + "&created=" + created + "&categories=" + actions;
	//alert(serverUrl);
	// This could be condensed into a method but, since it is just in two places it is fine for now. 
	// Re-factoring could be done eventually.
	$.ajax(
		{
			dataType: 'jsonp',
			jsonp: 'jsonp_callback',
			url: serverUrl,
			success: function (data) 
				{
					if (data != null)
					{
						var displayDiv = document.getElementById('previewWidgetDiv');
						html = createHtml(data, height);
						displayDiv.innerHTML = html;
					}
				}
		}
	);
}
