var ssIsSearch = false;
var ssResultsHideTimeOut = null;

function ssHtmlEscape(s)
{
	if (s == null)
		return null;
	
	return s.replace("&", "&amp;")
	        .replace("<", "&lt;")
	        .replace(">", "&gt;")
	        .replace("\"", "&quot;");
}
	
function ssHighlightString(str, hstr)
{
	if (str == null || str.length == 0)
		return "";

	if (hstr == null || hstr.length == 0)
		return "";

	var parts = str.toUpperCase().split(hstr.toUpperCase());
			
	var htmlStr = "";

	var index = 0;			
	for (var p = 0; p < parts.length; p ++) {
		if (p > 0) {
			htmlStr += "<b>" + ssHtmlEscape(str.substring(index, index + hstr.length)) + "</b>";
			index += hstr.length;
		}
		var plen = parts[p].length;
		htmlStr += ssHtmlEscape(str.substring(index, index + plen));
		index += plen;
	}

	return htmlStr;
}
		
function ssGetElementX(element)
{
	var x = 0;
	var obj = element;
	
	do {		
		x += obj.offsetLeft;
		obj = obj.offsetParent;
	} while (obj);
		
	return x;
}
	
function ssGetElementY(element)
{
	var y = 0;
	var obj = element;

	do {		
		y += obj.offsetTop;
		obj = obj.offsetParent;
	} while (obj);
		
	return y;
}

function ssCreateTable(ssDropDown) {

	var tableEl = document.createElement("TABLE");
	tableEl.cellPadding = "0";
	tableEl.cellSpacing = "0";
	tableEl.style.width = "200px";
	tableEl.style.border = "0px";

	tableEl.ssDropDown = ssDropDown;

	tableEl.onmouseover = function(e) { tableEl.ssDropDown.onListMouseOver(e); tableEl.ssDropDown.PauseAutoHide(); }
	tableEl.onmouseout = function(e) { tableEl.ssDropDown.onListMouseOut(e); tableEl.ssDropDown.ResumeAutoHide(); tableEl.ssDropDown.ResetAutoHideTimer(); }

	return tableEl;
}
	
function ssUpdateDynamicSearchResults_callback(response)
{
	var dt = response.value;
	var showDD = false;
	
	var typeID = response.context.typeID;
	var searchString = response.context.searchString;
	var ssDropDown = response.context.ssDropDown;

	ssDropDown.ResetAutoHideTimer();
		
	var pageTypeID = ssDropDown.typeID;

	if (ssDropDown.typeElementID != undefined) {
		var typeIDElem = document.getElementById(ssDropDown.typeElementID);
		if (typeIDElem != null) {
			pageTypeID = parseInt(typeIDElem.value);
		}
	}
	
	var pageSearchString = document.getElementById(ssDropDown.searchStringID).value;
	
	if (typeID != pageTypeID ||
		searchString != pageSearchString)
			return;

	var divEl = document.createElement("DIV");

	var tableEl = ssCreateTable(ssDropDown);
	
	if (dt != null) {

		var tbodyEl = document.createElement("TBODY");
	
		for (var i = 0; i < dt.Rows.length; i ++) {
			var dr = dt.Rows[i];
			var id = dr["ID"];
			var name = dr["Name"];
			var ValueID = null;
			if (typeID==6) ValueID = dr["ValueID"];
			
			var css = "ssDropDownNormalItem";
			if ((i==0) && (ssDropDown.ssSearchID != null)) {
				document.getElementById(ssDropDown.ssSearchID).value = escape(id);
				ssDropDown.SearchStringValue = name;
				css = "ssDropDownSelectedItem";
			} else {
			}
			
			var trEl = document.createElement("TR");
			var tdEl = document.createElement("TD");
			tdEl.noWrap = true;
			var spanEl = document.createElement("SPAN");
			trEl.className = css;
			trEl.searchTypeID = typeID;
			trEl.searchID = id;
			trEl.searchName = name;
			trEl.ValueID = ValueID;
			trEl.onclick = function() { ssDropDownItem_OnClickWrapper(ssDropDown, this, this.searchTypeID , this.searchID, this.searchName, this.ValueID); }
			trEl.onmouseout = function() { ssDropDownItem_OnMouseOut(this); }
			trEl.onmouseover = function() { ssDropDownItem_OnMouseOver(this); }
			spanEl.innerHTML = ssHighlightString(name, searchString);
			tdEl.appendChild(spanEl);
			trEl.appendChild(tdEl);
			tbodyEl.appendChild(trEl);
		}

		tableEl.appendChild(tbodyEl);	
		
		if (dt.Rows.length > 0)
			showDD = true;
	}

	divEl.appendChild(tableEl);
	
	ssDropDownItem_ShowResult(divEl,showDD,ssDropDown);
}

