// Handle for the interval
var interval = new Array();
// Status of the interval
var intstatus = new Array();
// Position of the inner div
var innerpos = new Array();
// Size of one childelement
var elementsize = new Array();
// Pixel moved per interval
var speed = new Array();
// Counts the number of images
var activeimg = new Array();
var imgnumber = new Array();


function initscroll(uid,esize,espeed) {
  imgnumber[uid] = document.getElementById('sireadflickrfeed_scroll_inner_'+uid).getElementsByTagName('li').length - 4;
  activeimg[uid] = 1;
  intstatus[uid] = 0;
  speed[uid] = espeed;
  innerpos[uid] = 0;
  elementsize[uid] = esize;
  // Create an array with handles to all elements
  liarray = document.getElementById('sireadflickrfeed_scroll_inner_'+uid).getElementsByTagName('li');

  // Duplicate the first 5 elements for the endless effect if there are more then 5 elements, or hide the arrows if there are 5 or less elements
  if(imgnumber[uid] >= 2) {
  	for(i=1;i<=5;i++) {
  		var newli = document.createElement("li");
  		newli.innerHTML = liarray[i-1].innerHTML;
  		document.getElementById('sireadflickrfeed_scroll_inner_'+uid).appendChild(newli);
  	}
  } else {
  	//if (document.getElementById('galerie_scroll_prev_'+uid)) document.getElementById('galerie_scroll_prev_'+uid).style.display="none";
  	//if (document.getElementById('galerie_scroll_next_'+uid)) document.getElementById('galerie_scroll_next_'+uid).style.display="none";
		document.getElementById('sireadflickrfeed_prev_'+uid).style.display="none";
  	document.getElementById('sireadflickrfeed_next_'+uid).style.display="none";
  }
}

function setactiveimg(action,uid) {
	// Update the activeimg depending on the action
	if(action == '-') {
		if(activeimg[uid] > 1) { activeimg[uid] --; } else { activeimg[uid] = imgnumber[uid]+4; innerpos[uid] = innerpos[uid] + (imgnumber[uid]+4)*elementsize[uid]; }
	} else if(action == '+') {
		if(activeimg[uid] < imgnumber[uid]+5) { activeimg[uid] ++; } else { activeimg[uid] = 2; innerpos[uid] = innerpos[uid] - (imgnumber[uid]+4)*elementsize[uid]; }
	}
	// Start the interval if the interval is not already running
	if(intstatus[uid] == 0) { interval[uid] = window.setInterval("scroll("+uid+");",40); intstatus[uid] = 1; }
}

function scroll(uid) {
	// Calculate the position where the inner div should be at the end of the movement
	var movetoinnerpos = elementsize[uid]*(activeimg[uid]-1);
	if(movetoinnerpos == innerpos[uid]) {
		// End the interval if the inner div is at the right position
		window.clearInterval(interval[uid]); intstatus[uid] = 0;
	} else {
		// Calculate the new innerpos
		if(innerpos[uid] < movetoinnerpos) { innerpos[uid] += speed[uid]; } else { innerpos[uid] -= speed[uid]; }
		innerpos[uid] = Math.round(innerpos[uid] * 10) / 10;
	}
	// Set the position of the innerdiv to the calculated position
	document.getElementById('sireadflickrfeed_scroll_inner_'+uid).style.marginLeft= '-'+innerpos[uid]+'px';
}