function scroller__create_table(scroller)
{
    var table = document.createElement('table');
    table.setAttribute('cellspacing', 0);
    table.setAttribute('cellpadding', 0);
    table.style.left = scroller['position'] + 'px';
    var tr = document.createElement('tr');
    var width = 0;
    while (width < scroller['div'].offsetWidth * 2)
        for (var pic_idx = 0; pic_idx < scroller['pictures'].length; pic_idx ++)
        {
            var pic = scroller['pictures'][pic_idx];
            if (!pic)
                continue;
            var td = document.createElement('td');
            var a = document.createElement('a');
            a.setAttribute('href', pic['hires_url']);
            a.onclick = new Function('show_picture("' + pic['hires_url'] + '");return false;');
            //a.setAttribute('href', 'javascript:show_picture("' + pic['hires_url'] + '")')
            a.setAttribute('target', '_blank');
            var img = document.createElement('img');
            img.setAttribute('width', pic['width']);
            img.setAttribute('height', scroller['height']);
            img.setAttribute('src', pic['url']);
            img.setAttribute('border', 0);
            a.appendChild(img);
            td.appendChild(a);
            tr.appendChild(td);
            width += pic.width;
        }
    table.appendChild(tr);
    return table;
}

function scroller__rotate(scroller)
{
    var rotated_pic;
    scroller['pictures'].push(rotated_pic = scroller['pictures'].shift());
    scroller['position'] += rotated_pic['width'];
    var new_table = scroller.create_table();
    scroller['div'].appendChild(new_table);
    scroller['div'].removeChild(scroller['table']);
    scroller['table'] = new_table;
}

var scrollers = {

    'default': {
        'name': 'default',
        'speed': 1,
        'height': 150,
        'pictures': [
            
            {
                'width': 106,
                'url': '/scroller/picture/1',
                'hires_url': '/scroller/picture/hires/1'
            },
            
            {
                'width': 115,
                'url': '/scroller/picture/2',
                'hires_url': '/scroller/picture/hires/2'
            },
            
            {
                'width': 173,
                'url': '/scroller/picture/4',
                'hires_url': '/scroller/picture/hires/4'
            },
            
            {
                'width': 103,
                'url': '/scroller/picture/6',
                'hires_url': '/scroller/picture/hires/6'
            },
            
            {
                'width': 166,
                'url': '/scroller/picture/7',
                'hires_url': '/scroller/picture/hires/7'
            },
            
            {
                'width': 94,
                'url': '/scroller/picture/12',
                'hires_url': '/scroller/picture/hires/12'
            },
            
            {
                'width': 112,
                'url': '/scroller/picture/9',
                'hires_url': '/scroller/picture/hires/9'
            },
            
            {
                'width': 80,
                'url': '/scroller/picture/3',
                'hires_url': '/scroller/picture/hires/3'
            },
            
            {
                'width': 100,
                'url': '/scroller/picture/10',
                'hires_url': '/scroller/picture/hires/10'
            },
            
            {
                'width': 102,
                'url': '/scroller/picture/11',
                'hires_url': '/scroller/picture/hires/11'
            },
            
            {
                'width': 139,
                'url': '/scroller/picture/5',
                'hires_url': '/scroller/picture/hires/5'
            },
            
            {
                'width': 90,
                'url': '/scroller/picture/8',
                'hires_url': '/scroller/picture/hires/8'
            },
            
        ],
        'create_table': function () {
            return scroller__create_table(this);
        },
        'rotate': function () {
            return scroller__rotate(this);
        }
    },

};

function scrollers__update()
{
    for (var scroller_name in scrollers)
    {
        var scroller = scrollers[scroller_name];
        scroller['position'] -= scroller['speed'];
        scroller['table'].style.left = scroller['position'] + 'px';
        if (scroller['pictures'][0]['width'] < -scroller['position'])
            scroller['rotate']();
            
    }
    
    scrollers__start();
}

function scrollers__stop()
{
  clearTimeout(scrollers__timeout);
}

function scrollers__start()
{
  scrollers__timeout = setTimeout('scrollers__update()', 100);
}

for (var scroller_name in scrollers)
{
    if (!scrollers[scroller_name])
    {
        delete scrollers[scroller_name];
        continue;
    }
    
    for (var pic_idx = scrollers[scroller_name]['pictures'].length; pic_idx >= 0; pic_idx --)
        if (!scrollers[scroller_name]['pictures'][pic_idx])
            delete scrollers[scroller_name]['pictures'][pic_idx];
}

var scrollers__timeout = null;
var scrollers__onload = window.onload;

window.onload = function () {
  for (var scroller_name in scrollers)
  {
    var scroller = scrollers[scroller_name];
    scroller['div'] = document.getElementById('scroller-' + scroller_name);
    scroller['div'].onmouseover = scrollers__stop;
    scroller['div'].onmouseout = scrollers__start;
    
    if (!scroller['div'])
    {
        delete scrollers[scroller_name];
        continue;
    }
    
    scroller['position'] = 0;
    scroller['table'] = scroller.create_table();
    scroller['div'].appendChild(scroller['table']);
  }
  
  if (scrollers__onload)
    scrollers__onload();
    
  scrollers__start();

};

