﻿/// <reference path="jquery-1.3.2-vsdoc.js" />
/// <reference path="miya_validator.js" />
/// <reference path="Justgo.js" />
/*| Inc/Ctl/UnitySearch.ascx |********************************************************************************/
/// <summary>pnlUnitySearchExpend 키 입력 이벤트</summary>
/// <param name="EvtKeyCode" type="Int">KeyCode of event</param>
/// <returns type="void" />
var KeyWordCursorCnt = -1;
var SelectedCursorKeyWord = "";
function txtUnitySearchWord_onkeyup() {
    
    var rtn = false;
    var SWord = document.getElementById("txtUnitySearchWord"); // Search word control - 검색어 컨트롤
    var SRCnt = document.getElementById("SRCnt"); 				// Search result count control - 전체 검색 결과 수 검색어 컨트롤
    var SRCurPos = document.getElementById("SRCurPos"); 			// Search result current position control - 자동완성 레이어 현재 커서 위치 검색어 컨트롤
    var lblLogs = document.getElementById("UnitySearchFrm_lblLogs");
    var iRtnCnt = (eval(SRCnt) ? SRCnt.value : 0);
    var iSRCurPos = (eval(SRCurPos) ? SRCurPos.value : -1);
    var CurCtl = null;
    var EvtKeyCode = event.keyCode;
    var ListCtl = document.getElementById("pnlUnitySearchWordExpend");
    //	try {
    if ((this.value != undefined) && (this.value != null) && (jQuery(this).val().trim().length > 0)) {

        if (EvtKeyCode == 13) {

            var searchURL = "";
            if (ListCtl.style.display == "block") {
                if (SelectedCursorKeyWord.length > 0) {

                    SWord.value = SelectedCursorKeyWord;
                }
            }
            SearchExpend_onblur();
            document.location.href = "http://www.justgo.kr/UnitySearch/Default.aspx?KeyWord=" + encodeURIComponent(SWord.value);
        }
        else if (EvtKeyCode == 38) { // UP key
            jQuery("#lstAutoComplateWordSR" + KeyWordCursorCnt).removeClass("hover");
            if (KeyWordCursorCnt <= 0) {
                KeyWordCursorCnt = iRtnCnt;
            }
            else {
                KeyWordCursorCnt--;
            }
            CurCtl = jQuery("#lstAutoComplateWordSR" + KeyWordCursorCnt);
            CurCtl.addClass("hover");

            SelectedCursorKeyWord = CurCtl.text();
        }
        else if (EvtKeyCode == 40) { //DOWN Key
            jQuery("#lstAutoComplateWordSR" + KeyWordCursorCnt).removeClass("hover");



            if (KeyWordCursorCnt >= iRtnCnt) {
                KeyWordCursorCnt = 0;
            }
            else {
                KeyWordCursorCnt++;
            }

            CurCtl = jQuery("#lstAutoComplateWordSR" + KeyWordCursorCnt);
            CurCtl.addClass("hover");

            SelectedCursorKeyWord = CurCtl.text();
        }
        else {
            rtn = true;
            if (SWord.value.trim().length > 0) {
                SetAutoComplateLayer();
            }
        }
    }
    //	} catch(e) {
    //		OnErrorHandler(e);
    //	}
    if (!rtn) {
        try {
            event.returnValue = rtn;
        } catch (e) {
        }
    }

    return rtn;
}


function theClick(arg) {

    var SWord = document.getElementById("txtUnitySearchWord");
    SWord.value = arg.innerText;
    AutoCompleteWordOverCheck = "N";
    SearchExpend_onblur();
    document.location.href = "http://www.justgo.kr/UnitySearch/Default.aspx?KeyWord=" + encodeURIComponent(SWord.value);
}

