//////////////////////////////////////////////////////////////////////////
//	Title       : One Search Decentralised Script
//	Version No. : 0.3
//	Release date: 3 Feb 2010
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
//               Search configuration
// Config items:
//    Search_Text_Box_Size     : the length of search text box in pixel
//    Server_URL               : the redirection url of search page
//    URLStr                   : the name of agency client
//    SAYT_URL                 : the ajax url of config
//    Popular_Search_URL       : the ajax url of popular searches
//    Popular_Search_Font_Size : the font style of popular searches
//    Display_Popular_Searches : Option to display popular searches
/////////////////////////////////////////////////////////////////////////
var config = {
    Search_Text_Box_Size : 23,
    Server_URL : 'http://app.search.gov.sg/ui/search.aspx',
    URLStr : document.domain,
    SAYT_URL : 'http://app.search.gov.sg/ui/saytconfig.aspx',
    Popular_Search_URL : 'http://app.search.gov.sg/ui/popularsearches.aspx',
    Popular_Search_Font_Size : '1.1em',
    Display_Popular_Searches : false
};
/////////////////////////////////////////////////////////////////////////
//                Search-as-you-type style configuration
// Config items:
//    list     : the style of list
//    item     : the style of item
//    itemOver : item background color when mouse over the item
//    keywords : keywords style
//    results  : results style
/////////////////////////////////////////////////////////////////////////
var SAYTStyle = {
    list : 'border:solid 1px #c9c9c9;background-color:#fff;text-align:left;list-style:none;z-index:2000;',
    item : 'background-color:inherit;padding:3px;cursor:pointer;word-break:break-all;',
    itemOver : 'background-color:#e5f0ff;padding:3px;cursor:pointer;word-break:break-all;',
    keywords : 'float:left;color:black;',
    results : 'float:right;color:green;text-align:right;'
};
//////////////////////////////////////////////////////////////////////////////
var sayt_q = null;
var sayt_config = [];
var sayt_popular = [];
var sayt_complete = [];
var sayt_item = -1;
var sayt_count = 0;
var sayt_div = null;
var sayt_script = null;
function sayt_init(divId) {
    sayt_getRemoteData(config.SAYT_URL + '?name=sayt_config', true);
    if (config.Display_Popular_Searches || typeof(defaultSite) != 'undefined') {
        sayt_getRemoteData(config.Popular_Search_URL + '?name=sayt_popular', true);
    }
    window.setTimeout("sayt_showEntry('" + divId + "')", 100);
}
function sayt_getRemoteData(url, flag) {
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = url;
    document.body.appendChild(script);
    if (!flag) {
        if (sayt_script) {
            var parent = sayt_script.parentNode;
            parent.removeChild(sayt_script);
        }
        sayt_script = script;
    }
}
function sayt_showEntry(divId) {
    var html = '';
    html += '<style type="text/css">';
    html += '.sayt_item{' + SAYTStyle.item + '}';
    html += '.sayt_itemOver{' + SAYTStyle.itemOver + '}';
    html += '<\/style>';
    html += '<div id="entry"><div id="topSearch">';
    html += '<input type="text" id="q" name="q" autocomplete="off" onkeyup="sayt_autoComplete(this,event);" onkeydown="sayt_goSearch(event);" onblur="sayt_closeWindow();" size="' + config.Search_Text_Box_Size + '" maxLength="' + (sayt_config[0] || 100) + '" \/>';
    html += '<a href="javascript:void(0)" title="Search"  id="btnGo" onclick="sayt_search();"><img border="0" alt="Search" src="http://internet-stg.sgdi.gov.sg/img/btnSearch.png"></a>';

    //html += '<input type="button" id="btnGo" style="background-color:#c84141;font-size:85%;color:white;cursor:pointer;" value="Go"';
    //if (typeof(defaultSite) == 'undefined') html += ' onclick="sayt_search();"';
    //html += ' />';
    html += '<\/div>';
    if (config.Display_Popular_Searches || typeof(defaultSite) != 'undefined') {
        html += '<dl id="popularSearch" style="font-size:' + config.Popular_Search_Font_Size + '"><\/dl>';
        window.setTimeout(function() {
            if (sayt_popular.length) {
                var popular = '<dt style="display:inline;margin:0;padding:0 2px;">Popular Searches<\/dt>';
                for (var i = 0; i < sayt_popular.length; i++) {
                    popular += '<dd style="display:inline;margin:0;padding:0 2px;"><a href="' + sayt_getUrl(sayt_popular[i], true) + '" >' + sayt_popular[i] + '</a><\/dd>';
                }
                document.getElementById('popularSearch').innerHTML = popular;
            }
        }, 100);
    }
    html += '<\/div>';
    document.getElementById(divId).innerHTML = html;
    document.getElementById('q').focus();
}
function sayt_getUrl(q, popular) {
    var url = config.Server_URL + '?q=' + encodeURIComponent(q);
    if (popular) url += '&popular=1';
    url += '&site=' + encodeURIComponent(sayt_config[4] || 'OneSearch_within_gov');
    url += '&client=' + encodeURIComponent(config.URLStr);
    url += '&proxystylesheet=default&output=xml_no_dtd';
    return url;
}
function sayt_search() {
    var q = document.getElementById('q').value;
    window.open(sayt_getUrl(q, false));
    return false;
}
function sayt_autoComplete(obj, e) {
    if (obj && obj.value != '') {
        e = e || window.event;
        var whichKey = (e.which) ? e.which : e.keyCode;
        if (whichKey != 13 && whichKey != 37 && whichKey != 38 && whichKey != 39 && whichKey != 40 && whichKey != 63233 && whichKey != 63235) {
            sayt_item = -1;
            sayt_q = obj;
            if (sayt_q.value.length >= (sayt_config[2] || 1) && sayt_config[3]) {
                sayt_getRemoteData(sayt_config[3] + '?name=sayt_complete&q=' + obj.value, false);
            }
            var evt;
            if (evt) window.clearTimeout(evt);
            evt = window.setTimeout(sayt_getAutoComplete, sayt_config[1] || 100);
        }
    } else {
        sayt_closeWindow();
    }
}
function sayt_getAutoComplete() {
    var html = '';
    if (sayt_complete.length) {
        sayt_count = sayt_complete.length;
        html += '<table width="100%" border="0" cellpadding="0" cellspacing="0" style="' + SAYTStyle.list + '">';
        for (var i = 0; i < sayt_complete.length; i++) {
            var item = sayt_complete[i];
            if (item) {
                var value = item[0].replace(new RegExp('\'', 'gm'), '\\\'');
                var text = item[1].replace(new RegExp('&lt;b&gt;', 'gm'), '<b>').replace(new RegExp('&lt;/b&gt;', 'gm'), '<\/b>');
                html += '<tr><td id="item' + i + '" style="' + SAYTStyle.item + '" onmouseover="this.style.cssText=\'' + SAYTStyle.itemOver + '\';sayt_q.value=\'' + value + '\';" onmouseout="this.style.cssText=\'' + SAYTStyle.item + '\';" onclick="sayt_selectCompleteItem(\'' + value + '\');">';
                html += '<span style="' + SAYTStyle.keywords + '">' + text + '<\/span>';
                if (item[2]) {
                    html += '<span style="' + SAYTStyle.results + '">' + item[2] + '&nbsp;results<\/span>';
                }
                html += '<div style="clear:both;"><\/div>';
                html += '<\/td><\/tr>';
            }
        }
        html += '<\/table>';
    }
    if (!sayt_div) {
        sayt_div = document.createElement('div');
        sayt_div.style.position = 'absolute';
        sayt_div.style.width = sayt_q.offsetWidth + 'px';
        sayt_div.style.zindex = 999999;
        document.body.appendChild(sayt_div);
    }
    sayt_div.style.top = (sayt_getTop(sayt_q) + sayt_q.offsetHeight) + 'px';
    sayt_div.style.left = sayt_getLeft(sayt_q) + 'px';
    sayt_div.innerHTML = html;
    sayt_div.style.display = 'block';
}
function sayt_goSearch(e) {
    e = e || window.event;
    var whichKey = (e.which) ? e.which : e.keyCode;
    switch (whichKey) {
        case 13: // Enter
            sayt_closeWindow();
            if (typeof(defaultSite) == 'undefined') document.getElementById('btnGo').click();
            break;
        case 27: // Escape
            sayt_closeWindow();
            break;
        case 40: // down arrow
        case 63233: // down arrow
        case 39: // right arrow
            if (sayt_count > 0) {
                sayt_clearItemStyle();
                if (sayt_item < sayt_count - 1)
                    ++sayt_item;
                else
                    sayt_item = sayt_count - 1;
                var item = document.getElementById('item' + sayt_item);
                if (item) item.onmouseover();
            }
            break;
        case 38: // up arrow
        case 63235: // up arrow
        case 37: // left arrow
            if (sayt_count > 0 && sayt_item > -1) {
                sayt_clearItemStyle();
                if (sayt_item > 0) {
                    var item = document.getElementById('item' + (--sayt_item));
                    if (item) item.onmouseover();
                } else {
                    sayt_closeWindow();
                }
            }
            break;
    }
}
function sayt_clearItemStyle() {
    for (var i = 0; i < sayt_count; i++) {
        var item = document.getElementById('item' + i);
        if (item) item.onmouseout();
    }
}
function sayt_selectCompleteItem(data) {
    if (sayt_q) {
        sayt_q.focus();
        sayt_q.value = data;
    }
    sayt_closeWindow();
}
function sayt_closeWindow() {
    if (sayt_div) {
        sayt_div.innerHTML = '';
        sayt_div.style.display = 'none';
    }
}
function sayt_getTop(e) {
    var offset = e.offsetTop;
    if (e.offsetParent) offset += sayt_getTop(e.offsetParent);
    return offset;
}
function sayt_getLeft(e) {
    var offset = e.offsetLeft;
    if (e.offsetParent) offset += sayt_getLeft(e.offsetParent);
    return offset;
}

function search_init() {
  sayt_init('os_entry');
}

function ShowResults() {

}

