﻿function ACComponent(inputId, popupId, beginChar, enablemove, searchFunc, renderFunc, enterRowFunc, selectRowFunc, showPopupFunc)
{
    this.termStartPos = -1; 
    this.termPrefix = '';
    this.queryType = '';
    this.rangeObj = null;
    this.selectedRowIndex = -1;
    this.mouseIn = false;
    this.inputControl = dojo.byId(inputId);
    this.popupControl = dojo.byId(popupId);
    this.beginChar = beginChar;
    this.enablemove = enablemove;
    this.termEndPos = -1;
    //dojo.event.connect(this.inputControl, "onkeyup", function(){return function(){this.onInputControlKeyUp(event);}}());
    dojo.event.connect(this.inputControl, "onkeyup", this, 'onInputControlKeyUp');
    dojo.event.connect(this.inputControl, "onblur", this, 'onInputControlBlur');
    if(renderFunc != null)
        this.renderFunction = renderFunc;
    else
        this.renderFunction = this.doRender;
    if(enterRowFunc != null)
        this.enterRowFunction = enterRowFunc;
    else
        this.enterRowFunction = this.doEnterRow;
    if(selectRowFunc != null)
        this.selectRowFunction = selectRowFunc;
    else
        this.selectRowFunction = this.doSelectRow;
    if(showPopupFunc != null)
        this.showPopupFunction = showPopupFunc;
    else
        this.showPopupFunction = this.showPopup;
    if(searchFunc != null)
        this.doSearch = searchFunc;
}

ACComponent.prototype.onInputControlBlur = function(evt)
{
    evt = evt ? evt : (window.event ? window.event : null);
    if(!mouseInDiv)
        this.hidePopup();
}

