/**
* webshop search class
*/
function clsWebshopSearch(p_oRef, p_sClass) {
	// set reference object
	this.m_oRef = (typeof(p_oRef) == 'object' ) ? p_oRef : null;
	this.m_sCSSClass 	= p_sClass;
	this.m_oResultDiv 	= null;
	this.m_oPrevConn 	= null;
	
	this.init = function() {
		// create result div
		if (this.m_oRef) {
			// Disable auto complete
			this.m_oRef.setAttribute("autocomplete", "off");
			
			// Create results div
			var oDiv = document.createElement('DIV');
			oDiv.id = this.m_oRef.id + '_results';
			oDiv.style.display = 'none';
			oDiv.className = this.m_sCSSClass;
			this.m_oResultDiv = this.m_oRef.parentNode.appendChild(oDiv);
			this.updateResultPos();
		}
	}
	
	this.updateResultPos = function() {
		var aPos = this.findPos(this.m_oRef);
		this.m_oResultDiv.style.left = (aPos[0] +  this.m_oRef.offsetWidth) + 'px';
		this.m_oResultDiv.style.top = aPos[1] + 'px';
	}
	
	this.search =  function(p_sValue, p_oEvent) {
		var iKeyCode = p_oEvent.keyCode;
		if (p_sValue.length > 2) {
			// show div
			if (this.m_oResultDiv.style.display == 'none') {
				this.updateResultPos();
				this.m_oResultDiv.parentNode.style.zIndex = 3000;
				this.m_oResultDiv.style.zIndex = 3001;
				this.m_oResultDiv.style.display = '';
			}
			if (iKeyCode != 13 ) {
			//&& iKeyCode != 8
				// check old connection buzzy
				if (this.m_oPrevConn != null && typeof(this.m_oPrevConn) == 'object') {
					if (this.m_oPrevConn.readyState != 4) {
						this.m_oPrevConn.abort();
					}
				}
			
				oRPC = new clsHTMLRPC('search');
				oRPC.setPlaceHolder(this.m_oResultDiv.id);
				this.m_oPrevConn = oRPC.query('search', [ 'q=' + this.encode(p_sValue) ], '', true);
				
			}
		} else {
			// hide
			this.m_oResultDiv.style.display = 'none';
		}
	}
	
	this.encode = function (str) {
		return escape(str).replace('+', '%2B').replace('%20', '+').replace('*', '%2A').replace('/', '%2F').replace('@', '%40');
	}
	
	this.findPos = function (obj) {
		var curleft = curtop = 0;
		var isIE = (navigator.appName=="Microsoft Internet Explorer");
		
			if (obj.offsetParent) {
				do {
								curleft += obj.offsetLeft;
								curtop += obj.offsetTop;
				} while (obj = obj.offsetParent);
				return [curleft,curtop];
			}
		
	}
	

	
	this.init();
	
}	
