window.onload = function() {
	refreshInput("search","search phrase");
}

function refreshInput(ID,Value){
	if(document.getElementById(ID)) {
		document.getElementById(ID).onfocus = function() {
			if (this.value == Value) {
				this.value = "";
			}
		}
		document.getElementById(ID).onblur = function() {
			if (this.value == "") {
				this.value = Value;
			}
		}
	}
}

function moduleAlternateItems( /*string*/ mode ) {
	var uls = document.getElementById("modules").getElementsByTagName("ul");
	for(var i=0;i<uls.length;i++) {
		if (uls[i].className.indexOf("resources-list-1") != -1) {
			lis = uls[i].getElementsByTagName("li");
			if (mode == "home") {
				for(var j=1;j<lis.length;j+=2) {
					lis[j].className = "odd";
				}
			}
			for(var k=0;k<lis.length;k++) {
				dt = (mode == "home") ? lis[k].getElementsByTagName("dt")[0] : lis[k];
				existingLinkHref = dt.getElementsByTagName("a")[0].href;
				newLink = document.createElement("a");
				newLink.href = existingLinkHref;
				newLink.className = "more";
				newLink.title = "Read more";
				image = document.createElement("img");
				image.src = "/wp-content/themes/helldesign/images/grid/dots_arrow4.gif";
				image.alt = "";
				newLink.appendChild(image);
				dt.appendChild(newLink);
			}
		}
	}
}

function columnize_ul( ul_id, parent_id ) {
	if (document.getElementById && document.appendChild) {
		var cols = 3;
		var original_ul = document.getElementById(ul_id);
		var to_insert_before = document.getElementById(parent_id).getElementsByTagName("div")[0];
		//var original_lis = original_ul.getElementsByTagName("li");
		var original_lis = children_of(original_ul);
		var items_per_col = Math.ceil(original_lis.length/cols);
		// Let's add a className to the already existing UL - it'll be the first col
		/*(original_ul.className == "") ? original_ul.className = "col1" : original_ul.className += " col1";*/
		if (original_lis.length > cols) {
			for (i=2;i<=cols;i++) {
				eval("var col"+i+" = document.createElement('ul');");
				/*eval("col"+i+".className = 'col"+i+"';");*/
				for (j = 0;j<items_per_col;j++) {
					if (typeof(original_lis[items_per_col]) != "undefined") {
						eval("col"+i+".appendChild(original_lis[items_per_col]);");
					}
				}
				//eval("document.getElementById('"+parent_id+"').appendChild(col"+i+");");
				eval("document.getElementById(parent_id).insertBefore(col"+i+",to_insert_before);");
			}
		}
	}
}

function readMoreLinks() {
	var rm_links = document.getElementById("posts-list").getElementsByTagName("a");
	for (var i=0;i<rm_links.length;i++) {
		if (rm_links[i].firstChild.nodeValue == "Read more") {
			rm_links[i].className = "more";
		}
	}
}

function fixCatetegoryList( id ) {
	listParent = document.getElementById(id); // parent element of the category list
	list = listParent.getElementsByTagName("ul")[0]; // the list itself
	//lis = list.getElementsByTagName("li"); // collection of <li> elements - each holds a category
	lis = children_of(list);
	for (var i=0;i<lis.length;i++) { // looping through <li> children of the list - which are main categories
		childUL = lis[i].getElementsByTagName("ul")[0]; // gettin <ul> list of subcategories
		if (typeof(childUL) != "undefined" && childUL.className == "children") {
			// we need to summarize posts in subcategories here - eeesh
			childULLI = childUL.getElementsByTagName("li");
			parentPostsSum = 0;
			rx = new RegExp("\(([0-9]+)\)");
			for (var j=0;j<childULLI.length;j++) {
				var currentNode = last_child(childULLI[j]).nodeValue;
				parentPostsSum += Number(rx.exec(currentNode)[1]);
			}
			counterNode = node_before(childUL);
			counterNodeValue = Number(rx.exec(counterNode.nodeValue)[1]);
			counterNode.nodeValue = " ("+ (parentPostsSum + counterNodeValue) + ") ";
			newLink = document.createElement("a");
			newLink.title = "Show category's children";
			newLink.href = "#";
			//newLink.className = "unfold";
			newLink.onclick = function() {
				catsList = node_after(this);
				if (catsList.style.display != "block") {
					catsList.style.display = "block";
					first_child(this).src = "/wp-content/themes/helldesign/images/grid/arrow_fold.gif";
					this.title = "Hide category's children";
				} else {
					catsList.style.display = "none";
					first_child(this).src = "/wp-content/themes/helldesign/images/grid/arrow_unfold.gif";
					this.title = "Show category's children";
				}
				return false;
			}
			newImage = document.createElement("img");
			newImage.src = "/wp-content/themes/helldesign/images/grid/arrow_unfold.gif";
			newImage.alt = "";
			newLink.appendChild(newImage);
			lis[i].insertBefore(newLink, childUL );
		}
	}
}
