var  UNINIT   = 0;
var  LOADING  = 1;
var  LOADED   = 2;
var  INTERACT = 3;
var  COMPLETE = 4;

// Preload and cache the ticker image used for the Ajax wait states
var myticker = '/full/img/icons/loading-ticker-a.gif';
var ticker  = new Image();
ticker.src = myticker;

var outputString = "";
var tickertext = '<img src="' + myticker + '" style="border: 0; padding-right: 5px; vertical-align: middle;" />fetching...';
var msgtxt = "";

//  visibility toggle for social links

var toggleState;
if ( document.getElementById('social-links').style.visibility != null 
	 ||
	 typeof (document.getElementById('social-links').style.visibility) === 'undefined' 
   )
	toggleState = document.getElementById('social-links').style.visibility;
else
	toggleState = hidden;
	
function loadcheck()
{
	alert("Yep, I loaded");
}

function getFortune()
{
}

function getContent(id,url)
{
	var req = makeRequest(url);

	if ( document.getElementById(id) && req )
	{
		showMessage(id,req);
	}
	
}

function showMessage(id,msgText)
{
	if ( document.getElementById(id) )
	{
		document.getElementById(id).innerHTML = msgText;
	}
}

function toggle()
{
	// trap load conditions for null or empty and treat as hidden.
	if (toggleState=='hidden' || toggleState=='' || toggleState==null)
	{
		toggleState='visible';
		document.getElementById('toggle-control').innerHTML = 'hide social';
	}
	else
	{
		toggleState='hidden';
		document.getElementById('toggle-control').innerHTML = 'show social';
	}
		
	document.getElementById('social-links').style.visibility = toggleState;
}

function refresh()
{
	getContent('social-links-content','/full/lib/php/social-links.inc.php');
}

// Pass the url to retrieve and the variable in which to store the returned text
function makeRequest(url)
{
	var ro = null;

	if (window.XMLHttpRequest)
	{ 
		// Mozilla, Safari,...
		ro = new XMLHttpRequest();
	} 
	else if (window.ActiveXObject) 
	{
		try 
		{
			ro = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try 
			{
				ro = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{
				return null;
			}
		}
	}

	if (!ro) 
	{
		return null;
	}
	else
	{
		ro.onreadystatechange = function() { handleResponse(ro); };
		ro.open('GET', url, true);
		ro.send(null);
		return ro.responseText;
	}

}

function handleResponse(ro) 
{
    switch (ro.readyState)
    {
	case UNINIT:
		showMessage('social-links-content','Not initialised');
		break;
	case LOADING:
		showMessage('social-links-content',tickertext);
		break;
	case LOADED:
		showMessage('social-links-content','Loaded');
		break;
	case INTERACT:
		showMessage('social-links-content',tickertext);
		break;
	case COMPLETE:
	        switch (ro.status)
		{
			case 200 :
				var res = ro.responseText;
				showMessage('social-links-content',res)
				break;
			case 403 : 
				showMessage('social-links-content',"Permission denied")
				break;
			case 404 : 
				showMessage('social-links-content',"No such file")
				break;
			case 500 : 
				showMessage('social-links-content',"Problem at server end")
				break;
			default  :
				showMessage('social-links-content',"Something weird has happened. The HTTP Response code was " + ro.status);
		}
    }
}

