﻿
var SEARCHHANDLER = {};

SEARCHHANDLER.Init = function() {
	SEARCHHANDLER.ClassNameForHightlightedListItems = 'highlight';
	SEARCHHANDLER.CurrentActiveSearchResult = 1;
	SEARCHHANDLER.Index = new Array();
	SEARCHHANDLER.Results = new Array();
	SEARCHHANDLER.LastSearchTerm = "";
	SEARCHHANDLER.Scope = 1;
	SEARCHHANDLER.SearchBox = document.getElementById('searchbox');
	EVENT.Add(SEARCHHANDLER.SearchBox, 'keyup', SEARCHHANDLER.SearchForm);
	EVENT.Add(SEARCHHANDLER.SearchBox, 'focus', SEARCHHANDLER.SearchFocus);
	EVENT.Add(SEARCHHANDLER.SearchBox, 'blur', SEARCHHANDLER.SearchBlur);
	SEARCHHANDLER.InitIndex();
	SEARCHHANDLER.ResultPanel = document.createElement('div');
	SEARCHHANDLER.ResultPanel.id = 'resultPanel';
	SEARCHHANDLER.ResultPanel.className = 'popup';
	document.body.appendChild(SEARCHHANDLER.ResultPanel);
	
	// highlight search results
	var pos = location.href.indexOf('?s=')
	if (pos > -1) {
		var pos2 = location.href.indexOf('#')
		if (pos2 > -1)
			searchword = location.href.substring(pos + 3, pos2);
		else
			searchword = location.href.substring(pos + 3);

		if (searchword.length > 1) {
			searchword = unescape(searchword).replace(/ +/g, '(\\s+|\\s*<[^>]*>\\s*)');
			var re = new RegExp(">([^<]*)(" + searchword + ")", "igm");
			var content = document.getElementById('maincontent');
			content.innerHTML = content.innerHTML.replace(re, ">$1<span class=\"searchhit\">$2</span>");
		}
	}
};
SEARCHHANDLER.InitIndex = function() {
	for (var i = 0; i < sData.length; i++) {
		SEARCHHANDLER.Index[i] = new SEARCHINDEXITEM(sData[i]);
	}
};
SEARCHHANDLER.SearchFocus = function() {
	if (SEARCHHANDLER.SearchBox.value == TRANSLATE.GetValue('BtnSearch'))
		SEARCHHANDLER.SearchBox.value = '';
};
SEARCHHANDLER.SearchBlur = function() {
	if (SEARCHHANDLER.SearchBox.value == '')
		SEARCHHANDLER.SearchBox.value = TRANSLATE.GetValue('BtnSearch');
};

