var thisxmlhttp = getXMLHTTP();

function getXMLHTTP() {
	// function to create an XmlHttp object
	var xmlHttp = null;

	try {
		xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch(e) {
		try {
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch(oc) {
			xmlHttp = null;
		}
	}

	if(!xmlHttp && typeof XMLHttpRequest != "undefined") {
		xmlHttp = new XMLHttpRequest();
	}

	return xmlHttp;
}

function changeNav(sectionName) {
	switch (sectionName) {
		case "" :
	//		document.getElementById("hot").style.display = 'none';
			document.getElementById("recent").style.display = 'none';
	//		document.getElementById("category").style.display = 'none';
			document.getElementById("archive").style.display = 'none';
			document.getElementById("blogroll").style.display = 'none';
			document.getElementById("link").style.display = 'none';
			document.getElementById("refer").style.display = 'none';
			document.getElementById("google").style.display = 'none';
			document.getElementById("comments").style.display = 'none';
			break;
		default  :
	//		document.getElementById("hot").style.display = 'none';
			document.getElementById("recent").style.display = 'none';
	//		document.getElementById("category").style.display = 'none';
			document.getElementById("archive").style.display = 'none';
			document.getElementById("blogroll").style.display = 'none';
			document.getElementById("link").style.display = 'none';
			document.getElementById("refer").style.display = 'none';
			document.getElementById("google").style.display = 'none';
			document.getElementById("comments").style.display = 'none';
			eval("document.getElementById(\"" + sectionName + "\").style.display = 'block';");
			break;
	}
}

function getLinks(){  
	var thishostname  = window.location.hostname;

	for (var i = 0; i < window.document.links.length; i ++){
		var pos = 0;
		var hrefValue = window.document.links[i].href.toLowerCase();
		
		pos = hrefValue.indexOf("javascript");

		if (window.document.links[i].hostname != thishostname && pos == -1) {
			window.document.links[i].target = "_blank";
		}
	}
}

function addEngine(name)
{
  if ((typeof window.sidebar == "object") && (typeof window.sidebar.addSearchEngine == "function")) {
    window.sidebar.addSearchEngine("http://www.phigsaidwhat.com/phigmentb/phigment.nsf/dx/" + name + ".src/$file/" + name + ".src","http://www.phigsaidwhat.com/phigmentb/phigment.nsf/dx/" + name + ".PNG/$file/" + name + ".PNG", name, "Domino" );
  } else {
    alert("You will need a Mozilla based browser to install a search plugin.");
  }
}

function getLists(refName) {
	document.getElementById('navlist').innerHTML = "<img src='ajax-loader.gif/$file/ajax-loader.gif'> Retrieving data";
	var divNav = refName + 'nav';
	var pageName = refName + "-navigation.htm";

	thisxmlhttp.open("GET", pageName);
	thisxmlhttp.onreadystatechange = function() {
		if (thisxmlhttp.readyState == 4 && thisxmlhttp.status == 200) {
			try {
				document.getElementById('navlist').innerHTML = thisxmlhttp.responseText;
				getLinks();
			}
			catch (err) {
				document.getElementById('navlist').innerHTML = "Thanks for trying to push my buttons.  Unfortunately, something's not working.  Won't you try a different button?";
			}
		}
	}
	thisxmlhttp.send(null);
}

function getAgent(refName) {
	document.getElementById('navlist').innerHTML = "<img src='ajax-loader.gif/$file/ajax-loader.gif'> Retrieving data";
	var thisurl = "";

	switch (refName) {
		case "google" :
			thistitle = "<div class='navlisthead'>Latest Searches</div>";
			thisurl = '../RGLookup?OpenAgent&type=' + refName;
			break;
		case "refer" :
			thistitle = "<div class='navlisthead'>Latest Referers</div>";
			thisurl = '../RGLookup?OpenAgent&type=' + refName;
			break;
		case "blogroll" :
			thistitle = "<div class='navlisthead'>Bloglines Blogroll</div>";
//			'http://www.phigsaidwhat.com/phigmentb/phigment.nsf/urlviaajax?OpenAgent&url=' + escape('http://rpc.bloglines.com/blogroll?html=1&id=Phigment');
			thisurl = '../urlviaajax?OpenAgent&url=' + escape('http://rpc.bloglines.com/blogroll?html=1&id=Phigment');
//			thisurl = '../blogroll?OpenAgent';
			break;
	}
	
	thisxmlhttp.open("GET",thisurl );
	thisxmlhttp.onreadystatechange = function() {
		if (thisxmlhttp.readyState == 4 && thisxmlhttp.status == 200) {
			try {
				document.getElementById('navlist').innerHTML = thistitle + thisxmlhttp.responseText;
				getLinks();
			}
			catch (err) {
				document.getElementById('navlist').innerHTML = "Thanks for trying to push my buttons.  Unfortunately, something's not working.  Won't you try a different button?";
			}
		}
	}
	thisxmlhttp.send(null);
}

function showImage(img){ return (function(){ img.style.display='inline' }) }

function getDeliciousList(refresh) {
	document.getElementById('deliciouslist').innerHTML = "<img src='ajax-loader.gif/$file/ajax-loader.gif'> Retrieving del.icio.us Bookmarks";
	
	thisxmlhttp.open("GET", "../urlviaajax?OpenAgent&url=" & escape("http://del.icio.us/feeds/json/Phigment?raw&count=50"));
	thisxmlhttp.onreadystatechange = function() {
		if (thisxmlhttp.readyState == 4 && thisxmlhttp.status == 200) {
			try {
				var dljson = thisxmlhttp.responseText;
				var jsonobj = dljson.parseJSON();
				var dl = document.getElementById('deliciouslist');
				var dlhead = document.createElement('div');
				dlhead.setAttribute('class','dlhead');
				dlhead.setAttribute('className','dlhead');
				dlhead.appendChild(document.createTextNode("del.icio.us Bookmarks"));
				dl.appendChild(dlhead);
				
				for (var i=0, jb; jb = jsonobj.bindings[i]; i++) {
					var li = document.createElement('div');
					var a = document.createElement('a');
					li.style.paddingLeft = '10px';
					a.setAttribute('href', post.u);
					a.appendChild(document.createTextNode(post.d));
					li.appendChild(a);
					if (post.n != null) {
						var p = document.createElement('p');
						p.appendChild(document.createTextNode(post.n));
						li.appendChild(p);
					}
					dl.appendChild(li);

//					var dldiv = document.createElement('div');
//					var a = document.createElement('a');
//					a.style.marginLeft = '20px' ;
//					var img = document.createElement('img');
//					img.style.position = 'absolute' ;
//					img.style.display = 'none' ;
//					img.height = img.width = 16 ;
//					img.src = jb.u.split('/').splice(0,3).join('/')+'/favicon.ico' ;
//					img.onload = showImage(img); 
//					a.setAttribute('href', jb.u);
//					a.appendChild(document.createTextNode(jb.d)) ;
//					dldiv.appendChild(img) ;
//					dldiv.appendChild(a) ;
//					if (jb.getAttribute("n") != null) { li.appendChild(document.createTextNode('<br />'+jb.n)); }
//					dl.appendChild(dldiv);
				}

				if (refresh == true) {getLinks();}
			}
			catch (err) {
				document.getElementById('deliciouslist').innerHTML = "Error Retreiving list from Del.icio.us.";
			}
		}
	}
	thisxmlhttp.send(null);
}
