function tag_whisperer(event){
	//po dokonceni do samostatne fce aby bylo mozno pouzit vsude
	var tag_name = '';
	var hide = false;
	var arr = new Array();
	
	var position;
	//alert(event.keyCode);
	//desable autocomplete
	if (typeof(whisper_field)==undefined) {
		return;
	}
	whisper_field.attr('autocomplete','off');
	
	if (event.which == '13') {
		//press ENTER
		event.preventDefault();
		if (list_position>0) { 
			add_tag($('.'+whisperer_class+' li.'+list_position).text());
			$('.'+whisperer_class).hide();
			list_position = 0;
		}
		else if (submit_form!='') {
			//submit form
			$(submit_form).submit();
		}
	}
	else if (event.which==40) {
		//alert('sipka dolu');
		//alert(list_position);
		if (list_count>0) {
			if (list_position>0 && list_position<list_count) {
				$('.'+whisperer_class+' li.'+list_position).removeClass(select_class);
			}
			if (list_position==0) {
				$('.'+whisperer_class).show();
			}
			if (list_position<list_count) {
				list_position++;
				$('.'+whisperer_class+' li.'+list_position).addClass(select_class);
			}
		}
	}
	else if (event.which==38) {
		//alert('sipka nahoru');
		if (list_count>0) {
			if (list_position>1) {
				$('.'+whisperer_class+' li.'+list_position).removeClass(select_class);
				list_position--;
				if (list_position>0) {
					$('.'+whisperer_class+' li.'+list_position).addClass(select_class);
				}
			}
		}
	}
	else if (event.which==27) {
		//press ESC
		hide = true;
	}
	else if (event.which==46) {
		//press delete
		//alert(whisper_field.val());
		tag_name=whisper_field.val().substr(0, whisper_field[0].selectionStart);
		if (whisper_field[0].selectionStart==whisper_field[0].selectionEnd) {
			tag_name+=whisper_field.val().substr(whisper_field[0].selectionEnd+1, whisper_field.val().length);
		}
		else {
			tag_name+=whisper_field.val().substr(whisper_field[0].selectionEnd, whisper_field.val().length);
		}
		arr = tag_split(tag_name);
		tag_name=arr[arr.length-1];
		//alert(tag_name);
		hide = true;
	}
	else if (event.which==8) {
		//press BACK SPACE
		tag_name = whisper_field.val().substr(0,whisper_field[0].selectionStart);
		if (whisper_field[0].selectionStart == whisper_field[0].selectionEnd) {
			tag_name = whisper_field.val().substr(0,whisper_field[0].selectionStart-1);
		}
		if (whisper_field[0].selectionStart != whisper_field.val().length) {
			tag_name +=  whisper_field.val().substr(whisper_field[0].selectionEnd,whisper_field.val().length)
		}
		arr = tag_split(tag_name);
		tag_name=arr[arr.length-1];
		//alert(tag_name);
		hide = true;
	}
	else if (event.which >= 48) { //ascii charcter
		arr = tag_split(whisper_field.val());
		tag_name=arr[arr.length-1] + String.fromCharCode(event.keyCode);
		hide = true;
	}
	if (tag_name.length >= min_tag_length) {
		//call ajax whisperer backend
		$('.'+whisperer_class).remove();
		//remove first blank space
		tag_name = ltrim(tag_name);
		list_position = 0; //reset list position, change list
		$.ajax({
			type: "POST",
			url: tag_url,
			data: 'tag_name=' + tag_name,
			dataType: 'json',
			success: function(data) {
			//alert(errorsMsg[4]);
				//alert(data.length);
				list_count=data.length;
				html = '<div class="'+whisperer_class+'">';
				if (data.length>0) {
					html += '<ul style="margin-left: 0px;">';
					i=1;
					while (i <= data.length) {
						html += '<li class="'+i+'">' + data[i-1] + '</li>';
						i++;
					}
					html += '</ul>';
				}
				html += '</div>';
				//console.log(html);
				$('body').append(html);
				position = whisper_field.position();
				$('.'+whisperer_class).css('top', position.top+22);
				$('.'+whisperer_class).css('left', position.left);					
				$('.'+whisperer_class).css('display', 'block');
			
			},
			error: function() {
				alert('AJAX error');
			}
		});
	}
	else if (hide==true) {
		$('.'+whisperer_class).hide();
		list_position=0;
	}
}

function body_clik_whisperer(e) {
	var original_target; //for difernt web browsers
	var local_name; //local tag name clicking
    if (whisperer_class!='') {
    	if (e.target.localName) {
    		local_name=e.target.localName;
    	}
    	else {
    		//for IE8 (shit :-) )
    		local_name=e.target.nodeName;
    	}
        if (local_name.toLowerCase() == 'li') {
            if (e.originalEvent.originalTarget) {
            	original_target= e.originalEvent.originalTarget;
            }
            else if (e.originalEvent.target) {
            	//for chrome safari, opera
            	original_target=e.originalEvent.target;
        	}
            else {
            	//IE
            	original_target=event.srcElement;
            }
            if (original_target.parentNode.parentNode.className == whisperer_class) {
                //selected tag by click
                //alert(e.originalEvent.originalTarget.className);
                add_tag($(original_target).text());
                whisper_field.focus();
            }
        }
    }
    //hide div
    $('.'+whisperer_class).hide();
}

function body_mousemove_whisperer(e) {
	var original_target; //for different web browsers
	var local_name; //local tag name
	if (whisperer_class!='') {
		if (list_position>0) {
			$('.'+whisperer_class+' li.'+list_position).removeClass(select_class);
		}
		if (e.target.localName) {
    		local_name=e.target.localName;
    	}
    	else {
    		//for IE8 (shit :-) )
    		local_name=e.target.nodeName;
    	}
		if (local_name.toLowerCase() == 'li') {
			if (e.originalEvent.originalTarget) {
            	original_target= e.originalEvent.originalTarget;
            }
            else if (e.originalEvent.target) {
        	//for chrome safari, maybe IE
            	original_target=e.originalEvent.target;
        	}
            else {
            	//IE
            	original_target=event.srcElement;
            }
			if (original_target.parentNode.parentNode.className == whisperer_class) {
				//change position by hover
				list_position=parseInt(original_target.className);
				$('.'+whisperer_class+' li.'+list_position).addClass(select_class);
			}
		}
		else {
			list_position =0;
		}
	}
	//hide div
}
 

function ltrim(string_to_trim) {
	return string_to_trim.replace(/^\s+/,"");
}

