var bodyIntervalId = 0;
var sliderIntervalId = 0;
var slider;
window.onload = function(){
	slider = document.getElementById('communitySlider');
};
var leftpix = 0;
var maxwidth = 3468;
var sliding = false;
var amount = 204;
var count = 0;

function moveright() {
	var left = parseInt(slider.style.left.replace('px',''));
	if (left > leftpix) {
		left = (left * 1) - 17;
		slider.style.left = left + 'px';
	} else {
		sliding = false;
		clearInterval(sliderIntervalId);
	}
}

function moveleft() {
	var left = parseInt(slider.style.left.replace('px',''));
	if (left < leftpix) {
		left = (left * 1) + 17;
		slider.style.left = left + 'px';
	} else {
		sliding = false;
		clearInterval(sliderIntervalId);
	}
}

function Slide(inc) {
	if (sliding) { 
		return; 
	}
	if (inc === '') { 
		inc = 1; 
	}
	count++;

	// give up after a while
	if (count > 20) {
		 clearInterval(bodyIntervalId);
	}

	sliding = true;
	leftpix -= inc * amount;
	var limit = (maxwidth - amount) * -1;
	if (leftpix < limit) {
		leftpix = 0;
		slider.style.left = leftpix + 'px';
		sliding = false;
		return;
	}
	if (leftpix > 0) {
		leftpix = limit;
		slider.style.left = leftpix + 'px';
		sliding = false;
		return;
	}
	if (inc > 0){
		sliderIntervalId = setInterval(moveright, 10);
	} else {
		sliderIntervalId = setInterval(moveleft, 10);
	}
}

function initialize() {
	var bodyIntervalId = setInterval(slide, 3000);
}
