var is_result = 0;
var is_total = 0;
var now_ul = 0;
var now_start = 0;
var is_val = "";
var is_val2 = "";

var one_line = 18;
var line_cnt = 15;

function searchResult_keyup(e)
{
	var event = e || window.event; 

	var val2 = find_radio_value('top_sca');

	if(event.keyCode==13) {
		if(is_total == 0 || now_ul == 0){
			top_search_submit();
		}else{
			searchThis(document.getElementById('s_rslt_' + now_ul).val);
		}
	
	}else if(event.keyCode==40){
		//clearTimeout(timerid_up);
	}else if(event.keyCode==38){
		//clearTimeout(timerid_down);
	}else{
		if(document.getElementById('top_stx').value != is_val || val2 != is_val2){
			now_start = 0;
			is_val = document.getElementById('top_stx').value;
			is_val2 = val2;
			ajax_start('searchResult', 'searchResult.php', document.getElementById('top_stx').value, val2, now_start, 0);
		}
	}
}

function searchResult_keyup_bak(e)
{
	clearTimeout(timerid_up);
	clearTimeout(timerid_down);

	var event = e || window.event; 

	var val2 = find_radio_value('top_sca');

	if(event.keyCode==13) {
		if(is_total == 0 || now_ul == 0){
			top_search_submit();
		}else{
			searchThis(document.getElementById('s_rslt_' + now_ul).val);
		}
	
	}else if(event.keyCode==40){
		if(is_result && is_total){
			var new_now_ul = (now_ul + 1 > is_total) ? is_total : now_ul + 1;

			if(new_now_ul != now_ul){
				/*
				for(i=1;i<=is_total;i++){
					var ob_ul = document.getElementById('s_rslt_' + i);
					ob_ul.className = "back_white";
				}
				*/
				if(now_ul > 0){
					document.getElementById('s_rslt_' + now_ul).className = "back_white";
				}
				now_ul = new_now_ul;
				document.getElementById('s_rslt_' + now_ul).className = "back_yellow";

				if(now_ul > line_cnt) document.getElementById('searchResultArea').scrollTop = (one_line * (now_ul - line_cnt));
				else document.getElementById('searchResultArea').scrollTop = 0;
			}
		}

		/*
		if(is_result < is_total){
			now_start = now_start + 1;
			if(document.getElementById('top_stx').value){
				ajax_start('searchResult', 'searchResult.php', document.getElementById('top_stx').value, val2, now_start, 1);
			}
		}
		*/
	}else if(event.keyCode==38){
		if(is_result && is_total && now_ul){
			var new_now_ul = (now_ul - 1 < 1) ? 1 : now_ul - 1;

			if(new_now_ul != now_ul){
				/*
				for(i=1;i<=is_total;i++){
					var ob_ul = document.getElementById('s_rslt_' + i);
					ob_ul.className = "back_white";
				}
				*/
				if(now_ul > 0){
					document.getElementById('s_rslt_' + now_ul).className = "back_white";
				}
				now_ul = new_now_ul;
				document.getElementById('s_rslt_' + now_ul).className = "back_yellow";

				if(now_ul > line_cnt) document.getElementById('searchResultArea').scrollTop = (one_line * (now_ul - line_cnt));
				else document.getElementById('searchResultArea').scrollTop = 0;

			}
		}
	}else{
		if(document.getElementById('top_stx').value != is_val || val2 != is_val2){
			now_start = 0;
			is_val = document.getElementById('top_stx').value;
			is_val2 = val2;
			ajax_start('searchResult', 'searchResult.php', document.getElementById('top_stx').value, val2, now_start, 0);
		}
	}
}

function searchResult_keydown(e)
{
	var event = e || window.event; 

	var val2 = find_radio_value('top_sca');

	if(event.keyCode==40){
		scroll_up();

	}else if(event.keyCode==38){
		scroll_down();
	}
}

function scroll_up()
{
		if(is_result && is_total){
			var new_now_ul = (now_ul + 1 > is_total) ? is_total : now_ul + 1;

			if(new_now_ul != now_ul){
				/*
				for(i=1;i<=is_total;i++){
					var ob_ul = document.getElementById('s_rslt_' + i);
					ob_ul.className = "back_white";
				}
				*/
				if(now_ul > 0){
					document.getElementById('s_rslt_' + now_ul).className = "back_white";
				}
				now_ul = new_now_ul;
				document.getElementById('s_rslt_' + now_ul).className = "back_yellow";

				if(now_ul > line_cnt) document.getElementById('searchResultArea').scrollTop = (one_line * (now_ul - line_cnt));
				else document.getElementById('searchResultArea').scrollTop = 0;
			}
		}

		//timerid_up = setTimeout("scroll_up()",1000);
}

function scroll_down()
{
		if(is_result && is_total && now_ul){
			var new_now_ul = (now_ul - 1 < 1) ? 1 : now_ul - 1;

			if(new_now_ul != now_ul){
				/*
				for(i=1;i<=is_total;i++){
					var ob_ul = document.getElementById('s_rslt_' + i);
					ob_ul.className = "back_white";
				}
				*/
				if(now_ul > 0){
					document.getElementById('s_rslt_' + now_ul).className = "back_white";
				}
				now_ul = new_now_ul;
				document.getElementById('s_rslt_' + now_ul).className = "back_yellow";

				if(now_ul > line_cnt) document.getElementById('searchResultArea').scrollTop = (one_line * (now_ul - line_cnt));
				else document.getElementById('searchResultArea').scrollTop = 0;

			}
		}

		//timerid_down = setTimeout("scroll_down()",1000);
}

function change_sel(arg)
{
	if(now_ul > 0){
		document.getElementById('s_rslt_' + now_ul).className = "back_white";
	}
	now_ul = arg;
	document.getElementById('s_rslt_' + now_ul).className = "back_yellow";

}
function searchResult_wheel()
{
	var scr_top = get_int(document.getElementById('searchResultArea').scrollTop + "px");
	//alert(scr_top);
	var bottom_ul = (scr_top == 0) ? line_cnt  : (Math.ceil((scr_top + 5) / one_line) + 1) + line_cnt;

	if(now_ul > 0){
		document.getElementById('s_rslt_' + now_ul).className = "back_white";
	}
	if(bottom_ul > is_total) bottom_ul = is_total;
	now_ul = bottom_ul;
	document.getElementById('s_rslt_' + now_ul).className = "back_yellow";
}

function searchResult2_keyup(e, val, obj)
{
	var event = e || window.event; 
	if(event.keyCode==13) {
		var disp_obj = document.getElementById('searchResultArea');
		document.getElementById('top_stx').value = val;
		is_result = 0;
		disp_obj.innerHTML = "";
		disp_obj.style.visibility = "hidden";
		document.getElementById('top_stx').focus();
	}
}

function searchThis(val)
{
		var disp_obj = document.getElementById('searchResultArea');
		document.getElementById('top_stx').value = val;
		is_result = 0;
		is_total = 0;
		now_ul = 0;
		disp_obj.innerHTML = "";
		disp_obj.style.visibility = "hidden";
		//document.getElementById('top_stx').focus();
		top_search_submit();
}

function top_search_submit()
{
	var f = document.fsearchTop;
	var err = 0;

	if(!f.top_stx.value){
		err = 1;
		alert("검색어를 입력해 주세요.");
		f.top_stx.focus();
	}

	if(err == 0){
		f.action = "/search/search.php";
		f.submit();
	}
}