ACComponent.prototype.onInputControlKeyUp = function(evt)
{
    evt = evt ? evt : (window.event ? window.event : null);
    var queryterm = this.inputControl.value;
    var KEYCODE_SPACE = 32;
    var KEYCODE_ENTER = 13;
    var KEYCODE_ESCAPE = 27;
    var KEYCODE_DOWN = 40;
    var KEYCODE_UP= 38;
    var KEYCODE_RIGHT = 39;
    var KEYCODE_LEFT = 37;
    switch(evt.keyCode)
    {
//        case this.beginKeycode:
//            {
//                for(var i = queryterm.length - 1; i >= 0; i--)
//                {
//                    if(queryterm.charAt(i) == ' ' || i == 0)
//                    {
//                        this.queryType = queryterm.substring(i == 0 ? 0 : i + 1, queryterm.length - 1).toLowerCase();
//                        break;
//                    }
//                }
//                if(checkQueryType(this.queryType))
//                {
//                    this.termStartPos = queryterm.length;
//                    if (this.enablemove && document.selection && document.selection.createRange) 
//    	                this.rangeobj = document.selection.createRange();
//                }
//            }
//            break;
        case KEYCODE_LEFT:
            break;
        case KEYCODE_ENTER:
            var isselect = (this.popupControl.style.visibility == 'visible');
            this.hidePopup();
            if(isselect)
            {
                var table = this.popupControl.getElementsByTagName('table')[0];
                if(this.selectedRowIndex != -1 && table != null)
                    this.selectRowFunction(table.rows[this.selectedRowIndex]);
            }
            else
                this.doSearch();
            break;
        case KEYCODE_ESCAPE:
            this.hidePopup();
            break;
        case KEYCODE_DOWN:
            var table = this.popupControl.getElementsByTagName('table')[0];
            if(table == 0 || table.rows.length == 0)
                return;
            if(this.selectedRowIndex == table.rows.length - 1)
            {
                this.enterRowFunction(table.rows[0], this.selectedRowIndex);
                this.selectedRowIndex = 0;
            }
            else
            {
                this.enterRowFunction(table.rows[this.selectedRowIndex + 1], this.selectedRowIndex);
                this.selectedRowIndex++;
            }
            break;
        case KEYCODE_UP:
            var table = this.popupControl.getElementsByTagName('table')[0];
            if(table == 0 || table.rows.length == 0)
                return;
            if(this.selectedRowIndex <= 0)
            {
                this.enterRowFunction(table.rows[table.rows.length - 1], this.selectedRowIndex);
                this.selectedRowIndex = table.rows.length - 1;
            }
            else
            {
                this.enterRowFunction(table.rows[this.selectedRowIndex - 1], this.selectedRowIndex);
                this.selectedRowIndex--;
            }
            break;
        case KEYCODE_RIGHT:
            if(this.termStartPos != -1 && this.selectedRowIndex >= 0 && this.popupControl.style.visibility == 'visible')
            {
                var selectedvalue = dojo.dom.textContent(this.popupControl.getElementsByTagName('table')[0].rows[this.selectedRowIndex].getElementsByTagName('span')[0]);
                queryterm = this.inputControl.value.substring(0, this.termStartPos) + selectedvalue;
                this.inputControl.value = queryterm;
            }
            else
                break;
        default:
            {
                if(navigator.appName == "Microsoft Internet Explorer"){
                if(this.beginChar == '' || this.beginChar == null)
                {
                    this.termStartPos = 0;
                    this.termEndPos = queryterm.length;
                    this.renderFunction(this.queryType, queryterm, this);
                    return;
                }
                var start = 0;
                // edit by seedyvar range = document.selection.createRange();
                
              //  var range_all = this.inputControl.createTextRange();
                
                var range = document.selection.createRange();
                
                
                var range_all = this.inputControl.createTextRange();
  
                for (start=0; range_all.compareEndPoints("StartToStart", range) < 0; start++)
                    range_all.moveStart('character', 1);
                
                this.termEndPos = start;
                var pos = -1;
                for(var i = start - 1; i >= 0; i--)
                {
                    if(queryterm.charAt(i) == this.beginChar)
                    {
                        pos = i + 1;
                        for(var j = i; j >= 0 ; j--)
                            if(queryterm.charAt(j) == ' ' || j == 0)
                            {
                                this.queryType = queryterm.substring(j == 0 ? 0 : j + 1, i).toLowerCase();
                                break;
                            }
                        break;
                    }
                }
                if(pos != -1 && checkQueryType(this.queryType))
                {
                    this.termStartPos = pos;
                    if (this.enablemove && document.selection && document.selection.createRange) 
    	                this.rangeobj = range;
                }
                if(this.termStartPos != -1)
                {
                    if(queryterm.length <= this.termStartPos)
                        this.hidePopup();
                    else
                    {
                        this.termPrefix = queryterm.substring(this.termStartPos, start);
                        if(this.termPrefix.charAt(0) == '"')
                            this.termPrefix = this.termPrefix.substring(1, this.termPrefix.length);
                        this.renderFunction(this.queryType, this.termPrefix, this);
                    }
                }
                }else
                {
                     if(this.beginChar == '' || this.beginChar == null)
                    {
                        this.termStartPos = 0;
                        this.termEndPos = queryterm.length;
                        this.renderFunction(this.queryType, queryterm, this);
                        return;
                    }
                    var start = 0;
                    
                    //var range_all = this.inputControl.createTextRange();
                    var inputValue = this.inputControl.value;
                    start = inputValue.length;
                   // this.renderFunction(this.queryType,this.termPrefix,this);
                    var pos = -1;
                    for (var i=inputValue.length -1;i>=0;i--){
                        if(queryterm.charAt(i)==this.beginChar)
                        {
                            pos = i+1;
                            for(var j=i;j>=0;j--)
                            {
                            
                                if(queryterm.charAt(j) ==' '||j==0)
                                {
                                    this.queryType = queryterm.substring(j ==0?0:j+1,i).toLowerCase();
                                    break;
                                }
                            }
                            break;
                        }
                    }
                    if(pos!=-1&&checkQueryType(this.queryType))
                    {
                        this.termStartPos = pos;
                    
                    }
                    if(this.termStartPos != -1)
                    {
                        if(queryterm.length <= this.termStartPos)
                            this.hidePopup();
                        else
                        {
                            this.termPrefix = queryterm.substring(this.termStartPos, start);
                            if(this.termPrefix.charAt(0) == '"')
                                this.termPrefix = this.termPrefix.substring(1, this.termPrefix.length);
                            this.renderFunction(this.queryType, this.termPrefix, this);
                        }
                    }
                    
                }
            }
            break;
    }
}

ACComponent.prototype.hidePopup = function()
{
    this.popupControl.style.visibility = 'hidden';
    if(dojo.byId('search-master-searchtype-select')!=null)
    	dojo.byId('search-master-searchtype-select').style.display='';
}

