// 2009.03.06
// +IE6 +Opera(9) +FF(2)

/* ### for Opera Event obj.onBlur ### */
//
// send to YOU script: $param[value] $param[limit]
//
var hBlur= new CBlur();
function CBlur(){
	function addEvent(obj, evt, func) {
		if (obj.addEventListener){obj.addEventListener(evt, func, false);}
		else if (obj.attachEvent){obj.attachEvent('on' + evt, func);}
	}//#
	this.isBlur= 1;
	this.Param;
	this.Func;
	this.onBlur= function(){try{hBlur.Func(hBlur.Param);}catch(err){}};
	addEvent(document,'mousedown',this.onBlur);
}//

var  hACInput= Array();

function CACInput(id,options){
	this.timeout= 10; // sec.
	var inteval= 100; // delay in ms
	var timeout_steps= this.timeout * (1000/inteval); // x.
	var tTimeOut= Array();
	var tReady= Array();
	this.onReady= function(objId,layout,func){
		var self= this;
		var obj= document.getElementById(objId);
		if( (layout && obj && obj.offsetWidth) || (!layout && obj) ){
			clearTimeout(tReady[objId]);
			func(this);
			return;
		}else if(!layout){
			if(!tTimeOut[objId]){tTimeOut[objId]= 0;}
			if(tTimeOut[objId] > timeout_steps && self.timeout){
				clearTimeout(tReady[objId]);
				tReady[objId]= null;
				alert('SYSERROR: Timeout > '+self.timeout+' sec. in hACInput['+self.id+'].\nObject '+objId+' not found!');
				return;
			}
			tTimeOut[objId]++;
		}
		tReady[objId]= setTimeout(function(){self.onReady(objId,layout,func);},inteval);
	}//
	
	if(!hACInput[id]){
		hACInput[id]= this;
	}else{
		alert('SYSERROR: AutoCompleteInput with ID "'+id+'" already exist!');
		return false;
	}
	
	// Define Property
	this.id= id;
	this.data= Array();
	this.data= options.data;
	this.selectedIndex= -1;
	this.countItems= 0;
	this.options= options;
	
	this.input= '';
	this.objInput= null; // Input
	this.objValue= null; // Value die per FORM gesendet wird 
	this.isUpDown= false;
	
	this.objContainer;	// Dropdown list
	this.isContainerOpen= 0; // Dropdown list is hidden/visible
	
	this.width= options.width;
	this.height= options.height;
		
	this.maxResults= options.maxResults;
	if(this.maxResults < 3){
		this.maxResults= 3;
	}
	
	// Define Methods 
	this.Create= fACInput_Create;
	this.Init= fACInput_Init;
	
	this.setInput= fACInput_setInput;
	this.onBlur= fACInput_onBlur;
	this.Close= fACInput_Close;
	this.isClick= 0; // for onBlur
	this.isFocused= 0; //

	this.onMouseOver= fACInput_MouseOver;
	this.onMouseOut= fACInput_MouseOut;
	this.onMouseDown= fACInput_MouseDown;
	this.onMouseUp= fACInput_MouseUp;
	this.onKeypress= fACInput_onKeypress;
	this.onKeyUpDown= fACInput_onKeyUpDown;
	this.scroll= fACInput_scroll;
	this.autoComplete= fACInput_autoComplete;
	this.getData= fACInput_getData;
	this.callback_getData= fACInput_callback_getData;
	
	//
	this.setValue= fACInput_setValue;
	this.Clear= fACInput_Clear; // leernt alle input
	
	//External functions
	this.ext_onSetInput= options.onSetInput;
	this.ext_onChangeInput= options.onChangeInput;
	
	this.onClick= options.onClick;
	this.onEnter= options.onEnter;
	
	// Create
	//this.Create();
	this.onReady(this.id,false,this.Create);
}//

