/*
par{
	unique_values:(true|false),
	onGetList:( function( input value ) ),
	onChagne:( function( multi_ac value ) ),
	onlyOnline:[false|true],
	disabledValues:[value:true],
	inputName:[string],
	groups:[false|true],
	userCustomValues:[false|true], - lietotājam ļauj pievienot paša ierakstītu tekstu
	onblurAdd:[false|true]
}
	
method, properties
	onGetList izsauc saraksta iegūšans metodi
	setList( [
		{
			caption:
			value:
			online:
		}
	] ) // saraksta usetošana pēc saraksta iegūšanas
	onChagne izsauc izmaiņu eventu
	value() atgriež vai uzseto vērību	
*/
function MultiAc( par ){
	var this_ = this;
	this.onlyOnline = par.onlyOnline || false;
	this.disabledValues = par.disabledValues || {};
	this.groups = par.groups || false;
	this.sex = par.sex || false;
	this.age = par.age || false;
	this.data = par.data || false;
	this.userCustomValues = par.userCustomValues || false;
	this.onAddFail = par.onAddFail || false;
	this.maxLength = par.maxLength || 0;
	//this.onblurAdd = par.onblurAdd || false;
	this.onblurAdd = true;
	this.defaults = {
		kay_wait:500,
		hide_list:300,
		unique_values:typeof par.unique_values == 'undefined' ? true : par.unique_values,
		cache:typeof par.cache == 'undefined' ? false : par.cache
	};
	var _ = this._ = {
		key_timer:false,
		hide_timer:false,
		scroll_timer:false,
		inp:document.createElement( 'input' ),
		list:document.createElement( 'div' ),
		listItems:[],
		curListItem:false,
		values:[],
		cache:{}
	};
	this.node = document.createElement( 'div' );
	if( $.browser.msie ){
		this._.hiddenInput = mkE( {
			tag:'<input name="' + ( par.inputName || '' ) + '" >',
			prop:{
				type:'hidden'				
			},
			attr:{
				name:par.inputName || ''
			}
		} ).append( this.node );
	} else {
		this._.hiddenInput = mkE( {
			tag:'input',
			prop:{
				type:'hidden'				
			},
			attr:{
				name:par.inputName || ''
			}
		} ).append( this.node );
	}
	this.node.onclick = function(){
		_.inp.focus();
	};
	this._.scrollContent = document.createElement( 'div' );
	this._.scrollContent.className = 'scroll_content';
	this.node.appendChild( this._.scrollContent );
	this._.values_table = document.createElement( 'table' );
	this._.values_table.className = 'values';
	this._.scrollContent.appendChild( this._.values_table );	
	this._.scrollContent.onscroll = function(){		
		this_.calcScrollButtons();
	};
	var tbody = document.createElement( 'tbody' );
	this._.values_table.appendChild( tbody );
	var tr = this._.tr = document.createElement( 'tr' );
	tbody.appendChild( tr );
	var td = this._.inptd = document.createElement( 'td' );
	tr.appendChild( td );
	td.appendChild( this._.inp );
	this._.inp.multi_ac = this; 
	this.node.className = 'multi_ac'
	this._.list.className = 'multi_ac_list'
	this._.list.innerHTML = 'azd asd asd asd';
	this._.inp.autocomplite = 'off';
	this._.inp.setAttribute( 'autocomplite', 'off' );
	this._.inp.onkeyup = MultiAc.inp_onkey;
	this._.inp.onkeydown = MultiAc.inp_onkeydown;
	this._.inp.onkeypress = MultiAc.inp_onkeypress;
	this._.inp.onfocus = MultiAc.inp_onfocus;
	this._.inp.onblur = MultiAc.inp_onblur;	
	this._.inp.onmouseup = function( e ){
		if( ! e ){
			e = event;
		}
		if( e.stopPropagation ){
			e.stopPropagation();
		}
		e.cancelBubble = true;
	};
	this._.scrollLeft = document.createElement( 'div' );
	this._.scrollLeft.className = 'scroll_left';
	this._.scrollLeft.style.display = 'none';
	this.node.appendChild( this._.scrollLeft );
	this._.scrollRight = document.createElement( 'div' );
	this._.scrollRight.className = 'scroll_right';
	this._.scrollRight.style.display = 'none';	
	// +++ content scrollint
	this._.scrollRight.onmouseover = function(){		
		if( this_._.scroll_timer ) clearInterval( this_._.scroll_timer );
		this_._.scroll_timer = setInterval( function(){
			if( this_._.scrollContent.scrollLeft < this_._.values_table.offsetWidth - this_.node.offsetWidth ){
				this_._.scrollContent.scrollLeft += 20;
				this_._.scrollLeft.style.display = '';
			} else {
				this_._.scrollRight.style.display = 'none';
			}
		}, 75 );
	};
	this._.scrollLeft.onmouseover = function(){
		if( this_._.scroll_timer ) clearInterval( this_._.scroll_timer );
		this_._.scroll_timer = setInterval( function(){
			if( this_._.scrollContent.scrollLeft > 0 ){
				this_._.scrollContent.scrollLeft -= 20;
				this_._.scrollRight.style.display = '';
			} else {
				this_._.scrollLeft.style.display = 'none';
			}
		}, 75 );
	};
	this._.scrollLeft.onmouseout = this._.scrollRight.onmouseout = function(){
		if( this_._.scroll_timer ) clearInterval( this_._.scroll_timer );
	};
	// --- content scrollint
	this.node.appendChild( this._.scrollRight );
	if( par.onGetList ){
		this.onGetList = par.onGetList;
	}
	if( par.onChange ){
		this.onChange = par.onChange;
	}
	this._.infoOverlay = mkE( {
		tag:'div',
		className:'multiAcText color2',
		text:par.infoText || xGlobal[ 'multiAc info' ] || '',
		prop:{
			onclick:function(){
				this.style.display = 'none';
				this_._.inp.focus();
			}
		}
	} ).append( this.node );
};

