// ===============================================================
// CBkort version 2.x, copyright Carl Bro GIS&IT, 2006
// ===============================================================
// $Archive: /Products/CBKort2/development/2.5/standard/wwwroot/WEB-INF/config/modules/standard/spatialquery/js/infobox.js $ 
// $Date: 12-08-09 16:27 $
// $Revision: 11 $ 
// $Author: Nsm $
// =============================================================== 

function Infobox()
{
    this.searchText = '';
    this.running = false;
    this.timer = null;
    this.timerInterval = 800;
    
    //page definitions
    this.activatePage = 'spatialquery-async';
    this.abortPage = 'spatialquery-abort';
    this.abortPendingPage = 'spatialquery-is-abort-pending'
    this.statusPage = 'spatialquery-get-query-status';
    this.listPage = 'spatialquery-get-result-formatted-list';
    this.detailPage = 'spatialquery-get-result-formatted-detail';
    
    this.dialog = null;
    this.dialogTitle = 'Info';
    this.dialogWidth = '340px';
    this.dialogContentHeight = 280;
    this.additionaldialog = null;

    this.htmlCreated = false;
    this.targets;
    
    this.option_showAll = true;
    this.option_showErrorAlert = true;

    this.statusrunning_c = 0;
    this.statusrunning_s = '';
    this.statusrunning_active = false;
    this.statusrunning_t = null;
    
    this.statusText;
}


Infobox.prototype.query = function(queryurl, searchText)
{
    this.queryurl = queryurl;
    this.searchText = searchText;
    this.targets = new Array();
    this.htmlCreated = false;

    if(!this.dialog)
    {
        this.dialog = new Dialog(this.dialogTitle,infoboxCloseDialog,'infobox',true,infoboxResize);
        this.dialog.setDialogWidth(this.dialogWidth);
        this.dialog.setMinSize(200,100);
    }
    this.dialog.addContentHTML('');
    this.createDialogHTML();
    
    //cancel running query
    this.cancelQuery();
    this.running = true;

    //Show cancle button
    this.showCancelMenu(true);
    
    //Hide seach button
    var e = getElement('infobox_seachmorebutton');
    if(e)
        hideBlock(e);

    //start new query
    var url = queryurl;
    url = replaceUrlParam('page', this.activatePage, queryurl);

    var cbh = new CBhttp();
    cbh.executeUrlAsync(url, true, null);

    //get query status
    this.startQuery();
    this.statusrunning(true);
}
Infobox.prototype.startQuery = function()
{
    var url = cbKort.getServletUrl();
    url += "?page="+this.abortPendingPage;
    url += "&sessionid=" + cbKort.getSessionId();

    var request = new CBhttp ();
    var is_abort_pending = request.executeUrl(url, false).get(0).getValue();
    if(is_abort_pending == 'true')
    {
        this.setStatusbarText(cbInfo.getString('spatialquery.status.waiting1'));
        this.timer = setTimeout('infobox.startQuery()',500);
    }
    else
        this.checkQueryStatus();
}
Infobox.prototype.cancelQuery = function()
{
    if(this.running)
    {
        this.running = false;
        clearTimeout(this.timer);
        this.queryDone();
        this.setTargetsStatusCancel();
        cbhttp_getRequestDom(replaceUrlParam('page', this.abortPage, this.queryurl),true);
    }
}
Infobox.prototype.queryDone = function()
{
    //Show list if there is only one target with hits
    var targetswithhits = 0;
    var tn = -1;
    for(var targetnumber=0;targetnumber<this.targets.length;targetnumber++)
    {
        if(this.targets[targetnumber].rowcount > 0)
        {
            targetswithhits++;
            tn = targetnumber;
        }
    }
    if(targetswithhits==1)
        this.showListAll(true);
    
    //Show details if there is only one target with one hit
    if(targetswithhits==1)
    {
        if(this.targets[tn].rowcount == 1)
            this.showDetailAll(tn,true);
    }
    else if(targetswithhits==0 && !this.option_showAll)
    {
        getElement('infoboxcontent_result').innerHTML = '<div id="infoboxcontent_nofound">'+cbInfo.getString('spatialquery.no_result')+'</div>';
    }
    
    //Hide statusbar
    var e = getElement('infobox_progressbar_background');
    if(e)
        hideBlock(e);
    //Stop the ... to apear in the statusbar
    this.statusrunning_active = false;
    
    //Hide cancle button
    this.showCancelMenu(false);
    var e = getElement('infobox_progressbar_cancel');
    if(e)
        hideBlock(e);

    //Show seach button
    var e = getElement('infobox_seachmorebutton');
    if(e)
        showBlock(e);
    //Set status text
    this.setStatusbarText(cbInfo.getString('spatialquery.infobox.done'));
}
Infobox.prototype.closeDialog = function()
{
    this.cancelQuery();
}

