/**
 *  Scroller Code
 *
**/

var shift_lock    = false;
var shift_visible = 3;
var shift_width   = 0;
var shift_height  = 0;

var scroller_mode = 'x';


function shift(o, mode) {

  o = typeof(o) == 'object' ? o : document.getElementById(o);

  if( o && shift_lock == false ) {

        shift_lock  = true;
    var li          = o.getElementsByTagName('li');

    if( li.length >= shift_visible ) {

      var first     = li[0];
      var last      = li[li.length - 1];
      var next      = shift_visible <= li.length ? li[shift_visible]      : last;
      var visible   = shift_visible <= li.length ? li[shift_visible - 1]  : last;

      shift_width   = typeof(first.clientWidth) == 'number' ? first.clientWidth : first.offsetWidth;
      shift_height  = typeof(first.clientHeight) == 'number' ? first.clientHeight : first.offsetHeight;

      scroller_mode = document.getElementById('scroller').className == 'box normal' ? 'y' : 'x';


      switch(mode) {
      case 'back':

        var ofade = fader(visible, 30);

        ofade.fade(
          0, function() {
              slide(
                o, '+',
                function() {
                  o.style.left = 0;
                  o.style.top  = 0;
                  ofade.fade_set(10);

                  var ifade = fader(last, 30);
                      ifade.fade_set(0);

                  o.insertBefore(last, first);

                  ifade.fade(
                    10, function() {
                          shift_lock = false;
                  });
                }
              ) // ! slide
        });

      break;
      default:

        var ofade = fader(first, 30);

        ofade.fade(
          0, function() {
              slide(
                o, '-',
                function() {
                  o.style.left = 0;
                  o.style.top  = 0;
                  ofade.fade_set(10);

                  var ifade = fader(next, 30);
                      ifade.fade_set(0);

                  o.appendChild(first);

                  ifade.fade(
                    10, function() {
                          shift_lock = false;    
                  });
                }
              ) // ! slide  
        });

      break;
      }

    }

  }

} // ! shift()



var slide_timeout = 1;
var slide_count   = 0;

function slide(o, mode, f) {

  if( o ) {

    slide_count++;

    var wh      = scroller_mode == 'x' ? shift_width : shift_height;
    var offset  = scroller_mode == 'x' ? o.offsetLeft : o.offsetTop;

    if( Math.abs(offset) < wh ) {

      var scroller_style = scroller_mode == 'x' ? 'left' : 'top';

      o.style[scroller_style] = (mode == '-' ? -(slide_count * 10) : (slide_count * 10)) + 'px';

      setTimeout(
        function() {
          slide(o, mode, f);
        },
        slide_timeout
      );

    } else if( f ) {

      slide_count = 0;

      setTimeout(f, 0);

    }

  }

} // ! slide()