MultiAc.inp_onfocus = function(){
	//var outermost = document.getElementById( 'outermost' );
	var outermost = false;
	if( this.multi_ac._.hide_timer ){
		clearTimeout( this.multi_ac._.hide_timer );
	}	
	( outermost ? outermost : document.body ).appendChild( this.multi_ac._.list );
	this.multi_ac.setListPosition();
	this.multi_ac._.infoOverlay.style.display = 'none';
	this.focused = true;
}; // MultiAc.inp_onfocus

MultiAc.inp_onblur = function(){
	var this_ = this;
	this.multi_ac._.hide_timer = setTimeout( function(){
		if( this_.multi_ac._.list.parentNode ){
			this_.multi_ac._.list.parentNode.removeChild( this_.multi_ac._.list );
		}
	}, this.multi_ac.defaults.hide_list );
	if( this.multi_ac.userCustomValues && this.multi_ac.onblurAdd ){
		setTimeout( function(){
			var addCustom = false;
			if( typeof this_.multi_ac.userCustomValues == 'function' ){
				var addCustom = this_.multi_ac.userCustomValues( this_.value );
				this_.value = addCustom.value;
			}
			if( ! empty( this_.value ) && ( ! addCustom || addCustom.ok ) ){
				this_.multi_ac.addValue( {
					caption:this_.value,
					value:'custom' + this_.value
				} );
			}
		}, 300 );
	}
	this.focused = false;
}; // MultiAc.inp_onblur

MultiAc.inp_onkeypress = function( e ){
	var this_ = this;	
	if( ! e ){
		e = window.event;
	}
	e.cancelBubble = true;
	if( e.stopPropagation ) e.stopPropagation();
	if( e.keyCode == 13 ) return false;
}; // MultiAc.inp_onkeydown

MultiAc.inp_onkeydown = function( e ){
	var this_ = this;	
	if( ! e ){
		e = window.event;
	}
	e.cancelBubble = true;
	if( e.stopPropagation ) e.stopPropagation();
	switch( e.keyCode ){
		case 8: // backspace
			if( typeof this.selectionStart != 'undefined' ){
				if( this.selectionStart == 0 && this.multi_ac._.values.length ){
					this.multi_ac.removeValue( this.multi_ac._.values[ this.multi_ac._.values.length - 1 ] );
				}
			} else { // For IE		 
				if( this.value.length == 0 && this.multi_ac._.values.length ){
					this.multi_ac.removeValue( this.multi_ac._.values[ this.multi_ac._.values.length - 1 ] );
				}
			}
			break;
	}	
}; // MultiAc.inp_onkeydown

