var moving = false;
function activeerPortfolio()
{
    var portfolios = $$('.portfolio');
    for ( var x = 0 ; x < portfolios.length ; ++x )
    {
        var portfolio = portfolios[x];
        
        var liCount = 0;
        for ( var y = 0 ; y < portfolio.childNodes.length ; ++y )
            if ( portfolio.childNodes[y].nodeName.toUpperCase() == 'LI' )
                liCount++;
        
        cleanWhitespace(portfolio);
        
        if ( liCount > 1 )
        {
            var ol = document.createElement('OL');
            for ( var z = 0 ; z < liCount ; ++z )
            {
                var li = document.createElement('LI');
                var a = document.createElement('A');
//                a.href = '#'; //'javascript:void();';
                a.id = 'p' + x + 'l' + z;
                if ( z == 0 )
                    a.className = 'current';
                a.onmouseup = function() { beweegPortfolio(this); }
                a.appendChild(document.createTextNode(z + 1));
                li.appendChild(a);
                ol.appendChild(li);
            }
            portfolio.parentNode.appendChild(ol);
        }
    }
}

function beweegPortfolio(obj)
{
    var li = obj.parentNode;
    var ol = li.parentNode;
    var slider = ol.parentNode;
    var ul = null;
    for ( var x = 0 ; x < slider.childNodes.length ; ++x )
    {
        if ( slider.childNodes[x].nodeName.toUpperCase() == 'UL' )
        {
            ul = slider.childNodes[x];
            break;
        }
    }
    
    if ( ul && !moving )
    {
        var left = parseInt(ul.style.left);
        if ( isNaN(left) ) { left = 0; }
        
        var currentIndex = Math.abs(left / 800);
        var newIndex = obj.id.replace(/p[0-9]+l/,'');
        var portfolio = obj.id.replace(/l[0-9]+/,'').replace(/p/,'');

        moving = true;
        $('p' + portfolio + 'l' + currentIndex).className = '';
        if ( currentIndex < newIndex )
            new Effect.Move(ul, { x: (-800 * (newIndex - currentIndex)), y: 0, mode: 'relative', afterFinish: function() { moving=false; $('p' + portfolio + 'l' + newIndex).className = 'current'; } });
        else if ( currentIndex > newIndex )
            new Effect.Move(ul, { x: (800 * (currentIndex - newIndex)), y: 0, mode: 'relative', afterFinish: function() { moving=false; $('p' + portfolio + 'l' + newIndex).className = 'current'; } });
    }    
}

function cleanWhitespace(node)
{
  for (var i=0; i<node.childNodes.length; i++)
  {
    var child = node.childNodes[i];
    if(child.nodeType == 3 && !/\S/.test(child.nodeValue))
    {
      node.removeChild(child);
      i--;
    }
    if(child.nodeType == 1)
    {
      cleanWhitespace(child);
    }
  }
  return node;
}