ACComponent.prototype.doEnterRow = function(rowobj, formerSelectedIndex)
{
    if(formerSelectedIndex != -1)
    {
        var formerRow = this.popupControl.getElementsByTagName('table')[0].rows[formerSelectedIndex];
        formerRow.className = formerRow.initialClassName;
        if(this.queryType != 'ccl')
            formerRow.cells[1].className = 'count';
    }
    rowobj.className='searchhelp-mouseover';    
    if(this.queryType != 'ccl')
        rowobj.cells[1].className = 'countmouseover';
}

ACComponent.prototype.doSelectRow = function(obj)
{
    obj = obj.getElementsByTagName('span')[0];
    var selectedvalue = dojo.dom.textContent(obj);
    if(this.queryType == 'ccl')
    {
        var pos = selectedvalue.indexOf(' ');
        if(pos != -1)
            selectedvalue = selectedvalue.substring(0, pos);
    }
    if(selectedvalue.indexOf(' ') != -1)
        selectedvalue = '"' + selectedvalue + '"';
    var endpos = this.termEndPos;
    if(this.inputControl.value.charAt(endpos) == '"')
        endpos++;
    if(endpos!=-1)
        this.inputControl.value = this.inputControl.value.substring(0, this.termStartPos) + selectedvalue + this.inputControl.value.substring(endpos);
    else
        this.inputControl.value = this.inputControl.value.substring(0, this.termStartPos) + selectedvalue;
    this.inputControl.focus();
    this.hidePopup();
}

ACComponent.prototype.showPopup = function()
{
    var left = (this.enablemove && this.rangeobj != null ? this.rangeobj.offsetLeft : 0) + calculateOffset(this.inputControl, 'offsetLeft');
    var top =  (this.enablemove && this.rangeobj != null ? this.rangeobj.offsetTop : 0) + calculateOffset(this.inputControl, 'offsetTop') + 22;
    if(top + this.popupControl.offsetHeight > document.documentElement.offsetHeight)
        top = top - this.popupControl.offsetHeight - 22;
    this.popupControl.style.left = left + "px";
    this.popupControl.style.top = top + "px";
    var select = dojo.byId('search-master-searchtype-select');
    if(select != null)
    {
        var selectleft = calculateOffset(select, 'offsetLeft');
        var selectright = selectleft + 75;
        if(selectright >= left && selectleft <= (left + 300))
            select.style.display = 'none';   
    }
    this.popupControl.style.visibility = 'visible';	   
}

ACComponent.prototype.doRender = function(type, word, com)
{
    //var idi = (dojo.byId('searchtype-c').checked ? 4 : 3);
    //var idi = dojo.byId('searchtype-select').value;
    var idi = getSearchTypeValue();
    var urlPrefix = '';
    switch(type)
    {
        case 'an':
        case 'ann':
            urlPrefix = c_sCheckANxml;
            break;
        case 'abst':
        case 'kw':
        case 'c':
        case 'b':
        case 'calm':
        case 'ttl':
        case 'r':
            urlPrefix = c_sCheckKWxml;
            break;
        case 'ccl':
            urlPrefix = c_sCheckCCLxml;
            break;
    }
    var request = new dojo.io.Request(urlPrefix + '&idi=' + idi, 'text/html', null, false);
    var form = document.createElement('form');
    var input = document.createElement('textarea');
    input.name = 'sp';
    input.value = word;
    form.appendChild(input);
    dojo.lang.mixin(request, {
        encoding: g_encoding,
        formNode: form,
        method: 'post',
        load: function(type, evaldObj){
            com.popupControl.innerHTML = XSLTtrans(evaldObj, 'searchhelp.xsl');
            com.popupControl.comObject = com;
            com.selectedRowIndex = -1;
            if(com.popupControl.getElementsByTagName('table')[0].rows.length > 0)
                 com.showPopupFunction();    
            else
                 com.hidePopup();                         
        },
        error: function(type, error) { this.popupControl.innerHTML = error.message; }
    });
    dojo.io.bind(request);    
}

function checkQueryType(type)
{
    switch(type)
    {
        case 'an':
        case 'ann':
        case 'abst':
        case 'kw':
        case 'c':
        case 'b':
        case 'calm':
        case 'ttl':
        case 'r':
        case 'ccl':
            return true;
        default:
            return false;
    }
}