/**
 * @author Ubiquiti
 */
function searchplugin() {
	var usp = this;
	this.pointer = null;
	window.hideSuggestions = true;
	this.searchButton = document.getElementById("usp_searchbutton");
	this.searchField = document.getElementById("usp_searchfield");
	this.suggestionsPanel = document.getElementById("usp_suggestionspanel");
	this.list = this.suggestionsPanel.getElementsByTagName('div');
	searchField.onkeyup = function(e) {
		e = e || window.event;
		switch(e.keyCode) {
			case 38:
				//up
				usp.selectSuggestionDiv(-1);
				break;
			case 40:
				//down
				usp.selectSuggestionDiv(1);
				break;
			case 13:
				//enter
				searchButton.onclick();
				break;
			case 27:
				//esc
				window.hideSuggestions = true;
				searchField.onblur();
				break;
			default:
				savedQuery = searchField.value;
				var scriptElement = document.createElement("script");
				scriptElement.setAttribute("language", "javascript");
				if((searchField.value == "") || (searchField.value.length < 2)) {
					window.hideSuggestions = true;
					searchField.onblur();
					break;
				}
				if(searchField.value.length > 1) {
					scriptElement.setAttribute("src", "http://ubiquiti.dyndns.org/uspServer/xd/usp/suggestions?c=10&q=" + searchField.value);
					document.getElementsByTagName("head").item(0).appendChild(scriptElement);
				}
		}
	}
	searchField.onblur = function() {
		if(window.hideSuggestions === true) {
			document.getElementById("usp_suggestionspanel").style.visibility = "hidden";
			document.getElementById("usp_suggestionspanel").innerHTML = "";
		}
	}
	window.onresize = function() {
		suggestionsPanel.style.left = searchField.offsetLeft;
	}

	searchButton.onclick = function() {
		var scriptElement = document.createElement("script");
		scriptElement.setAttribute("language", "javascript");
		scriptElement.setAttribute("src", "http://ubiquiti.dyndns.org/uspServer/xd/usp/encrypt?q=" + searchField.value);
		document.getElementsByTagName("head").item(0).appendChild(scriptElement);
	}

	this.selectSuggestionDiv = function(inc) {
		if(this.pointer === null) {
			this.pointer = 0;
			this.list[this.pointer].style.backgroundColor = 'lightblue';
			this.searchField.value = this.list[this.pointer].innerHTML;
		} else {
			this.pointer += inc;
			if(this.pointer > 0 && this.pointer < this.list.length - 1) {
				this.list[this.pointer - 1].style.backgroundColor = 'white';
				this.list[this.pointer].style.backgroundColor = 'lightblue';
				this.list[this.pointer + 1].style.backgroundColor = 'white';
				this.searchField.value = this.list[this.pointer].innerHTML;
			} else if(this.pointer === 0) {
				this.list[this.pointer].style.backgroundColor = 'lightblue';
				this.list[this.pointer + 1].style.backgroundColor = 'white';
				this.searchField.value = this.list[this.pointer].innerHTML;
			} else if(this.pointer === this.list.length - 1) {
				this.list[this.pointer - 1].style.backgroundColor = 'white';
				this.list[this.pointer].style.backgroundColor = 'lightblue';
				this.searchField.value = this.list[this.pointer].innerHTML;
			} else if(this.pointer === this.list.length) {
				this.searchField.value = savedQuery;
				this.pointer = -1;
				this.list[this.list.length - 1].style.backgroundColor = 'white';
			} else if(this.pointer === -1) {
				this.searchField.value = savedQuery;
				this.pointer = this.list.length;
				this.list[0].style.backgroundColor = 'white';
			} else if(this.pointer === this.list.length + 1) {
				this.pointer = 0;
				this.list[this.pointer].style.backgroundColor = 'lightblue';
				this.list[this.pointer + 1].style.backgroundColor = 'white';
				this.searchField.value = this.list[this.pointer].innerHTML;
			} else if(this.pointer === -2) {
				this.pointer = this.list.length - 1;
				this.list[this.pointer - 1].style.backgroundColor = 'white';
				this.list[this.pointer].style.backgroundColor = 'lightblue';
				this.searchField.value = this.list[this.pointer].innerHTML;
			}
		}
	}
}

function parseSuggestions(data) {
    window.hideSuggestions = false;
	this.pointer = null;
	var searchField = document.getElementById("usp_searchfield");
	var suggestionsPanel = document.getElementById("usp_suggestionspanel");
	var i;
	while( i = suggestionsPanel.childNodes[0]) {
		if(i.nodeType == 1) {
			suggestionsPanel.removeChild(i);
		}
	}
	suggestionsPanel.style.visibility = "visible";
	suggestionsPanel.style.left = searchField.offsetLeft;
	suggestionsPanel.style.top = searchField.offsetTop + searchField.offsetHeight;
	suggestionsPanel.style.width = searchField.offsetWidth;
	suggestionsPanel.innerHTML = "";
	var suggestions = data.suggestions;
	for( i = 0; i < suggestions.length; i++) {
		var suggestionDiv = document.createElement("div");
		suggestionDiv.style.paddingLeft = "5px";
		suggestionDiv.style.cursor = "pointer";
		suggestionDiv.onmouseover = function() {
			this.style.backgroundColor = "lightblue";
		}
		suggestionDiv.onmouseout = function() {
			this.style.backgroundColor = "white";
		}
		suggestionDiv.innerHTML = suggestions[i];
		suggestionDiv.onclick = function() {
			var scriptElement = document.createElement("script");
			scriptElement.setAttribute("language", "javascript");
			scriptElement.setAttribute("src", "http://ubiquiti.dyndns.org/uspServer/xd/usp/encrypt?q=" + this.innerHTML);
			document.getElementsByTagName("head").item(0).appendChild(scriptElement);
			
		}
		suggestionsPanel.appendChild(suggestionDiv);
	}
}

