﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("Kredor.AutoSuggest");

Kredor.AutoSuggest.AutoSuggestBehavior = function(element) {
	Kredor.AutoSuggest.AutoSuggestBehavior.initializeBase(this, [element]);

	this._signatures;
	this._summaries;
	this._uniqueid;
}

Kredor.AutoSuggest.AutoSuggestBehavior.prototype = {
	get_signatures: function() {
		/// <value type="Boolean">
		/// Whether this behavior is available for the current element
		/// </value>

		return this._signatures;
	},
	set_signatures: function(value) {
		if (this._signatures != value) {
			this._signatures = value;
			this.raisePropertyChanged("signatures");
		}
	},
	get_summaries: function() {
		/// <value type="Boolean">
		/// Whether this behavior is available for the current element
		/// </value>

		return this._summaries;
	},
	set_summaries: function(value) {
		if (this._summaries != value) {
			this._summaries = value;
			this.raisePropertyChanged("summaries");
		}
	},

	initialize: function() {
		Kredor.AutoSuggest.AutoSuggestBehavior.callBaseMethod(this, 'initialize');
		// Add custom initialization here
		new AutoSuggest(this.get_element(), this._signatures.split(";"), this._summaries.split(";"));

	},
	dispose: function() {
		//Add custom dispose actions here
		Kredor.AutoSuggest.AutoSuggestBehavior.callBaseMethod(this, 'dispose');
	}
}
Kredor.AutoSuggest.AutoSuggestBehavior.registerClass('Kredor.AutoSuggest.AutoSuggestBehavior', Sys.UI.Behavior);

if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();

