/* Interface do objeto Link
 * {
 *		long type: 0 para "não-link", 1 para "itself", 2 para "url", 3 para "reference", 4 para "document"
 *		String uri: endereço do alvo:
 *			[id do objeto] para type=1
 *			[endereço-http] para type=2
 *			[id do alvo] para type=3
 *			[id do documento] para type=4
 *		boolean newWindow: óbvio
 *		String text: texto do link (texto clicável)
 * }
 */
function _linkConstrString()
{
	return "{idRef: " + (this.idRef? this.idRef : "null") + ", type:" + this.type + ",uri:'" + this.uri + "',newWindow:" + this.newWindow + ",text:" + (!this.text? "null" : "'" + this.text + "'") + ", usage: '" + this.usage + "'}";
}

function _linkToString()
{
	var suffix = "";
	var url;
	
	if ((this.type == 1) || (this.type == 3))
		url = "go?" + this.uri;
	else
		url = this.uri;
	
	if (this.params && (this.params != ''))
	{
		if (url.indexOf('?') != -1)
			suffix = '&' + this.params;
		else
			suffix = '?' + this.params;
	}
	
	return url + suffix;
}

function _linkToHTML(strInnerText, strFeatures)
{
	if (this.type == 0)
		return strInnerText;

	var res = "<a href=\"" + this.toString() + "\"";

	if (this.newWindow)
		res += " target=\"_new\"";

	if (strFeatures)
		res += " " + strFeatures;

	res += ">" + strInnerText + "</a>";
	return res;
}


function _linkToElement(strInnerText)
{
	if (this.type == 0)
	{
		if (strInnerText)
			return document.createTextNode(strInnerText);
		else
			return null;
	}

	var oRes = document.createElement("a");
	oRes.setAttribute("href", this.toString());

	if (this.newWindow)
		oRes.setAttribute("target", "_new");

	oRes.innerText = strInnerText;
	return oRes;
}

function _linkToXML()
{
	var res = "<link type='" + this.type + "' uri='";
	
	var uri = this.uri;
	uri = uri.replace(/\&/g, "&amp;");
	uri = uri.replace(/\</g, "&lt;");
	uri = uri.replace(/\>/g, "&gt;");
	uri = uri.replace(/\'/g, "&apos;");
	uri = uri.replace(/\"/g, "&quot;");

	res += uri + "' idRef='" + (this.idRef? this.idRef : "") + "' newWindow='" + (this.newWindow? "true" : "false") + "'/>";
	return res;
}

function Link(idType, strURI, newWindow, strText, idRef, usage)
{
	// Este construtor pode ser chamado de duas formas: usando
	// os seis parâmetros ou um só contendo uma string com
	// o link serializado, de onde será construído um objeto Link
	if (arguments.length == 1)
	{
		// de-serializa o objeto
		var desLink = null;
		try { eval("desLink = " + arguments[0]); } catch (e) { desLink = null; }
		
		if (desLink != null)
		{
			// cria um objeto Link baseado em desLink
			this.type = desLink.type;
			this.uri = desLink.uri;
			this.newWindow = desLink.newWindow;
			this.text = desLink.text;
			this.idRef = desLink.idRef;
			this.usage = desLink.usage;
		}
	}
	else
	{
		this.type = idType;
		this.uri = strURI;
		this.newWindow = newWindow;
		this.text = strText;
		this.idRef = idRef;
		this.usage = usage;
	}
	
	// acrescenta "http://", "mailto:"
	if (this.type == 2)
	{
		var url = (this.uri? this.uri.toLowerCase() : "");
		var iHttp = url.indexOf("http://");
		var iMailto = url.indexOf("mailto:");
		var iProto = url.indexOf(":");
		if ((iHttp != 0) && (iMailto != 0) && ((iProto == -1) || (iProto > 10)) )
		{
			var iAt = url.indexOf("@");
			var iSlash = url.indexOf("/");
			if (iSlash > 0)
				this.uri = "http://" + this.uri;
			else if (iAt > 0)
				this.uri = "mailto:" + this.uri;
			else
				this.uri = "http://" + this.uri;
		}
	}

	this.constructorString = _linkConstrString;
	this.toString = _linkToString;
	this.toHTML = _linkToHTML;
	this.toElement = _linkToElement;
	this.toXML = _linkToXML;
}

function getLink(idItem_or_oItem)
{
	alert("getLink() inexistente em client");
	return null;
}
