// Suggest Start
$(function() {
	
	
	$("#keywords").bind("keyup focus", function() {
		if ($("#keywords").val() != "") {
			$.post('/Product/suggest', {
				title: $("#keywords").val()
			}, function(rs) {
				var ssA = document.getElementById('suggestA');
				var ss = document.getElementById('suggest');
				var ssC = document.getElementById('suggestC');
				if (rs.length > 0) {
					ssA.style.display = "block";
					ss.style.display = "block";
					ssC.style.display = "block";
					ssA.style.cursor = "pointer";

					// 受け取った文字列を配列に代入
					var sug_arr = new Array();
					sug_arr = rs.split("\t");
					ss.innerHTML = '';

					if (sug_arr.length > 0) {
						if (sug_arr.length == 1 && (delSpace(sug_arr[0]) == "" || delSpace(sug_arr[0]) == null)) {
							ss.innerHTML = '';
							ssA.style.display = "none";
							ss.style.display = "none";
							ssC.style.display = "none";
						} else {
							// 最大個数の定義（多すぎるとブラウザがハング）
							var cnt = 0;
							var max_cnt = 30;		// サジェスト候補の最大値
							if (sug_arr.length > max_cnt) {
								cnt = max_cnt;
							} else {
								cnt = sug_arr.length;
							}

							for(var i=0;i<cnt;i++) {
								if (delSpace(sug_arr[i]) != "") {
									var tmp_str = '<div onmouseover="javascript:suggestOver(this);" ';
									tmp_str += 'onmouseout="javascript:suggestOut(this);" ';
									tmp_str += 'onclick="javascript:setValue(this.innerHTML);" ';
									tmp_str += 'class="suggest_link">' + delSpace(sug_arr[i]) + '</div>';
									ss.innerHTML += tmp_str;
								}
							}
						}
					} else {
						ss.innerHTML = '';
						ssA.style.display = "none";
						ss.style.display = "none";
						ssC.style.display = "none";
					}
				} else {
					ss.innerHTML = '';
					ssA.style.display = "none";
					ss.style.display = "none";
					ssC.style.display = "none";
				}
			});
		}
	});
	
	//フォーカスが外れたら、プルダウンを消す
	/*
	$("fieldset").blur(function(){
		suggestClose();
	});
	*/
});


// Close function
function suggestClose() {
	document.getElementById("suggest").innerHTML = '';
	document.getElementById("suggestA").style.display = "none";
	document.getElementById("suggest").style.display = "none";
	document.getElementById("suggestC").style.display = "none";
}


// Mouse over function
function suggestOver(div_value) {
	div_value.className = 'suggest_link_over';
	div_value.style.backgroundColor = "#69c";
}
// Mouse out function
function suggestOut(div_value) {
	div_value.className = 'suggest_link';
	div_value.style.backgroundColor = "#fff";
}

// Click function
function setValue(value) {
	document.getElementById("keywords").value = delSpace(value);
	document.getElementById("suggest").innerHTML = '';
	document.getElementById("suggestA").style.display = "none";
	document.getElementById("suggest").style.display = "none";
}

// 文字列のエンコード
function encodeURL(str) {
	var character = '';
	var unicode = '';
	var string = '';
	var i = 0;

	for (i = 0; i < str.length; i++) {
		character = str.charAt(i);
		unicode = str.charCodeAt(i);

		if (character == ' ') {
			string += '+';
		} else {
			if (unicode == 0x2a || unicode == 0x2d || unicode == 0x2e || unicode == 0x5f || ((unicode >= 0x30) && (unicode <= 0x39)) || ((unicode >= 0x41) && (unicode <= 0x5a)) || ((unicode >= 0x61) && (unicode <= 0x7a))) {
				string = string + character;
			} else {
				if ((unicode >= 0x0) && (unicode <= 0x7f)) {
					character   = '0' + unicode.toString(16);
					string += '%' + character.substr(character.length - 2);
				} else if (unicode > 0x1fffff) {
					string += '%' + (0xf0 + ((unicode & 0x1c0000) >> 18)).toString(16);
					string += '%' + (0x80 + ((unicode & 0x3f000) >> 12)).toString(16);
					string += '%' + (0x80 + ((unicode & 0xfc0) >> 6)).toString(16);
					string += '%' + (0x80 + (unicode & 0x3f)).toString(16);
				} else if (unicode > 0x7ff) {
					string += '%' + (0xe0 + ((unicode & 0xf000) >> 12)).toString(16);
					string += '%' + (0x80 + ((unicode & 0xfc0) >> 6)).toString(16);
					string += '%' + (0x80 + (unicode & 0x3f)).toString(16);
				} else {
					string += '%' + (0xc0 + ((unicode & 0x7c0) >> 6)).toString(16);
					string += '%' + (0x80 + (unicode & 0x3f)).toString(16);
				}
			}
		}
	}

	return string;
}

// 前後のスペースを削除
function delSpace(p_val){
	var flg = 1;

	// 先頭のスペースを取る
	for(i=0; i<p_val.length; i++){
		if((p_val.substring(i, i+1) != ' ') && (p_val.substring(i, i+1) != '　') && (p_val.substring(i, i+1) != "\n") && (p_val.substring(i, i+1) != "\r")) {
			p_val = p_val.substring(i, p_val.length+1);
			flg = 0; break;
		}
	}

	// 末尾のスペースを取る
	for(i=p_val.length-1; i>=0; i--){
		if((p_val.substring(i, i+1) != ' ') && (p_val.substring(i, i+1) != '　') && (p_val.substring(i, i+1) != "\n") && (p_val.substring(i, i+1) != "\r")) {
			p_val = p_val.substring(0, i+1);
			flg = 0; break;
		}
	}

	// すべてスペースの場合はクリア
	if(flg){ p_val = ''; }

	return(p_val);
}