MultiAc.inp_onkey = function( e ){
	var this_ = this;
	if( ! e ) e = event;
	e.cancelBubble = true;
	if( e.stopPropagation ){
		e.stopPropagation();
	}
	switch( e.keyCode ){	
		case 13: // enter
			if( this.multi_ac._.curListItem === false ){
				if( this.multi_ac.userCustomValues ){
					var addCustom = false;
					if( typeof this.multi_ac.userCustomValues == 'function' ){
						var addCustom = this_.multi_ac.userCustomValues( this_.value );
						this.value = addCustom.value;
					}
					if( ! empty( this.value ) && ( ! addCustom || addCustom.ok ) ){
						this.multi_ac.addValue( {
							caption:this.value,
							value:'custom' + this.value
						} );
					}
				}
			} else {
				this.multi_ac._.listItems[ this.multi_ac._.curListItem ].node.onclick();
			}
			return false;
			break;
		case 27: // esc
			this.value = '';
			break;
		case 38: // up
			if( this.multi_ac._.listItems.length && this.multi_ac._.curListItem === false ){
				this.multi_ac._.listItems[ 0 ].node.onmouseover();
			} else if( this.multi_ac._.listItems.length && this.multi_ac._.curListItem > 0 ){				
				this.multi_ac._.listItems[ this.multi_ac._.curListItem - 1 ].node.onmouseover();
			} else if( this.multi_ac._.listItems.length && this.multi_ac._.curListItem == 0 ){				
				this.multi_ac._.listItems[ this.multi_ac._.listItems.length - 1 ].node.onmouseover(); 
			} 
			break;
		case 40: // down
			if( this.multi_ac._.listItems.length && this.multi_ac._.curListItem === false ){
				this.multi_ac._.listItems[ 0 ].node.onmouseover();
			} else if( this.multi_ac._.listItems.length && this.multi_ac._.curListItem < this.multi_ac._.listItems.length - 1 ){				
				this.multi_ac._.listItems[ this.multi_ac._.curListItem + 1 ].node.onmouseover();
			}
			break;
	}
	if( this.multi_ac._.key_timer ){
		clearTimeout( this.multi_ac._.key_timer );
	}
	if( this.value_ != this.value ){
		this.multi_ac._.key_timer = setTimeout( function(){		
				clearNode( this_.multi_ac._.list );
				var img = document.createElement( 'img' );
				img.src = pimg + '/img/load.gif';
				this_.multi_ac._.list.appendChild( img );
				this_.multi_ac._.list.style.display = '';
				if( this_.multi_ac.defaults.cache && this_.multi_ac._.cache[ this_.multi_ac._.inp.value ] ){
					this_.multi_ac.setList( this_.multi_ac._.cache[ this_.multi_ac._.inp.value ] );
					return;
				}
				if( this_.value == '' ){
					this_.multi_ac.setList( [] );
				} else {
					if( this_.multi_ac.onGetList ){
						this_.multi_ac.onGetList.apply( this_.multi_ac, [ this_.value, {
							groups:this_.multi_ac.groups,
							data:this_.multi_ac.data,
							sex:this_.multi_ac.sex,
							age:this_.multi_ac.age
						} ] );
					}
				}
		}, this.multi_ac.defaults.kay_wait );
	} 
	this.value_ = this.value;
	this.multi_ac.setListPosition();
}; // MultiAc.inp_onkey

//MultiAc.prototype.toString = function(){ return 'draugiem.lv multi autocomplite' };

MultiAc.prototype.setListPosition = function(){	
	var scrollTop = 0;
	var parent = this.node.parentNode;
	while( parent ){
		if( parent.scrollTop ){
			scrollTop += parent.scrollTop;
		}
		parent = parent.parentNode;
	}
	this._.list.style.left = MultiAc.findPosX( this._.inp ) - this._.scrollContent.scrollLeft + 'px';
	this._.list.style.top = MultiAc.findPosY( this._.inp ) + this._.inp.offsetHeight - scrollTop + 'px';
	this._.list.style.display = ( this._.listItems.length ? '' : 'none' );
}

MultiAc.prototype.append = function( node ){
	if( typeof node == 'string' ){
		document.getElementById( node ).appendChild( this.node );
		return this;
	}
	node.appendChild( this.node );
	return this;
}; // MultiAc.prototype.append

MultiAc.prototype.remove = function(){
	if( this.node.parentNode ){
		this.node.parentNode.removeChild( this.node );
	}
	return this;
}; // MultiAc.prototype.remove

