var currentCategory;

function Load() {
	ShowSubCategories(0);
	document.getElementById("query").focus();
}

//-------
function ShowLinks(id) {
	document.getElementById("linkstop").style.display="none";
	document.getElementById("linksmid").style.display="none";
	document.getElementById("linksbottom").style.display="none";
	
	Links.ShowTopLinks(id,ShowTopLinks_callback)
	Links.ShowMidLinks(id,ShowMidLinks_callback)
	Links.ShowBottomLinks(id,ShowBottomLinks_callback)
}
function ShowTopLinks_callback(response) {
	 if (response.error != null){
	  alert(response.error);
	  return;
	 }
	var collection = response.value;
	var links = document.getElementById("linkstop");
	links.innerHTML="";
	
	var div;
	var link;
	if (links) {
		if (collection.length!=0) {
			for (var i=0; i < collection.length; i++) {
				link = document.createElement("a")
				link.setAttribute("href","../redirect.aspx?id=" + collection[i].Id)
				link.setAttribute("target","_blank")
				link.appendChild(document.createTextNode(collection[i].Name))	
				div = document.createElement("div")
				div.appendChild(link)
				div.style.paddingTop="4px"
				links.appendChild(div)
				div = document.createElement("div")
				div.appendChild(document.createTextNode(collection[i].Description))
				links.appendChild(div)
			}	 
			links.style.display="block"
		}
	}	 
}
function ShowMidLinks_callback(response) {
	 if (response.error != null){
	  alert(response.error);
	  return;
	 }
	var collection = response.value;
	var links = document.getElementById("linksmid");
	links.innerHTML="";
	var div;
	var link;
	if (links) {
		if (collection.length!=0) {
			for (var i=0; i < collection.length; i++) {
				link = document.createElement("a")
				link.setAttribute("href","../redirect.aspx?id=" + collection[i].Id)
				link.setAttribute("target","_blank")
				link.appendChild(document.createTextNode(collection[i].Name))	
				div = document.createElement("div")
				div.appendChild(link)
				div.style.paddingTop="4px"
				links.appendChild(div)
				div = document.createElement("div")
				div.appendChild(document.createTextNode(collection[i].Description))
				links.appendChild(div)
			}	 
			links.style.display="block"
		}
	}
}
function ShowBottomLinks_callback(response) {
	 if (response.error != null){
	  alert(response.error);
	  return;
	 }
	var collection = response.value;
	var links = document.getElementById("linksbottom");
	links.innerHTML="";
	var div;
	var link;
	if (links) {
		if (collection.length!=0) {	
			for (var i=0; i < collection.length; i++) {
				link = document.createElement("a")
				link.setAttribute("href","../redirect.aspx?id=" + collection[i].Id)
				link.setAttribute("target","_blank")
				link.appendChild(document.createTextNode(collection[i].Name))	
				div = document.createElement("div")
				div.appendChild(link)
				div.style.paddingTop="4px"
				links.appendChild(div)
			}	 
			links.style.display="block"
		}
	}
}
//-------

//-------
function ShowSearch() {
	var query = document.getElementById("query").value;
	if (query!="") {
		document.getElementById("linkstop").style.display="none";
		document.getElementById("linksmid").style.display="none";
		document.getElementById("linksbottom").style.display="none";
		Links.ShowSearch(query,ShowSearch_callback)
	}
}
function ShowSearch_callback(response) {
	 if (response.error != null){
	  alert(response.error);
	  return;
	 }
	var links = document.getElementById("linksbottom");
	links.innerHTML="";
	var collection = response.value;
	var links = document.getElementById("linksbottom");
	links.innerHTML="";
	links = document.getElementById("linksmid");
	links.innerHTML="";
	links = document.getElementById("linkstop");
	links.innerHTML="";
	var div;
	var link;
	if (links) {
		if (collection.length==0) {
			links.innerHTML="Ingen søkeresultat.";
		}else{
			links.appendChild(document.createTextNode("Det ble funnet " + collection.length + " lenker."))
			links.style.display="block"
		}
		for (var i=0; i < collection.length; i++) {
			link = document.createElement("a")
			link.setAttribute("href","../redirect.aspx?id=" + collection[i].Id)
			link.setAttribute("target","_blank")
			link.appendChild(document.createTextNode(collection[i].Name))	
			div = document.createElement("div")
			div.style.paddingTop="4px"
			div.appendChild(link)
			links.appendChild(div)
			div = document.createElement("div")
			div.appendChild(document.createTextNode(collection[i].Description))
			links.appendChild(div)
 
		}
	}
}
//-------

//-------
function ShowCategories(query) {
	currentCategory=document.getElementById("cat0")
	Links.ShowCategories(query,ShowCategories_callback)
}
function ShowCategories_callback(response) {
	 if (response.error != null){
	  alert(response.error);
	  return;
	 }

	var categories = document.getElementById("cat0")
	PopulateCategory(categories,response.value)
}
//-------

//-------
function ShowSubCategories(id) {
	ShowLinks(id)
	currentCategory=document.getElementById("cat" + id)
	if (id!=0) {
		CloseChildren(currentCategory.parentNode)
	}else{
		currentCategory.innerHTML=""
	}
	if (currentCategory) {
		if (currentCategory.innerHTML=="") {
			Links.ShowSubCategories(id,ShowSubCategories_callback)
		}else{
			currentCategory.style.display="block"
		}
	}else{
		alert('finner ikke div')
	}
}
function ShowSubCategories_callback(response) {
	 if (response.error != null){
	  alert(response.error);
	  return;
	 }

	if (currentCategory) {
		PopulateCategory(currentCategory,response.value)
		//currentCategory.style.display="block"
	}else{
		alert('finner ikke currentCategory')
	}
}
//-------
//##################################################################################################
function PopulateCategory(container,collection) {
	var id=currentCategory.id.substring(3)
	if (container.innerHTML) container.innerHTML="";
	var category;
	var subcategories;
	if (collection.length!=0) {
		for (var i=0; i < collection.length; i++) {
			category = document.createElement("div")
			category.className="categoryName";
			category.id="catname" + collection[i].Id;
	
			category.onclick = function() {
				ShowSubCategories(this.id.substring(7))
			}
			//	category.setAttribute("onclick","ShowSubCategories(" + collection[i].Id + ")
	
			category.appendChild(document.createTextNode(collection[i].Name));
			container.appendChild(category);
			subcategories = document.createElement("div");
			subcategories.id="cat" + collection[i].Id;
			subcategories.className="menu";
			subcategories.style.display="none";
			container.appendChild(subcategories);
		}
		currentCategory.style.display="block"
	}
	document.getElementById("catname" + id).style.textDecoration='underline'
}
function CloseChildren(container) {
	var divs = container.childNodes
	for (var i=0; i < divs.length; i++) {
		if (divs[i].id.substring(0,7)!="catname") {
				divs[i].style.display="none"
		}else{
			divs[i].style.textDecoration="none"
		}
	}
}

