var sokbox = document.getElementById("search");

function checkForm() {
	answer = true;
	if (siw && siw.selectingSomething) {
		answer = false;
	}
	var search = document.getElementById("search").value;
	if (search == '' || search == 'Super lätt sök!') {
		answer = false;
	}
	return answer;
}

/* start dhtml building blocks */
function freezeEvent(e) {
	if (e.preventDefault) e.preventDefault();
	e.returnValue = false;
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();
	return false;
}

function isWithinNode(e,i,c,t,obj) {
	answer = false;
	te = e;
	while(te && !answer) {
		if	((te.id && (te.id == i)) || (te.className && (te.className == i+"Class"))
			|| (!t && c && te.className && (te.className == c))
			|| (!t && c && te.className && (te.className.indexOf(c) != -1))
			|| (t && te.tagName && (te.tagName.toLowerCase() == t))
			|| (obj && (te == obj))
			) {
			answer = te;
		} else {
			te = te.parentNode;
		}
	}
	return te;
}

function getEvent(event) {
	return (event ? event : window.event);
}

function getEventElement(e) {
	return (e.srcElement ? e.srcElement: (e.target ? e.target : e.currentTarget));
}

/* end dhtml building blocks */

function handleKeyPress(event) {
	e = getEvent(event);
	eL = getEventElement(e);

	upEl = isWithinNode(eL,null,"wickEnabled",null,null);

	kc = e["keyCode"];

	if (siw && ((kc == 13) || (kc == 9))) {
		siw.selectingSomething = true;
		if (siw.isSafari) siw.inputBox.blur();   //hack to "wake up" safari
		siw.inputBox.focus();
		siw.inputBox.value = siw.inputBox.value.replace(/[ \r\n\t\f\s]+$/gi,' ');
		hideSmartInputFloater();
	} else if (upEl && (kc != 38) && (kc != 40) && (kc != 37) && (kc != 39) && (kc != 13) && (kc != 27)) {
		if (!siw || (siw && !siw.selectingSomething)) {
			processSmartInput(upEl);
		}
	} else if (siw && siw.inputBox) {
		//siw.inputBox.focus(); //kinda part of the hack.
	}
}//handleKeyPress()


function handleKeyDown(event) {
	e = getEvent(event);
	eL = getEventElement(e);

	if (siw && (kc = e["keyCode"])) {
		if (kc == 40) {
			siw.selectingSomething = true;
			freezeEvent(e);
			if (siw.isGecko) siw.inputBox.blur(); /* Gecko hack */
			selectNextSmartInputMatchItem();
		} else if (kc == 38) {
			siw.selectingSomething = true;
			freezeEvent(e);
			if (siw.isGecko) siw.inputBox.blur();
			selectPreviousSmartInputMatchItem();
		} else if (kc == 9) {
			siw.selectingSomething = true;
			activateCurrentSmartInputMatch();
			freezeEvent(e);
		} else if (kc == 13) {
			document.getElementById("searchForm").submit();
		} else if (kc == 27)  {
			hideSmartInputFloater();
			freezeEvent(e);
		} else {
			siw.selectingSomething = false;
		}
	}
}//handleKeyDown()

function handleFocus(event) {
	e = getEvent(event);
	eL = getEventElement(e);
	if (focEl = isWithinNode(eL,null,"wickEnabled",null,null)) {
		if (!siw || (siw && !siw.selectingSomething)) processSmartInput(focEl);
	}
}//handleFocus()

function handleBlur(event) {
	e = getEvent(event);
	eL = getEventElement(e);
	if (blurEl = isWithinNode(eL,null,"wickEnabled",null,null)) {
		if (siw && siw.selectingSomething) {
			//hideSmartInputFloater();
		}
	}
}//handleBlur()

function handleClick(event) {
	e = getEvent(event);
	eL = getEventElement(e);
	if (siw && siw.selectingSomething) {
	//	if (siw && (mEl = isWithinNode(eL,null,"matchedSmartInputItem",null,null))) {
	//		selectFromMouseOver(mEl);
	//	}
	//	selectFromMouseClick();
	//	document.getElementById("searchForm").submit();
	} else if (siw) {
		if (sokbox.value == 'Super lätt sök!') {
			sokbox.value='';
		}
	}
}//handleClick()