MultiAc.listItem_onmouseover = function(){
	var last = this.multi_ac._.curListItem;	
	if( last !== false && this.multi_ac._.listItems[ last ] ){
		removeClassName( this.multi_ac._.listItems[ last ].node, 'hover' );
	}
	this.multi_ac._.curListItem = this.multi_ac_list_item_i;
	addClassName( this, 'hover' );
}; // MultiAc.listItem_onmouseover

MultiAc.listItem_onclick = function(){
	this.multi_ac.addValue( {
		caption:this.multi_ac_list_item.data.caption,
		value:this.multi_ac_list_item.data.value
	} );
	this.multi_ac.setList( [] );
}; // MultiAc.listItem_onclick

MultiAc.prototype.setList = function( data ){
	$multiAc = this;
	this._.cache[ this._.inp.value ] = data;
	clearNode( this._.list );
	this._.listItems = [];
	this._.curListItem = false;
	for( var i = 0; i < data.length; i ++ ){
		var div = mkE( {
			tag:'div',
			className:( data[ i ].group ? 'multiAcItemGroup' : '' ),
			text:htmlspecialchars_decode( data[ i ].caption ),
			prop:{
				multi_ac:this,
				multi_ac_list_item_i:i,
				onmouseover:MultiAc.listItem_onmouseover
			}
		} )
		if( ! this.disabledValues[ data[ i ].value ] && ( ! this.onlyOnline || data[ i ].online ) && ! data[ i ].disabled ){
			div.onclick = MultiAc.listItem_onclick;
		} else {
			div.onclick = function(){};
			addClassName( div, 'multiAcItemDisabled' );
		}		
		this._.list.appendChild( div );
		addClassName( div, 'item' );
		if( data[ i ].online ){
			addClassName( div, 'online' + data[ i ].online + 'Icon' );
		}
		this._.listItems.push( div.multi_ac_list_item = {
			node:div,
			data:data[ i ],
			value:data[ i ].value
		} );
	}
	this.setListPosition();
	//if( data.length )	this.setListPosition(); else this._.list.style.display = 'none';
}; // MultiAc.prototype.setList

MultiAc.prototype.addInputValue = function(){
	if( ! this.userCustomValues ){
		return false;
	}
	if( ! empty( this._.inp.value ) ){
		this.addValue( {
			caption:this._.inp.value,
			value:'custom' + this._.inp.value
		} );
	}
};

MultiAc.valueItem = function( par ){	
	var this_ = this;
	this.value = par.value;
	this.caption = par.caption;
	var tdm_text, el;
	this.node = mkE( {
		tag:'td',
		els:[
			el = mkE( {
				tag:'table',
				className:'value',
				attr:{
					cellspacing:'0'
				},
				els:[
					mkE( {
						tag:'tbody',
						els:[
							mkE( {
								tag:'tr',
								els:[
									mkE( {
										tag:'td',
										className:'l',
										els:[
											mkE( {
												tag:'div'
											} )
										]
									} ),
									mkE( {
										tag:'td',
										className:'m',
										els:[
											tdm_text = mkE( {
												tag:'div',
												className:'caption',
												text:par.caption
											} )
										]
									} ),
									mkE( {
										tag:'td',
										className:'m',
										els:[
											mkE( {
												tag:'div',
												className:'del deleteIcon deleteIconSize',
												prop:{
													onclick:function(){
														par.multi_ac.removeValue( this_ );
													}
												}
											} )
										]
									} ),
									mkE( {
										tag:'td',
										className:'r',
										els:[
											mkE( {
												tag:'div'
											} )
										]
									} )
								]
							} )
						]
					} )
				]
			} )
		]
	} );
	par.multi_ac._.tr.insertBefore( this.node, par.multi_ac._.inptd );	
	$( el ).hide().fadeIn( 'fast' );
	if( tdm_text.offsetWidth > 120 ){
		tdm_text.style.width = '120px';	
	}
}; // MultiAc.valueItem

