function expandSearchCat(cat)
{
    var liContainer = $('li-' + cat);
    if ( liContainer )
    {
        // check if child-li already exists;
        var childUL = false;
        for ( var x = 0 ; x < liContainer.childNodes.length ; ++x )
            if ( liContainer.childNodes.item(x).nodeName == 'UL' )
                childUL = liContainer.childNodes.item(x);
                
        if ( childUL )
        {
            childUL.style.display = 'block';
        }
        else
        {
            var spinner = document.createElement('IMG');
            spinner.src = '/images/spinner.gif';
            spinner.style.border = '0';
            spinner.style.marginLeft = '45px';
            
            var childLI = document.createElement('LI');
            childLI.appendChild(spinner);
            var childUL = document.createElement('UL');
            childUL.style.listStyle = 'none';
            childUL.style.listStyleImage = 'none';
            childUL.appendChild(childLI);
            liContainer.appendChild(childUL);
            
            new Ajax.Request('/ajaxcats.php', {
                                                 method: 'get'
                                                 ,parameters: { category: cat }
                                                 ,onSuccess: function(transport) { laadCats(cat,transport.responseText); }
                                                 });
        }
        
        var arrow = $('arr-' + cat);
        if ( arrow )
            arrow.src = '/images/arrow_dwn.gif';
            
        var anchor = $('anc-' + cat);
        if ( anchor )
        {
            anchor.href = '#';
            anchor.onmouseup = function () { collapseSearchCat(cat); }
        }
    }
}

function collapseSearchCat(cat)
{
    var liContainer = $('li-' + cat);
    if ( liContainer )
    {
        for ( var x = 0 ; x < liContainer.childNodes.length ; ++x )
            if ( liContainer.childNodes.item(x).nodeName == 'UL' )
                liContainer.childNodes.item(x).style.display = 'none';
        
        var arrow = $('arr-' + cat);
        if ( arrow )
            arrow.src = '/images/arrow_right.gif';
            
        var anchor = $('anc-' + cat);
        if ( anchor )
        {
            anchor.href = '#';
            anchor.onmouseup = function () { expandSearchCat(cat); }
        }
    }
}

function laadCats(cat, json)
{
    var data = $A(json.evalJSON(false));
    var liContainer = $('li-' + cat);
    if ( liContainer )
    {
        var ul;
        for ( var x = 0 ; x < liContainer.childNodes.length ; ++x )
            if ( liContainer.childNodes.item(x).nodeName == 'UL' )
                ul = liContainer.childNodes.item(x);
        if ( ul )
        {
            // clean ul
            var itemCount = ul.childNodes.length;
            for ( var x = 0 ; x < itemCount ; ++x )
                ul.removeChild(ul.childNodes.item(0));
            // insert data
            data.each( function(item) {
                
                if ( item['c'] == 'Y' )
                {
                    var img = document.createElement('IMG');
                    img.id = 'arr-' + item['id'];
                    img.src = '/images/arrow_right.gif';
                    img.style.marginRight = '7px';
                    
                    var anchor = document.createElement('A');
                    anchor.id = 'anc-' + item['id'];
                    anchor.href = '#';
                    anchor.onmouseup = function() { expandSearchCat(item['id']); }
                    anchor.appendChild(img);
                }
                else
                {
                    var anchor = document.createElement('SPAN');
                    anchor.className = 'spacer';
                }
                
                var cb = document.createElement('INPUT');
                cb.type = 'checkbox';
                cb.name = 'cat-' + item['id'];
                cb.id = 'cat-' + item['id'];
                cb.value = item['id'];
                cb.style.border = '0';
                cb.style.marginRight = '7px';
                
                var label = document.createElement('LABEL');
                label.htmlFor = 'cat-' + item['id'];
                label.appendChild(document.createTextNode(item['n']));
            
                var li = document.createElement('LI');
                li.id = 'li-' + item['id'];
                li.appendChild(anchor);
                li.appendChild(cb);
                li.appendChild(label);
                ul.appendChild(li);
            });
        }
    }
}


function blurSimpleSearch()
{
    if ( $('simplesearch').value == '' )
        $('simplesearch').value = zoeken;
}

function focusSimpleSearch()
{
    if ( $('simplesearch').value == zoeken )
        $('simplesearch').value = '';
}

function showColorPicker()
{
    document.location.href='#content';
    $('content').className = 'dimmed';
    $('colorpicker').style.display = 'block';
}

function showDistributionChart()
{
    document.location.href='#content';
    $('content').className = 'dimmed';
    $('colordistributionchart').style.display = 'block';
}

function closeDistributionChart()
{
    $('content').className = '';
    $('colordistributionchart').style.display = 'none';
}

function preSearchSubmit()
{
    if ( $('simplesearch').value != '' && $('simplesearch').value != zoeken && $('search_key_1').value == '' )
    {
        if ( navigator.appName == 'Microsoft Internet Explorer' ) // a.k.a. P.O.S.
            document.forms.srchfrm.search_key_1.value = document.forms.srchfrm.simplesearch.value;
        else
            $('search_key_1').value = $('simplesearch').value;
    }

    var checkedCats = [];
    var inputs = $('catcontainer').getElementsByTagName('INPUT');
    for ( var x = 0 ; x < inputs.length ; ++x )
    {
        if ( inputs.item(x).checked && inputs.item(x).id.substr(0,4) == 'cat-' )
            checkedCats[checkedCats.length] = inputs.item(x).value;
    }
    $('search-cats').value = checkedCats.join(',');
    return true;
}
