function jump() {
	var q=prompt("Search for:","");
	if (q===null || q==='') {return;};
	var lang = document.getElementById("language").value;
	window.location = "http://keishi.net/ipodia/ipodia.php?q="+q+"&l="+lang;
}
function savePage() {
	alert("Save next page to your bookmarks.");
	var target = window.location + "&save=true";
	window.location = "http://keishi.net/picklr/?url="+escape(target);
}
var TextFinder = function() {
	var currentMatch = 0;
	var matches = document.getElementsByName('iPoDiA');
	var box, boxStyle, content;
	window.addEventListener("load", function() {
		content = document.getElementById('content');
		box = document.getElementById('findBox');
		boxStyle = box.style;
	}, false);
	function searchWithinTextNode(node, q, len) {
		var pos, skip, spannode, middlebit, endbit, middleclone;
		skip = 0;
		pos=node.data.toUpperCase().indexOf(q);
		if(pos>=0){
			spannode=document.createElement("SPAN");
			spannode.setAttribute('name', 'iPoDiA');
			spannode.style.backgroundColor="yellow !important";
			middlebit=node.splitText(pos);
			endbit=middlebit.splitText(len);
			middleclone=middlebit.cloneNode(true);
			spannode.appendChild(middleclone);
			middlebit.parentNode.replaceChild(spannode,middlebit);
			skip=1;
		}
		return skip;
	};

	function joinNodeLists() {
		if (!arguments.length) {
			return;
		}
		var newList = [];
		for (var i = 0; i < arguments.length; i++) {
			var list = arguments[i];
			for (var j = 0; j < list.length; j++) {
				newList[newList.length] = list[j];
			}
		}
		return newList;
	}
	return {
		find: function() {
			s=prompt("Find text:","");
			if (s===null || s==='') {
				return;
			};
			s = s.toUpperCase();
			var len = s.length;
			this.deselect();
			x=new RegExp(s,"gi");
			var elements = joinNodeLists(content.getElementsByTagName('h1'),
				content.getElementsByTagName('h2'),
				content.getElementsByTagName('p'),
				content.getElementsByTagName('a'),
				content.getElementsByTagName('span'),
				content.getElementsByTagName('b')) || [];
			//alert(!!joinNodeLists(content.getElementsByTagName('h1'), content.getElementsByTagName('h2'), content.getElementsByTagName('p'), content.getElementsByTagName('a'), content.getElementsByTagName('span'), content.getElementsByTagName('b')));
			//alert(elements);
			var spannode, middlebit, endbit, middleclone;
			for (var i=0; i < elements.length; i++) {
				for (var j=0; j < elements[i].childNodes.length; j++) {
					var node = elements[i].childNodes[j];
					if (node.nodeType === 3) {
						if (node.data.length >= len) {
							var pos=node.data.toUpperCase().indexOf(s);
							if(pos>=0){
								spannode=document.createElement("SPAN");
								spannode.setAttribute('name', 'iPoDiA');
								middlebit=node.splitText(pos);
								endbit=middlebit.splitText(len);
								middlebit.parentNode.removeChild(middlebit);
								spannode.appendChild(middlebit);
								endbit.parentNode.insertBefore(spannode, endbit);
							}
						}
					}
				}
			}
			matches = document.getElementsByName('iPoDiA');
			alert("Found "+matches.length+" matches.");
			if (matches.length < 1) {return;};
			currentMatch = 0;
			window.scrollTo(0,matches[currentMatch].offsetTop);
			boxStyle.display = 'block';
			boxStyle.top = (matches[currentMatch].offsetTop+120)+"px";
			boxStyle.left = (matches[currentMatch].offsetLeft)+"px";
			if (currentMatch===matches.length-1) {
				document.getElementById('findBoxNext').setAttribute('disabled', '');
			} else {
				document.getElementById('findBoxNext').removeAttribute('disabled');
			}
			document.getElementById('findBoxPrev').setAttribute('disabled', '');
		},
		deselect: function() {
			matches = document.getElementsByName('iPoDiA');
			if (matches) {
				while (0 < matches.length) {
					matches[0].removeAttribute("name");
					//matches[i].parentNode.replaceChild(document.createTextNode(matches[i].innerHTML || ""), matches[i]);
				}
			};
			/*
			b = content.innerHTML;
			b=b.replace("/<span name="+matchName+" style=\"color:#000 !important;background-color:yellow !important;font-weight:bold !important;\">(.*?)<\/span>/", '$1');
			content.innerHTML=b;
			*/
			boxStyle.display = 'none';
		},
		next: function() {
			if (matches.length-1 > currentMatch) {
				currentMatch++;
				window.scrollTo(0,matches[currentMatch].offsetTop);
				boxStyle.top = (matches[currentMatch].offsetTop+120)+"px";
				boxStyle.left = (matches[currentMatch].offsetLeft)+"px";
				if (currentMatch===matches.length-1) {
					document.getElementById('findBoxNext').setAttribute('disabled', '');
				} else {
					document.getElementById('findBoxNext').removeAttribute('disabled');
				}
				if (currentMatch===0) {
					document.getElementById('findBoxPrev').setAttribute('disabled', '');
				} else {
					document.getElementById('findBoxPrev').removeAttribute('disabled');
				}
			}
			
		},
		prev: function() {
			if (0 < currentMatch) {
				currentMatch--;
				window.scrollTo(0,matches[currentMatch].offsetTop);
				boxStyle.top = (matches[currentMatch].offsetTop+120)+"px";
				boxStyle.left = (matches[currentMatch].offsetLeft)+"px";
				if (currentMatch===matches.length-1) {
					document.getElementById('findBoxNext').setAttribute('disabled', '');
				} else {
					document.getElementById('findBoxNext').removeAttribute('disabled');
				}
				if (currentMatch===0) {
					document.getElementById('findBoxPrev').setAttribute('disabled', '');
				} else {
					document.getElementById('findBoxPrev').removeAttribute('disabled');
				}
			}
			
		},
		dismissBox: function() {
			this.deselect();
			boxStyle.display = 'none';
		}
	};
}();