function scrollActionProcucts() {
	var self = this;
	
	this.speed = 5000;
	this.startDelay = 5000;
	this.width = 150;
	this.visibleCount = 4;
	this.products = $('#media_line .inside');
	this.status = 1;
	this.position = 0;
	this.productsCount = $('a', this.products).length;

	$('#media_line').mouseover(function() {
			self.stop();
		})
		.mouseout(function() {
			self.start();
		});
	
	$('#media_line .next').click(function() {
		self.movePrev();
	});
	
	$('#media_line .back').click(function() {
		self.moveNext();
	});
	

	this.start = function() {
		this.status = 1;
	}

	this.stop = function() {
		this.status = 0;
	}
	
	
	this.moveNext = function() {
		var newPosition;

		this.position--;

		if(this.position < - (this.productsCount - this.visibleCount)) {
			this.position = 0;
		}

		newPosition = (this.position * this.width);

		if(newPosition <= - (this.productsCount * this.width)) {
			newPosition = (this.productsCount - (this.visibleCount - 1)) * this.width;
		}

		newPosition = newPosition + 'px';
		this.products.animate({left: newPosition});
	}
	
	
	this.movePrev = function() {
		var newPosition;

		this.position++;

		if(this.position > 0) {
			this.position = - (this.productsCount - this.visibleCount);
		}

		newPosition = (this.position * this.width);

		if(newPosition >= this.productsCount * this.width) {
			newPosition = (this.productsCount - (this.visibleCount - 1)) * this.width;
		}

		newPosition = newPosition + 'px';
		this.products.animate({left: newPosition});
	}


	this.change = function() {
		if(this.status) {
			this.moveNext();

			self = this;
			this.timer = setTimeout(function() {self.change();}, this.speed);
		}
		else {
			self = this;
			this.timer = setTimeout(function() {self.change();}, 1000);
		}
	}

	if(this.productsCount > this.visibleCount) {
		setTimeout(function() {self.change()}, this.startDelay);
	}
}

$(document).ready(function() {
	var actionProducts = new scrollActionProcucts();
});

