/**
 * KMP 2009 Frontend Ask Question Live Search Client Script
 * 
 * @author S. [LinuZz] Ostapenko
 * @copyright KMP Development Team, 2007-2009
 * @since 5.1
 */
	onDomReady(function(){
	    var textarea = document.getElementById('ask-question'); // question textarea
		window.EnableActiveResponse = true;
	    if (textarea) {
			var proposes_div_id = 'ask-proposes';
			var proposes = document.getElementById(proposes_div_id); // proposes holder div
		    var loader = document.getElementById('ask-loader'); // icon
		    
			var input = document.getElementById('askSubject');
			if (!input) {			
				var input = {
					value: ''
				};
			}

			var value = textarea.value + input.value;
		    
		    loader.style.display = 'none';

		    var t = window.setInterval(function() { // check on every 2 seconds
			    if (value != textarea.value + input.value && window.EnableActiveResponse) { // text changed
				    if ((textarea.value + input.value).length < 3) {
					    proposes.innerHTML = '';
				    } else {
					    loader.style.display = 'inline';
					    new Ajax.Request(base_href + 'asksearch', {
						    onSuccess: function(transport){
							    eval ('var obj = ' + transport.responseText);
//								var debug_msg = '';
//								if (obj && obj.debug) {
//									var debug_msg = obj.debug;
//									obj = obj.result;
//								}
								var txt = '';
								if (obj.length > 0) {
									txt += '<div id="ask-proposes-data">';
									txt += '<h3 style="margin-left: 0px; margin-bottom: 3px;">' + i18n['lbl_do_these_answers_help'] + ' <a onclick="document.getElementById(\'' + proposes_div_id + '\').innerHTML = \'\';" href="javascript: return false;"><img style="vertical-align: middle;" src="' + base_url + 'js/js-local/build/images/layer/close-icon.png" /></a></h3>';
									txt += '<p>' + i18n['lbl_found_some_answers'] + '</p>';
									txt += '<div class="sr-1"><div class="sr-data-case"><table class="list-1">';
							    for (var i = 0; i < obj.length; i++) {
										txt += '<tr><td class="list-article">';
										if (obj[i].type.toLowerCase() == 'attachment') {
											txt += '<img class="attachment-img" src="' + base_url + 'images/si/' + obj[i].mime_type + '.png" /> ';
											txt += '<a class="list-item attachment-item" target="_blank" href="' + base_href + 'getAttach/' + obj[i].attach_id + '/' + obj[i].code + '/' + obj[i].attach_filename + '">' + obj[i].attach_name + '</a> ';
											txt += '[';
											txt += i18n['lbl_attachment_in'] + ' <a target="_blank" href="' + base_href + 'article/' + obj[i].code + '">' + obj[i].title + '</a>';
											if (obj[i].featured == 'Y') {
												txt += '<img class="featured-status" src="' + base_url + 'images/local/FirstTheme/section-star.gif" />';
											}
											if (obj[i].favorite == 'Y') {
												txt += '<img src="' + base_url + 'images/local/FirstTheme/ico-rating-y.gif" />';
											}
											txt += ']';
										} else {
											txt += '<a class="list-item" target="_blank" href="' + base_href + 'article/' + obj[i].code + '">' + obj[i].title + '</a>';
											if (obj[i].featured == 'Y') {
												txt += '<img class="featured-status" src="' + base_url + 'images/local/FirstTheme/section-star.gif" />';
											}
											if (obj[i].favorite == 'Y') {
												txt += '<img src="' + base_url + 'images/local/FirstTheme/ico-rating-y.gif" />';
											}
										}
										txt += obj[i].articlePath;
										txt += '' + obj[i].description + '';
										txt += '<div class="statistic-data">';
										txt += obj[i].comments;
										txt += obj[i].attachments;
										txt += '</div>';
										txt += '</td></tr>';
							    }
									txt += '</table></div></div></div>';
									
								}
//								if (debug_msg) {
//									txt += '<div style="border: 2px solid red; font-size: 12px !important;">'+debug_msg+'</div>';
//								}
							    proposes.innerHTML = txt;
							    loader.style.display = 'none';
						    },
						    
						    onFailure: function(){
							    proposes.innerHTML = '';
							    loader.style.display = 'none';
						    },
						    
						    parameters: {
							    searchQuestion: textarea.value,
								searchSubject: input.value								
						    },
						    
						    method: 'post'
					    });
				    }
				    value = textarea.value + input.value;
			    }
		    }, 2000);
	    }
	})
// show ask-question
	function displayElements(obj, EnableActiveResponse) {
		window.EnableActiveResponse = EnableActiveResponse;
		document.getElementById('ask-question-case').className = "form-case";
	}