Infobox.prototype.checkQueryStatus = function()
{
    //check status - read responce - use statusPage
    var url = cbKort.getServletUrl();
    url += "?page="+this.statusPage;
    url += "&sessionid=" + cbKort.getSessionId();

    var request = new CBhttp ();
    var pcol = request.executeUrl(url, false);
    if (pcol!=null)
    {
        var rowList = pcol.get(0);
        if(rowList!=null)
        {
            for (var i=0; i<rowList.size(); i++)
            {
                if(!this.targets[i] || this.targets[i].stat != 'ready')
                {
                    var row = rowList.row(i);
                    var stat = row.column("status").getValue();
                    var targetnumber = row.column("targetnumber").getValue();
                    var targetname = row.column("targetname").getValue();
                    var displayname = row.column("displayname").getValue();
                    var rowcount = parseInt(row.column("rowcount").getValue());
                    var rawpos = null;
                    var formattedpos = null;
                    var errortext = null;
                    var htmlset = false;
                    if(stat == 'ready' || stat == 'maxresulterror')
                    {
                        if(rowcount > 0)
                        {
                            rawpos = row.column("rawpos").getValue();
                            formattedpos = row.column("formattedpos").getValue();
                        }
                    }
                    else if(stat == 'queryerror')
                    {
                        errortext = row.column("errortext").getValue();
                    }
                    this.targets[i] = {stat:stat,displayname:displayname,targetnumber:targetnumber,targetname:targetname,rowcount:rowcount,rawpos:rawpos,formattedpos:formattedpos,htmlset:htmlset,errortext:errortext};
                }
            }
        }
        else
        {
            this.timer = setTimeout('infobox.checkQueryStatus()',this.timerInterval);
            return;
        }
    }

    //Create boxcontent for targets
    if(!this.htmlCreated)
        this.createTargetsHTML();

    //set status for tagets that's running or are a new ready targets
    //counts the number of ready targets
    var totalcount = 0;
    var readycount = 0;
    for(var i=0;i<this.targets.length;i++)
    {
        totalcount++;
        // - maxresulterror - skipped - queryerror - waiting - running - ready
        if(this.targets[i].stat != 'running' && this.targets[i].stat != 'waiting')
        {
            readycount++;
            if(this.targets[i].htmlset == false)
                this.targets[i].htmlset = this.setTargetsStatus(i);
        }
        else if(this.targets[i].stat == 'running')
        {
            this.setTargetsStatus(i);
        }
    }
    
    //if finish, then stop running
    if(readycount == totalcount)
    {
        this.running = false;
        this.queryDone();
        clearTimeout(this.timer);
    }

    //set statusbar
    this.setStatusbar(totalcount,readycount);
    
    if(this.running)
        this.timer = setTimeout('infobox.checkQueryStatus()',this.timerInterval);
}

function showTest()
{
}