// Based on a script by Joe Kepley
function AutoSuggest(h, j, k) { var l = this; this.elem = h; this.suggestions = j; this.tooltips = k; this.eligible = new Array(); this.inputText = null; this.position = 0; this.highlighted = -1; this.popupSize = 30; this.div = document.createElement("div"); this.div.appendChild(document.createElement("ul")); this.div.style.position = 'absolute'; document.body.appendChild(this.div); this.tooltip = document.createElement("div"); document.body.appendChild(this.tooltip); this.elemblurred = false; this.ulblurred = false; var m = 9; var n = 32; var o = 27; var p = 38; var q = 40; var r = 16; var s = 13; var t = 187; var u = 107; var v = 188; var w = 190; var z = 57; var A = 48; var B = 8; h.setAttribute("autocomplete", "off"); if (!h.id) { var C = "autosuggest" + idCounter; idCounter++; h.id = C } h.onblur = function(a) { l.elemblurred = true; if (l.elemblurred && l.ulblurred) { l.hideDiv(); l.hideToolTip() } }; h.onkeydown = function(a) { var b = l.getKeyCode(a); switch (b) { case s: l.useSuggestion(); l.cancelEvent(a); break; case t: case u: case v: case w: case z: case A: case m: case n: l.useSuggestion(b, a); break; case o: l.hideDiv(); break; case p: if (l.highlighted > 0) { l.highlighted--; l.cancelEvent(a) } else if (l.highlighted == 0) { if (l.rangeStartIndex > 0) { l.rangeStartIndex--; l.buildAndShowDiv() } l.cancelEvent(a) } l.changeHighlight(b); l.cancelEvent(a); break; case q: if (l.highlighted < (l.eligible.length - 1)) { l.highlighted++; l.cancelEvent(a) } else { if (l.rangeStartIndex + l.popupSize < j.length) { l.rangeStartIndex++; l.buildAndShowDiv() } l.cancelEvent(a) } l.changeHighlight(b); l.cancelEvent(a); break } }; h.onkeyup = function(a) { var b = l.getKeyCode(a); switch (b) { case s: case m: case n: case t: case v: case z: case A: case o: case p: case q: case r: l.inputText = this.value; return; default: var c = l.inputText; if (c == null) c = ""; if (this.value.length > c.length) { var i = 0; while (i < this.value.length && this.value.charAt(i) == c.charAt(i)) { i++ } i++ } else { i = this.value.length } l.position = i; var d = l.getPrefix(this.value.substring(0, i)); if (this.value != l.inputText && d != null) { l.inputText = this.value; l.rangeStartIndex = l.getRangeStart(d); l.buildAndShowDiv(); l.showToolTip() } else { l.hideDiv() } if (d == null) l.inputText = this.value } }; this.re = /(^|^\(|\.| *\| *|^[^(]+ *\, *|\" *= *|\" *[<>]+ *|\b[0-9]+ *= *|\b[0-9]+ *[<>]+ *|\b(Or|And|Given|Between|And Not|Or Not|And Between)[ (]+)([a-z][a-z0-9]+ (?=m))?(\.|[a-z_][a-z0-9_]*)$/i; this.reLastWord = /(\.|[a-z_][a-z0-9_]*)$/i; this.getPrefix = function(a) { var b = l.re.exec(a); if (b == null) return null; return b[4] }; this.buildAndShowDiv = function() { var a = l.getRange(l.rangeStartIndex); if (a.length > 0) { l.eligible = a; l.createDiv(); l.positionDiv(); l.showDiv() } else { l.highlighted = -1; l.changeHighlight() } }; this.useSuggestion = function(a, b) { if (this.highlighted > -1) { var c = this.elem.value.substring(0, l.position); var d = this.elem.value.substring(l.position); c = c.replace(l.reLastWord, this.eligible[this.highlighted]); if (this.eligible[this.highlighted].indexOf("(") > 0 && this.eligible[this.highlighted].indexOf("(") < this.eligible[this.highlighted].length - 2) { switch (a) { case t: case u: this.elem.value = c + " =" + d; l.cancelEvent(b); break; case v: this.elem.value = c + " <" + d; l.cancelEvent(b); break; case w: this.elem.value = c + " >" + d; l.cancelEvent(b); break; case z: this.elem.value = c + d; l.cancelEvent(b); break; case A: this.elem.value = c + " )" + d; l.cancelEvent(b); break; case n: this.elem.value = c + " " + d; l.cancelEvent(b); break; default: this.elem.value = c + d } this.createSelection(this.elem, c.lastIndexOf("(") + 1, c.lastIndexOf(")")) } else { this.elem.value = c + d; this.createSelection(this.elem, c.length, c.length) } l.inputText = this.elem.value; this.hideDiv(); setTimeout("document.getElementById('" + this.elem.id + "').focus()", 0) } }; this.createSelection = function(a, b, c) { if (a.createTextRange) { var d = a.createTextRange(); d.collapse(true); d.moveStart('character', b); d.moveEnd('character', c - b); d.select() } else if (a.setSelectionRange) { a.setSelectionRange(b, c) } else if (a.selectionStart) { a.selectionStart = b; a.selectionEnd = c } a.focus() }; this.rangeStartIndex = 0; this.getRangeStart = function(a) { if (a == null) { return } var b = -1; for (i in this.suggestions) { if (this.suggestions[i].toLowerCase().indexOf(a.toLowerCase()) == "0") { if (b == -1) { b = i } } } var c = b - l.popupSize / 2 + 1; c = Math.max(0, c); c = Math.min(c, Math.max(0, j.length - l.popupSize)); l.highlighted = b - c; return c }; this.getRange = function(a) { var b = a + l.popupSize - 1; var c = new Array(); for (i in this.suggestions) { if (a <= i && i <= b) { var d = this.suggestions[i]; c[c.length] = d } } return c }; this.showDiv = function() { this.div.style.display = 'block'; this.elemblurred = false; this.ulblurred = false }; this.hideDiv = function() { this.div.style.display = 'none'; this.highlighted = -1; this.hideToolTip() }; this.changeHighlight = function() { var a = this.div.getElementsByTagName('LI'); for (i in a) { var b = a[i]; if (this.highlighted == i) { b.className = "selected" } else { b.className = "" } } this.showToolTip() }; this.positionDiv = function() { var a = this.elem; var x = 0; var y = a.offsetHeight; while (a.offsetParent && a.tagName.toUpperCase() != 'BODY') { x += a.offsetLeft; y += a.offsetTop; a = a.offsetParent } x += a.offsetLeft; y += a.offsetTop; this.div.style.left = x + 'px'; this.div.style.top = y + 'px' }; this.createDiv = function() { var e = document.createElement('ul'); for (i in this.eligible) { var f = this.eligible[i]; var g = document.createElement('li'); var a = document.createElement('a'); a.href = "javascript:false"; a.innerHTML = f.replace(/\(.+\)/, '(...)'); g.appendChild(a); if (l.highlighted == i) { g.className = "selected" } e.appendChild(g) } this.div.replaceChild(e, this.div.childNodes[0]); e.onmouseover = function(a) { var b = l.getEventSource(a); while (b.parentNode && b.tagName.toUpperCase() != 'LI') { b = b.parentNode } var c = l.div.getElementsByTagName('LI'); for (i in c) { var d = c[i]; if (d == b) { l.highlighted = i; break } } l.changeHighlight(); l.ulblurred = false }; e.onmouseout = function(a) { l.ulblurred = true; if (l.elemblurred && l.ulblurred) { l.hideDiv(); l.hideToolTip() } }; e.onclick = function(a) { l.useSuggestion(); l.hideDiv(); l.cancelEvent(a); return false }; this.div.className = "suggestion_list"; this.div.style.position = 'absolute' }; this.getKeyCode = function(a) { if (a) { return a.keyCode } if (window.event) { return window.event.keyCode } }; this.getEventSource = function(a) { if (a) { return a.target } if (window.event) { return window.event.srcElement } }; this.cancelEvent = function(a) { if (a) { a.preventDefault(); a.stopPropagation() } if (window.event) { window.event.returnValue = false } }; this.showToolTip = function() { if (l.highlighted < 0) return; this.tooltip.innerHTML = this.suggestions[l.highlighted] + "<br/>" + this.tooltips[l.highlighted]; this.tooltip.className = "tooltip"; this.tooltip.style.position = 'absolute'; this.positionToolTip(); this.tooltip.style.display = 'block' }; this.displayToolTip = function() { this.tooltip.style.display = 'block' }; this.positionToolTip = function() { var a = this.div; var x = a.offsetWidth; var y = 0 + (a.offsetHeight / l.eligible.length) * l.highlighted; while (a.offsetParent && a.tagName.toUpperCase() != 'BODY') { x += a.offsetLeft; y += a.offsetTop; a = a.offsetParent } x += a.offsetLeft; y += a.offsetTop; this.tooltip.style.left = x + 'px'; this.tooltip.style.top = y + 'px' }; this.hideToolTip = function() { this.tooltip.style.display = 'none' } } var idCounter = 0;
Type.registerNamespace('Kredor.AutoSuggest');Kredor.AutoSuggest.Resource={};
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();