


var tmp_product_count = 0;
var tmp_product_offset = 0;
var tmp_scroll_stop = false;
function tmp_product_left() {
  tmp_product_offset--;
  if (tmp_product_offset < 0) tmp_product_offset = 0;
  pbu.$("#pcontainer").stop().animate({"marginLeft":(-tmp_product_offset*172)+"px"}, 250);
}

function tmp_product_right() {
  tmp_product_offset++;
  if (tmp_product_offset > tmp_product_count - 3) tmp_product_offset = tmp_product_count - 3;
  pbu.$("#pcontainer").stop().animate({"marginLeft":(-tmp_product_offset*172)+"px"}, 250);
}

function
ProductScroller(container, data)
{
  this.ses_usr_id = data.ses_usr_id;
  this.direction = 1;
  this.div = pbu.$("\
  <div id='productdisplay'>\
   <div id='navleft'><a href='#' title='Bakåt' onclick='tmp_scroll_stop = true; tmp_product_left();return false;'>Bakåt</a></div>\
   <div id='navright'><a href='#' title='Visa fler' onclick='tmp_scroll_stop = true; tmp_product_right();return false;'>Visa fler</a></div>\
   <div id='productholder'>\
    <div id='pcontainer' style='margin-left:0px;'>\
     <ul>\
     </ul>\
    </div>\
   </div>\
  </div>\
")[0];
  var scroller = this;
  pbu.$(container).empty();
  container.appendChild(this.div);
  this.staticproducts = data.products;
  this.dynamicproducts = [];
  this.applyUpdate();
  window.setInterval(function() {
    if (tmp_scroll_stop)
      return;
    if (tmp_product_offset >= tmp_product_count - 3)
      scroller.direction = -1;
    if (tmp_product_offset <= 0)
      scroller.direction = 1;
    if (scroller.direction == 1)
      tmp_product_right();
    else
      tmp_product_left();
  }, 5000);
}

ProductScroller.prototype.applyUpdate = function() {
  var products = [];
  var ul = pbu.$("ul", this.div)[0];
  pbu.$(ul).empty();
  for (var i = 0; i < this.staticproducts.length; i++) {
    products.push(this.staticproducts[i]);
    if (this.dynamicproducts.length > 0 && i < 3) {
      products.push(this.dynamicproducts[(i+pixboxGlobal.serverTime)%this.dynamicproducts.length]);
    }
  }
  tmp_product_count = products.length;
  for (var i = 0; i < products.length; i++) {
    var li = pbu.$("<li><div class='productpic'><a href=''><img style='width:150px;height:125px;' src='"+products[i].icon+"' alt='"+pbu.htmlescape(products[i].name)+"' /></a></div><div class='productdesc'><a href=''>"+pbu.htmlescape(products[i].name)+" <span class='arrow'>&raquo;</span></a></div></li>")[0];
    pbu.$("a", li).attr("href", products[i].url);
    ul.appendChild(li);
  }
}