SEARCHHANDLER.SearchForm = function(e) {
	SEARCHHANDLER.HandleEvent(e);
	SEARCHHANDLER.LastSearchTerm = unescape(SEARCHHANDLER.SearchBox.value);
	if (SEARCHHANDLER.LastSearchTerm.length < 2) {
		SEARCHHANDLER.ResultPanel.style.display = 'none';
		return;
	}
	SEARCHHANDLER.SearchWords();
	var hits = SEARCHHANDLER.Results.length;
	SEARCHHANDLER.PrintResult(hits);
	if (typeof sC == "object" && typeof sC.hhLogSearch == "function") {
		sC.hhLogSearch(SEARCHHANDLER.LastSearchTerm, hits);
	}
	return false;
};
SEARCHHANDLER.SearchWords = function() {
	SEARCHHANDLER.Results = new Array();
	for (var i = 0; i < SEARCHHANDLER.Index.length; i++) {
		var CurrentIndex = SEARCHHANDLER.Index[i];
		CurrentIndex.Score = 0;
		var findTerm = new RegExp(SEARCHHANDLER.LastSearchTerm, "i");
		var headingFound = CurrentIndex.Title.search(findTerm); //search heading
		if (headingFound >= 0) {
			CurrentIndex.Score += 10; //if term found in page title, add 10 to score
		}
		var rn = CurrentIndex.Text.match(findTerm);
		if (rn && rn.length > 0) {
			CurrentIndex.Score += rn.length; //add numner of matches to score.
		}
		if (CurrentIndex.Score > 0 && SEARCHHANDLER.IsInScope(CurrentIndex, SEARCHHANDLER.Scope)) {
			SEARCHHANDLER.Results[SEARCHHANDLER.Results.length] = CurrentIndex;
		}
	}
	SEARCHHANDLER.SortSearchResult();
};
SEARCHHANDLER.HandleEvent = function(e) {
	EVENT.Abort(e);
};
SEARCHHANDLER.IsInScope = function(row, type) {
	//## TYPE: 1=ALL, 2=SECTION, 3=PAGE
	if (type == 1) return true; //if 1, always return true(filter off)
	var section = "";
	var page = "";
	var url = window.location.toString();
	var aUrl = url.split("/");
	section = aUrl[aUrl.length - 2];
	if (type == 3) {
		page = aUrl[aUrl.length - 1];
		page = page.substring(0, page.indexOf("."));
		return row.BreadCrumb == (section + "/" + page);
	}
	if (row.BreadCrumb.indexOf(section) > 0) {
		return true;
	} else {
		if (page == "" || page == aRow[5]) return true;
		else {
			return false;
		}
	}
};
SEARCHHANDLER.PrintResult = function(hits) {
	POPUP.Close();
	SEARCHHANDLER.ResultPanel.style.display = 'block';
	SEARCHHANDLER.ResultPanel.innerHTML = '<div class="pop-corner-tr"></div><div class="pop-corner-br"></div><div class="pop-corner-bl"></div><div class="pop-corner-tl"></div><div class="pop-top"></div><div class="pop-main"></div><div class="pop-bottom"></div><div class="pop-right"></div><div class="pop-left"></div><div class="pop-close" onclick="SEARCHHANDLER.Clear();"><span>' + TRANSLATE.GetValue('Close') + '</span></div><h2 id="pop-topic"></h2><div id="pop-content"></div>';
	var popupHeader = SEARCHHANDLER.ResultPanel.childNodes[10];
	var popupContent = SEARCHHANDLER.ResultPanel.childNodes[11];

	if (hits == 0) {
		popupContent.innerHTML = '<strong>' + TRANSLATE.GetValue('SearchEmpty') + '</strong>';
	} else {
		var strOutput = TRANSLATE.GetValue('Searchresult');
		strOutput = strOutput.replace('{SEARCHWORD}', SEARCHHANDLER.LastSearchTerm);
		strOutput = strOutput.replace('{RESULTS} on', '');
		strOutput = strOutput.replace('{RESULTS} på', '');
		//strOutput = strOutput.replace('{RESULTS}', '');
		strOutput = strOutput.replace('{PAGES}', hits);
		popupHeader.innerHTML = strOutput;
		strOutput = '';
		for (var i = 0, n = hits; i < n; i++) {
			strOutput += '<li><a href="' + SEARCHHANDLER.Results[i].Url + '?s=' + escape(SEARCHHANDLER.LastSearchTerm) + '">' + SEARCHHANDLER.Results[i].Title + '</a></li>';
		}
		popupContent.innerHTML += '<ul>' + strOutput + '</ul>';
		if (COLUMNLAYOUT.scrollPanel && (popupContent.offsetHeight > COLUMNLAYOUT.scrollPanel.offsetHeight)) {
			popupContent.style.height = (COLUMNLAYOUT.scrollPanel.offsetHeight - popupContent.offsetTop - SEARCHHANDLER.ResultPanel.childNodes[0].offsetHeight - SEARCHHANDLER.ResultPanel.childNodes[1].offsetHeight) + 'px';
		}
		else if (TABLELAYOUT.Table && (popupContent.offsetHeight > document.documentElement.clientHeight)) {
			popupContent.style.height = (document.documentElement.clientHeight - SEARCHHANDLER.ResultPanel.offsetTop - popupContent.offsetTop - SEARCHHANDLER.ResultPanel.childNodes[0].offsetHeight - SEARCHHANDLER.ResultPanel.childNodes[1].offsetHeight) + 'px';
		}
	}

	popupHeight = popupContent.offsetHeight + popupContent.offsetTop;
	SEARCHHANDLER.ResultPanel.childNodes[5].style.height = popupHeight + 'px'; // Main
	SEARCHHANDLER.ResultPanel.childNodes[7].style.height = popupHeight + 'px'; // Right
	SEARCHHANDLER.ResultPanel.childNodes[8].style.height = popupHeight + 'px'; // Left
	//popupHeight -= (popupContent.offsetTop - SEARCHHANDLER.ResultPanel.childNodes[0].offsetHeight);
	//popupContent.style.height = popupHeight + 'px';

	popupHeight += SEARCHHANDLER.ResultPanel.childNodes[0].offsetHeight + SEARCHHANDLER.ResultPanel.childNodes[1].offsetHeight;
	SEARCHHANDLER.ResultPanel.style.height = popupHeight + 'px';
	return false;
};

