/*********************************************************************************
 Copyright (c) 2002-2005 Armin Burger
 
 Permission is hereby granted, free of charge, to any person obtaining 
 a copy of this software and associated documentation files (the "Software"), 
 to deal in the Software without restriction, including without limitation 
 the rights to use, copy, modify, merge, publish, distribute, sublicense, 
 and/or sell copies of the Software, and to permit persons to whom the Software 
 is furnished to do so, subject to the following conditions:
 
 The above copyright notice and this permission notice shall be included 
 in all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 
 FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHOR OR 
 COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 
 IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 
 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
**********************************************************************************/

/**********************************************************
 * 
 *
 *
 *********************************************************/
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');
}