function resultatClick(id) {
	if (siw && siw.selectingSomething) {
		//id = id.replace("ord_", "")
		sokbox.value = siw.matchCollection[id].value.replace("<b>", "").replace("</b>", "");
		document.getElementById("searchForm").submit();
	}
}

function handleMouseOver(event) {
	e = getEvent(event);
	eL = getEventElement(e);
	if (siw && (mEl = isWithinNode(eL,null,"matchedSmartInputItem",null,null))) {
		siw.selectingSomething = true;
		//selectFromMouseOver(mEl);
	} else if (isWithinNode(eL,null,"siwCredit",null,null)) {
		siw.selectingSomething = true;
	}else if (siw) {
		siw.selectingSomething = false;
	}
}//handleMouseOver

function showSmartInputFloater() {
	document.getElementById("smartInputResults").style.display = "";
}

function hideSmartInputFloater() {
	document.getElementById("smartInputResults").style.display = "none";
}

function processSmartInput(inputBox) {
	if (!siw) siw = new smartInputWindow();
	siw.inputBox = inputBox;

	classData = inputBox.className.split(" ");
	siwDirectives = null;
	for (i=0;(!siwDirectives && classData[i]);i++) {
		if (classData[i].indexOf("wickEnabled") != -1) {
			siwDirectives = classData[i];
		}
	}

	setSmartInputData();
	if (siw.matchCollection && (siw.matchCollection.length > 0)) selectSmartInputMatchItem(0);
	content = getSmartInputBoxContent();
	if (content) {
		modifySmartInputBoxContent(content);
	}
}//processSmartInput()

function smartInputMatch(cleanValue, value) {
	this.cleanValue = cleanValue;
	this.value = value;
	this.isSelected = false;
}//smartInputMatch

function simplify(s) {
	return s.toLowerCase().replace(/^[ \s\f\t\n\r]+/,'').replace(/[ \s\f\t\n\r]+$/,'');
	//.replace(/[Ž,,,‘,\u00E9,\u00E8,\u00EA,\u00EB]/gi,"e").replace(/[ˆ,‰,\u00E0,\u00E2]/gi,"a").
}//simplify

function getUserInputToMatch(s) {
	a = s;
	fields = s.split(",");
	if (fields.length > 0) a = fields[fields.length - 1];
	return a;
}//getUserInputToMatch

function getUserInputBase() {
	s = siw.inputBox.value;
	a = s;
	if ((lastComma = s.lastIndexOf(",")) != -1) {
		a = a.replace(/^(.*\,[ \r\n\t\f\s]*).*$/i,'$1');
	} else {
		a = "";
	}
	return a;
}//getUserInputBase()

