$(document).ready(function(){                                                                                                     
	show_placeholder();
	$("input[name='coverage']").keyup(function(e){get_suggestion(this, e)}).focus(function(){hide_placeholder()}).blur(function(){show_placeholder()});
	$(document).keyup(function(e){
		if (suggestion_showed && (e.keyCode == '38' || e.keyCode == '40')) {
			suggestion_highlight(false, e.keyCode == '38' ? -1 : 1);
		}
		if (suggestion_showed && (e.keyCode == '13')) {
			var _obj = $("div.suggestion-variant.selected");
			if (_obj.length) suggestion_click(_obj[0]);
		}
		if (e.keyCode == 27) {
			$("input[name='coverage']").val('');
			$("#coverage").find(".gsm,.cdma,div.cover-wrpper").hide();
		}
		/*
		if (e.ctrlKey && e.keyCode == 'G'.charCodeAt(0) || e.ctrlKey && e.shiftKey && e.keyCode == 'G'.charCodeAt(0) || e.keyCode == 'G'.charCodeAt(0)) {
			$("#coverage").toggle();
			return false;
		}
		*/
	});
});

var request_in_progress = false,
	suggestion_showed = false;
	
function get_suggestion (obj, e) {
	$("#coverage").find(".gsm,.cdma,div.cover-wrpper").hide()
	if (typeof e != 'undefined') {
		if (e.keyCode == '38' || e.keyCode == '40' || e.keyCode == '13') return true;
	}
	if (!obj.value) {
		$("#suggestions").hide();
		suggestion_showed = false;
		return false;
	}
	request_in_progress = true;
	$.ajax({
		url:'/loader/',
		data:{
			type: 'coverage',
			search: obj.value,
			rnd: Math.round(Math.random() * 1000000)
		},
		dataType:'html',
		success:function(data){success_suggestion(data)},
		error:function(e, text){error_suggestion(e, text)}
	});
}

//var __data = false;
function success_suggestion (data) {
	var _suggestions = $(data);//.find("div.finded-row");
	//console.log(data);
	//__data = data;
	if (_suggestions.length) {
		var _i = false, _html = '', _have_null = false;
		_suggestions.each(function(){
			$this = $(this);
			if ($this.hasClass('finded-row')) {
			_html += '<div class="suggestion-variant' + ( _i ? ' odd': '' ) + '">'
				+ '<span class="name">' + $this.find("div.area-name").html() + '</span>'
				+ '<span class="gsm" style="display:none">' +  $this.find("div.have-gsm").html() + '</span>'
				+ '<span class="cdma" style="display:none">' +  $this.find("div.have-cdma").html() + '</span>'
				+ '</div>';
			_i = !_i;
			}
		});
		if(_html.length) {
			$("#suggestions").html(_html).show();
			suggestion_showed = true;
			$("div.suggestion-variant").mousemove(function(){suggestion_highlight(this)}).click(function(){suggestion_click(this)});
			// TODO events
		} else {
			$("#suggestions").hide();
			suggestion_showed = false;
		}
	} else {
		$("#suggestions").hide();
		suggestion_showed = false;
	}
	request_in_progress = false;
}

function error_suggestion (e, text) {
	$("#suggestions").hide();
	request_in_progress = false;
	suggestion_showed = false;
}

function suggestion_click (obj) {
	var $obj = $(obj),
		name = $obj.find("span.name").text(),
		gsm = $obj.find(".gsm").text(),
		cdma = $obj.find(".cdma").text();

	$("#suggestions").hide();
	suggestion_showed = false;
	$("input[name='coverage']").val(name);
	if ((gsm == 0 || gsm == 1) && (cdma == 0 || cdma == 1))
		show_coverage(gsm, cdma);
}

function show_coverage (gsm, cdma) {
	var _coverage = $("#coverage");
	_coverage.find(".gsm,.cdma,div.cover-wrpper").show();
	if (gsm == 1) {
		var _p = _coverage.find(".gsm").addClass("covered").parent();
		_p.find("span.cover-yes").show();
		_p.find("span.cover-no").hide();
	} else {
		//_coverage.find(".gsm").removeClass("covered");
		var _p = _coverage.find(".gsm").removeClass("covered").parent();
		_p.find("span.cover-yes").hide();
		_p.find("span.cover-no").show();
	}
	if (cdma == 1) {
		//_coverage.find(".cdma").addClass("covered");
		var _p = _coverage.find(".cdma").addClass("covered").parent();
		_p.find("span.cover-yes").show();
		_p.find("span.cover-no").hide();		
	} else {
		//_coverage.find(".cdma").removeClass("covered");
		var _p = _coverage.find(".cdma").removeClass("covered").parent();
		_p.find("span.cover-yes").hide();
		_p.find("span.cover-no").show();		
	}
	//_coverage.show()
}

function suggestion_highlight (obj, by_arrow) {
	if (typeof by_arrow != 'undefined' && by_arrow != 0) {
		_currnent = $("div.suggestion-variant.selected");
		var _next = false;
		if (_currnent.length) {
			_next = 
				by_arrow == 1 
				? (_currnent[0].nextSibling ? _currnent[0].nextSibling : false)
				: (_currnent[0].previousSibling ? _currnent[0].previousSibling : false);
		}
		$("div.suggestion-variant").removeClass('selected');
		if (_next) {
			$(_next).addClass('selected');
		} else {
			by_arrow == 1 
				? $("div.suggestion-variant:first").addClass('selected')
				: $("div.suggestion-variant:last").addClass('selected');
		}
	} else {
		$("div.suggestion-variant").removeClass('selected');
		$(obj).addClass('selected');
	}
}

function show_placeholder () {
	if ($.browser.msie || $.browser.opera || $.browser.mozilla) {
		var _input = $("input[name='coverage']");
		if (_input.attr("placeholder") && !_input.val()) {
			_input.addClass("show-placeholder").val(_input.attr("placeholder"));
		} else {
			_input.removeClass("show-placeholder");
		}
	}
}
function hide_placeholder () {
	if ($.browser.msie || $.browser.opera || $.browser.mozilla) {
		var _input = $("input[name='coverage']");
		if (_input.attr("placeholder") && _input.val() == _input.attr("placeholder")) {
			_input.removeClass("show-placeholder").val("");
		} else {
		}
	}
}

