﻿var iTemp = 0;
var txtMainSearchKeywordName = "txtMainSearchKeyword";
var vFFClick = "";
var vFFMainClick = "";

function getElementByIdFF(obj) {
    if ($(obj).attr('id') != "txtSearch" && $(obj).parents().attr('id') != "") {
        return getElementByIdFF($(obj).parents());
    }

    if ($(obj).attr('id') == "txtSearch")
        return $(obj).attr('id');
}

function getMainElementByIdFF(obj) {
    if ($(obj).attr('id') != "txtMainSearch" && $(obj).parents().attr('id') != "") {
        return getMainElementByIdFF($(obj).parents());
    }

    if ($(obj).attr('id') == "txtMainSearch")
        return $(obj).attr('id');
}

$(document).mousedown(function(e) {
    if ($.browser.mozilla) {
        var vId = $(e.target).parents().attr('id'); //getElementByIdFF($(e.target).parents());

        if (vId != "txtSearch")
            vId = $(e.target).parents().parents().attr('id');
        if (vId != "txtSearch")
            vId = $(e.target).parents().parents().parents().attr('id');
        if (vId != "txtSearch")
            vId = $(e.target).parents().parents().parents().parents().attr('id');

        if (vId != "txtSearch") {
            if (document.getElementById('txtSearch') != null) document.getElementById('txtSearch').style.display = 'none';
        }
        else {
            var vData = $(e.target).text();
            OnItemClick(vData);
        }

        var vMainId = $(e.target).parents().attr('id');

        if (vMainId != "txtMainSearch")
            vMainId = $(e.target).parents().parents().attr('id');
        if (vMainId != "txtMainSearch")
            vMainId = $(e.target).parents().parents().parents().attr('id');
        if (vMainId != "txtMainSearch")
            vMainId = $(e.target).parents().parents().parents().parents().attr('id');

        if (vMainId != "txtMainSearch") {
            document.getElementById('txtMainSearch').style.display = 'none';
        } else {
            var vData = $(e.target).text();
            OnMainItemClick(vData);
        }
    }
    else {
        var vId = findElementId(window.event.srcElement);

        if (vId != "txtSearch") {
            if (document.getElementById('txtSearch') != null) document.getElementById('txtSearch').style.display = 'none';
        }

        var vMainId = findMainElementId(window.event.srcElement);
        if (vMainId != "txtMainSearch") {
            document.getElementById('txtMainSearch').style.display = 'none';
        }
    }
});