Infobox.prototype.createDialogHTML = function()
{
    var t = '';
    //Header
    t += '<div id="infoboxcontentbox">';
    t += '  <div id="infoboxcontent_headerbox" class="infoboxcontent_headerbox">';
    t += '    <table style="border:0px;width:100%;"><tr>';
    t += '      <td class="infoboxcontent_cell">';
    t += '        <div class="infobox_detail_showinmap" onmouseout="hideHint();" onmouseover="showHint(event,\''+ cbInfo.getString('spatialquery.locate_in_map_2') +'\')" onclick="infobox_showInMapExecute();"><div>';
    t += '      </td>';
    t += '      <td class="infoboxcontent_imagecell">';
    t += '        <div class="infoboxcontent_headertext">'+ cbInfo.getString('spatialquery.you_have_queried_by')+' '+this.searchText+'</div>';
    t += '      </td>';
    t += '      <td style="vertical-align: top;" align="right">';
    t += '        <div><table class="infoboxcontent_morefunctions_table"><tr>' +
            '         <td><div id="infoboxcontent_morefunctions" class="infoboxcontent_morefunctions infoboxcontent_morefunctions_unselected" onclick="infobox.showMoreFuntions(this);"></div></td>' +
            '         <td><div id="infobox_reportbuttonimg" class="infobox_menuitem_img" onclick="infobox.report();"><div></td>' +
            '         <td><div class="infoboxcontent_reportfunction" onclick="infobox.report();">'+ cbInfo.getString('spatialquery.report') + '</div></td>' +
            '     </tr></table></div>';
    t += '      </td>';
    t += '    </table></tr>';
    t += '  </div>';
    t += '  <div style="display:none;" id="infoboxcontent_reportbox" class="infoboxcontent_headerbox infoboxcontent_reportbox">';
    t += '    <table style="border:0px;width:100%;"><tr>';
    t += '      <td class="infoboxcontent_cell">';
    t += '        <div class="infoboxcontent_headertext">' + cbInfo.getString('spatialquery.report') + '</div>';
    t += '        <div class=""><select id="infobox_reportformat"></select></div>' +
            '     <div class="">' +
            '       <input id="infobox_report_includeheader" type="checkbox" checked="true"/>' + cbInfo.getString('spatialquery.report.useheaderpage') + '' +
            '     </div>' +
            '     ';
    t += '      </td>';
    t += '      <td style="vertical-align: top;" align="right">';
    t += '        <div><table class="infoboxcontent_morefunctions_table"><tr>' +
            '       <td><div id="" class="infoboxcontent_morefunctions infoboxcontent_morefunctions_unselected" onclick="infobox.showMoreFuntions(this);" style="width:0px;border-right:0px solid #C0C0C0;"></div></td>' +
            '       <td><div id="infobox_backbuttonimg" class="infobox_menuitem_img" onclick="infobox.reportBack();"><div></td>' +
            '       <td><div class="infoboxcontent_reportfunction" onclick="infobox.reportBack();">'+ cbInfo.getString('spatialquery.report.button.back') +'</div></td>' +
            '     </tr>' +
            '     </table>' +
            '     <table><tr><td><div style="padding:5px;"><button id="" class="menubutton" style="" onclick="infobox.reportOK();">' + cbInfo.getString("spatialquery.report.button.showreport") + '</button></div></td></tr></table>' +
            '     </div>' +
            '     ' +
            '     ';
    t += '      </td>';
    t += '    </table></tr>';
    t += '  </div>';
    //Result content
    t += '  <div id="infoboxcontent_result" style="height:'+this.dialogContentHeight+'px;" class="infoboxcontent"></div>';
    //Reportcontent
    t += '  <div id="infoboxcontent_report" style="height:'+this.dialogContentHeight+'px;display:none;" class="infoboxcontent"></div>';
    //Statusbar
    t += '  <div id="infoboxcontent_statusbar" class="infoboxcontent_statusbar">';
    t += '    <table style="border:0px;width:100%;"><tr align="right">';
    t += '      <td style="width:100%;" align="right">';
    t += '        <div id="infobox_statusbar_text" class="infobox_statusbar_text" style="position:absolute;left:0px;">Status</div>';
    t += '        <div id="infobox_progressbar_background" onclick="infobox.showCancel(getCoords(event));" class="infobox_progressbar_left"><div id="infobox_progressbar" class="infobox_progressbar_right"/></div>';
    t += '      </td>';
    t += '    </table></tr>';
    t += '  </div>';
    t += '</div>';
    
    t += '<div id="infobox_progressbar_cancel" style="display:none;" class="infobox_progressbar_cancelbutton" onmouseover="this.className=\'infobox_progressbar_cancelbutton infobox_buttonhover\'" onmouseout="this.className=\'infobox_progressbar_cancelbutton\'" onclick="infobox.cancelQuery();hideBlock(this);"><table><tr><td><div id="infobox_statusbarcancelbuttonimg"><div></td><td>' + cbInfo.getString('spatialquery.infobox.cancel') + '</td></tr></table></div>';
    t += '<div id="infobox_menu" style="display:none;" class="infobox_menu">' +
         '  <table class="infobox_menuitem_table">' +
         '    <tr onmouseover="this.className=\'infobox_menuitem infobox_menuitem_hover\'" onmouseout="this.className=\'infobox_menuitem\'">' +
         '      <td id="infobox_cancelbutton_imgcell" class="infobox_menuitem_cell"><div id="infobox_cancelbuttonimg" class="infobox_menuitem_img"><div></td>' +
         '      <td id="infobox_cancelbutton_cell" class="infobox_menuitem_cell">' +
         '        <div id="infobox_cancelbutton" onclick="infobox.cancelQuery();infobox.showMoreFuntions();" class="infobox_menuitem_text">' + cbInfo.getString('spatialquery.infobox.cancel') + '</div>' +
         '      </td>' +
         '    </tr>' +
         '    <tr onmouseover="this.className=\'infobox_menuitem infobox_menuitem_hover\'" onmouseout="this.className=\'infobox_menuitem\'">' +
         '      <td class="infobox_menuitem_cell"><div id="infobox_searchmorebuttonimg" class="infobox_menuitem_img"><div></td>' +
         '      <td class="infobox_menuitem_cell">' +
         '        <div onclick="infobox.searchMore();infobox.showMoreFuntions();" class="infobox_menuitem_text">'+cbInfo.getString('spatialquery.infobox.searchagain')+'</div>' +
         '      </td>' +
         '    </tr>' +
         '    <tr onmouseover="this.className=\'infobox_menuitem infobox_menuitem_hover\'" onmouseout="this.className=\'infobox_menuitem\'">' +
         '      <td class="infobox_menuitem_cell"><div id="infobox_showallbuttonimg" class="infobox_menuitem_img"><div></td>' +
         '      <td class="infobox_menuitem_cell">' +
         '        <div onclick="infobox.showListAll(true);infobox.showMoreFuntions();" class="infobox_menuitem_text">'+cbInfo.getString('spatialquery.infobox.expandall')+'</div>' +
         '      </td>' +
         '    </tr>' +
         '    <tr onmouseover="this.className=\'infobox_menuitem infobox_menuitem_hover\'" onmouseout="this.className=\'infobox_menuitem\'">' +
         '      <td class="infobox_menuitem_cell"><div id="infobox_hideallbuttonimg" class="infobox_menuitem_img"><div></td>' +
         '      <td class="infobox_menuitem_cell">' +
         '        <div onclick="infobox.showListAll(false);infobox.showMoreFuntions();" class="infobox_menuitem_text">'+cbInfo.getString('spatialquery.infobox.collapsall')+'</div>' +
         '      </td>' +
         '    </tr>' +
         '    <tr onmouseover="this.className=\'infobox_menuitem infobox_menuitem_hover\'" onmouseout="this.className=\'infobox_menuitem\'">' +
         '      <td class="infobox_menuitem_cell infobox_menuitem_imgcell"><div id="infobox_showsearchedbuttonimg" class="infobox_menuitem_img"><div></td>' +
         '      <td class="infobox_menuitem_cell">' +
         '        <div id="infobox_searchedtargersbutton" onclick="infobox.showSearchedTargers();infobox.showMoreFuntions();" class="infobox_menuitem_text">'+cbInfo.getString('spatialquery.infobox.showhidesearch')+'</div>' +
         '      </td>' +
         '    </tr>' +
         '  </table>' +
         '</div>';

    //content to this.dialog
    this.dialog.addContentHTML(t);
    this.dialog.showDialog();
}
Infobox.prototype.createTargetsHTML = function()
{
    this.htmlCreated = true;
    var t = '';
    if(this.targets.length)
    {
        for(var j=0;j<this.targets.length;j++)
        {
            t += '      <div id="infoboxcontent_'+j+'"'+(this.option_showAll ? '' : ' style="display:none;" class="infoboxcontent_target_unselected"')+'><table><tr><td><div id="infoboxcontent_foldimage_'+j+'" class="infoboxcontent_foldimage_start"></div></td><td>'+this.targets[j].displayname+' <span id="infoboxcontent_status_'+j+'">('+this.getStatusText('waiting')+')</span></td></tr></table></div>';
            t += '      <div id="infoboxcontent_list_'+j+'" style="display:none;" class="infoboxcontent_list"></div>';
        }
    }
    else
    {
        t += '      <div id="infoboxcontent_nofound">' +cbInfo.getString('spatialquery.no_result') +  '</div>';
    }
    //content to dialog
    getElement('infoboxcontent_result').innerHTML = t;
}
Infobox.prototype.showCancel = function(coord)
{
    var de = getElement('dialoginfobox');
    var x = coord[0]-getX(de)+3;
    var y = coord[1]-getY(de)-25;
    
    var e = getElement('infobox_progressbar_cancel'); 
    e.style.top = y+'px';
    e.style.left = x+'px';

    if(isBlock(e))
        hideBlock(e);
    else
        showBlock(e);
}
Infobox.prototype.showCancelMenu = function(show)
{
    var e = getElement('infobox_cancelbutton');
    if(e)
        (show ? showBlock(e) : hideBlock(e));

    var e = getElement('infobox_cancelbuttonimg');
    if(e)
        (show ? showBlock(e) : hideBlock(e));

    var classname = (show ? 'infobox_menuitem_cell' : 'infobox_menuitem_cell_noborder');
    var e = getElement('infobox_cancelbutton_imgcell');
    if(e)
        e.className = classname;
    var e = getElement('infobox_cancelbutton_cell');
    if(e)
        e.className = classname;
}
Infobox.prototype.showMoreFuntions = function(button)
{
    var me = getElement('infoboxcontent_morefunctions');
    var e = getElement('infobox_menu');
    if(isBlock(e))
    {
        me.className = 'infoboxcontent_morefunctions infoboxcontent_morefunctions_unselected';
        hideBlock(e);
    }
    else
    {
        me.className = 'infoboxcontent_morefunctions infoboxcontent_morefunctions_selected';
        showBlock(e);
    }
}

