var hubionmac_linklist = ""
linklister_onload();document.getElementById('search_links').focus()

function linklister_onload() {
	
		// theurl = (document.URL.slice(0,document.URL.lastIndexOf('/'))+"/links.txt")
		var theurl = "http://hubionmac.com/downloads/linklist/links.txt"
		var http = false;
		
			if(navigator.appName == "Microsoft Internet Explorer") {
			  http = new ActiveXObject("Microsoft.XMLHTTP");
			} else {
			  http = new XMLHttpRequest();
			}
		
			http.open("GET", theurl);
			http.onreadystatechange=function() {
		
			  if(http.readyState == 4) {
				hubionmac_linklist = http.responseText;
				build_link_list(hubionmac_linklist,"");
			  }
			}
			http.send(null);
}

function build_link_list (alist,thekey) {
	var links = alist.split('\n')
	var link_urls = new Array()
	var link_infos = new Array()
	var link_dates = new Array()
	var link_tags = new Array()
	for (var i=links.length-1; i > -1; i--) {
		if (links[i].split("\t").length==4){
			if (links[i].toLowerCase().indexOf(thekey)>-1) {
				link_urls.push(links[i].split("\t")[0])
				link_infos.push(links[i].split("\t")[1])
				// link_dates.push(new Date(links[i].split('\t')[2].replace(/-/g,"/")))
				link_dates.push(links[i].split('\t')[2])
				link_tags.push(links[i].split("\t")[3].toLowerCase().split(",").sort())
			}
		}
	}

	var thetable = document.createElement("table")
	thetable.setAttribute("class","linktable")
	var thetablebody = document.createElement("tbody")
	var colorswitch = 1
	for (var i=0; i < link_urls.length; i++) {

		var thetablebodyrow = document.createElement("tr")
		if (colorswitch==0)
		{
			thetablebodyrow.setAttribute("class","linktable_row light")
			colorswitch++
		}
		else
		{
			thetablebodyrow.setAttribute("class","linktable_row dark")
			colorswitch--
		}
		// Link
		var a_td = document.createElement("td")
		a_td.setAttribute("class","link_td")
		var thelinkurl = document.createElement("a")
		thelinkurl.setAttribute("href",link_urls[i])
		thelinkurl.setAttribute("target","_blank")
		thelinkurl.setAttribute("rel","nofollow external")
		
		var thelinkurlspan = document.createElement("span")
		if (link_urls[i].length<60)
			thelinkurlspan.appendChild(document.createTextNode("  ("+link_urls[i]+")"))
		else
			thelinkurlspan.appendChild(document.createTextNode("  ("+link_urls[i].slice(0,60)+"...)"))
		thelinkurl.appendChild(document.createTextNode(link_infos[i]))
		a_td.appendChild(thelinkurl)
		a_td.appendChild(document.createElement("br"))
		a_td.appendChild(thelinkurlspan)
		
		thetablebodyrow.appendChild(a_td)
		// Tags
		var a_td = document.createElement("td")
		for (var k=0; k < link_tags[i].length; k++) {
			var current_linktag_link = document.createElement("a")
			current_linktag_link.setAttribute("href","#")
			current_linktag_link.setAttribute("onclick","showLinkstag(this)")
			current_linktag_link.innerHTML = link_tags[i][k]
			a_td.appendChild(current_linktag_link)
			if (k<link_tags[i].length-1)
			{
				a_td.appendChild(document.createTextNode(", "))
				
			}
		};
		thetablebodyrow.appendChild(a_td)
		//Dates
		var a_td = document.createElement("td")
		a_td.setAttribute("class","date_td")
		a_td.appendChild(document.createTextNode(link_dates[i]))
		thetablebodyrow.appendChild(a_td)
		
		// Finish Line
		thetablebody.appendChild(thetablebodyrow)
		thetable.appendChild(thetablebody)
	};
	document.getElementById("urllist").innerHTML=""
	document.getElementById("urllist").appendChild(thetable)
}

function showLinkstag (e) {
	document.getElementById('search_links').value = e.innerHTML
	build_link_list(hubionmac_linklist,e.innerHTML.toLowerCase())
	document.getElementById('search_links').select()
}


function doSearch(search_box) {	
	var q = search_box.value;
	q = q.toLowerCase();
	build_link_list(hubionmac_linklist,q);
}