var fACInput_Create= function(parent){
	var self= this;
	if(parent){self= parent;}
	
	var tmpl= '';
	tmpl= 
		'<iframe id="iframe_'+self.id+'" style="position:absolute;top:0px;width:331px;height:100px;visibility:hidden;background-color:#fff;margin:0px;"></iframe>'+
		'<input id="value_'+self.id+'" type="hidden" name="'+self.id+'" value="">'+
		
		'<table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse">'+
		'<tr><td style="padding:0;border:0;">'+
		'<input id="input_'+self.id+'" type="text" style="_margin-bottom:-1px;padding-left:0;padding-right:0;">'+
		'</td></tr>'+
		'<tr><td style="padding:0;border:0;">'+
		'<div style="position:absolute;">'+
			'<iframe id="IE6bugfix1_'+self.id+'" style="display:none;position:absolute;z-index:-1;_filter:mask();border:0;"><!-- IE6 BugFix --></iframe>'+
			'<div id="container_'+self.id+'" class="aciCombobox" style="position:absolute;display:none;visibility:hidden;overflow:auto;">'+
			'<div id="content_'+self.id+'" style="overflow:hidden;"><!-- AutocompleteList !--></div>'+
			'</div>'+
		'</div>'+
		'</td></tr>'+
		'</table>';
	
	var obj= document.getElementById(self.id);
	obj.id= 'div_'+self.id;
	obj.innerHTML= tmpl;
	
	self.objValue= document.getElementById('value_'+self.id);
	var objInput= document.getElementById('input_'+self.id);
	objInput.parent= self;
	objInput.style.width= self.width + 'px';
	objInput.onkeydown= fACInput_onKeypress;
	objInput.onkeyup= fACInput_onKeypress;
	objInput.onfocus= function(){this.isFocused=1;};
	objInput.onblur= fACInput_onBlur;
	objInput.onclick= function(){try{this.parent.onClick();}catch(err){}}
	self.objInput= objInput;
	
	self.objContainer= document.getElementById('container_'+self.id);
	self.objContainer.parent= self;
	if(isIE){self.objContainer.style.height= self.height + 2;}
	self.objContainer.onmousedown= function(){hBlur.isBlur=0;this.parent.isClick=1;if(isOpera){this.parent.objInput.focus();}} // fix for IE (MUST DEI!!!)

	var offsetWidth= (parseInt(objInput.offsetWidth) - parseInt(objInput.clientWidth))/2 - 1;
	self.objContainer.style.width= (self.width + offsetWidth) + 'px';
	self.objContainer.style.height= self.height + 'px';
	
}//

var fACInput_Init= function(){
	var i= 0;
	var row;
	var tmpl= '';
	while(row= this.data[i]){
		var item; 
		if(item= row.tmpl){}
		else{item= row.item;}
		tmpl+=
			'<div id="'+this.id+'_item_'+i+'" '+
				'onmouseover="hACInput[\''+this.id+'\'].onMouseOver('+i+');" '+
				'onmouseout="hACInput[\''+this.id+'\'].onMouseOut('+i+');" '+
				'onmousedown="hACInput[\''+this.id+'\'].onMouseDown();" '+
				'onmouseup="hACInput[\''+this.id+'\'].onMouseUp('+i+');" '+
			'><nobr>'+item+'</nobr></div>'+"\n"+
			'';
		i++;
	}//
	this.countItems= i;
	this.selectedIndex= -1;
	
	this.objContainer.style.display= 'block';
	var objContent= document.getElementById('content_'+this.id);
	objContent.innerHTML= tmpl;

	var h= parseInt(objContent.scrollHeight); //alert(h);
	if(h > this.height){
		h= this.height;
	}else if(!h){h= 5;}
	
	//if(isIE){h+= 2;}// fix for IE (MUST DEI!!!)
	this.objContainer.style.height= h+'px';
	
	var w= parseInt(this.objContainer.clientWidth);
	objContent.style.width= w + 'px';

	this.objContainer.scrollTop= 0;
	this.objContainer.style.visibility= 'visible';
	
	var IE6bugfix1= document.getElementById('IE6bugfix1_'+this.id);
	IE6bugfix1.style.height= parseInt(this.objContainer.offsetHeight) + 'px';
	IE6bugfix1.style.width= parseInt(this.objContainer.offsetWidth) + 'px';
	IE6bugfix1.style.display= 'block';
	
	this.isContainerOpen++;
	return true;
}//

var fACInput_autoComplete= function(input){
	this.objValue.value= input;
	if(input == this.input){
		return false;
	}
	this.input= input;
	
	if(this.ext_onChangeInput){
		try{
			this.ext_onChangeInput();
		}catch(err){}
	}
	this.getData();
	//this.Init();
}//

var fACInput_setValue= function(value){
	this.objValue.value= value;
}

var fACInput_Clear= function(){
	this.objValue.value= '';
	this.objInput.value= '';
}

var fACInput_getData= function(){
	var parent_id= this.id;	
	var d= new Date();
	var url= this.options.callback+'&search='+this.input+'&limit='+this.maxResults+'&callback=hACInput[\''+this.id+'\'].callback_getData&'+d.getTime();
	document.getElementById('iframe_'+this.id).src= url;
}//

var fACInput_callback_getData= function(data){
	if(!data){data= {};}
	this.data= data;
	this.Init();
}//

var fACInput_setInput= function(index){
	if(index < 0){return false;}
	var value= this.data[index].value;
	if(index < 0){
		value= '';
		return false;
	}
	this.objValue.value= value;
	this.objInput.value= this.data[index].item;
	this.Close();
	
	if(this.ext_onSetInput){
		try{
			this.ext_onSetInput();
		}catch(err){}
	}
	return true;
}//

var fACInput_Close= function(parent){
	var self= this;
	if(parent){self= parent;}
	if(!self.isContainerOpen){return false;}
	self.isContainerOpen= 0;
	self.objContainer.style.visibility= 'hidden';
	self.objContainer.style.display= 'none';
	document.getElementById('IE6bugfix1_'+self.id).style.display= 'none';
}//

//### EVENT FUNCTIONS ###//
var fACInput_setCursor= function(index){
	this.selectedIndex= index;
	var objRow= document.getElementById(this.id+'_item_'+index);
	objRow.className= 'aciCursorSelected';
}//