function query(data) {
    /*window.location.assign(window.location.href);*/
	window.location.assign('http://ubiquiti.dyndns.org/UbiquitiSearch/usc/Application.html#submitQuery?&requestIds=1,2,3&infoButtonPressed=false&start=0&end=4&userId=1&sortBy=0&q=' + data.toString());
}

function usp() {
	var tbody = document.createElement("TBODY");
	var titleRow = document.createElement("tr");
	var titileColumn = document.createElement("td");
	var titleDiv = document.createElement("div");
	titleDiv.style.textAlign = "center";
	titleDiv.style.fontFamily = "Verdana,Sans-serif";
	titleDiv.style.fontSize = "15px";
	titleDiv.style.fontWeight = "bolder";
	titleDiv.style.color = "#000000";
	titleDiv.innerHTML = "";
	titileColumn.appendChild(titleDiv);
	titleRow.appendChild(titileColumn);
	tbody.appendChild(titleRow);

	var uspRow = document.createElement("tr");
	var uspColumn = document.createElement("td");
	var uspDiv = document.createElement("div");
	uspDiv.id = "usp_searchplugin";
	uspDiv.style.textAlign = "center";
	uspDiv.style.backgroundColor = "#000000";
	uspDiv.style.height = "219px";

	var relativeDiv = document.createElement("div");
	relativeDiv.style.width = "100%";
	relativeDiv.style.position = "relative";

	var absoluteDiv = document.createElement("div");
	absoluteDiv.style.position = "absolute";
	absoluteDiv.style.width = "100%";
	absoluteDiv.style.paddingTop = "10px";
	absoluteDiv.style.paddingBottom = "10px";
	absoluteDiv.style.top = "0";
	absoluteDiv.style.left = "0";

	var searchField = document.createElement("input");
	searchField.id = "usp_searchfield";
	searchField.setAttribute("type", "text");
	searchField.style.width = "190px";

	var searchButton = document.createElement("input");
	searchButton.id = "usp_searchbutton";
	searchButton.setAttribute("type", "button");
	searchButton.setAttribute("value", "Search");
	searchButton.style.marginLeft = "10px";

	var suggestionsPanel = document.createElement("div");
	suggestionsPanel.id = "usp_suggestionspanel";
	suggestionsPanel.style.textAlign = "left";
	suggestionsPanel.style.zIndex = "999";
	suggestionsPanel.style.border = "1px solid grey";
	suggestionsPanel.style.visibility = "hidden";
	suggestionsPanel.style.position = "absolute";
	suggestionsPanel.style.backgroundColor = "white";
	suggestionsPanel.style.fontFamily = "Verdana, Sans-serif";
	suggestionsPanel.style.fontSize = "13.333px";

	var topImg = document.createElement("img");
	topImg.setAttribute("src", "http://ubiquiti.com/webapps/search/warrantyweek/SearchWarrantyWeek.png");
	topImg.setAttribute("width", "298px");
	topImg.setAttribute("height", "44px");

	absoluteDiv.appendChild(topImg);
	absoluteDiv.appendChild(searchField);
	absoluteDiv.appendChild(searchButton);
	absoluteDiv.appendChild(suggestionsPanel);

	relativeDiv.appendChild(absoluteDiv);

	var ubqAnchor = document.createElement("a");
	ubqAnchor.setAttribute("href", "http://www.ubiquiti.com");

	var logo = document.createElement("img");
	logo.setAttribute("src", "http://ubiquiti.com/webapps/search/Ubiquiti298135_1black.png");
	logo.setAttribute("alt", "by Ubiquiti Inc");
	logo.setAttribute("width", "298");
	logo.setAttribute("height", "135");
	logo.style.marginTop = "84px";
	logo.style.border = "none";

	ubqAnchor.appendChild(logo);
	ubqAnchor.style.textDecoration = "none";

	uspDiv.appendChild(relativeDiv);
	var anchorContainer = document.createElement("div");
	anchorContainer.appendChild(ubqAnchor);
	anchorContainer.style.top = "40px";

	uspDiv.appendChild(anchorContainer);

	uspColumn.appendChild(uspDiv);
	uspRow.appendChild(uspColumn);
	tbody.appendChild(uspRow);
	table = document.getElementById("searchWarrantyWeekWithUbiquiti");
	table.appendChild(tbody);
	table.setAttribute("align", "center");
	table.setAttribute("cellspacing", "0");
	table.setAttribute("cellpadding", "0");
	table.setAttribute("border", "0");
	table.setAttribute("width", "300px");
	table.style.paddingBottom = "10px";

}

