var ShareThis = {};

ShareThis.showSet = function(url,source) {
	ShareThis.source = source;
	if (document.getElementById('sharethis')) {
		document.getElementById('sharethis').style.display = 'block';
	}
	else {
		if (!url) { url = this.findSet(); }
		if (url) { this.loadSet(url); } 
		else { ShareThis.displayError("No XML file found"); } 
	}
}

ShareThis.findSet = function() {
	var links = document.getElementsByTagName("link");
	for(var i=0;i < links.length;i++) {
		if (links.item(i).getAttribute("rel") == "sharethis") {
			return links.item(i).getAttribute("href");
		}
	}
}

ShareThis.loadSet = function(url) {
	try {
		var request = this.createXMLHttpRequest();
		request.onreadystatechange = function() {
			if (request.readyState == 4) {
				if (request.status == 200) {
					ShareThis.displaySet(request.responseXML);
				}
			}
		}
		request.open("GET",url,true);
		request.send(null);
	}
	catch (err) { ShareThis.displayError("Error: " + err); }
}

ShareThis.displayError = function(message) {
	var div = document.createElement("div");
	div.setAttribute("id","sharethis");
	div.appendChild(document.createTextNode(message));
	
	var coords = ShareThis.findPos(ShareThis.source);
	div.style.position = "absolute";
	div.style.left = coords[0] + "px";
	div.style.top = coords[1] + "px";
	
	document.getElementsByTagName("body").item(0).appendChild(div);
}

ShareThis.displaySet = function(xml) {
	var title = escape(document.title);
	var url = escape(window.location);

	var div = document.createElement("div");
	div.setAttribute("id","sharethis");
	var ul = document.createElement("ul");
	var items = xml.getElementsByTagName("share");
	for(var i=0;i < items.length;i++) {
		var share = items.item(i);
		var li = document.createElement("li");
		
		var href = share.getAttribute("href").replace("[TITLE]",title).replace("[URL]",url);
		href = href.replace(/&#38;/g,"&");
		var anchor = document.createElement("a");
		anchor.setAttribute("href",href);
		anchor.setAttribute("title",share.getAttribute("over"));
		var img = document.createElement("img");
		img.setAttribute("width",16);
		img.setAttribute("height",16);
		img.setAttribute("alt","");
		img.setAttribute("src",share.getAttribute("icon"));
		anchor.appendChild(img);
		li.appendChild(anchor);
		var span = document.createElement("span");
		span.appendChild(document.createTextNode(share.getAttribute("title")));
		li.appendChild(span);
		ul.appendChild(li);
	}
	div.appendChild(ul);
	
	var close = document.createElement("span");
	close.className = "closebox";
	close.innerHTML = '[<a href="javascript:void(0)" onclick="ShareThis.hideSet()">x</a>]';
	div.appendChild(close);
	
	var br = document.createElement("br");
	br.style.clear = "both";
	div.appendChild(br);
	
	var coords = ShareThis.findPos(ShareThis.source);
	div.style.position = "absolute";
	div.style.left = coords[0] + "px";
	div.style.top = coords[1] + "px";
	
	document.getElementsByTagName("body").item(0).appendChild(div);
}

ShareThis.hideSet = function() {
	document.getElementById('sharethis').style.display = 'none';
}

ShareThis.createXMLHttpRequest = function() {
	try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) {}
	try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {}
	try { return new XMLHttpRequest(); } catch(e) {}
	return null;
}

ShareThis.findPos = function(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