SEARCHHANDLER.SortSearchResult = function() {
	var newResultArray = new Array();
	for (var pageIterator = 0, max = SEARCHHANDLER.Results.length; pageIterator < max; pageIterator++) {
		var maxScore = 0;
		var scorePos = 0;
		for (var scoreIterator = 0, n = SEARCHHANDLER.Results.length; scoreIterator < n; scoreIterator++) {
			if (SEARCHHANDLER.Results[scoreIterator].Score > maxScore) {
				maxScore = SEARCHHANDLER.Results[scoreIterator].Score;
				scorePos = scoreIterator;
			}
		}
		newResultArray[pageIterator] = SEARCHHANDLER.Results[scorePos];
		SEARCHHANDLER.Results[scorePos] = -1;
	}
	SEARCHHANDLER.Results = newResultArray;
};
SEARCHHANDLER.GetSearchPattern = function() {
	if (!this.SearchingQuote) {
		var criteria = SEARCHHANDLER.LastSearchTerm.split(" ")[0];
		return new RegExp(criteria, "i");
	} else {
		var l = SEARCHHANDLER.LastSearchTerm.replace(/"/gi, "");
		return new RegExp(l, "i");
	}
};
SEARCHINDEXITEM = function(data) {
	//title, url, breadcrumb, textmassa
	var splitRow = data.split("^");
	this.Title = splitRow[0];
	if (document.body.id == 'startpage')
		this.Url = splitRow[1].substring(3);
	else
		this.Url = splitRow[1];
	this.BreadCrumb = splitRow[2];
	this.Text = splitRow[3];
	this.Score = 0;
};
SEARCHTERM = function(data) {
	this.Exclude = false;
	if (data.charAt(0) == '-') this.Exclude = true;
	this.Text = data.replace(/^\-|^\+/gi, ""); //remove starting -+
};
SEARCHHANDLER.InputOnFocus = function(e) {
	if (this.value == 'Sök' || this.value == 'Search' || this.value == 'Cerca') {
		this.value = '';
	}
};
SEARCHHANDLER.InputOnBlur = function(e) {
	if (this.value == '') {
		switch (TRANSLATE.Lang) {
			case 'sv': this.value = 'Sök'; break;
			case 'en': this.value = 'Search'; break;
			case 'it': this.value = 'Cerca'; break;
			default: this.value = 'Search'; break;
		}
	}
};
SEARCHHANDLER.Clear = function() {
	SEARCHHANDLER.SearchBox.value = '';
	SEARCHHANDLER.SearchBlur();
	SEARCHHANDLER.ResultPanel.style.display = 'none';
};
