function log(message) {
	if (!window.console) {
		return;
	}
	if(log.arguments.length > 1) {
		$(log.arguments).each(function(k,v) {
			console.log(v);
		});
	} else {
		console.log(message);
	}
};

function Suggest(input) {
	var self = this;

	this.entObj = document.createElement("div");
	this.entObj.className = "SuggestResult";
	this.URL    = 'http://www.medstore-online.com/suggest.php';
	this.frm    = document.getElementById('search_frm'); 

	this.input  = input;
	$(this.input).attr('autocomplete', 'off');
	this.TempValue = this.input.value;

	this.Try = 0;
	this.inFocus  = false;
	this.Complite = false;
	this.RowHover = false;
	this.isShow = false;

	$(this.input).focus(function() { self.OnFocus(); });
	$(this.input).blur(function() { self.OnBlur(); });
	$(this.input).keyup(function(event) {
		self.OnKeyUp(event);
	});
};
Suggest.prototype = {
	OnFocus : function() {
		this.inFocus = true;
	},
	OnBlur : function() {
		var self = this;
		this.hide();
		this.dropCodeValue();
		this.setCurrentResult();
		this.inFocus = false;
	},
	OnKeyUp : function(event) {
		var self = this;
		if (this.input.value != this.TempValue) {
			this.dropCodeValue();
		}
		if (this.input.value != this.TempValue && this.input.value.length > 3) {
			this.TempValue = this.input.value;
			var Temp = this.TempValue;
			this.Try = 0;
			setTimeout(function() {
				self.makeHint(Temp);
			}, 350);
		}  else if (this.input.value != this.TempValue) {
			self.hide();
		}
	},
	OnKeyDown : function(event) {
		var self = this.Suggest;
		switch (event.keyCode) {
			case 27:
				if($(self.entObj).parent()[0] == document.body) {
					self.hide();
				}
			break;
			case 38:
				self.setPrevRowHover();
			break;
			case 40:
				self.setNextRowHover();
			break;
			case 13:
				event.preventDefault();
				self.hide();
				self.setCurrentResult(true);
			break;
		}
	},
	makeHint : function(CurrentQuery) {
		var self = this;

		if (this.TempValue == CurrentQuery) {
			$.ajax({
				type: "GET",
				url: this.URL,
				dataType: "json",
				data: { filter : CurrentQuery },
				success: function(json) {
					if(json.status == 'reload') {
						window.location.reload();
						return;	
					}
					self.draw(json.response, CurrentQuery);
				},
				error: function(rt, st) {
//console.log(rt);
//console.log(st);
					if (self.Try < 3) {
						self.Try += 1;
						setTimeout(function() {
							self.makeHint(CurrentQuery);
						}, 350);
						return;
					}
				}
			});
		}
	},
	draw : function(response, CurrentQuery) {
		var self = this;
		if (this.entObj.firstChild) {
			for (var ch = 0; this.entObj.childNodes.length > 0; ch++) {
				this.entObj.removeChild(this.entObj.firstChild);
			}
		}

		var resultTable = document.createElement("table");
		resultTable.className = "SuggestResult";

		var responseItems = response.result;
		var responseTotal = response.total;

		if(responseTotal > 0) {
			jQuery.each(responseItems, function(i,responseItem) {
				var responseRow = resultTable.insertRow(i);
				responseRow.onmouseover = function() {
					self.RowHover = true;
					self.setRowHover(this);
				};
				responseRow.onmouseout = function() {
					self.RowHover = false;
					self.dropRowHover(this);
				};
				responseRow.onclick = function() {
					self.hide();
				};
				self.queryString = self.input.value;


				var responseTextCell = responseRow.insertCell(0);
				responseTextCell.innerHTML = responseItem;
				responseTextCell.setAttribute('Text', responseItem);
			});
			resultTable.onmouseout = function() {
				self.setRowHover(self.selectedRow);
			};
			this.resultTable = resultTable;
			this.setFirstRowHover();
			this.setRowHover(this.selectedRow);

			this.entObj.appendChild(this.resultTable);
			if (this.inFocus == true && this.Complite != true) {
				this.show();
			} else {
				if (CurrentQuery == this.TempValue) {
					this.setCurrentResult();
				}
			}
		}
	},
	show : function() {
		var self = this;
		this.isShow = true;

		$(this.input).unbind("keydown", self.OnKeyDown);
		$(this.input).bind("keydown", self.OnKeyDown);
		this.setPosition();
		this.entObj.style.visibility = "visible";
	},
	setPosition : function() {
		var Left = 7;
		var Top  = 33;

		Left += $(this.input).offset().left - $(this.input).scrollLeft();
		Top += $(this.input).offset().top - $(this.input).scrollTop();
		document.body.appendChild(this.entObj);
		$(this.entObj).css({
			"top"  : Top+'px',
        	"left" : Left+'px'
    	});
	},
	setRowHover : function(Row) {
		if (this.selectedRow) {
			this.dropRowHover(this.selectedRow);
		}
		$(Row).addClass("hover");
		this.selectedRow = Row;
	},
	dropRowHover : function(Row){
		$(Row).removeClass("hover");
	},
	setFirstRowHover : function() {
		this.selectedRow = this.resultTable.firstChild.firstChild;
		this.setRowHover(this.selectedRow);
	},
	setPrevRowHover : function() {
		if (this.selectedRow != null) {
			var PrewRow = this.selectedRow.previousSibling;
			if (PrewRow) {
				this.dropRowHover(this.selectedRow);
				this.setRowHover(PrewRow);
				this.selectedRow = PrewRow;
			}
		}
	},
	setNextRowHover : function() {
		if (this.selectedRow != null) {
			var NextRow = this.selectedRow.nextSibling;
			if (NextRow) {
				this.dropRowHover(this.selectedRow);
				this.setRowHover(NextRow);
				this.selectedRow = NextRow;
			}
		}
	},
	dropCodeValue : function() {
		if (this.input.value != this.TempValue) {
			this.Complite = false;
			this.input.title = '';
		}
	},
	hide : function() {
		var self = this;

		$(this.input).unbind("keydown", self.OnKeyDown);
		this.isShow = false;
		if ($(this.entObj).parent()[0] == document.body) {
			this.entObj.style.visibility = "hidden";
			document.body.removeChild(this.entObj);
		}
	},
	setCurrentResult : function(Focused) {
		if (this.input.value != '' && this.input.value == this.TempValue && this.TempValue == this.queryString && this.Complite != true) {
			if (this.selectedRow != null) {
				this.Complite = true;
				
				this.input.value = this.selectedRow.firstChild.getAttribute('Text');
				this.TempValue = this.input.value;
				
				this.RowHover = false;

		//		this.frm.submit();
			}
		}
	}
};
$(function() {
	var input = document.getElementById('Suggest');
	if (input) {
		input.Suggest = new Suggest(input);
	}
});