function runMatchingLogic(userInput, standalone) {
	userInput = simplify(userInput);
	uifc = userInput.charAt(0).toLowerCase();
	if (uifc == '"') uifc = (n = userInput.charAt(1)) ? n.toLowerCase() : "z";
	if (standalone) userInput = uifc;
	if (siw) siw.matchCollection = new Array();
	pointerToCollectionToUse = collection;
	if (siw && siw.revisedCollection && (siw.revisedCollection.length > 0) && siw.lastUserInput && (userInput.indexOf(siw.lastUserInput) == 0)) {
		pointerToCollectionToUse = siw.revisedCollection;
	} else if (collectionIndex[userInput] && (collectionIndex[userInput].length > 0)) {
		pointerToCollectionToUse = collectionIndex[userInput];
	} else if (collectionIndex[uifc] && (collectionIndex[uifc].length > 0)) {
		pointerToCollectionToUse = collectionIndex[uifc];
	} else if (siw && (userInput.length == 1) && (!collectionIndex[uifc])) {
		siw.buildIndex = true;
	} else if (siw) {
		siw.buildIndex = false;
	}
	
	tempCollection = new Array();

	re1m = new RegExp("^([ \"\>\<\-]*)("+userInput+")","i");
	re2m = new RegExp("([ \"\>\<\-]+)("+userInput+")","i");
	re1 = new RegExp("^([ \"\}\{\-]*)("+userInput+")","gi");
	re2 = new RegExp("([ \"\}\{\-]+)("+userInput+")","gi");
	
	for (i=0,j=0;(i<pointerToCollectionToUse.length);i++) {
		displayMatches = ((!standalone) && (j < siw.MAX_MATCHES));
		entry = pointerToCollectionToUse[i];
		mEntry = simplify(entry);
		if (!standalone && (mEntry.indexOf(userInput) == 0)) {
			userInput = userInput.replace(/\>/gi,'\\}').replace(/\< ?/gi,'\\{');
			re = new RegExp("(" + userInput + ")","i");
			if (displayMatches) {
				siw.matchCollection[j] = new smartInputMatch(entry, mEntry.replace(/\>/gi,'}').replace(/\< ?/gi,'{').replace(re,"<b>$1</b>"));
			}
			tempCollection[j] = entry;
			j++;		
		} else if (mEntry.match(re1m) || mEntry.match(re2m)) {
			if (!standalone && displayMatches) {
				siw.matchCollection[j] = new smartInputMatch(entry, mEntry.replace(/\>/gi,'}').replace(/\</gi,'{').replace(re1,"$1<b>$2</b>").replace(re2,"$1<b>$2</b>"));
			}
			tempCollection[j] = entry;
			j++;
		}
	}//loop thru collection
	if (siw) {
		siw.lastUserInput = userInput;
		siw.revisedCollection = tempCollection.join(",").split(",");
		collectionIndex[userInput] = tempCollection.join(",").split(",");
	}
	if (standalone || siw.buildIndex) {
		collectionIndex[uifc] = tempCollection.join(",").split(",");
		if (siw) siw.buildIndex = false;
	}
}//runMatchingLogic