$(document).ready(function() {
    /* 설정값 세팅 시작 */
    if (document.getElementById('txtSearch') != null) document.getElementById('txtSearch').style.display = "none";
    if ($("#ctl00_txtMainSearchKeyword").val() != undefined) txtMainSearchKeywordName = "ctl00_txtMainSearchKeyword";

    $("#" + txtMainSearchKeywordName).attr("autocomplete", "off");
    $("#ctl00_txtSearchKeyword").attr("autocomplete", "off");

    if ($.browser.mozilla) {
        $("#" + txtMainSearchKeywordName).keypress(function(e) {
            return MainFFInput();
        });

        $("#" + txtMainSearchKeywordName).focus(function() {
            return MainFFInput();
        });

        $("#ctl00_txtSearchKeyword").keypress(function(e) {
            return FFInput();
        });

        $("#ctl00_txtSearchKeyword").focus(function() {
            return FFInput();
        });
    }
    /* 설정값 세팅 끝 */

    $("#ctl00_ibtnSearch").click(function() {
        if ($("#ctl00_txtSearchKeyword").val() == "") {
            alert("Please, enter a search word.");
            $("#ctl00_txtSearchKeyword").focus();
            return false;
        }
    });

    $("#ctl00_txtSearchKeyword").keypress(function(e) { // enterkey 처리
        if (e.keyCode == 13) {
            $("#ctl00_ibtnSearch").click();
            return false;
        }
    });

    $("#ctl00_txtSearchKeyword").focus(function() { // focus 처리
        if ($("#ctl00_txtSearchKeyword").val() != "" && $("#ctl00_txtSearchKeyword").val() == beforeText) {
            if (vSearchWord.length > 0) {
                document.getElementById('txtSearch').style.display = 'block';
            }
            return;
        }

        if ($("#ctl00_txtSearchKeyword").val() != "" && document.getElementById('txtSearch').style.display != 'block') {
            startRequest("Search");
        }
    })

    $("#ctl00_txtSearchKeyword").keyup(function(e) { // keyup 처리
        switch (e.keyCode) {
            case 37:    // Left 
            case 39:    // Right 
                break;
            case 38:  // Up
                $("#liItem" + vNowPosition).css("background", "");
                if (vNowPosition > -1) {
                    vNowPosition--;
                }

                $("#ctl00_txtSearchKeyword").val(vSearchWord[vNowPosition]);
                $("#liItem" + vNowPosition).css("background", "#fff0eb");
                $("#liItem" + (vNowPosition + 1)).css("background", "");
                break;
            case 40:  // Down
                $("#liItem" + vNowPosition).css("background", "");
                if (vSearchWord.length - 1 > vNowPosition) {
                    vNowPosition++;
                }

                $("#ctl00_txtSearchKeyword").val(vSearchWord[vNowPosition]);
                $("#liItem" + vNowPosition).css("background", "#fff0eb");
                $("#liItem" + (vNowPosition - 1)).css("background", "");
                break;
            default:
                if (e.keyCode == 229) return;
                if ($("#ctl00_txtSearchKeyword").val() == beforeText) return;
                startRequest("Search");
                break;
        }
        beforeText = $("#ctl00_txtSearchKeyword").val();
    });

    /* GNB 검색창 시작 */
    $("#ibtnMainSearch").click(function() {
        if ($("#" + txtMainSearchKeywordName).val() == "") {
            alert("Please, enter a search word.");
            $("#" + txtMainSearchKeywordName).focus();
            return false;
        }

        document.location.href = encodeURI("/search_new/search1.aspx?query=" + $("#" + txtMainSearchKeywordName).val());
        return false;
    });

    $("#" + txtMainSearchKeywordName).keypress(function(e) { // enterkey 처리
        if (e.keyCode == 13) {
            $("#ibtnMainSearch").click();
            return false;
        }
    });

    $("#" + txtMainSearchKeywordName).focus(function() { // focus 처리
        if ($("#" + txtMainSearchKeywordName).val() != "" && $("#" + txtMainSearchKeywordName).val() == MainbeforeText) {
            if (vMainSearchWord.length > 0) {
                document.getElementById('txtMainSearch').style.display = 'block';
            }
            return;
        }
        
        if ($("#" + txtMainSearchKeywordName).val() != "" && document.getElementById('txtMainSearch').style.display == 'none') {
            startRequest("MainSearch");
        }
    })

    $("#" + txtMainSearchKeywordName).keyup(function(e) { // keyup 처리
        switch (e.keyCode) {
            case 37:    // Left 
            case 39:    // Right 
                break;
            case 38:  // Up
                $("#liMaMainItem" + vMainNowPosition).css("background", "");
                if (vMainNowPosition > -1) {
                    vMainNowPosition--;
                }

                $("#" + txtMainSearchKeywordName).val(vMainSearchWord[vMainNowPosition]);
                $("#liMainItem" + vMainNowPosition).css("background", "#fff0eb");
                $("#liMainItem" + (vMainNowPosition + 1)).css("background", "");
                break;
            case 40:  // Down
                $("#liMainItem" + vMainNowPosition).css("background", "");
                if (vMainSearchWord.length - 1 > vMainNowPosition) {
                    vMainNowPosition++;
                }

                $("#" + txtMainSearchKeywordName).val(vMainSearchWord[vMainNowPosition]);
                $("#liMainItem" + vMainNowPosition).css("background", "#fff0eb");
                $("#liMainItem" + (vMainNowPosition - 1)).css("background", "");
                break;
            default:
                if ($("#" + txtMainSearchKeywordName).val() == MainbeforeText) return;

                startRequest("MainSearch");
                break;
        }
        MainbeforeText = $("#" + txtMainSearchKeywordName).val();
    });
    /* GNB 검색창 끝 */
});

function MainFFInput() {
    if (MainbeforeText != $("#" + txtMainSearchKeywordName).val() && vFFMainClick == "") {
        MainbeforeText = $("#" + txtMainSearchKeywordName).val();
        startRequest("MainSearch");
    }
    setTimeout("MainFFInput()", 10);
}

function FFInput() {
    if (beforeText != $("#ctl00_txtSearchKeyword").val() && vFFClick == "") {
        beforeText = $("#ctl00_txtSearchKeyword").val();
        startRequest("Search");
    }
    
    setTimeout("FFInput()", 10);
}

var xmlHttp;

var vSearchType = '';
var vMainNowPosition = -1;
var vMainSearchWord = new Array();
var MainbeforeText = '';

var vNowPosition = -1;
var vSearchWord = new Array();
var beforeText = '';

// XMLHttpRequest 객체를 생성한다.
function createXMLHttpRequest() {
    if (window.ActiveXObject) {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    } else if (window.XMLHttpRequest) {
        xmlHttp = new XMLHttpRequest();
    }
}

// 요청타입 변수를 설정하고, 콜백 함수를 붙이고 요청을 수행한다.
function startRequest(type) {
    try {
        vSearchType = type;

        // 초기화
        if (vSearchType == "MainSearch") {
            //txtSearch.innerHTML = "";
            vMainNowPosition = -1;
            vMainSearchWord = new Array();
        } else if (vSearchType == "Search") {
            vNowPosition = -1;
            vSearchWord = new Array();
        }

        createXMLHttpRequest();
        xmlHttp.onreadystatechange = handleStateChange;
        if (vSearchType == "MainSearch") {
            xmlHttp.open("GET", "/search_new/GetSearchData.aspx?query=" + encodeURI($("#" + txtMainSearchKeywordName).val()), true);
        } else if (vSearchType == "Search") {
            xmlHttp.open("GET", "/search_new/GetSearchData.aspx?query=" + encodeURI($("#ctl00_txtSearchKeyword").val()), true);
        }
        xmlHttp.send(null);
    } catch (e) {
        //alert(e + ":error");
        alert("System Error.");
    }
}