MultiAc.prototype.addValue = function( par ){
	this._.inp.value_ = '';
	this._.inp.value = '';
	this._.infoOverlay.style.display = 'none';
	if( this.defaults.unique_values ){
		for( var i = 0; i < this._.values.length;  i ++ ){
			if( this._.values[ i ].value == par.value ){
				if( this.onAddFail ){
					this.onAddFail();
				}
				return false;
			}
		}
	}
	if( this.maxLength ){
		this._.inp.readOnly = ( this._.values.length >= this.maxLength );
	}
	if( this._.inp.readOnly ){
		if( this.onAddFail ){
			this.onAddFail();
		}
		return false;
	}
	var valueItem = new MultiAc.valueItem( {
		caption:par.caption,
		value:par.value,
		multi_ac:this
	} );
	this._.values.push( valueItem );
	var values = this.value();
	if( this.onChange ){
		this.onChange( values, valueItem );
	}
	this._.hiddenInput.value = values.join( ',' );
	this.calcScrollButtons();
	if( this._.values_table.offsetWidth > this.node.offsetWidth ){
		this._.scrollContent.scrollLeft = this._.values_table.offsetWidth - this.node.offsetWidth - 20;
	} else {
		this._.scrollContent.scrollLeft = 0;
	}
	if( this.friendList ){
		this.friendList.value( par.value, true );
	}
	if( this.maxLength ){
		this._.inp.readOnly = ( this._.values.length >= this.maxLength );
	}
	if( this._.inp.readOnly ){
		if( this.focused ){
			this._.inp.blur();
		}
	} else {
		this._.inp.focus();
	}
	return par;
}; // MultiAc.prototype.addValue

MultiAc.prototype.removeValue = function( valueItem, value ){
	var this_ = this;
	var index = -1;
	if( valueItem ){
		if( typeof this._.values.indexOf != 'undefined' ){
			var index = this._.values.indexOf( valueItem );
		} else { // For IE
			for( var i = 0; i < this._.values.length; i ++ ){
				if( this._.values[ i ] === valueItem ){
					index = i;
					break;
				}
			}
		}
	}
	if( value ){
		for( var i = 0; i < this._.values.length; i ++ ){
			if( this._.values[ i ].value == value ){
				valueItem =  this._.values[ i ]; 
				index = i;
				break;
			}
		}
	}
	if( index == -1 ){
		return;
	}
	this._.values.splice( index, 1 );	
	$( valueItem.node ).fadeOut( 'fast', function(){
		removeNode( valueItem.node );
		if( this_._.values_table.offsetWidth > this_.node.offsetWidth ){
			this_._.scrollContent.scrollLeft = this_._.values_table.offsetWidth - this_.node.offsetWidth - 20; 
		} else {
			this_._.scrollContent.scrollLeft = 0;
		}
		this_.calcScrollButtons();
	} );
	var values = this.value();
	if( this.maxLength ){
		this._.inp.readOnly = ( values.length >= this.maxLength );
	}
	if( this.onChange ){
		this.onChange( values, valueItem );
	}
	this._.hiddenInput.value = values.join( ',' );
	if( this.friendList ){
		this.friendList.value( valueItem.value, false );
	}
}; // MultiAc.prototype.removeValue

MultiAc.prototype.clear = function(){
	while( this._.values[ 0 ] ){
		this.removeValue( this._.values[ 0 ] );
	}
};

MultiAc.prototype.value = function(){
	var value = [];
	for( var i = 0; i < this._.values.length;  i ++ ){
		value.push( this._.values[ i ].value );
	}
	return value;
}; // MultiAc.prototype.value

MultiAc.prototype.valueObj = function(){
	return this._.values;
};

MultiAc.prototype.calcScrollButtons = function(){
	if( this._.values_table.offsetWidth > this.node.offsetWidth ){
		this._.scrollLeft.style.display = '';
		this._.scrollRight.style.display = '';		
	} else {
		this._.scrollContent.scrollLeft = 0;
		this._.scrollLeft.style.display = 'none';
		this._.scrollRight.style.display = 'none';
	}
}; // MultiAc.prototype.calcScrollButtons

MultiAc.prototype.focus = function(){
	this._.inp.focus();
	return this;
};

MultiAc.findPosX = function( obj, parent ){
	var curleft = 0;
	if( obj.offsetParent ){
		while ( obj.offsetParent && obj.offsetParent != parent ){
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	} else if( obj.x ){
		curleft += obj.x;
	}
	return curleft;
}; // MultiAc.findPosX
 
MultiAc.findPosY = function( obj, parent ){
	var curtop = 0;
	if ( obj.offsetParent ) {
		while ( obj.offsetParent && obj.offsetParent != parent ){
			if( $.browser.msie && obj.id == 'infobox3_content_table2' ){
				curtop += obj.offsetTop / 2;
			} else {
				curtop += obj.offsetTop;
			}			
			obj = obj.offsetParent;
		}
	} else if ( obj.y ){
		curtop += obj.y;
	}
	return curtop;
}; // MultiAc.findPosY

D.multiAC = MultiAc;