﻿function Carousel (wrapName, caroName, careItemSelector, rightflipperName, leftflipperName, imageCount){
	// Let's define some variables first
	var wrapper = $('#' + wrapName); // The outer wrapper
	var carousel = $('#' + caroName); // The inner wrapper
	var items = carousel.find('li'); // The different elements, this is an array
	var itemwidth = 328; // The full width of a single item (incl. borders, padding, etc ... if there is any)
	var maxmargin = (imageCount) * itemwidth - itemwidth;
	
	//The function to browse forward
	function next_item(pos){
		if(pos == -maxmargin){
			carousel.animate({left: 0},500);
		} else {
			var newposition = pos - itemwidth;
			carousel.animate({left: newposition},500)
		}
	}

	// The function to browse backward
	function previous_item(pos){
		if(pos == 0){
			carousel.animate({left: -maxmargin},500);
		} else {
			var newposition = pos + itemwidth;
			carousel.animate({left: newposition},500);
		}
	}
	
	$('#' + rightflipperName).click(function(){
		var position = parseInt(carousel.css('left'));
		next_item(position);
	});

	$('#' + leftflipperName).click(function ()
	{
		var position = parseInt(carousel.css('left'));
		previous_item(position);
	});
}