Infobox.prototype.showListAll = function(show)
{
    for(var targetnumber=0;targetnumber<this.targets.length;targetnumber++)
    {
        if(this.targets[targetnumber].rowcount > 0)
            this.showList(targetnumber,show);
    }
}
Infobox.prototype.showDetailAll = function(targetnumber,show)
{
    for(var targethitnumber=0;targethitnumber<this.targets[targetnumber].rowcount;targethitnumber++)
        this.showDetail(targetnumber,targethitnumber,show);
}
Infobox.prototype.showSearchedTargers = function()
{
    this.option_showAll = !this.option_showAll;
    for(var i=0;i<this.targets.length;i++)
    {
        if(this.targets[i].rowcount == 0)
        {
            var e = getElement('infoboxcontent_'+i);
            if(this.option_showAll)
                showBlock(e);
            else
                hideBlock(e);
        }
    }
}
Infobox.prototype.searchMore = function()
{
    this.dialog.hideDialog();
    if(!this.additionaldialog)
    {
        this.additionaldialog = new Dialog(this.dialogTitle,null,'infobox_additionaldialog',true,null);
        this.additionaldialog.setDialogWidth('220px');
        this.additionaldialog.addContentHTML('');
        
        var sh ='<div class="divcontent">' +
                ' <table class="divtable" style="width:100%;padding:3px;">' +
                '  <tr><td colspan="2">' +
                '    <table><tr>' +
                '    <td class="infoboxcontent_cell">' +
                '      <div class="infobox_detail_showinmap" onmouseout="hideHint();" onmouseover="showHint(event,\''+ cbInfo.getString('spatialquery.locate_in_map_2') +'\')" onclick="infobox_showInMapExecute();"><div>' +
                '    </td>' +
                '    <td>'+cbInfo.getString('spatialquery.you_have_queried_by')+' <span id="infobox_searchmore_searchtext"></span></td>' +
                '    </tr></table>' +
                '  </td></tr>' +
                '  <tr>' +
                '    <td colspan="2"><span id="infobox_searchmore_options"></span></td>' +
                '  </tr>' +
                '  <tr align="right"><td colspan="2">' +
                '    <button id="" class="menubutton" onclick="infobox.searchMoreOK();">'+cbInfo.getString("standard.button.ok")+'</button>' +
                '    <button id="" class="menubutton" onclick="infobox.dialog.showDialog();infobox.additionaldialog.closeDialog();">'+cbInfo.getString("standard.button.cancel")+'</button>' +
                '  </td></tr>' +
                ' </table>' +
                '</div>';
    
        this.additionaldialog.addContentHTML(sh);
    }
    getElement('infobox_searchmore_searchtext').innerHTML = this.searchText;
    getElement('infobox_searchmore_options').innerHTML = spatialqueryoptions.getOptionDialogContent();
    
    this.additionaldialog.showDialog();
}
Infobox.prototype.searchMoreOK = function()
{
    this.additionaldialog.closeDialog();
    var queryurl = this.queryurl;
    queryurl = replaceUrlParam('layers',cbKort.getLayers(),queryurl);
    queryurl = spatialqueryoptions.replaceOptionParams(queryurl);
    this.query(queryurl,this.searchText);
}