var fACInput_onBlur= function(){
	this.isFocused= 0;
	if(isOpera){
		hBlur.isBlur= 1;
		hBlur.Param= this.parent.id;
		hBlur.Func= fACInput_Blur;
		return false;
	}

	if(!this.parent.isClick){
		this.parent.Close(this.parent);
	}else{
		this.parent.isClick= 0;
		this.focus();
	}
	return true;
}//

var fACInput_Blur= function(id){
	if(hBlur.isBlur){
		hACInput[id].Close(hACInput[id]);
	}
}//

var fACInput_MouseDown= function(){
	this.isClick++;
}

var fACInput_MouseUp= function(index){
	this.isClick= 0;
	this.setInput(index);
	this.objInput.focus();
	setCaretPosition(this.objInput,this.objInput.value.length); // fix for IE (MUST DEI!!!)
	return true;
}

var fACInput_MouseOver= function(index){
	if(this.selectedIndex >= 0){
		document.getElementById(this.id+'_item_'+this.selectedIndex).className= 'aciCursorDefault';
	}
	document.getElementById(this.id+'_item_'+index).className= 'aciCursorSelected';
	this.selectedIndex= index;
}//

var fACInput_MouseOut= function(index){
	return false;
}//
	
var fACInput_onKeypress= function(evt){
	if(!evt){evt= window.event;}
	var event_name= evt.type.toLowerCase();
	
	var self= this.parent;
	var input= this.value;
	var keyCode= (evt.which) ? evt.which : evt.keyCode;
	
	if( event_name == 'blur' ){}
	
	if( event_name == 'keydown' || event_name == 'keypress'){
		if(keyCode == 38 || keyCode == 40){ // up / down
			self.onKeyUpDown(keyCode);
			self.isUpDown= true;
			return false;
		}else
		if(keyCode == 27){ // Esc 
			self.Close(self);
			this.focus();
			return false; // fix for IE (MUST DEI!!!)
		}else
		if(keyCode == 9){ // Tab
			self.Close(self);
			return true;
		}if(keyCode == 13){ // Enter
			if(self.objContainer.style.display != 'none' && self.selectedIndex > -1){
				self.setInput(self.selectedIndex);
			}else{
				try{self.onEnter();}catch(err){}
				self.input= '';
				self.Close(self);
			}
			return false;
		}else{
			self.isUpDown= false;
			return true;
		}
	}else
	if( event_name == 'keyup' && self.isUpDown){
		return false;
	}
	
	if(keyCode == 32){ // Space
		self.input= input;
	}else
	if(keyCode == 27){ // Esc
		return false;
	}else
	if(keyCode == 13){ // Enter
		return false;
	}
		
	else{
		self.autoComplete(input);
	}
	
	return true;
}//

var fACInput_onKeyUpDown= function(key){
	if(!this.isContainerOpen || this.data.length < 1){return false;}
	var index= this.selectedIndex;
	var offset= 0;
	if(key == 38){ // up
		if(this.selectedIndex > 0){
			this.selectedIndex--;
		}
	}else
	if(key == 40){ // down
		if( this.selectedIndex < this.countItems-1 ){
			this.selectedIndex++;
		}
	}
	if(index >= 0){
		document.getElementById(this.id+'_item_'+index).className= 'aciCursorDefault'; 	//# 1. del cursor
	}
	this.scroll();																	//# 2. scroll offset
	document.getElementById(this.id+'_item_'+this.selectedIndex).className= 'aciCursorSelected'; //#3 set new cursor
}//

//############################
//
//	SCROLL ON KEY UP/DOWN
//
//############################
var fACInput_scroll= function(){
	var index= this.selectedIndex;
	var objDiv= document.getElementById(this.id+'_item_'+index);
	var curPos= objDiv.offsetTop - this.objContainer.scrollTop;
	
	var offsetBrowser= 0;
	if(isIE){offsetBrowser= 0;}else
	if(isGecko){offsetBrowser= 1;}else
	if(isOpera){offsetBrowser= -1;}else
	if(isSafari){}
	
	if(curPos < 0){
		this.objContainer.scrollTop+= curPos + offsetBrowser;
		return;
	}
	
	var offsetBrowser= 0;
	if(isIE){offsetBrowser= 2;}else
	if(isGecko){offsetBrowser= 3;}else
	if(isOpera){offsetBrowser= 1;}else
	if(isSafari){}
	
	var offsetScroll= objDiv.offsetHeight - this.objContainer.offsetHeight + curPos;
	if(offsetScroll > 0 ){ 
		this.objContainer.scrollTop+= offsetScroll + offsetBrowser;
	}
}//#

// ::: MISC ::: //

function setCaretPosition(ctrl, pos){
	if(ctrl.setSelectionRange){
		ctrl.focus();
		ctrl.setSelectionRange(pos,pos);
	}else 
	if (ctrl.createTextRange) {
		var range = ctrl.createTextRange();
		range.collapse(true);
		range.moveEnd('character', pos);
		range.moveStart('character', pos);
		range.select();
	}
}//#