var _cBox = null;

var _shift = 991;
var _duration = 500;

var fx = null;
var _scroll = false;
var _where = 0;
var els = [];

var items = [];
var path2items = 'items.xml';
var path2projects = 'projects.html';

var _popup_title = null;
var _popup_website = null;
var _popup_text = null;
var _popup_link = null;
var _popup_active = null;
var _popup = null;

var timer = null;
var timeout = 3000; /* 3 sec */

var _nbScroll = 0;

function initMarquee() {
      _cBox = $('marquee');
      if (_cBox)
      {
         addItems();
      }  
}

function addItems() {
   var temp = [];
   temp = _cBox.getElements("div.slide");
   
   temp.each(function(tmp){
      tmp._html = tmp.innerHTML;
      items.push($(tmp));
   });
   _cBox.innerHTML = "";
   _cBox._left = -991;
   _cBox.style.left = "-991px";
   for (i = 0; i < temp.length; i ++)
   {
      addItem(i, 0, i*_shift);
   }
   makeCarousel();
}

function addItem(_index, where, _left) {
   var div = document.createElement("div");
   div._index = _index;
   div.style.left = _left + "px";
   div._left = _left;
   
   div.className = "slide";
   div.id = items[_index].id;
   div.innerHTML = items[_index]._html;

   if (where) {
         var node = _cBox.getElements("div.slide")[0];
         _cBox.insertBefore(div, node);
   }
   else {
      _cBox.appendChild(div);
   }
}


function makeCarousel() {
   fx = new Fx.Styles(_cBox, {
      duration:_duration,
            wait:true,
            transition: Fx.Transitions.Quad.easeInOut,
            onStart: function(element) {
               _scroll = false;

               if (_where == 0)
               {
                  
                  if (items[els[items.length-1]._index+1])
                  {
                     addItem(els[items.length-1]._index+1, 0, els[items.length-1]._left+_shift);
                  }
                  else
                  {
                     addItem(0, 0, els[items.length-1]._left+_shift);
                  }
                  
                  element._left = element._left - _shift;
                  
               }
               if (_where == 1)
               {
                  
                  if (items[els[0]._index-1])
                  {
                     addItem(els[0]._index-1, 1, els[0]._left - _shift);
                  }
                  else
                  {
                     addItem(items.length-1, 1, els[0]._left - _shift);
                  }
                  
                  element._left = element._left + _shift;
               }
               
            },
            onComplete: function(element) {
               if (_where == 0)
               {
                  els[0].remove();
               }
               if (_where == 1)
               {
                  els[items.length-1].remove();
               }
               
               els = element.getElements("div.slide");

               //if (_nbScroll<50) _scroll = true;
               // --- max number of swapping (prevent memory problem in ie) /EriC
                  if (_nbScroll<50) {
                     _scroll = true;
                  }
                  else {
                    clearTimeout(timer);
                    _where = 0;
                    _scroll = false;

                 
                 }
                 

         }
      }, _cBox);
     
     /*
      $('left-arrow').onmouseover = function() {
        // if (_scroll) {
            clearTimeout(timer);
            _where = 1;
            alert();
            _nbScroll=0;
            _scroll = true;
            fx.start({
               'left': _cBox._left + _shift
            });
        // }
         return false;
      };
      */
      $('left-arrow').onclick = function() {
     //    if (_scroll) {
            clearTimeout(timer);
            _where = 1;
    //        alert();
            _nbScroll=0;
            _scroll = true;
            fx.start({
               'left': _cBox._left + _shift
            });
            parseSifr();
     //    }
         return false;
      };

      $('right-arrow').onclick = function() {
            _nbScroll=0;
    //     if (_scroll) {
            clearTimeout(timer);
            _where = 0;
            _scroll = false;
            fx.start({
               'left': _cBox._left - _shift
            });
            parseSifr();
    //     }
         return false;
      };
      $(_cBox.parentNode).addEvent("mouseenter", function() {
         $('right-arrow').style.visibility = "visible";
         $('left-arrow').style.visibility = "visible";
      });
      $(_cBox.parentNode).addEvent("mouseleave", function() {
         $('right-arrow').style.visibility = "hidden";
         $('left-arrow').style.visibility = "hidden";
      });
      
      els = _cBox.getElements("div.slide");
      _scroll = true;
      
      timer = setTimeout("scroll()", timeout);
}
function scroll() {
   if (_scroll)
   {
      _nbScroll++;
     // alert(_nbScroll);
      _scroll = false;
      if (_where)
      {
         fx.start({
            'left': _cBox._left + _shift
         });
      }
      else
      {
         fx.start({
            'left': _cBox._left - _shift
         });
      }
      clearTimeout(timer);
      timer = setTimeout("scroll()", timeout);
   }
}

if (window.addEventListener) {
   window.addEventListener("load", initMarquee, false);
}
else if (window.attachEvent) {
   window.attachEvent("onload", initMarquee);
}