Infobox.prototype.report = function()
{
    if(true)
    {
        var t = '';
        if(this.targets.length)
        {
            for(var j=0;j<this.targets.length;j++)
            {
                var e = getElement('infoboxcontent_status_'+j);
                if (e)
                    statustext = e.innerHTML;
                t += '      <div><input id="infoboxcontent_reporttarget_'+(this.targets[j].targetnumber)+'" type="checkbox" checked="checked" style="padding:0px;margin:0px;"/><span class="infobox_report_targettext">'+this.targets[j].displayname+' '+statustext+'</span></div>';
            }
        }
        else
        {
            t += '      <div id="infoboxcontent_nofound">' +cbInfo.getString('spatialquery.no_result') +  '</div>';
        }
        var e = getElement('infoboxcontent_report');
        if (e)
            e.innerHTML = t;
    }
    getReportselector(getElement('infobox_reportformat'));
    
    var reportheader = getElement('infoboxcontent_reportbox');
    var reportcontent = getElement('infoboxcontent_report');
    
    var resultheader = getElement('infoboxcontent_headerbox');
    var resultsontent = getElement('infoboxcontent_result');
    var h = getHeight(resultsontent) + getHeight(resultheader);
    var h1 = getHeight(resultsontent);
    var h2 = getHeight(resultheader);
    
    showBlock(getElement('infoboxcontent_reportbox'));
    showBlock(getElement('infoboxcontent_report'));
    
    hideBlock(getElement('infoboxcontent_headerbox'));
    hideBlock(getElement('infoboxcontent_result'));
    var h3 = getHeight(reportheader);
    var h4 = h1 + h2 - h3;
    setHeight(getElement('infoboxcontent_report'),h - getHeight(reportheader)-(ie ? 0 : 10));
}

Infobox.prototype.reportBack = function()
{
    hideBlock(getElement('infoboxcontent_reportbox'));
    hideBlock(getElement('infoboxcontent_report'));
    
    showBlock(getElement('infoboxcontent_headerbox'));
    showBlock(getElement('infoboxcontent_result'));
}

Infobox.prototype.reportOK = function()
{
    var targetlist = '';
    for(var a=0;a<this.targets.length;a++)
    {
        var e=getElement('infoboxcontent_reporttarget_'+this.targets[a].targetnumber);
        if (e && e.checked)
           targetlist += (a ? ',' : '') + this.targets[a].targetnumber;
    }
    var page=getElement('infobox_reportformat').value;
    var includeheader = getElement('infobox_report_includeheader').checked;
    spatialquery_printReport (this.queryurl, this.searchText, page, targetlist, includeheader);
}

Infobox.prototype.getStatusText = function(statustext)
{
    if(!this.statusText)
    {
        this.statusText = {
            'maxresulterror':cbInfo.getString('spatialquery.status.maxresulterror'),
            'skipped':cbInfo.getString('spatialquery.status.skipped'),
            'queryerror':cbInfo.getString('spatialquery.status.queryerror'),
            'waiting':cbInfo.getString('spatialquery.status.waiting'),
            'cancelled':cbInfo.getString('spatialquery.status.cancelled'),
            'running':cbInfo.getString('spatialquery.status.running'),
            'ready':cbInfo.getString('spatialquery.status.ready')
        };
    }
    return this.statusText[statustext];
}