function ssDropDownItem_ShowResult(tableEl,IsShow,ssDropDown) {

	document.getElementById(ssDropDown.dropDownID).innerHTML = "";
   	document.getElementById(ssDropDown.dropDownID).appendChild(tableEl);
	document.getElementById(ssDropDown.dropDownID).ssDropDown = ssDropDown;
	if (IsShow)
	  ssDropDown.Show();
	else	
          ssDropDown.Hide();

	return false;
}
		
function ssDropDownItem_OnMouseOver(e)
{
	e.className = "ssDropDownSelectedItem";
}

function ssDropDownItem_OnMouseOut(e)
{
	e.className = "ssDropDownNormalItem";
}

function ssDropDownItem_OnClickWrapper(ssDropDown, e, typeID, id, name, ValueID)
{
	name = unescape(name);

	if (ssDropDown.showNameOnSelect) {
		document.getElementById(ssDropDown.searchStringID).value = name;	
	}

	ssDropDown.Hide();
	
	if (ssDropDown.onItemSelected != null) {
	  if (ValueID != null)
	    ssDropDown.onItemSelected(typeID, id, name, ValueID);
	  else
		  ssDropDown.onItemSelected(typeID, id, name);
	}
}

/* Constructor */
function ssDropDown()
{
	this.showNameOnSelect = true;
	this.autoHide = false;
	this.autoHideTimer = null;
	this.pauseAutoHide = false;
	this.autoHideTime = 1000;
}

ssDropDown.prototype.onAutoHide = function() {

}

ssDropDown.prototype.AutoHide = function()
{
	if (this.pauseAutoHide)
		return;

	this.onAutoHide();

 	this.Hide();
}

ssDropDown.prototype.Hide = function()
{
	var dd = document.getElementById(this.dropDownID);
	dd.style.display = "none";

 	if (this.autoHideTimer != null) {
		this.autoHideTimer.cancel();	 	 	
		this.autoHideTimer = null;
 	}
}

ssDropDown.prototype.onResetAutoHideTimer = function() {

}

ssDropDown.prototype.onPauseAutoHide = function() {

}

ssDropDown.prototype.onResumeAutoHide = function() {

}

ssDropDown.prototype.PauseAutoHide = function() {
	this.pauseAutoHide = true;
	this.onPauseAutoHide();
}

ssDropDown.prototype.ResumeAutoHide = function() {
	this.pauseAutoHide = false;
	this.onResumeAutoHide();
}

ssDropDown.prototype.ResetAutoHideTimer = function() {

	if (this.pauseAutoHide)
		return;

	if (this.autoHide) {

		this.onResetAutoHideTimer();

	 	if (this.autoHideTimer != null) {
			this.autoHideTimer.cancel();	 	 	
	 	}

 		this.autoHideTimer = new CCallWrapper(this, this.autoHideTime, 'AutoHide');
 		CCallWrapper.asyncExecute(this.autoHideTimer);
	}
}

