BWS.Slider = function() { }

BWS.Slider.Init = function (id) {
	this.slider  = document.getElementById('slider-' + id);
	this.speed   = 30;
	this.width   = this.slider.firstChild.clientWidth;
	this.slotNum = 4;

	this.prev    = document.getElementById('slider-' + id + '-prev');
	this.next    = document.getElementById('slider-' + id + '-next');
	this.marginLeftNow = parseInt(this.slider.style.marginLeft);
	this.marginLeftTo  = parseInt(this.slider.style.marginLeft);
	var tmp = this;

	if (this.next) NG.addEventListener(this.prev, 'click', function(e) {tmp.Move(1);});
	if (this.next) NG.addEventListener(this.next, 'click', function(e) {tmp.Move(-1);});

}

BWS.Slider.setMargin = function(o) {
	this.marginLeftNow = this.marginLeftNow + (o * this.width / this.speed);
	this.slider.style.marginLeft = this.marginLeftNow +'px';
}

BWS.Slider.Move = function (o) {
	// restrict the movement to the end
	if ( (o > 0 && this.marginLeftTo < 0) || 
		 (o < 0 && -1*this.marginLeftTo + this.slotNum*this.width < parseInt(this.slider.style.width)) ) this.marginLeftTo = this.marginLeftTo + (this.width * o);

	if (this.marginLeftTo > this.marginLeftNow) this.setMargin(1);
	else if (this.marginLeftTo < this.marginLeftNow) this.setMargin(-1);
		
	var tmp = this;
	if (this.marginLeftTo != this.marginLeftNow) setTimeout(function() { tmp.Move(0); }, this.speed);

}