Infobox.prototype.setTargetsStatus = function(targetnumber)
{
    var se = getElement('infoboxcontent_status_'+targetnumber);
    if(se)
    {
        var statustext = '';
        //set status in html
        //- maxresulterror- skipped- queryerror- waiting- running- ready
        if(this.targets[targetnumber].stat == 'running')
        {
            //set running text
            statustext = '('+this.getStatusText(this.targets[targetnumber].stat)+')';
        }
        else
        {
            var maxresult = this.targets[targetnumber].stat=='maxresulterror' ? '+' : '';
            
            if(this.targets[targetnumber].stat == 'ready' || this.targets[targetnumber].stat == 'maxresulterror')
            {
                //remove waiting/running text
                //set count on targethtml
                statustext = '('+this.targets[targetnumber].rowcount + maxresult + ')';
    
                //make element clickable if rowcount > 0
                if(this.targets[targetnumber].rowcount > 0)
                {
                    getElement('infoboxcontent_foldimage_'+targetnumber).className = 'infoboxcontent_foldimage_unselected';
                    var e = getElement('infoboxcontent_'+targetnumber);
                    e.style.cursor = 'pointer';
                    if(!this.option_showAll)
                        showBlock(e);
                    e.onclick = new Function('infobox.showList('+targetnumber+')');
                }
            }
            else if(this.targets[targetnumber].stat == 'queryerror')
            {
                //remove waiting/running text
                //set count on targethtml
                statustext = '('+cbInfo.getString('standard.error.error')+')';
    
                if(this.targets[targetnumber].errortext)
                {
                    var e = getElement('infoboxcontent_'+targetnumber);
                    if(!this.option_showAll)
                        showBlock(e);
                    if(this.option_showErrorAlert)
                    {
                        e.style.cursor = 'pointer';
                        e.onclick = new Function('infobox.showError('+targetnumber+')');
                    }
                }
            }
            else if(this.targets[targetnumber].stat == 'skipped')
            {
                //remove waiting/running text
                //set count on targethtml
                statustext = '('+this.getStatusText(this.targets[targetnumber].stat)+')';
            }
    
        }
        se.innerHTML = statustext;
        return true;
    }
    else
        return false;
}

