/*
 * gallerySlider 1.0
 *
 * http://www.slyks.pl
 *
 * Copyrights 2011 Pawel Kargol (www.slyks.pl)
 * 
 */

(function($) {
	$.fn.gallerySlider = function(o) {
		return this.each(function() {
			new $.gallerySlider(this, o);
		});
	};
	var defaults = {
		speed : 200
	};
	$.gallerySlider = function(el, o) {
		var self = this;
		this.o = $.extend( {}, defaults, o || {});
		this.$list = $(el);
		
		$(window).load(function() {
			self.init();
		});
	};
	$.gallerySlider.fn = $.gallerySlider.prototype = {};
	$.gallerySlider.fn.extend = $.gallerySlider.extend = $.extend;
	$.gallerySlider.fn
			.extend( {
				init : function() {
					this.$items = this.$list.children();
					this.$clip = this.$list.parent().parent().parent();
					this.$container = this.$clip.parent();
					
					this.itemW = this.$items.eq(0).outerWidth(true);
					this.clipMax = this.$clip.width();
					this.dimension = 'width';

					this.itemsVisible = this.clipMax/this.itemW;
					
					this.pos = 0;
					this.posMax = this.$items.length * this.itemW;
					this.$list.css(this.dimension, this.posMax + 'px');
		
					var self = this;
					this.funcMoveBack = function() { self.moveBack(); return false; };
					this.funcMoveForward = function() { self.moveForward(); return false; };
					this.$btnBack = $('#btnLeft',this.$container).addClass('disabled').click(this.funcMoveBack);
					this.$btnForward = $('#btnRight',this.$container).click(this.funcMoveForward);
				},
				moveForward : function() {
					if(this.pos > -(this.$items.length-this.itemsVisible)) {
						this.pos -= 1;
						this.$list.animate({left: this.pos*this.itemW}, this.o.speed);
					}
				},
				moveBack : function() {
					if(this.pos < 0) {
						this.pos += 1;
						this.$list.animate({left: this.pos*this.itemW}, this.o.speed);
					}
				}
			});
})(jQuery);