// 응답을 처리하는 콜백함수이다. 요청시 설정했던 요청 타입에 따라 응답을 처리한다.
function handleStateChange() {
    if (xmlHttp.readyState == 4) {
        if (xmlHttp.status == 200) {
            getXMLData();
        }
    }
}

function getXMLData() {
    var xmlDoc = xmlHttp.responseXML;
    var ARKlistNode = xmlDoc.getElementsByTagName("ARKList")[0];

    if (ARKlistNode != null) {
        outXMLData(ARKlistNode.childNodes);
    }
    else {
        if (vSearchType == "MainSearch") {
            document.getElementById('txtMainSearch').style.display = 'none';
        } else if (vSearchType == "Search") {
            document.getElementById('txtSearch').style.display = 'none';
        }
    }
}

function outXMLData(nodes) {
    var vHtml = '';
    var iNum = 0;
    for (var i = 1; i < nodes.length; i++) {
        var vHKeyword = nodes[i].getAttribute("HKeyword");
        var vKeyword = nodes[i].getAttribute("keyword");

//        if (i == 1) {
//            vHtml += "<ul class=\"txtSearch\">";
//        }        
            if (vSearchType == "MainSearch") {
                vHtml += "<LI id='liMainItem" + iNum + "' onmouseover='OnMainItemMouseOver(this, \"" + iNum + "\");' onmouseout='OnMainItemMouseOut(this);' style='cursor:pointer;'  onclick=\"OnMainItemClick('" + vKeyword + "');\"><A href='#'>" + vHKeyword + "</A></LI>";
                vMainSearchWord[iNum] = vKeyword;
            } else if (vSearchType == "Search") {
                vHtml += "<LI id='liItem" + iNum + "' onmouseover='OnItemMouseOver(this, \"" + iNum + "\");' onmouseout='OnItemMouseOut(this);' style='cursor:pointer;'  onclick=\"OnItemClick('" + vKeyword + "');\"><A href='#'>" + vHKeyword + "</A></LI>";
                vSearchWord[iNum] = vKeyword;
            }        

//        if (i == nodes.length-1) {
//            vHtml += "</ul>";
//        }
        
        iNum++;
        if (iNum >= 5) break;
    }
    
    if (vSearchType == "MainSearch") {
        if (iNum == 0) {
            document.getElementById('txtMainSearch').style.display = 'none';
        }
        else if (document.getElementById('txtMainSearch').style.display != 'block') {
            document.getElementById('txtMainSearch').style.display = 'block';
        }

        //txtMainSearch.innerHTML = vHtml;
        $("#txtMainSearch").html(vHtml);
    } else if (vSearchType == "Search") {
        if (iNum == 0) {
            document.getElementById('txtSearch').style.display = 'none';
        }
        else if (document.getElementById('txtSearch').style.display != 'block') {
            //if (iTemp != 0) {
            document.getElementById('txtSearch').style.display = 'block';
            //}
        }

        $("#txtSearch").html(vHtml);
        iTemp++;
        //$('#txtSearch').focus();
    }
}

function OnItemMouseOver(obj, num) {
    $("#liItem" + vNowPosition).css("background", "");
    $("#" + obj.id).css("background", "#fff0eb");
    vNowPosition = num;
}

function OnItemMouseOut(obj) {
    $("#" + obj.id).css("background", "");
}

function OnItemClick(keyword) {
    vFFClick = "true";
    $("#ctl00_txtSearchKeyword").val(keyword);
    $("#ctl00_ibtnSearch").click();
}

function OnMainItemMouseOver(obj, num) {
    $("#liMainItem" + vMainNowPosition).css("background", "");
    $("#" + obj.id).css("background", "#fff0eb");
    vMainNowPosition = num;
}

function OnMainItemMouseOut(obj) {
    $("#" + obj.id).css("background", "");
}

function OnMainItemClick(keyword) {
    vFFMainClick = "true";
    $("#" + txtMainSearchKeywordName).val(keyword);
    $("#ibtnMainSearch").click();
}

function findElementId(obj) {
    if (obj.id != "txtSearch" && obj.parentElement != null) {
        return findElementId(obj.parentElement);
    }

    if (obj.id == "txtSearch")
        return obj.id;
}

function findMainElementId(obj) {
    if (obj.id != "txtMainSearch" && obj.parentElement != null) {
        return findMainElementId(obj.parentElement);
    }

    if (obj.id == "txtMainSearch")
        return obj.id;
}
