var Search = {
	currentPage: 0,
	resultsFound: false,
	resultsList: [],
	rootNode: {},
	layout: "",

	setDisplay: function () {
		if(this.getLayout() === "v16") {
			this.displayResultsV16();
		} else if(this.getLayout() === "v15" || this.getLayout() === "v14") {
			this.displayResultsV15();
		} else {
			this.displayResultsV8();
		}
	},

	setLayout: function (layout) {
		this.layout = layout;
	},

	getLayout: function () {
		return this.layout;
	},

	keyPress: function (e) {
		var unicode=e.keyCode? e.keyCode : e.charCode
		if(unicode == 13) {
			Search.startSearch();
		}
	},
	
	getKeyWord: function (term, text) {
		var newText = "";
		var regExp = new RegExp(term, "gim");
		if(text.match(regExp)) {
			var newTerm = text.match(regExp)[0];
			newText = text.replace(regExp, "<strong>" + newTerm + "</strong>");
			return newText;
		}
		return text;
	},

	getDocument: function (xmlHttp) {
		this.rootNode = {};
		var response = xmlHttp.responseXML;
		var rootNode = response.documentElement;
		this.rootNode = rootNode;
		this.matchCriteria();
	},

	resetData: function () {
		this.resultsList = [];
		this.currentPage = 0;
		this.resultsFound = false;
	},
	
	matchCriteria: function () {
		var rootNode = this.rootNode;
		this.resetData();
		var title, description, url, keyWords;
		var searchField = document.getElementById("searchField").value;
		var documentSize = rootNode.childNodes.length;
		var criteria = new RegExp(searchField, "gim");

		if(searchField.length <= 0) {
			document.getElementById("results").innerHTML = "<p class=\"ibm-error\">Pros&iacute;m V&aacute;s vyplnte vyhled&aacute;vac&iacute; krit&eacute;ria.</p>";
			return false;
		}

		for (var i = 0; i < documentSize; i++) {
			if(rootNode.childNodes[i].nodeName != "#text") {
				url = rootNode.childNodes[i].getElementsByTagName("url")[0].firstChild.nodeValue;
				title = rootNode.childNodes[i].getElementsByTagName("title")[0].firstChild.nodeValue;
				description = rootNode.childNodes[i].getElementsByTagName("description")[0].firstChild.nodeValue;
				keyWords = rootNode.childNodes[i].getElementsByTagName("bad_type")[0].firstChild.nodeValue;
				if (url.match(criteria) || title.match(criteria) || description.match(criteria) || url.match(criteria)) {
					this.resultsList[this.resultsList.length] = i;
					this.resultsFound = true;
				}
			}
		}
		if(this.resultsFound) {
			this.setDisplay();
		} else {
			document.getElementById("results").innerHTML = "<p><strong>Bylo n&aacute;jden&yacute;ch 0 v&yacute;sledk&#367; vyhled&aacute;vac&iacute;.</strong></p>";
			return false;
		}
	},

	nextPage: function () {
		var rootNode = this.rootNode;
		var resultsPerPage = Number(document.getElementById("resultsPerPage").value);
		this.currentPage =this.currentPage + resultsPerPage;
		this.setDisplay();
	},

	previousPage: function () {
		var rootNode = this.rootNode;
		var resultsPerPage = Number(document.getElementById("resultsPerPage").value);
		this.currentPage =this.currentPage - resultsPerPage;
		this.setDisplay();
		
	},

	displayResultsV16: function () {
		var rootNode = this.rootNode;
		var resultsList = this.resultsList;
		var totalResults = resultsList.length;
		var resultsPerPage = Number(document.getElementById("resultsPerPage").value);
		var currentPage = this.currentPage;
		var perPage = Number(currentPage + resultsPerPage);
		var htmlCode = "";
		var headerFooter = "";
		
		if (totalResults > perPage) {
			if(currentPage == 0) {
				headerFooter = "<p align='right'><strong>" + (currentPage+ 1) + "</strong> - <strong>" + perPage + "</strong> z <strong>" + totalResults + "</strong> v&yacute;sledk&#367; | <a class='ibm-forward-link' href='javascript:void(0)' onclick='Search.nextPage();'>Dal&scaron;&iacute;</a></p>";
			} else if (currentPage > 0 && currentPage <= totalResults) {
				headerFooter = "<p align='right'><strong>" + (currentPage+ 1) + "</strong> - <strong>" + perPage + "</strong> z <strong>" + totalResults + "</strong> v&yacute;sledk&#367; | <a class='ibm-forward-link' href='javascript:void(0)' onclick='Search.previousPage();'>P&#345;edchoz&iacute;</a> | <a class='ibm-forward-link' href='javascript:void(0)' onclick='Search.nextPage();'>Dal&scaron;&iacute;</a></p>";
			}
			
		} else if (totalResults <= resultsPerPage){
			perPage = totalResults;
			headerFooter = "<p align='right'>" + (currentPage+ 1) + " - " + perPage + " z " + totalResults + " v&yacute;sledk&#367;</p>";

		} else if (currentPage >= resultsPerPage || totalResults == perPage) {
			perPage = totalResults
			headerFooter = "<p align='right'><strong>" + (currentPage+ 1) + "</strong> - <strong>" + perPage + "</strong> z <strong>" + totalResults + "</strong> v&yacute;sledk&#367; | <a class='ibm-forward-link' href='javascript:void(0)' onclick='Search.previousPage();'>P&#345;edchoz&iacute;</a></p>";
		}

		htmlCode = headerFooter;
		htmlCode += "<div class='ibm-rule'><hr /></div>";

		for(var i = currentPage; i < perPage; i++) {
			var term = document.getElementById("searchField").value;
			var url = rootNode.childNodes[resultsList[i]].getElementsByTagName("url")[0].firstChild.nodeValue;
			var title = this.getKeyWord(term, rootNode.childNodes[resultsList[i]].getElementsByTagName("title")[0].firstChild.nodeValue);
			var description = this.getKeyWord(term, rootNode.childNodes[resultsList[i]].getElementsByTagName("description")[0].firstChild.nodeValue);
			
			htmlCode += "<p><a href='" + url + "'>" + title + "</a></p>";
			htmlCode += "<p>" + description + "</p>";
			if(url.length > 80) {
				htmlCode += "<p><strong><a href='" + url + "'>" + url.substr(0, 80) + "...</a></strong></p>";
			} else {
				htmlCode += "<p><strong><a href='" + url + "'>" + url + "</a></strong></p>";
			}
			htmlCode += "<div class='ibm-rule'><hr /></div>";
		}

		htmlCode += headerFooter;

		document.getElementById("results").innerHTML = "<div class=\"ibm-container\"><h2>V&yacute;sledky hled&aacute;n&iacute;</h2><div class=\"ibm-container-body\">" + htmlCode + "</div></div>";
	},

	displayResultsV15: function () {
		var rootNode = this.rootNode;
		var resultsList = this.resultsList;
		var totalResults = resultsList.length;
		var resultsPerPage = Number(document.getElementById("resultsPerPage").value);
		var currentPage = this.currentPage;
		var perPage = Number(currentPage + resultsPerPage);
		var htmlCode = "";
		var headerFooter = "";
		var space = "<tr><td valign=\"top\"><img width=\"1\" height=\"5\" border=\"0\" alt=\"\" src=\"//www.ibm.com/i/c.gif\"/></td></tr>";
		var rule = "<tr class=\"dotted\"><td valign=\"top\"><img width=\"1\" height=\"1\" border=\"0\" alt=\"\" src=\"//www.ibm.com/i/c.gif\"/></td></tr>";

		headerFooter = "<tr>";
		headerFooter += "<td>";

		if (totalResults > perPage) {
			if(currentPage == 0) {
				headerFooter += "<p align='right'><strong>" + (currentPage+ 1) + "</strong> - <strong>" + perPage + "</strong> z <strong>" + totalResults + "</strong> v&yacute;sledk&#367; | <a class='ibm-forward-link' href='javascript:void(0)' onclick='Search.nextPage();'>Dal&scaron;&iacute;</a></p>";
			} else if (currentPage > 0 && currentPage <= totalResults) {
				headerFooter = "<p align='right'><strong>" + (currentPage+ 1) + "</strong> - <strong>" + perPage + "</strong> z <strong>" + totalResults + "</strong> v&yacute;sledk&#367; | <a class='ibm-forward-link' href='javascript:void(0)' onclick='Search.previousPage();'>P&#345;edchoz&iacute;</a> | <a class='ibm-forward-link' href='javascript:void(0)' onclick='Search.nextPage();'>Dal&scaron;&iacute;</a></p>";
			}
			
		} else if (totalResults <= resultsPerPage){
			perPage = totalResults;
			headerFooter = "<p align='right'>" + (currentPage+ 1) + " - " + perPage + " of " + totalResults + " results</p>";

		} else if (currentPage >= resultsPerPage || totalResults == perPage) {
			perPage = totalResults
			headerFooter = "<p align='right'><strong>" + (currentPage+ 1) + "</strong> - <strong>" + perPage + "</strong> z <strong>" + totalResults + "</strong> v&yacute;sledk&#367; | <a class='ibm-forward-link' href='javascript:void(0)' onclick='Search.previousPage();'>P&#345;edchoz&iacute;</a></p>";
		}

		headerFooter += "</td>";
		headerFooter += "</tr>";

		htmlCode = headerFooter;
		htmlCode += rule;

		for(var i = currentPage; i <perPage; i++) {
			var term = document.getElementById("searchField").value;
			var url = rootNode.childNodes[resultsList[i]].getElementsByTagName("url")[0].firstChild.nodeValue;
			var title = this.getKeyWord(term, rootNode.childNodes[resultsList[i]].getElementsByTagName("title")[0].firstChild.nodeValue);
			var description = this.getKeyWord(term, rootNode.childNodes[resultsList[i]].getElementsByTagName("description")[0].firstChild.nodeValue);

			htmlCode += "<tr>";
			htmlCode += "<td>";
			htmlCode += "<p><a href='" + url + "'>" + title + "</a</p>";
			htmlCode += "<p>" + description + "</p>";
			if(url.length > 50) {
				htmlCode += "<p><strong><a href='" + url + "'>" + url.substr(0, 50) + "...</a></strong></p>";
			} else {
				htmlCode += "<p><strong><a href='" + url + "'>" + url + "</a></strong></p>";
			}
			htmlCode += "</td>";
			htmlCode += "</tr>";
			htmlCode += rule;
		}


		htmlCode += headerFooter;
		
		document.getElementById("results").innerHTML = "<br /><table width=\"443\" class=\"v14-gray-table-border\" cellpadding=\"0\" cellspacing=\"0\"><tr><td class=\"v14-header-2\">V&yacute;sledky hled&aacute;n&iacute;</td></tr>" + htmlCode + "</table>";

	},

	displayResultsV8: function () {
		var rootNode = this.rootNode;
		var resultsList = this.resultsList;
		var totalResults = resultsList.length;
		var resultsPerPage = Number(document.getElementById("resultsPerPage").value);
		var currentPage = this.currentPage;
		var perPage = Number(currentPage + resultsPerPage);
		var htmlCode = "";
		var headerFooter = "";
		
		if (totalResults > perPage) {
			if(currentPage == 0) {
				headerFooter = "<p align='right'><strong>" + (currentPage+ 1) + "</strong> - <strong>" + perPage + "</strong> z <strong>" + totalResults + "</strong> v&yacute;sledk&#367; | <a class='ibm-forward-link' href='javascript:void(0)' onclick='Search.nextPage();'>Dal&scaron;&iacute;</a></p>";
			} else if (currentPage > 0 && currentPage <= totalResults) {
				headerFooter = "<p align='right'><strong>" + (currentPage+ 1) + "</strong> - <strong>" + perPage + "</strong> z <strong>" + totalResults + "</strong> v&yacute;sledk&#367; | <a class='ibm-forward-link' href='javascript:void(0)' onclick='Search.previousPage();'>P&#345;edchoz&iacute;</a> | <a class='ibm-forward-link' href='javascript:void(0)' onclick='Search.nextPage();'>Dal&scaron;&iacute;</a></p>";
			}
			
		} else if (totalResults <= resultsPerPage){
			perPage = totalResults;
			headerFooter = "<p align='right'>" + (currentPage+ 1) + " - " + perPage + " of " + totalResults + " results</p>";

		} else if (currentPage >= resultsPerPage || totalResults == perPage) {
			perPage = totalResults
			headerFooter = "<p align='right'><strong>" + (currentPage+ 1) + "</strong> - <strong>" + perPage + "</strong> z <strong>" + totalResults + "</strong> v&yacute;sledk&#367; | <a class='ibm-forward-link' href='javascript:void(0)' onclick='Search.previousPage();'>P&#345;edchoz&iacute;</a></p>";
		}

		htmlCode = headerFooter;
		htmlCode += "<div class=\"hrule-dots\"></div>";
		
		for(var i = currentPage; i < perPage; i++) {
			var term = document.getElementById("searchField").value;
			var url = rootNode.childNodes[resultsList[i]].getElementsByTagName("url")[0].firstChild.nodeValue;
			var title = this.getKeyWord(term, rootNode.childNodes[resultsList[i]].getElementsByTagName("title")[0].firstChild.nodeValue);
			var description = this.getKeyWord(term, rootNode.childNodes[resultsList[i]].getElementsByTagName("description")[0].firstChild.nodeValue);
			
			htmlCode += "<p><a href='" + url + "'>" + title + "</a></p>";
			htmlCode += "<p>" + description + "</p>";
			if(url.length > 70) {
				htmlCode += "<p><a href='" + url + "'>" + url.substr(0, 70) + "...</a></p>";
			} else {
				htmlCode += "<p><strong><a href='" + url + "'>" + url + "</a></strong></p>";
			}
			htmlCode += "<br />";
			htmlCode += "<div class=\"hrule-dots\"></div>";
		}

		htmlCode += headerFooter;
		
		document.getElementById("results").innerHTML = "<br /><div class=\"callout\"><h2 class=\"bar-green-dark\">V&yacute;sledky</h2>" + htmlCode + "</div>";
		
	},
	
	startSearch: function() {
		Ajax.createRequest("GET",url, null);
	}
};