﻿function DoSearch() {
    var searchText = document.getElementById('epgSearchText').value;
    var epgSearch = document.getElementById('EpgSearch');
    var epgMatrix = document.getElementById('EpgMatrix');
    epgSearch.innerHTML = searchText;
    epgSearch.style.display = 'block';
    epgMatrix.style.display = 'none';
}

function EpgAutoSearchBox(inputID, boxID, scriptURL, autoSubmit, submitURL) {
    var input = document.getElementById(inputID);
    var box = document.getElementById(boxID);
    var boxVisible = false;
    var boxItemIndex = -1;
    var boxItems = new Array();
    var boxNodes = new Array();
    var changeTimeout = 300;
    var changeTimeoutID = -1;

    var globalItems;

    var value;
    var autoCompleteLoader;
    var cssBase = "";
    var events = new Object();
    var hasInputFocus = false;
    var boxOver = false;

    if (box && input) {
        Penki_HookEvent(input, "keypress", onKeyPress);
        if (scriptURL) {
            Penki_HookEvent(input, "keydown", onKeyDown);
            Penki_HookEvent(input, "keyup", onKeyUp);
            Penki_HookEvent(input, "blur", onBlur);
            Penki_HookEvent(input, "focus", onFocus);
            Penki_HookEvent(box, "mouseover", onBoxOver);
            Penki_HookEvent(box, "mouseout", onBoxOut);
            value = input.value;
            if (input.className) {
                cssBase = input.className;
            }
            else {
                cssBase = "InputText"
            }
            box.className = cssBase + '_autoComplete_Box';
        }
    }

    function onBoxOver() {
        boxOver = true;
    }

    function onBoxOut() {
        boxOver = false;
    }


    this.addEvent = function(name, callBack) {
        if (!events[name]) {
            var array = new Array();
            events[name] = array;
            array.push(callBack);
        }
        else {
            this.events[name].push(callBack);
        }
    }

    this.removeEvent = function(name, callBack) {
        if (events[name]) {
            var array = events[name];
            var i = 0;
            for (i = 0; i < array.length; i++) {
                if (array[i] == callBack) {
                    break;
                }
            }
            if (i < array.length) {
                array.splice(i, 1);
                if (array.length == 0) {
                    events[name] = null;
                    delete events[name];
                }
            }
        }
    }
    function raiseEvent(name) {
        var array = events[name];
        var v = true;
        if (array) {
            var i;
            for (i = 0; i < array.length; i++) {
                try {
                    var ev = array[i]();
                    if (ev == false) {
                        v = false;
                    }
                }
                catch (e) { }
            }
        }
        return v;
    }

    this.getValue = function() {
        return input.value.Trim() + '';
    }

    this.clearValue = function() {
        blurCurrentItem();
        input.value = '';
        fillBoxItems(new Array());
        value = '';
        hideBox()
    }

    function onSubmit() {
        return raiseEvent('submit');
    }


    function onFocus() {
        hasInputFocus = true;
        if (boxItems.length > 0) {
            box.style.display = 'block';
            boxVisible = true;
        }
    }

    function onBlur() {
        hasInputFocus = false;
        if (!boxOver) {
            hideBox();
        }
    }


    function onKeyPress(e) {
        if (e && e.keyCode) {
            switch (e.keyCode) {
                case 13:
                    if (autoSubmit) {
                        if ((submitURL + '') != '') {
                            if (onSubmit()) {
                                input.form.action = submitURL;
                                input.form.submit();
                            }
                            cancelEvent(e);
                        }
                        else {
                            if (!onSubmit()) {
                                cancelEvent(e);
                            }
                        }
                    }
                    else {
                        onSubmit()
                        cancelEvent(e);
                    }
                    break;
                default:
                    break;
            }
        }
    }


    function onKeyUp(e) {
        if (e && e.keyCode) {
            switch (e.keyCode) {
                case 38:
                    break;
                case 40:
                    break;
                default:
                    var newValue = input.value.Trim();
                    if ((newValue + '') != (value + '')) {
                        valueChanged(newValue)
                    }
                    break;
            }
        }
    }

    function onKeyDown(e) {
        if (e && e.keyCode) {
            switch (e.keyCode) {
                case 38:
                    goUP();
                    break;
                case 40:
                    goDOWN();
                    break;
            }
        }
    }

    function goUP() {
        if (boxVisible && boxItems.length > 0) {
            var i = boxItemIndex;
            blurCurrentItem();
            if (i == 0) {
                boxItemIndex = -1;
                input.value = value;
            }
            else {
                if (i == -1) {
                    boxItemIndex = boxItems.length - 1;
                }
                else {
                    boxItemIndex = i - 1;
                }
                input.value = boxItems[boxItemIndex].Value;
                focusCurrentItem();
            }
        }
    }

    function goDOWN() {
        if (boxVisible && boxItems.length > 0) {
            var i = boxItemIndex;
            blurCurrentItem();
            if (i == boxItems.length - 1) {
                boxItemIndex = -1;
                input.value = value;
            }
            else {
                boxItemIndex = i + 1;
                input.value = boxItems[boxItemIndex].Value;
                focusCurrentItem();
            }
        }
    }


    function blurCurrentItem() {
        if (boxItemIndex > -1) {
            if (cssBase) {
                boxNodes[boxItemIndex].className = cssBase + '_autoComplete_item';
            }
            else {
                boxNodes[boxItemIndex].style.fontWeight = 'normal';
            }
            boxItemIndex = -1;
        }
    }
    function focusCurrentItem() {
        if (boxItemIndex > -1) {
            if (cssBase) {
                boxNodes[boxItemIndex].className = cssBase + '_autoComplete_item_over';
            }
            else {
                boxNodes[boxItemIndex].style.fontWeight = 'bold';
            }
        }
    }

    function valueChanged(newValue) {
        if ((newValue + '') != (value + '') && (scriptURL + '') != null) {
            //alert("new: [" + newValue + "]\nold: [" + value + "]" )
            value = newValue;
            hideBox();
            cancelLoad();
            clearChangedTimeout();
            changeTimeoutID = window.setTimeout(valueChangedTimeout, changeTimeout)
        }
    }
    function valueChangedTimeout() {
        changeTimeoutID = -1;
        cancelLoad();
        if (value == "") {
            hideBox()
        }
        else {
            autoCompleteLoader = new Penki_SctiptLoader(input.parentNode, scriptURL + (scriptURL.indexOf('?') > 0 ? "&" : "?") + "value=" + escape(value) + "&naming=" + escape(input.id) + "_Items", onScriptLoad, value, true);
        }
    }

    function onScriptLoad(loader) {
        boxItemIndex = -1;
        if (loader.getTag() == value) {
            var items = null;
            try {
                items = window[input.id + "_Items"];
            }
            catch (e)
                { items = new Array(); }
            if (!items) {
                items = new Array();
            }
            globalItems = items;

            //alert("loaded '" + value + "' in " + (endDate - startDate));            

            //items.push(new AutoCompleteItem("loaded '" + value + "' in", loader.GetDuration()));

            fillBoxItems(items);
            if (boxItems.length > 0) {
                if (hasInputFocus) {
                    box.style.display = 'block';
                    boxVisible = true;
                }
                var i;
                var handler;
                for (i = 0; i < boxNodes.length; i++) {
                    handler = new itemHandler(i);
                    Penki_HookEvent(boxNodes[i], "mouseover", handler.Over);
                    Penki_HookEvent(boxNodes[i], "click", handler.Click);
                }
            }
            else {
                hideBox();
            }
        }
        else {
            hideBox();
        }


        function itemHandler(i) {
            this.Over = function() { ItemOver(i) };
            this.Click = function() { ItemClick(i) };
        }
    }

    function ItemOver(index) {
        if (index > -1) {
            blurCurrentItem();
            boxItemIndex = index;
            focusCurrentItem();
        }

    }

    function ItemClick(index) {
        if (index > -1) {
            input.value = boxItems[index].Value
            boxOver = false;

            if (onSubmit()) {
                window.location = "EpgSearch.aspx?Find=" + escape(globalItems[index].Value);
            }
            hideBox();
        }
    }


    function fillBoxItems(items) {
        delete boxItems;
        boxItems = new Array();
        delete boxNodes;
        boxNodes = new Array();

        while (box.firstChild) {
            box.removeChild(box.firstChild);
        }
        var div;
        var table;
        var tbody;
        var tr;
        var td;
        var textNode;
        var i;
        for (i = 0; i < items.length; i++) {
            if (items[i].Value) {
                div = document.createElement("div")
                box.appendChild(div);
                table = document.createElement("table")
                table.cellPadding = "4";
                table.cellSpacing = "0";
                table.width = "100%";
                div.appendChild(table);
                tbody = document.createElement("tbody")
                table.appendChild(tbody);
                tr = document.createElement("tr");
                tbody.appendChild(tr);
                td = document.createElement("td");
                tr.appendChild(td);
                textNode = document.createTextNode(items[i].Value + '');
                td.appendChild(textNode);

                td = document.createElement("td");
                tr.appendChild(td);
                td.align = "right";
                if (items[i].Count) {
                    textNode = document.createTextNode(items[i].Count + '');
                    td.appendChild(textNode);
                }
                if (cssBase) {
                    div.className = cssBase + '_autoComplete_item';
                }
                div.style.cursor = 'pointer';

                boxNodes.push(div);
                boxItems.push(items[i]);
            }
        }
    }

    function clearChangedTimeout() {
        if (changeTimeoutID > -1) {
            window.clearTimeout(changeTimeoutID);
        }
        changeTimeoutID = -1;
    }
    function hideBox() {
        if (boxVisible) {
            blurCurrentItem();
            boxVisible = false;
            boxOver = false;
            box.style.display = 'none';
        }
    }
    function cancelEvent(e) {
        if (e) {
            if (e.preventDefault) {
                e.preventDefault(); // Mozilla and Opera
            }
            else {
                e.cancelBubble = true; // IE and Opera
                e.returnValue = false; // IE and Opera
            }
        }
    }
    function cancelLoad() {
        if (autoCompleteLoader) {
            autoCompleteLoader.Dispose();
            delete autoCompleteLoader;
        }
    }
}