ssDropDown.prototype.Show = function()
{
	var ssStr = document.getElementById(this.searchStringID);
	var dd = document.getElementById(this.dropDownID);

	var dx = ssGetElementX(ssStr);
	
	dd.style.left  = dx + "px";
	dd.style.top   = (ssGetElementY(ssStr) + ssStr.offsetHeight) + "px";

	dd.style.width = "auto";
	dd.style.overflow = "visible";
	dd.style.visibility = "hidden";
	dd.style.display = "block";


	if (dd.offsetHeight > 350) {
		dd.style.overflow = "auto";
		dd.style.height = "300px";
	}

	if (dd.offsetWidth < ssStr.offsetWidth)
		dd.style.width = ssStr.offsetWidth + "px";
	
	/*
	alert("dd.clientWidth = " + dd.offsetWidth);
	alert("dd.offsetLeft = " + dd.offsetLeft);
	alert("document.body.clientWidth = " + document.body.clientWidth);
	*/

	if (dd.offsetLeft + dd.offsetWidth > document.body.clientWidth)
		dd.style.left = (document.body.clientWidth - dd.offsetWidth) + "px";

	dd.style.visibility = "visible";

	this.ResetAutoHideTimer();
}

ssDropDown.prototype.OnKeyDown = function(e) {

	var key;

	if (!e)
		e = window.event;

	if (e && e.which) {
	 	key = e.which;
	} else {
		key = event.keyCode;
	}

	if ((key >= 48 || key == 8) && !ssIsSearch) { 
		ssIsSearch = true;

		var tableEl = ssCreateTable(this);

		var tbodyEl = document.createElement("TBODY");
	
		var trEl = document.createElement("TR");
		trEl.className = "ssDropDownNormalItem";

		var tdEl = document.createElement("TD");
		tdEl.innerHTML = "&nbsp;Loading...";
		tdEl.noWrap = true;

        trEl.appendChild(tdEl);
        tbodyEl.appendChild(trEl);
        tableEl.appendChild(tbodyEl);

		ssDropDownItem_ShowResult(tableEl,true,this);
		this.timer = new CCallWrapper(this, 100, 'Update');
		CCallWrapper.asyncExecute(this.timer);
	}

	if (key == 13)
	 	this.onEnter();

	return false;
}

ssDropDown.prototype.onEnter = function() {
}

ssDropDown.prototype.Update = function()
{
	if (this.timer != null) {
		this.timer.cancel();
		this.timer = null;
	}
	ssIsSearch = false;	
	var typeID = this.typeID;

	if (this.typeElementID != undefined) {
		var typeIDElem = document.getElementById(this.typeElementID);
		if (typeIDElem != null) {
			typeID = parseInt(typeIDElem.value);
		}
	}
		
	var str = document.getElementById(this.searchStringID).value;
	
	this.UpdateDynamicSearchResults(typeID, str);	
}

ssDropDown.prototype.UpdateDynamicSearchResults = function(typeID, str)
{
	this.ResetAutoHideTimer();
	
	var context = {};
	context.typeID = typeID;
	context.searchString = str;
	context.ssDropDown = this;
	
	SSService.GetDynamicSearchResults(typeID, str, ssUpdateDynamicSearchResults_callback, context);
}

ssDropDown.prototype.onListMouseOver = function(e) {

}

ssDropDown.prototype.onListMouseOut = function(e) {

}

ssDropDown.prototype.OnMouseOver = function(e) {
	this.PauseAutoHide();
}

ssDropDown.prototype.OnMouseOut = function(e) {
	this.ResumeAutoHide();
	this.ResetAutoHideTimer();
}

ssDropDown.prototype.attach = function(inputEl) {
	var self = this;
	inputEl.onkeydown = function(e) { self.OnKeyDown(e); }
	inputEl.onmouseover = function(e) { self.OnMouseOver(e); }
	inputEl.onmouseout = function(e) { self.OnMouseOut(e); }
}
