function Slider() {
	// Animation speed
	this.speed = 600; // 600ms
	this.timeoutTime = 8000; // 8sec
	this.slideWidth = 0;
	this.lastSlideDiv = "";
	// Will hold all slide div names
	this.slides = new Array();
	this.slideCount = 0;
	this.timeout;
	var self = this;

	$(document).ready(function() {
		// Initialize variables and prepare the slides
		self.init();

		
		// Moves slide to the left
		$('#slider-arrow-left').click(function() {
			self.slideFromLeft();
		});

		// Moves slide to the right
		$('#slider-arrow-right').click(function() {
			self.slideFromRight();
		});
	});
}

Slider.prototype = {
	/**
	 * Initializes variables and prepare the slides
	 */
	init : function() {
		this.slideWidth = $('#slide-0').width();
		this.slideCount = $('div[id^="slide-"]').length;
		// $('#slider-main').css('width', (this.slideWidth*this.slideCount)+'px');
		this.lastSlideDiv = "#slide-" + (this.slideCount - 1);

		// Initialize slide div names
		for (var i = 0; i < this.slideCount; ++i) {
			this.slides[i] = "#slide-"+i;
			$(this.slides[i]).css('left', i*this.slideWidth+'px');
			// If this is the last slide, make it to be before first one
			if (i == this.slideCount-1) {
				var newPos = (parseInt($(this.slides[this.slideCount-1]).css('left'))) - this.slideCount * this.slideWidth;
				$(this.slides[this.slideCount-1]).css('left', newPos+'px');
				var next = this.slides.pop();
				this.slides.unshift(next);
			}
		}

		// Set the slider auto-timer
		this.resetTimeout();
	},

	/**
	 * Moves the big picture - #slider-main - from left to right
	 */
	slideFromLeft : function() {
		// If the animation is NOT in progress...
		if (!$('#slider-main').is(':animated')) {
			var self = this;
			$('#slider-main').animate({left: this.slideWidth}, this.speed, "", function() { self.shiftLeft() });
		}
	},

	/**
	 * Moves the the big picture - #slider-main - from right to left
	 */
	slideFromRight : function() {
		// If the animation is NOT in progress...
		if (!$('#slider-main').is(':animated')) {
			var self = this;
			$('#slider-main').animate({left: -(this.slideWidth) }, this.speed, "", function() { self.shiftRight() });
		}
	},

	/**
	 * Makes the last slide element the first one.
	 */
	 shiftLeft : function() {
		var newPos = (parseInt($(this.slides[this.slideCount-1]).css('left'))) - this.slideCount * this.slideWidth;
		$(this.slides[this.slideCount-1]).css('left', newPos+'px');
		var next = this.slides.pop();
		this.slides.unshift(next);

		// Reset slider and slides position to avoid huge margins
		this.resetPositionFromLeft();

		// Reset timeout
		this.resetTimeout();
	},

	/**
	 * Makes the first slide element the last one.
	 */
	 shiftRight : function() {
		var newPos = (parseInt($(this.slides[0]).css('left'))) + (this.slides.length) * this.slideWidth;
		$(this.slides[0]).css('left', newPos+'px');
		var next = this.slides.shift();
		this.slides.push(next);

		this.resetPositionFromRight();

		// Reset timeout
		this.resetTimeout();
	},

	/**
	 * Resets the #slider-main position and moves the slides accordingly
	 * (in order to prevent incrementing margin)
	 */
	resetPositionFromLeft : function() {
		for (var i = 0; i < this.slideCount; ++i) {
			var currentSlidePos = parseInt($(this.slides[i]).css('left'));
			$(this.slides[i]).css('left', (currentSlidePos+this.slideWidth)+'px');
		}
		$('#slider-main').css('left', '0px');
		// alert(currentSliderPos-this.slideWidth);
	},
	resetPositionFromRight : function() {
		for (var i = 0; i < this.slideCount; ++i) {
			var currentSlidePos = parseInt($(this.slides[i]).css('left'));
			$(this.slides[i]).css('left', (currentSlidePos-this.slideWidth)+'px');
		}
		$('#slider-main').css('left', '0px');
	},

	resetTimeout : function() {
		var self = this;
		clearTimeout(this.timeout);
		this.timeout = setTimeout(function() { self.slideFromRight() } , self.timeoutTime);
	},
}