function setSmartInputData() {
	if (siw) {
		orgUserInput = siw.inputBox.value;
		orgUserInput = getUserInputToMatch(orgUserInput);
		userInput = orgUserInput.toLowerCase().replace(/[\r\n\t\f\s]+/gi,' ').replace(/^ +/gi,'').replace(/ +$/gi,'').replace(/ +/gi,' ').replace(/\\/gi,'').replace(/\[/gi,'').replace(/\(/gi,'').replace(/\./gi,'\.').replace(/\?/gi,'');
		if (userInput && (userInput != "") && (userInput != '"')) {
			runMatchingLogic(userInput);
			showSmartInputFloater();
		}//if userinput not blank and is meaningful
		else {
			siw.matchCollection = null;
			hideSmartInputFloater();
		}
	}//siw exists ... uhmkaaayyyyy
}//setSmartInputData

function getSmartInputBoxContent() {
	a = null;
	if (siw && siw.matchCollection && (siw.matchCollection.length > 0)) {
		a = '<div style="background: url(pic/lampa.png) no-repeat left center; padding-left: 18px;"><b>Förslag:</b></div>';
		for (i = 0;i < siw.matchCollection.length; i++) {
			s1 = siw.matchCollection[i].value.replace(/\{ */gi,"&lt;").replace(/\} */gi,"&gt;");
			s2 = siw.matchCollection[i].value.replace("<b>", "").replace("</b>", "");
			a += '<div class="matchedSmartInputItem" id="ord_' + i + '" title="' + s2 + '" onclick="resultatClick(' + i + ');">' + s1 + '</div>';
		}//
	} else {
		hideSmartInputFloater();
	}//siw exists
	return a;
}

function modifySmartInputBoxContent(content) {
	document.getElementById("smartInputResults").innerHTML = content;
}

function selectFromMouseOver(o) {
	currentIndex = getCurrentlySelectedSmartInputItem();
	if (currentIndex != null) deSelectSmartInputMatchItem(currentIndex);
	newIndex = getIndexFromElement(o);
	selectSmartInputMatchItem(newIndex);
	modifySmartInputBoxContent(getSmartInputBoxContent());
}//selectFromMouseOver

function selectFromMouseClick() {
	//selectFromMouseOver(mEl);
	activateCurrentSmartInputMatch();
	siw.inputBox.focus();
}

function getIndexFromElement(o) {
	index = 0;
	while(o = o.previousSibling) {
		index++;
	}
	return index;
}

function getCurrentlySelectedSmartInputItem() {
	answer = null;
	for (i = 0; ((i < siw.matchCollection.length) && !answer) ; i++) {
		if (siw.matchCollection[i].isSelected)
			answer = i;
	}
	return answer;
}

function selectSmartInputMatchItem(index) {
	siw.matchCollection[index].isSelected = true;
}//selectSmartInputMatchItem()

function deSelectSmartInputMatchItem(index) {
	siw.matchCollection[index].isSelected = false;
}//deSelectSmartInputMatchItem()

function selectNextSmartInputMatchItem() {
	currentIndex = getCurrentlySelectedSmartInputItem();
	if (currentIndex != null) {
		deSelectSmartInputMatchItem(currentIndex);
		if ((currentIndex + 1) < siw.matchCollection.length)
			selectSmartInputMatchItem(currentIndex + 1);
		else
			selectSmartInputMatchItem(0);
	} else {
		selectSmartInputMatchItem(0);
	}
	modifySmartInputBoxContent(getSmartInputBoxContent());
}//selectNextSmartInputMatchItem

function selectPreviousSmartInputMatchItem() {
	currentIndex = getCurrentlySelectedSmartInputItem();
	if (currentIndex != null) {
		deSelectSmartInputMatchItem(currentIndex);
		if ((currentIndex - 1) >= 0)
			selectSmartInputMatchItem(currentIndex - 1);
		else
			selectSmartInputMatchItem(siw.matchCollection.length - 1);
	} else {
		selectSmartInputMatchItem(siw.matchCollection.length - 1);
	}
	modifySmartInputBoxContent(getSmartInputBoxContent());
}//selectPreviousSmartInputMatchItem

function activateCurrentSmartInputMatch() {
	baseValue = getUserInputBase();
	if ((selIndex = getCurrentlySelectedSmartInputItem() - 1) != null) {
		addedValue = siw.matchCollection[selIndex].cleanValue;
		theString = (baseValue ? baseValue : "") + addedValue;
		siw.inputBox.value = theString;
		runMatchingLogic(addedValue, true);
	}
}//activateCurrentSmartInputMatch

function smartInputWindow () {
	this.selectedSmartInputItem = null;
	this.MAX_MATCHES = 15;
	this.isGecko = (navigator.userAgent.indexOf("Gecko/200") != -1);
	this.isSafari = (navigator.userAgent.indexOf("Safari") != -1);
	this.isWinIE = ((navigator.userAgent.indexOf("Win") != -1 ) && (navigator.userAgent.indexOf("MSIE") != -1 ));
}//smartInputWindow Object

function registerSmartInputListeners() {
	sokbox.setAttribute("autocomplete","OFF");
	sokbox.onfocus = handleFocus;
	sokbox.onblur = handleBlur;
	sokbox.onkeydown = handleKeyDown;
	sokbox.onkeyup = handleKeyPress;
}

siw = null;

if (document.addEventListener) {
	document.addEventListener("keydown", handleKeyDown, false);
	document.addEventListener("keyup", handleKeyPress, false);
	document.addEventListener("mouseup", handleClick, false);
	document.addEventListener("mouseover", handleMouseOver, false);
} else {
	document.onkeydown = handleKeyDown;
	document.onkeyup = handleKeyPress;
	document.onmouseup = handleClick;
	document.onmouseover = handleMouseOver;
}

registerSmartInputListeners();

//note: instruct users to the fact that no commas should be present in entries.
//it would make things insanely messy.
//this is why i'm filtering commas here:
for (x=0;x<collection.length;x++) {
	collection[x] = collection[x].replace(/\,/gi,'');
}//

collectionIndex = new Array();

ds = "";
function debug(s) {
	ds += ( s + "\n");
}

