/**
 * @author Marek BielaÅ?czuk
 */
autoc = function(dObj, sList, bDropDownList) {
var molAutoComplete = {
    objColor        : "#a3ceff",
    objColorRGB     : "rgb(163, 206, 255)",
    objColorInact   : "#ffffff",
    objColorInactRGB: "rgb(255, 255, 255)",
    dropdownlist    : document.createElement('div'), //(document.getElementById("dropdownmenu") || document.createElement('div') ),
	obj				: null,
	prefix          : dObj.getAttribute("id"),
	DropDownList    : false,
	list			: null,
	init: function(inputObj, ilist, bdropDownList) {
		this.obj = inputObj;
		this.list = ilist;
		this.DropDownList = bdropDownList;
		if (this.DropDownList)
			this.obj.setAttribute("readonly","true");
	    this.addEvent(this.obj, "click", this.dropAll);
	    this.addEvent(this.obj, "keyup", this.event_keyup);
	    this.addEvent(this.obj, "keydown", this.arrows);	
	    this.addEvent(this.obj, "keypress", this.key_handle);
		this.obj.setAttribute("autocomplete", "off");
	},
	key_handle: function(event) {
        if (/9|40|38/.test(event.keyCode)) {
		    return true;
        }
        else 
		if (event.keyCode == 13) {
			return false;
        }
        else {
            molAutoComplete.event_keyup(event);
		    return true;
        }
	},
    event_keyup: function(event){
        if (/9|40|38/.test(event.keyCode)) return true;
        if (((event.keyCode >= 48) && (event.keyCode <= 90)) || ((event.keyCode >= 96) && (event.keyCode <= 111)) || ((event.keyCode >= 186) && (event.keyCode <= 222)) || (/8|46/.test(event.keyCode))) {
            molAutoComplete.drop();
        }
        else 
            if (event.keyCode == 13) {
                molAutoComplete.obj.value = molAutoComplete.dropdownlist.childNodes[molAutoComplete.getSelection()].innerHTML.substr(0,30);;
				if (molAutoComplete.DropDownList) {
					document.getElementById(molAutoComplete.obj.getAttribute("id")+"_value").value = molAutoComplete.getSelection();
				}
                molAutoComplete.closeList();
				molAutoComplete.obj.focus();
            }
            else 
                if (((event.keyCode >= 112) && (event.keyCode <= 123)) || (/91|92|144|145|27/.test(event.keyCode))) {
                    molAutoComplete.closeList();
                }
		return true;
    },
	
    arrows: function(event){
		if (!document.getElementById(molAutoComplete.prefix+"molPane")&& ((event.keyCode==40)||(event.keyCode==38)) )	{ 
			molAutoComplete.drop();            
			return true;			
			}
		
        if (molAutoComplete.dropdownlist.innerHTML.length == 0) { 
            return true;
			}
        if (event.keyCode == 9) {
            molAutoComplete.closeList();
            return true;
        }
        if (event.keyCode == 40) {
            for (var i = 0; i < molAutoComplete.dropdownlist.childNodes.length; i++) {
                if ((molAutoComplete.dropdownlist.childNodes[i].style.backgroundColor == molAutoComplete.objColor) || (molAutoComplete.dropdownlist.childNodes[i].style.backgroundColor == molAutoComplete.objColorRGB)) {
                    if (i == (molAutoComplete.dropdownlist.childNodes.length - 1)) 
                        break;
                    molAutoComplete.dropdownlist.childNodes[i].style.backgroundColor = molAutoComplete.objColorInact;
                    molAutoComplete.dropdownlist.childNodes[i + 1].style.backgroundColor = molAutoComplete.objColor ;
                    if (i > 12) {
                        molAutoComplete.dropdownlist.scrollTop += 18;
                    }
                    break;
                }
            }
        }
        else 
            if (event.keyCode == 38) {
                for (var i = 0; i < molAutoComplete.dropdownlist.childNodes.length; i++) {
                    if ((molAutoComplete.dropdownlist.childNodes[i].style.backgroundColor == molAutoComplete.objColor ) || (molAutoComplete.dropdownlist.childNodes[i].style.backgroundColor == molAutoComplete.objColorRGB)) {
                        if (i == 0) 
                            break;
                        molAutoComplete.dropdownlist.childNodes[i].style.backgroundColor = molAutoComplete.objColorInact;
                        molAutoComplete.dropdownlist.childNodes[i - 1].style.backgroundColor = molAutoComplete.objColor;
                        if (molAutoComplete.dropdownlist.scrollTop > 0) {
                            if ((i * 18) <= molAutoComplete.dropdownlist.scrollTop) 
                                molAutoComplete.dropdownlist.scrollTop -= (molAutoComplete.dropdownlist.scrollTop > 18) ? 18 : molAutoComplete.dropdownlist.scrollTop;
								molAutoComplete.dropdownlist.focus();
                        }
                        break;
                    }
                }
            }
		return true;
    },
    dropAll: function(){
        molAutoComplete.showDropDown(false);
	},
    drop: function(){
        molAutoComplete.showDropDown(!this.DropDownList);
    },
    showDropDown: function(applyFilter) {
        pane = document.createElement('div');
		this.closeList();
        pane.setAttribute('id', this.prefix+"molPane");
        
        pane.style.position   = "absolute";
        pane.style.overflow   = "hidden";
        pane.style.border     = "1px solid #999";
        pane.style.background = this.objColorInact;
        
        pTop = 0;
        pLeft = 0;
        var padding;
		
        sObj = this.obj;
        
        while (sObj) {
            pLeft += sObj.offsetLeft;
            pTop += sObj.offsetTop;
			
			if (padding = this.getStyle(sObj, 'padding-top')) pTop += parseInt(padding);
			if (padding = this.getStyle(sObj, 'padding-left')) pLeft += parseInt(padding);
			
            sObj = sObj.offsetParent;
        }
        
        pane.style.left = pLeft + "px";
        pane.style.top = (pTop + this.obj.offsetHeight + 2) + "px";
        
        //////////////////////////////////////////////////////////////////////////
        
        if (!this.dropdownlist) {
			this.dropdownlist = document.createElement('div');
		}
        
        this.dropdownlist.setAttribute('id', this.prefix+"dropdownmenu");
        this.dropdownlist.style.lineHeight = "18px";
        this.dropdownlist.style.padding = "2px";
        this.dropdownlist.style.textAlign = "left";        
		this.dropdownlist.style.overflow = "auto"; //"scroll";
        this.dropdownlist.style.position = "absolute";
		
        if (this.getListLengthPx() > 268) {
            this.dropdownlist.style.height = "268px";
            this.dropdownlist.style.maxHeight = "268px";
        }
        else {
            this.dropdownlist.style.Height = (this.getListLengthPx()+18)+ "px";
        }
        
        if ((this.getListMaxWidth()+20) < this.obj.offsetWidth) {
            this.dropdownlist.style.width = (this.obj.offsetWidth-4) + "px";
        }
        else {
            this.dropdownlist.style.width = (this.getListMaxWidth()+20) + "px";
        }
        
        this.dropdownlist.style.top = "1";
        this.dropdownlist.style.left = "1";

		if (applyFilter)
	        this.prepareList(this.obj.value, this.getSelection());
		else
	        this.prepareList('', this.getSelection());

        pane.appendChild(this.dropdownlist);
        document.body.appendChild(pane);

		
        pane.style.width = (this.dropdownlist.offsetWidth + 2) + "px";
		
		
        if (this.getListLengthPx() > 268) {
            pane.style.height = "272px";
            pane.style.maxHeight = "272px";
			this.dropdownlist.style.overflow = "scroll";
        }
        else {
            //pane.style.height =  (this.getListLengthPx()+22) + "px";
			pane.style.height =  (this.getListLengthPx()+4) + "px";
			this.dropdownlist.style.overflow = "auto";
        }
		
        //pane.style.height = (this.dropdownlist.offsetHeight + 2) + "px";  //(this.dropdownlist.offsetHeight - (this.dropdownlist.offsetHeight - this.dropdownlist.scrollHeight)) + "px";
       return true;
    },
    prepareList: function(filter, selection){
    //
    // przygotowanie listy elemettÃ³w do wyÅ?wietlenia
    //
        while (this.dropdownlist.childNodes[0]) {
            this.dropdownlist.removeChild(this.dropdownlist.childNodes[0]);
        }
        var ItemsList = this.list.split("|");
        if (filter == '') {
            this.dropdownlist.appendChild(this.newItem(ItemsList[0], this.objColor));
            for (a = 1; a < ItemsList.length; a++) {
				if (ItemsList[a] != '')
					this.dropdownlist.appendChild(this.newItem(ItemsList[a], this.objColorInact));
            }
        }
        else {
            act = 0;
            for (a = 0; a < ItemsList.length; a++) {
                var re = new RegExp("^" + filter + "[\\w]*", "gi");
                var re_sp = new RegExp("^ " + filter + "[\\w]*", "gi");
                if (ItemsList[a].match(re) || ItemsList[a].match(re_sp)) {
                    if (act == 0) {
                        this.dropdownlist.appendChild(this.newItem(ItemsList[a], this.objColor));
                        act = 1;
                    }
                    else {
                        this.dropdownlist.appendChild(this.newItem(ItemsList[a], this.objColorInact));
                    }
                }
            }
        }
    },
    newItem: function(Caption, color){
        var aItem = document.createElement('a');
        aItem.style.background = color;
        aItem.style.display = "block";
        aItem.style.cursor = "pointer";				
        aItem.innerHTML = Caption;
        
        this.addEvent(aItem, "click", this.newItemOnClick);
        this.addEvent(aItem, "mouseover", this.newItemMouseOver);
        
        return aItem;
    },
    newItemOnClick: function(event) {
		event = event || window.event;
		var iobj = event.target || event.srcElement;
		
		if (molAutoComplete.DropDownList) {
			document.getElementById(molAutoComplete.obj.getAttribute("id")+"_value").value = molAutoComplete.getSelection();
		}
        molAutoComplete.obj.value = iobj.innerHTML.substr(0,30);
        molAutoComplete.closeList();
	},
	newItemMouseOver: function(event) {
		event = event || window.event;
		var iobj = event.target || event.srcElement;
        molAutoComplete.clearSelection();
        iobj.style.background = molAutoComplete.objColor;		
	},
    getListLength: function(){
		var Itcount = 0;
        IL = this.list.split("|");
        for (a = 0; a < IL.length; a++) {
			if (IL[a] != '')
				Itcount++;
        }
        return Itcount; //IL.length;
    },
    
    getListLengthPx: function(){
        return this.getListLength() * 18;
    },
    getListMaxWidth: function(){
        ruler = document.createElement('span');	
	    ruler.style.visibility = "hidden";
	    ruler.style.whiteSpace = "nowrap";
		document.body.appendChild(ruler);
        var wdt = 0;
        IL = this.list.split("|");
        for (i = 0; i < IL.length; i++) {
        	ruler.innerHTML = IL[i];
        	if (ruler.offsetWidth > wdt)
                wdt = ruler.offsetWidth;
        }
        return wdt;
		document.body.removeChild(ruler);
    },
    getSelection: function(){
        var i = 0;
        if (this.dropdownlist) {
            if (!this.dropdownlist.childNodes) 
                return -1;
            if (this.dropdownlist.childNodes.length == 0) 
                return -1;
            for (i = 0; i < this.dropdownlist.childNodes.length; i++) {
                if ((this.dropdownlist.childNodes[i].style.backgroundColor == this.objColor) || (this.dropdownlist.childNodes[i].style.backgroundColor == this.objColorRGB)) 
                    break;
            }
        }
        return i;
    },
    clearSelection: function (){
        if (this.dropdownlist) {
            if (!this.dropdownlist.childNodes) 
                return -1;
            if (this.dropdownlist.childNodes.length == 0) 
                return -1;
            for (i = 0; i < this.dropdownlist.childNodes.length; i++) {
                this.dropdownlist.childNodes[i].style.backgroundColor = this.objColorInact;
            }
        }
		return true;
    },
    closeList: function(sObj){
		if (sObj) 
			mp = sObj.getAttribute("id") + "molPane";
		else
			mp = this.obj.getAttribute("id") + "molPane";
			var dropdown = document.getElementById(mp);
			while (dropdown) {
				document.body.removeChild(dropdown);
				dropdown = document.getElementById(mp);
			}
    },
    addEvent: function (_obj, evType, fn){
        if (_obj.addEventListener) {
            _obj.addEventListener(evType, fn, false);
            return true;
        }
        else 
            if (_obj.attachEvent) {
                var r = _obj.attachEvent("on" + evType, fn);
                return r;
            }
            else {
                return false;
            }
    },
	isChildByName: function(id, child){
		var eobj = document.getElementById(id);
		if (eobj)
			return IsChild(eobj, child);
		else
			return false;
	},
	isChild : function (parent,child) {
		while(child) {
			if (child == parent) 
				return true;
			child=child.parentNode;
		}
		return false;
	},
	getStyle: function (obj, styleProp)
	{
		if (obj.currentStyle)
			return obj.currentStyle[styleProp];
		else if (window.getComputedStyle)
		return document.defaultView.getComputedStyle(obj,null).getPropertyValue(styleProp);
	}
};

	function blur(event){
		event = event || window.event;
		var aobj = event.target || event.srcElement;
		if (aobj != dObj) {
			if (aobj.getAttribute("id") != aobj.getAttribute("id")+"molPane") {
				if (aobj.getAttribute("id") != aobj.getAttribute("id")+"dropdownmenu") {  // && !molAutoComplete.isChildByName("molPane",aobj))
					molAutoComplete.closeList(dObj);
				}
			}
		}
	}

	molAutoComplete.init(dObj, sList, bDropDownList);
	molAutoComplete.addEvent(document, "click", blur);

}
