﻿
var suggestionServerURL = "/spot/tagSearch.aspx";
var req; var isIE = false;
var lastQry = "";
var firstQry = "";
var searchTxt = "";

var move = function(e) {
    var elTarget = YAHOO.util.Event.getTarget(e).id;
    event_target = elTarget.toString();
    var clickString = event_target.indexOf("INNERBOX");
    var Form = document.forms[0];
    var qryString = event_target.indexOf(getElement(Form, "qry"));
    var L = document.getElementById("OUTERBOX");
    var grps = document.getElementById("INNERBOX").getElementsByTagName("li");

    if (qryString != 0 && grps.length > 1 && firstQry != lastQry) {
        if (L.style.display == "block") {
            showSuggestions(false);
        } else {
            showSuggestions(true);
        }
    }else if (clickString != 0) {
        L.style.display = 'none';
    }
};

///검색창 이외의 곳 클릭시 Layer none
YAHOO.util.Event.on(document, "click", move);

function searchOnload() {
    if (window.XMLHttpRequest) {
        //FF 계열, IE7 을 위한 XMLHTTP 생성
        req = new XMLHttpRequest();
    }
    else {
        //IE6 이하를 위한 XMLHTTP 생성
        req = new ActiveXObject("Microsoft.XMLHTTP");
    }

    //브라우저의 자동완성기능을 OFF
    var Form = document.forms[0];
    getElement(Form, "qry").setAttribute("autocomplete", "off");

    //파이어폭스이면 한글입력때문에 감시함수를 활성화함.
    if (navigator.appName.indexOf("Microsoft") == -1)
        window.setTimeout("checkQryFF()", 500);

}

function checkQryFF() {
    //파이어폭스에서는 한글을 입력할때 onKeyUp 이벤트가 발생하지 않기때문에
    //짧은 주기로 계속 감시를 해야함.
    var Form = document.forms[0];
    var qry = getElement(Form, "qry").value;
    window.setTimeout("checkQryFF()", 500);

    if (qry.length > 0 && qry != lastQry) {
        reqSuggestion();
    }

}

function reqSuggestion() {
    var Form = document.forms[0];
    var qry = getElement(Form, "qry").value;
    if (qry.trim().length > 0 && qry != lastQry) {
        if (req && qry != "") {
            var sugUrl = suggestionServerURL + "?q=" + escape(qry);
            req.open("GET", sugUrl, true);
            req.onreadystatechange = function() {
                if (req.readyState == 4) {
                    if (req.status == 200) {
                        //alert(req.responseText);
                        handleResponse(req);
                    }
                }
            };

            req.send(null);
            lastQry = qry;
            firstQry = qry;
        }
    }
    else if (qry == "") {
        lastQry = qry;
        firstQry = qry;
        showSuggestions(false);
    }
}


function handleResponse(req) {
    var res = eval("(" + req.responseText + ")");
    if (res.Suggestions.length > 0) {
        removeAllChildren(document.getElementById("INNERBOX"));
        for (i = 0; i < res.Suggestions.length; i++) {
            var tagLi = document.createElement("li");
            var aLi = document.createElement("a");
            var txt = document.createTextNode(res.Suggestions[i]);
            aLi.setAttribute("href", "/spot/search.aspx?tag=" + res.Suggestions[i]);
            tagLi.name = "tagLi";
            tagLi.appendChild(txt);
            aLi.appendChild(tagLi);
                        
            addEvent(tagLi, "mouseover", changeaOverColor);
            addEvent(tagLi, "mouseout", changeaOutColor);
           // addEvent(tagLi, "click", tagLayerClick);
            document.getElementById("INNERBOX").appendChild(aLi);
        }
        showSuggestions(true);
    }
    else {
        showSuggestions(false);
    }

}
function removeAllChildren(node) {
    if (node == null) return;
    while (node.hasChildNodes()) {
        node.removeChild(node.firstChild);
    }
}

function showSuggestions(show) {
    if (show) {
        document.getElementById("OUTERBOX").style.display = "block";
    } else {
        document.getElementById("OUTERBOX").style.display = "none";
    }
}

function txtClcik(event) {
    if (document.getElementById("OUTERBOX").style.display == "block") {
        showSuggestions(false);
    } else {
        showSuggestions(true);
    }    
}

function changeaOverColor(event) {
    var obj = null;
    if (getBrowser() == BROWSER_IE) {
        obj = event.srcElement;
    } else {
        obj = event.target;
    }

    obj.className = "bgColor";
//    getElement(Form, "qry").value = getInnerText(obj);
//    lastQry = getElement(Form, "qry").value;
}

function changeaOutColor(event) {
    var obj = null;
    if (getBrowser() == BROWSER_IE) {
        obj = event.srcElement;
    } else {
        obj = event.target;
    }

    obj.className = "";
}

function getInnerText(obj) {
    if (getBrowser() == BROWSER_FIREFOX) {
        return obj.textContent;
    } else {
        return obj.innerText;
    }
}

function chageTagPlus(event) {
    var obj = null;
    Form = document.forms[0];
    if (getBrowser() == BROWSER_IE) {
        obj = event.srcElement;
    } else {
        obj = event.target;
    }

    var color = true;
    var KeyResult = event.which;
    if (typeof (KeyResult) == "undefined") {
        KeyResult = event.keyCode
    }

    var grps = document.getElementById("INNERBOX").getElementsByTagName("li");

    if (KeyResult == 13) {
        //liClick();
    } else if (grps.length > 0 && (KeyResult == 38 || KeyResult == 40 || KeyResult == 13)) {
        showSuggestions(true);
        for (i = 0; i < grps.length; i++) {
            var grp = grps[i];

            if (grp.className.match("bgColor")) {
                if (KeyResult == 38) {

                    if (grps[i - 1] != null) {
                        grps[i - 1].className = "bgColor";
                        color = false;
                        grp.className = "";

                        getElement(Form, "qry").value = getInnerText(grps[i - 1]);

                        lastQry = getElement(Form, "qry").value;
                        break;
                    }
                    else {
                        color = false;
                        grp.className = "";
                        getElement(Form, "qry").value = firstQry;
                        lastQry = getElement(Form, "qry").value;
                        showSuggestions(false);
                    }
                } else if (KeyResult == 40 || KeyResult == 13) {
                    if (grps[i + 1] != null) {
                        grps[i + 1].className = "bgColor";
                        color = false;
                        grp.className = "";
                        getElement(Form, "qry").value = getInnerText(grps[i + 1]);
                        lastQry = getElement(Form, "qry").value;
                        break;
                    } else {
                        color = false;
                    }
                }
            }
        }
        
        if (color == true && grps.length > 0) {
            if (grps.length == 1)
                grps.className = "bgColor";
            else
                grps[0].className = "bgColor";

            getElement(Form, "qry").value = getInnerText(document.getElementById("INNERBOX").firstChild);

            lastQry = getElement(Form, "qry").value;

        }
        
        getElement(Form, "qry").focus();
        
    } else {
        reqSuggestion();
    }
}

function liClick() {
    var qry = escape(getElement(Form, "qry").value);
    if ((req && qry != "") && (searchTxt != qry)) {
        var sugUrl = "/spot/tagHitUpdate.aspx?q=" + qry;
        req.open("GET", sugUrl, true);
        req.onreadystatechange = function() {
            if (req.readyState == 4) {
                if (req.status == 200) {
                    searchTxt = qry;
                    HitResponse(req);
                }
            }
        };

        req.send(null);
        lastQry = qry;
    }
}

function HitResponse(req) {
    var res = req.responseText;
    location.href = "/spot/search.aspx?tag=" + escape(res);
}
