//Config Values
var catalogTableId = "tbl-catalog";
var ODvalues = new Array();
var IDvalues = new Array();

function cleanWhitespace(node) {
	for (var x = 0; x < node.childNodes.length; x++) {
		var childNode = node.childNodes[x];
		if (childNode.nodeType == 3 && !/\S/.test(childNode.nodeValue)) {
			node.removeChild(node.childNodes[x]);
			x--;
		}
		if (childNode.nodeType == 1) { cleanWhitespace(childNode); }
	}
}

function msort(array, begin, end){
	var size=end-begin;
	if(size<2) return;

	var begin_right=begin+Math.floor(size/2);

	msort(array, begin, begin_right);
	msort(array, begin_right, end);
	merge(array, begin, begin_right, end);
}

function merge(array, begin, begin_right, end){
	for(;begin<begin_right; ++begin) {
		if(array[begin]>array[begin_right]) {
			var v=array[begin];
			array[begin]=array[begin_right];
			insert(array, begin_right, end, v);
		}
	}
}

Array.prototype.swap=function(a, b){
	var tmp=this[a];
	this[a]=this[b];
	this[b]=tmp;
}

function insert(array, begin, end, v){
	while(begin+1<end && array[begin+1]<v) {
		array.swap(begin, begin+1);
		++begin;
	}
	array[begin]=v;
}

function buildIndex(){
	var catTableRows = document.getElementById(catalogTableId).getElementsByTagName("tr");
	for(var i = 1; i < (catTableRows.length - 1); i++){
		ODvalues[ODvalues.length] = {link: i, value: parseFloat(catTableRows[i].childNodes[1].firstChild.nodeValue)}
		IDvalues[IDvalues.length] = {link: i, value: parseFloat(catTableRows[i].childNodes[2].firstChild.nodeValue)}
	}
	msort(ODvalues, 0, ODvalues.length);
	msort(IDvalues, 0, IDvalues.length);
}

function executeSearch(){
	var searchTerm = document.getElementById("searchterm").value/1;
	var searchColumn = document.getElementById("searchcolumn").value;
	var searchResultMessage = document.getElementById("searchresultmessage");
  	var catTableRows = document.getElementById(catalogTableId).getElementsByTagName("tr");
	var valueArray = searchColumn/1 == 0 ? ODvalues : IDvalues;
	var termfound = false;
	searchResultMessage.style.display = "none";

	if(isNaN(searchTerm)) { searchResultMessage.style.display = "block"; return false; }

	for(var i = 1; i < catTableRows.length; i++){
		catTableRows[i].className = catTableRows[i].className.replace(/\bfounditem\b/, "");
		catTableRows[i].onmouseover = function(){ this.style.backgroundColor="#BBB"; }
		catTableRows[i].onmouseout = function(){ this.style.backgroundColor=""; }
	}
	
	if(searchTerm > valueArray[valueArray.length - 1].value){
		var URL = window.location.href.toString();
		window.location = URL.substring(0, URL.indexOf(window.location.hash))  + "#" + (valueArray[valueArray.length-1].link + 1);
		catTableRows[catTableRows.length-1].className += " founditem";
		catTableRows[catTableRows.length-1].onmouseover = "";
		catTableRows[catTableRows.length-1].onmouseout = "";
		termfound = true;
	}
	
	if(!termfound){
		for(var j = 0; j < valueArray.length; j++){
			if(searchTerm == valueArray[j].value || searchTerm < valueArray[j+1].value){
				var URL = window.location.href.toString();
				window.location = URL.substring(0, URL.indexOf(window.location.hash))  + "#" + valueArray[j].link;
				catTableRows[j+1].className += " founditem";
				catTableRows[j+1].onmouseover = "";
				catTableRows[j+1].onmouseout = "";
				termfound = true;
				break;
			}
		}
	}
	
	if(!termfound) { searchResultMessage.style.display = "block"; }
	return false;
}

function catalogInit() {
	if(!document.getElementById){ return; }
	var catTable = document.getElementById(catalogTableId);
	var catTableRows = catTable.getElementsByTagName("tr");
	var searchForm = document.getElementById("searchform");
	var part, partrow;
	var URL = window.location.href.toString();

	cleanWhitespace(catTable);

	for(var j = 1; j < catTableRows.length; j++){
		if(j % 2 == 0){ catTableRows[j].className += " odd"; }
		link = document.createElement('a');
		link.name = link.id = (j - 1);
		catTableRows[j].firstChild.appendChild(link);
		if(j == (catTableRows.length-1)) { break; }
		catTableRows[j].onmouseover = function(){ this.style.backgroundColor="#BBB"; }
		catTableRows[j].onmouseout = function(){ this.style.backgroundColor=""; }
		catTableRows[j].onclick = function(){ if(!/\bselecteditem\b/.test(this.className)){ this.className += " selecteditem"; this.onmouseover = ""; this.onmouseout = ""; }
						      else{ this.className = this.className.replace(/\bselecteditem\b/, ""); this.onmouseover = function(){ this.style.backgroundColor="#BBB"; }; this.onmouseout = function(){ this.style.backgroundColor=""; }; }
    						}
  	}
  	
	buildIndex();
	searchForm.onsubmit  = executeSearch;
	searchForm.style.display = "block";
	
	var y;
	if (self.innerHeight){ y = self.innerHeight; }
	else if (document.documentElement && document.documentElement.clientHeight){ y = document.documentElement.clientHeight; }
	else if (document.body) { y = document.body.clientHeight; }

	catTable.style.marginBottom = (y + "px");

	if(window.location.href.indexOf('#') >= 0){
		window.location.href = window.location.href;
		part = URL.substring((URL.indexOf("#") + 1), URL.length);
		part = document.getElementById(part);
		if(part){
			partrow = part.parentNode.parentNode;
			partrow.className += " founditem";
			partrow.onmouseover = "";
			partrow.onmouseout = "";
		}

	}
}

var cataloghead, catalogheadYpos;
function catalogScroll() {
	if(catalogheadYpos <= findPageScroll()) { cataloghead.className = "fixCatalogHeader"; }
	else { cataloghead.className = "staticCatalogHeader"; }
}

function scrollInit() {
	cataloghead = document.getElementById("cataloghead");
	catalogheadYpos = findPos(cataloghead);
}

function findPos(obj) {
	var curtop = 0;
	if (obj.offsetParent) {
		curtop = obj.offsetTop;
		while (obj = obj.offsetParent) { curtop += obj.offsetTop; }
	}
	return curtop;
}

function findPageScroll() {
	var y;
	if (self.pageYOffset) { y = self.pageYOffset; }
	else if (document.documentElement && document.documentElement.scrollTop) { y = document.documentElement.scrollTop; }
	else if (document.body) { y = document.body.scrollTop; }
	return y;
}

function addEvent( obj, type, fn ) {
	if ( obj.attachEvent ) {
		obj['e'+type+fn] = fn;
		obj[type+fn] = function(){obj['e'+type+fn]( window.event );}
		obj.attachEvent( 'on'+type, obj[type+fn] );
	}
	else { obj.addEventListener( type, fn, false ); }
}

addEvent(window, 'load', catalogInit);
addEvent(window, 'load', scrollInit);
addEvent(window, 'scroll', catalogScroll);