/** Scroller (wymaga wersji prototype >= 1.5.0)
 *
 * wymagane:
 * container_id: id kontenera ze scrollerem.
 * highlight_class: klasa boxow z highlightami.
 *
 * opcjonalne:
 * interval: co ile sekund przesuwamy (0.01)
 * offset: co ile pikseli przesuwamy (2)
 */
function initScroller(container_id, highlight_class, interval, offset) {
	var interval = (interval == null) ? 0.01 : interval;
	var offset = (offset == null) ? 2 : offset;
	var scroller = $(container_id);
	var highlights = $$('#' + container_id + ' .' + highlight_class); // lista highlightow
	var scroller_width = scroller.getDimensions().width;
	var highlight_width = highlights.first().getDimensions().width; // szerokosc pojedynczego elementu
	var highlights_count = highlights.length;
	
	scroller.setStyle({position: 'relative', overflow: 'hidden'});
	
	if((highlights_count-1) * highlight_width < scroller_width) {
	  //var missing_count = Math.floor((scroller_width - highlights_count * highlight_width)/highlight_width) + 10
	  var missing_count = 12;
	    for(var i = 0; i <= missing_count; i++) {  //ie 9 fix < replace with <=
	      var index = i %  highlights.length;
	      scroller.appendChild(highlights[index].cloneNode(true));
	    }
	  // update
	  for(i=0;i<highlights_count;i++) {
	  	scroller.removeChild( highlights[i] ); //ie 9 fix
	  }
	  highlights = $$('#' + container_id + ' .' + highlight_class);
	  highlights_count = highlights.length;
	  
	}
	

	// pozycja i styl poczatkowy
	highlights.each(function(value, index) {
	  value.setStyle({position: 'absolute', left: (index * highlight_width) + "px"});
	});
	function tick() {
	  highlights.each(function(value, index) {
	    var left = parseInt(value.getStyle('left')) - offset; // obcinamy px
	    if(left + highlight_width <= 0) {
	     left = ((highlights_count - 1) * highlight_width) + left + highlight_width;
	    }
		left=left+"px";
	    value.setStyle({left: left});
	  });
	}
	var executer = new PeriodicalExecuter(tick, interval);
	Event.observe(scroller, 'mouseover', function() {executer.callback = function(){}});
	Event.observe(scroller, 'mouseout', function() {executer.callback = tick});
};



function initScroller2(container_id, highlight_class, interval, offset) {
var interval = (interval == null) ? 0.01 : interval;
var offset = (offset == null) ? 2 : offset;
var scroller = $(container_id);
var highlights = $$('#' + container_id + ' .' + highlight_class); // lista highlightow
var scroller_width = scroller.getDimensions().height;
var highlight_width = highlights.first().getDimensions().height; // szerokosc pojedynczego elementu
var highlights_count = highlights.length;

scroller.setStyle({position: 'relative', overflow: 'hidden'});

if((highlights_count-1) * highlight_width < scroller_width) {
  var missing_count = Math.floor((scroller_width - highlights_count * highlight_width)/highlight_width) + 10
  
    for(var i = 0; i <= missing_count; i++) { //ie 9 fix < replace with <=
      var index = i %  highlights.length;
      
       scroller.appendChild(highlights[index].cloneNode(true));
      
    }
    scroller.removeChild( highlights[0] ); //ie 9 fix
  // update
  highlights = scroller.select('.'+highlight_class );
  //$$('#' + container_id + ' .' + highlight_class);
  highlights_count = highlights.length;
  //alert( highlights_count );
  
}



// pozycja i styl poczatkowy
highlights.each(function(value, index) {
  value.setStyle({position: 'absolute', top: (index * highlight_width) + "px"});
});


function tick() {
  highlights.each(function(value, index) {
    var top = parseInt(value.getStyle('top')) - offset; // obcinamy px
    if(top + highlight_width <= 0) {
     top = ((highlights_count - 1) * highlight_width) + top + highlight_width;
    }
	top=top+"px";
    value.setStyle({top: top});
  });
}
var executer = new PeriodicalExecuter(tick, interval);
Event.observe(scroller, 'mouseover', function() {executer.callback = function(){}});
Event.observe(scroller, 'mouseout', function() {executer.callback = tick});
};