/// <summary>자동완성 검색어 레이어를 설정하고 활성한다.</summary>
/// <returns type="void" />
function SetAutoComplateLayer() {

    var sRtn = "";
    var oParms = "";
    var lblLogs = document.getElementById("UnitySearchFrm_lblLogs");
    var pnlUSWE = document.getElementById("pnlUnitySearchWordExpend");
    var lstACW = document.getElementById("lstAutoComplateWord");
    var SWord = document.getElementById("txtUnitySearchWord"); // Search word control - 검색어 컨트롤
    var SRCnt = document.getElementById("SRCnt"); 				// Search result count control - 전체 검색 결과 수 검색어 컨트롤
    var SRCurPos = document.getElementById("SRCurPos"); 			// Search result current position control - 자동완성 레이어 현재 커서 위치 검색어 컨트롤

    var arrCate;
    var arrCate1;
    var arrCate2;
    var iTotCnt = -1;

    var strTmp = "";

    try {
        SearchExpend_onblur();
        strSearchWord = SWord.value.trim();
        if (strSearchWord.length > 0) { // 입력된 검색어가 1자 이상 이면

            oParms = "KeyWord=" + encodeURIComponent(strSearchWord);

            sRtn = jQuery.ajax({ url: "http://travel.justgo.kr/chAlliance/Justgo/Document/LinkToKeywordDB.ashx", type: 'get', data: oParms, async: false }).responseText;
            
            if (eval(pnlUSWE) && eval(lstACW) && (sRtn.trim().length > 0)) {
                
                lstACW.innerHTML = "";
                arrCate = sRtn.split('∬');

                if ((arrCate != null) && (arrCate != undefined) && (arrCate.length > 0)) {

                    if ((arrCate[0] != null) && (arrCate[0] != undefined) && (arrCate[0].length > 0)) {
                        arrCate1 = arrCate[0].split('∫');
                        for (var i = 0; i < arrCate1.length; i++) {
                            if (arrCate1[i].indexOf(SWord.value.trim()) > -1) {
                                iTotCnt++;
                                strTmp = '<li id="lstAutoComplateWordSR' + iTotCnt + '" onclick="javascript:theClick(this);" onmouseover="javascript:lstAutoComplateWordSR_onmouseover(this);">' + arrCate1[i].replace(SWord.value.trim(), "<strong>" + SWord.value.trim() + "</strong>") + '</li>';
                            }
                            lstACW.innerHTML += strTmp;
                        }
                    }
                    if ((arrCate[1] != null) && (arrCate[1] != undefined) && (arrCate[1].length > 0)) {
                        arrCate2 = arrCate[1].split('∫');
                        for (var j = 0; j < arrCate2.length; j++) {
                            if (arrCate2[j].indexOf(SWord.value.trim()) > -1) {
                                iTotCnt++;
                                if ((j == 0) && (arrCate1 != null) && (arrCate1 != undefined) && (arrCate1.length > 0)) {
                                    strTmp = '<li id="lstAutoComplateWordSR' + iTotCnt + '" class="lstSplitor"  onclick="javascript:theClick(this);" onmouseover="javascript:lstAutoComplateWordSR_onmouseover(this);">' + arrCate2[j].replace(SWord.value.trim(), "<strong>" + SWord.value.trim() + "</b>") + '</li>';
                                } else {
                                    strTmp = '<li id="lstAutoComplateWordSR' + iTotCnt + '"onclick="javascript:theClick(this);" onmouseover="javascript:lstAutoComplateWordSR_onmouseover(this);">' + arrCate2[j].replace(SWord.value.trim(), "<strong>" + SWord.value.trim() + "</b>") + '</li>';
                                }
                            }
                            lstACW.innerHTML += strTmp;
                        }
                    }
                }

                SRCurPos.value = -1;
                SRCnt.value = iTotCnt + 1;

                if (iTotCnt > -1) {
                    pnlUSWE.style.display = "block";
                } else {
                    pnlUSWE.style.display = "none";
                }
            }
        }
    } catch (e) {
        OnErrorHandler(e);
    }

}
function lstAutoComplateWordSR_onmouseover(arg) {
    /*var SWord = document.getElementById("txtUnitySearchWord");	// Search word control - 검색어 컨트롤
    //SWord.value = CurCtl.innerText;
    //alert(arg.innerText);
    if (eval(SWord)) {
    SWord.value = arg.innerText
    }*/
}
function lstAutoComplateWordSR_onmouseout() {
}

/// <summary>pnlUnitySearchExpend 클릭 이벤트</summary>
/// <returns type="Boolean" />
function pnlUnitySearchExpend_onclick() {

    var pnlUSWE = document.getElementById("pnlUnitySearchWordExpend");
    var SRCnt = document.getElementById("SRCnt");
    var SRCurPos = document.getElementById("SRCurPos");

    var RtnCnt = (eval(SRCnt) ? SRCnt.value : 0);
    var SRCurPos = (eval(SRCnt) ? SRCnt.value : -1);

    try {
        if (pnlUSWE.style.display.length < 1) {
            pnlUSWE.style.display = "none";
        }
        //alert("ctl.style.display: " + ctl.style.display);
    } catch (e) {
        OnErrorHandler(e);
    }
    return false;
}
/// <summary>btnUnitySearchExpend 클릭 이벤트</summary>
/// <returns type="Boolean" />
function btnUnitySearchExpend_onclick() {
    var pnlUSWE = document.getElementById("pnlUnitySearchWordExpend");
    //alert(pnlUSWE.style.display);
    if (pnlUSWE.style.display != "block") {
        SetAutoComplateLayer();
    }
    else {
        //alert(pnlUSWE.style.display);
        pnlUSWE.style.display = "none";
    }

    try {
        event.returnValue = false;
    } catch (e) {
    }
    return false;
}


var AutoCompleteWordOverCheck = "N";

function SearchExpend_onblur() {
    var SWord = jQuery('#txtUnitySearchWord');
    if (AutoCompleteWordOverCheck == "Y") {
        jQuery('#pnlUnitySearchWordExpend').show();
    }
    else {
        jQuery('#pnlUnitySearchWordExpend').hide();
    }

    if (SWord.val().trim().length < 1) {
        SWord.val('세상의 모든 여행');
    }
}


function lstAutoComplateWord_onmouseover() {
    AutoCompleteWordOverCheck = "Y";
    //jQuery('#pnlUnitySearchWordExpend').show();
}

function lstAutoComplateWord_onmouseout() {
    AutoCompleteWordOverCheck = "N";
}

function OpenExpDiv() {
    var tDiv = jQuery('#pnlUnitySearchWordExpend');

    if (tDiv.style.display == "none") {
        tDiv.style.display = "blank";
    }
    else {
        tDiv.style.display = "none";
    }
}

function SearchExpend_onfocus() {
    var SWord = jQuery('#txtUnitySearchWord');

    if ((SWord.val().trim().length < 1) || (SWord.val().trim() == "세상의 모든 여행")) {
        SWord.val("");
    }
}
jQuery(document).ready(function() {
    jQuery('#txtUnitySearchWord').focus(SearchExpend_onfocus);
    jQuery('#txtUnitySearchWord').blur(SearchExpend_onblur);
    jQuery('#txtUnitySearchWord').keyup(txtUnitySearchWord_onkeyup);
});
/************************************************************************************************************/
