
/******************************************************************************
 *
 * Purpose: functions for formatting TOC and legend output
 * Author:  Armin Burger
 *
 ******************************************************************************
 *
 * Copyright (c) 2003-2006 Armin Burger
 *
 * This file is part of p.mapper.
 *
 * p.mapper is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version. See the COPYING file.
 *
 * p.mapper is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with p.mapper; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 ******************************************************************************/


var legendContainer = new Object();

/**
 * initialize and write TOC by calling XMLHttp function 'updateToc()' 
 * 
 */
function initToc() {
    legendContainer.checkedGrpList = defGroupList;
    var tocurl = PM_XAJAX_LOCATION + 'x_toc.php?' + SID;
    updateToc(tocurl, ''); 
}


/**
 * Toggle groups and categories (and related images)
 * attached as onClick script to plus/minus icons and links
 */
function tg(group) {
    var crIE = (document.all && !document.getElementById && !navigator.userAgent.match(/Opera|Konqueror|Safari/i) ? true : false);
    var divobj = document.getElementById(group);
    var dheight;
    var visToSet;

    if (divobj.style.visibility == 'hidden') {
        visToSet = 'visible';
        if (crIE) {        	
            dheight = 0+"px";
            divobj.style.width = 'auto';
        } else {        	
            dheight = 'auto';
            divobj.style.overflow = "visible";
        }
        document.getElementById(group + '_timg').src = 'images/tree/minus.gif';
    } else {
        visToSet = 'hidden';
        if (crIE) {
            divobj.style.width = 0+"px";
            dheight = 1+"px";
        } else {
            dheight = 0+"px";
        }
        divobj.style.overflow = 'hidden';
        document.getElementById(group + '_timg').src = 'images/tree/plus.gif';
    }
    
    divobj.style.height = dheight;
    divobj.style.visibility = visToSet;    
    //document.getElementById('pmtree').style.overflow = 'auto';
}
 

/**
 * Initialize TOC tree
 * attached as onLoad script to hidden image in legend/TOC
 */
function treeInit(catStyle, grpStyle) { 
    //alert('init');
    legendContainer.catStyle = catStyle;
    
    if (catStyle == 'tree') {    	
        var divList = document.getElementsByTagName('div');
        var catcList = [];
        var grpcList = [];

        var c = 0;
        var g = 0;
        for (var d=0; d<divList.length; d++) {
            if (divList[d].className == 'catc') {
                catcList[c] = divList[d];
                c++;
                //alert(divList[d].id);
            } else if (divList[d].className == 'grpc') {
                grpcList[g] = divList[d];
                g++;
            }
        }
        
        if (catcList.length > 0) {
            for (var i=0; i<catcList.length; i++) {  
                catcList[i].style.visibility='hidden';
                tg(catcList[i].id);
            }
        } 
        
        if (grpStyle == 'tree') {
            if (grpcList.length > 0) {
                for (var j=0; j<grpcList.length; j++) {
                    grpcList[j].style.visibility='visible';
                    tg(grpcList[j].id);
                }
            }
        }
        
        legendContainer.catcList = catcList;
        legendContainer.grpcList = grpcList;
        
    }
    
    // In case of reload from 'swapToLayerView()' reset active categories for Layer View
    if (legendContainer.catcListV) {
        resetLayerViewSettings();
    }
    
    resetCheckedGroups();
}    


/**
 * Sets popup legedn over map visible
 * attached as onClick script to button
 */
function showPopupLegend() {
    var tocurl = PM_XAJAX_LOCATION + 'x_toc.php?' + SID + '&legendonly=1';
    showMapLegend(tocurl, ''); 
}


/**
 * for legStyle 'swap': swap from LAYER view to LEGEND view
 * attached as onClick script to button
 */
function swapToLegendView() {
    if (legendContainer.catStyle == 'tree') getActiveCats();
    var tocurl = PM_XAJAX_LOCATION + 'x_toc.php?' + SID + '&legendonly=1&swaplegend=1';
    swapLegend(tocurl, ''); 
    
    getCheckedGroups();
}

/**
 * for legStyle 'swap': swap from LEGEND view to LAYER view
 * attached as onClick script to button
 */
function swapToLayerView() {
    var legDiv = document.getElementById('toclegend');
    var tocDiv = document.getElementById('toc');
    //legDiv.style.visibility = 'hidden';
    tocDiv.style.visibility = 'visible';
    tocDiv.innerHTML = legendContainer.legTxt;
	resetCheckedGroups();
    // update TOC CSS depending on scale
    var tocurl = PM_XAJAX_LOCATION + 'x_toc_update.php?' + SID;
    updateTocScale(tocurl, '');
    
    //window.setTimeout("resetLayerViewSettings()", 200);
}

/**
 * for legStyle 'swap': when swapping back from LEGEND view to LAYER view:
 * reset settings for opened tree nodes
 */
function resetLayerViewSettings() {
    catcListV = legendContainer.catcListV;
    catcListH = legendContainer.catcListH;
    
    for (var v=0; v<catcListV.length; v++) {  
        document.getElementById(catcListV[v].id).style.visibility = 'hidden';
        tg(catcListV[v].id);
    }
    
    for (var h=0; h<catcListH.length; h++) {  
        document.getElementById(catcListH[h].id).style.visibility = 'visible';
        tg(catcListH[h].id);
    }
}

function resetCheckedGroups() {
    //alert(legendContainer.checkedGrpList);
    if (legendContainer.checkedGrpList) {
        var checkedGrpList = legendContainer.checkedGrpList;
        var checkedGrpListL = checkedGrpList.length;
        
        if (checkedGrpListL > 0) {
            for (var x=0; x<checkedGrpListL; x++) {
                var chkGrp = document.getElementById(checkedGrpList[x]);
                if (chkGrp) {
                    chkGrp.checked = true;
                }
            }
        }
    }
}

/**
 * Get and set active (opened) categories
 * store arrays for visible and hidden category contents in object property 
 */
function getActiveCats() { 
    var catcList = legendContainer.catcList;
    
    var catcListV = [];
    var catcListH = [];
    var v = 0;
    var h = 0;
    
    for (var i=0; i<catcList.length; i++) {  
        if (catcList[i].style.visibility == 'visible') {
            catcListV[v] = catcList[i];
            v++;
        } else {
            catcListH[h] = catcList[i];
            h++;
        }
    }
    
    legendContainer.catcListV = catcListV;
    legendContainer.catcListH = catcListH;
}

function getCheckedGroups() {
    var checkedGrpList = [];
    var g = 0;
    var cbxList = document.getElementsByName('groupscbx');
    if (cbxList.length > 0) {
        for (var i=0; i<cbxList.length; i++) {
            if (cbxList[i].checked) {
                checkedGrpList[g] = cbxList[i].id;
                g++;
            }
        }
    }
    
    /*
    var radList = document.getElementsByName('groupsrad');
    if (radList.length > 0) {
        for (var j=0; j<radList.length; j++) {
            if (radList[j].checked) {
                checkedGrpList[g] = radList[j].id;
                g++;
            }
        }
    }
    */
    
    legendContainer.checkedGrpList = checkedGrpList;
}



function setDefGroups() {
    //for (var i=0; i<defGroups.length; i++) {
        //document.getElementById('laycbx_' + defGroups[i]).checked = true ;
    //}

    //tg('cat_admin');
}