Infobox.prototype.showError = function(targetnumber)
{
    var t = this.targets[targetnumber].errortext;
    t = t.substring(0,1000);
    t = t.replace(/"/g,"'");
    alert(t);
}

Infobox.prototype.setTargetsStatusCancel = function()
{
    //set cancel status
    for(var i=0;i<this.targets.length;i++)   
    {
        //- maxresulterror- skipped- queryerror- waiting- running- ready
        if(this.targets[i].stat != 'ready' && this.targets[i].stat != 'maxresulterror' && this.targets[i].stat != 'skipped' && this.targets[i].stat != 'queryerror')
        {
            this.targets[i].stat = 'cancelled';
            var e = getElement('infoboxcontent_status_'+i);
            if(e)
                e.innerHTML = '('+this.getStatusText(this.targets[i].stat)+')';
        }
    }
}

//Statusbar functions
Infobox.prototype.setStatusbar = function(totalcount,readycount)
{
    if(totalcount == readycount)
    {
        //hide statusbar
        hideBlock(getElement('infobox_progressbar_background'));
    }
    else
    {
        //show statusbar
        showBlock(getElement('infobox_progressbar_background'));

        //set size of progress bar
        var pbw = getWidth(getElement('infobox_progressbar_background'));;
        //show progress
        var w = (totalcount - readycount) * pbw / totalcount;
        setWidth(getElement('infobox_progressbar'),w);

        this.setStatusbarText(cbInfo.getString('spatialquery.infobox.statusbarsearching')+' '+this.targets[readycount].displayname+'<span id="infobox_statusrunning">'+this.statusrunning_s+'</span>');
    }
}
Infobox.prototype.setStatusbarText = function(text)
{
    var e = getElement('infobox_statusbar_text');
    if(e)
        e.innerHTML = text;
}
Infobox.prototype.statusrunning = function(start)
{
    var es = getElement('infobox_statusrunning');
    if(start)
        this.statusrunning_active = true;
    if(this.statusrunning_active)
    {
        if(es)
        {
            es.innerHTML = this.statusrunning_s;
            this.statusrunning_s+='.';
            this.statusrunning_c++;
            if(this.statusrunning_c==4) {
                this.statusrunning_c = 0;
                this.statusrunning_s = '';
            }
        }
        this.statusrunning_t = setTimeout('infobox.statusrunning();',500);
    }
    else
    {
        clearTimeout(this.statusrunning_t);
        this.statusrunning_s = '';
        this.statusrunning_c = 0;
        if(es)
            es.innerHTML = '';
    }
}


//
Infobox.prototype.showList = function(targetnumber,show)
{
    var hide = false;
    if(typeof(show) != "undefined")
    {
        if(!show)
            hide=true;
    }

    var e = getElement('infoboxcontent_list_'+targetnumber);
    if(!this.targets[targetnumber].tagethitsready && this.targets[targetnumber].formattedpos != null && !hide)
    {
    	this.targets[targetnumber].tagethitsready = true;
        this.targets[targetnumber].targethits = new Array();
        var html = '';
        var url = cbKort.getServletUrl();
        url += "?page="+this.listPage;
        url += "&sessionid=" + cbKort.getSessionId();
        url += "&position=" + this.targets[targetnumber].formattedpos;
    
        var request = new CBhttp ();
        var pcol = request.executeUrl(url, false);
        if (pcol!=null)
        {
            var rowList = pcol.get(0);
            if(rowList!=null)
            {
                var base = 4;
                for (var i=base; i<rowList.size(); i++)
                {
                    var node = rowList.get(i);
                    
                    if(node.isRowList())
                    {
                        var row = node.row(0);
                        var targethitnumber = i - base;
                        var listheading = row.column("value").getValue();
                        var orgpcolid = row.column("org-pcolid").getValue();
                        var displaybuffer;
                        if(row.column("displaybuffer")) displaybuffer = row.column("displaybuffer").getValue();
                        else  displaybuffer = '100';
                        this.targets[targetnumber].targethits[targethitnumber] = {listheading:listheading,orgpcolid:orgpcolid,displaybuffer:displaybuffer};
                        html += '<div id="infoboxcontent_list_'+targetnumber+'_'+targethitnumber+'">' +
                                '  <table class="infoboxcontent_list_detail_table"><tr><td>' +
                                '    <span style="cursor:pointer;" onclick="infobox.showDetail('+targetnumber+','+targethitnumber+');">' +
                                '      <table><tr>' +
                                '        <td><div id="infoboxcontent_foldimage_'+targetnumber+'_'+targethitnumber+'" class="infoboxcontent_foldimage_unselected"></div></td>' +
                                '        <td>'+this.targets[targetnumber].targethits[targethitnumber].listheading+'</td>' +
                                '      </tr></table>' +
                                '    </span>' +
                                '  </td>';
                        if(this.targets[targetnumber].targethits[targethitnumber].orgpcolid)
                            html += '  <td align="right"><div class="infobox_detail_showinmap" onmouseout="hideHint();" onmouseover="showHint(event,\''+ cbInfo.getString('spatialquery.locate_in_map_2') +'\')" onclick="infobox_showInMap(\'' + this.targets[targetnumber].rawpos + '\', \''  + this.targets[targetnumber].targethits[targethitnumber].orgpcolid+'\', \''+this.targets[targetnumber].targethits[targethitnumber].displaybuffer+'\')"><div></td>';
                        html += '  </tr></table>' +
                                '</div>';
                        html += '<div id="infoboxcontent_list_'+targetnumber+'_'+targethitnumber+'_detail" style="display:none;" class="infoboxcontent_list_detail"></div>'               
                    }
                }
            }
        }
        //set the html for the element
        e.innerHTML = html;
    }

    if(typeof(show) != "undefined")
    {
        if(!show)
        {
            getElement('infoboxcontent_foldimage_'+targetnumber).className = 'infoboxcontent_foldimage_unselected';
            getElement('infoboxcontent_'+targetnumber).className = 'infoboxcontent_target_unselected';
            hideBlock(e);
        }
        else
        {
            getElement('infoboxcontent_foldimage_'+targetnumber).className = 'infoboxcontent_foldimage_selected';
            getElement('infoboxcontent_'+targetnumber).className = 'infoboxcontent_target_selected';
            showBlock(e);
        }
    }
    else
    {
        if(isBlock(e))
        {
            getElement('infoboxcontent_foldimage_'+targetnumber).className = 'infoboxcontent_foldimage_unselected';
            getElement('infoboxcontent_'+targetnumber).className = 'infoboxcontent_target_unselected';
            hideBlock(e);
        }
        else
        {
            getElement('infoboxcontent_foldimage_'+targetnumber).className = 'infoboxcontent_foldimage_selected';
            getElement('infoboxcontent_'+targetnumber).className = 'infoboxcontent_target_selected';
            showBlock(e);
        }
    }
}

Infobox.prototype.showDetail = function(targetnumber,targethitnumber,show)
{
    var hide = false;
    if(typeof(show) != "undefined")
    {
        if(!show)
            hide=true;
    }
    
    var e = getElement('infoboxcontent_list_'+targetnumber+'_'+targethitnumber+'_detail');
    if(e.innerHTML == '' && !hide)
    {
        var html = '';
        var servertargethitnumber = targethitnumber+1;  //targethit on server is one based while targethitnumber is zero based (array possition)
        var url = cbKort.getServletUrl();
        url += "?page="+this.detailPage;
        url += "&sessionid=" + cbKort.getSessionId();
        url += "&position=" + this.targets[targetnumber].formattedpos;
        url += "&targethitnumber=" + servertargethitnumber;
    
        var request = new CBhttp ();
        var pcol = request.executeUrl(url, false);
        if (pcol!=null)
        {
            var rowList = pcol.get(0).get(0);
            if(rowList!=null)
            {
                for (var i=0; i<rowList.size(); i++)
                {
                    var row = rowList.row(i);
                    var l = row.column("label").getValue();
                    if(l == null)
                        l = '';
                    var v = row.column("value").getValue();
                    if(v == null)
                        v = '';
                    var f = row.column("format").getValue();
                    
                    if(f!='heading' && f!='org-pcolid' && f!='property')
                    {
                        var classname = (i%2==0 ? 'uneven' : 'even'); //starts with zero
                        if(f=='hyperlink')
                            html += '<tr class="infobox_detail infobox_detail_'+classname+' infobox_detail_hyperlink"><td colspan="2"><a target="_blank" href="'+v+'">'+l+'</a></td></tr>';
                        else
                            html += '<tr class="infobox_detail infobox_detail_'+classname+'"><td>'+l+'</td><td>'+v+'</td></tr>';
                    }
                    else if(f=='property')
                    {
                        if (l=='nodetail_javascriptlink')
                            html += '<tr class="infobox_detail infobox_detail_'+classname+'"><td colspan="2"><a href="javascript:'+v+'">'+cbInfo.getString('spatialquery.show')+'</a></td>';
                    }
                }
                if(rowList.size()>0)
                {
                    var htmlstart = '<table class="infoboxcontent_list_detail_table">';
                    var htmlend = '</table>';
                    html = htmlstart+html+htmlend;
                }
            }
        }
        //set the html for the element
        e.innerHTML = html;
    }
    
    if(typeof(show) != "undefined")
    {
        if(!show)
        {
            getElement('infoboxcontent_foldimage_'+targetnumber+'_'+targethitnumber).className = 'infoboxcontent_foldimage_unselected';
            hideBlock(e);
        }
        else
        {
            getElement('infoboxcontent_foldimage_'+targetnumber+'_'+targethitnumber).className = 'infoboxcontent_foldimage_selected';
            showBlock(e);
        }
    }
    else
    {
        if(isBlock(e))
        {
            getElement('infoboxcontent_foldimage_'+targetnumber+'_'+targethitnumber).className = 'infoboxcontent_foldimage_unselected';
            hideBlock(e);
        }
        else
        {
            getElement('infoboxcontent_foldimage_'+targetnumber+'_'+targethitnumber).className = 'infoboxcontent_foldimage_selected';
            showBlock(e);
        }
    }
}

// *********************


function infobox_showInMap (targetnumber, pcolid, displaybuffer)
{
    showWaitingBox();
    setTimeout('infobox_showInMapExecute("' + targetnumber + '","'+ pcolid+'","'+displaybuffer+'")',100);
}

function infobox_showInMapExecute(targetnumber, pcolid, displaybuffer)
{
    if(!displaybuffer)
        displaybuffer = '25+pct!400+minimum';
    
    var request = new CBhttp();

    var url = cbKort.getServletUrl();
    url += "?page=spatialquery-remove-highlight-dynlayer";
    url += "&sessionid=" + cbKort.getSessionId();

    request.executeUrl(url, false);


    var url = cbKort.getServletUrl();
    url += "?page=spatialquery-add-dynlayer-from-pcolid";
    if(pcolid) //case: found object
    {
        url += "&savename=spatialquery_result_raw";
        url += "&saveposition=" + targetnumber;
        url += "&pcolid=" + pcolid;
    }
    else  //case: selector object
    {
        url += "&savename=spatialquery_selectors";
    }
    url += "&dynamiclayer=user*";
    url += "&append=false";
    url += "&sessionid=" + cbKort.getSessionId();

    request.executeUrl(url, false);

    var url = cbKort.getFormParamAsUrl();
    url = replaceUrlParam('page','spatialquery-get-map-zoom-to-datasource',url);
    url = replaceUrlParam('datasource','userdatasource',url);
    url = replaceUrlParam('displaybuffer',displaybuffer,url);
    url = replaceUrlParam('zoomdir','0',url);
    // create map 
    updateMapData(getMapDataRequest(url));

    hideWaitingBox();
}


// *********************






var infoboxHeight;
var infoboxWidth;
function infoboxResize(dw,dh,start)
{
    var e = getElement('infoboxcontent_result');
    var re = getElement('infoboxcontent_report');
    if(e && re)
    {
        if(start)
        {
            infoboxHeight = getHeight(e)-(ie ? 0 : 8);
            if(getHeight(e)==0)
                 infoboxHeight = getHeight(re)-(ie ? 0 : 8);
            infoboxWidth = infobox.dialogWidth;
        }
        if(dh)
            infobox.dialogContentHeight = infoboxHeight+dh;
        var w = infoboxWidth+dw;
        infobox.dialogWidth = w+'px';
        setHeight(e,infobox.dialogContentHeight);
        setHeight(re,infobox.dialogContentHeight);
    }
}

var infobox = new Infobox();
var infoboxtest = 0;

function infoboxCloseDialog()
{
    infobox.closeDialog();
}
