;(function ($) {
	$.fn.scrollableSizer = function (options) {

		var Sizer = function (o) {
			this.o = o;
			this.api = this.o.scrollable();
			this.scroll_config = this.api.getConf();
			this.items = this.api.getItems();
			//this.width = Number(o.parent().innerWidth()) - Number(o.parent().css('padding'));
			//console.log("parent width: %d", o.parent().width());
			//console.log("parent padding: %d", o.parent().css('padding'));
			this.width = o.parent().width();
			this.item_width = $(this.items[0]).outerWidth();
			this.rpp = this.scroll_config.size;
		};

		Sizer.prototype.setWidth = function () {
			this.o.width(this.width);
			this.items.css({
				'margin-left': 0,
				'margin-right': 0
			});

			if ((this.item_width * this.rpp) > this.width) {
				this.rpp = Math.floor(this.width / this.item_width);
			}

			var ideal_item_width = this.width / this.rpp;
			var diff = ideal_item_width - this.item_width;

			var margin = (Math.floor(diff / 2));
			this.items.css({
				'margin-left': margin,
				'margin-right': margin
			});

			this.scroll_config.size = this.rpp;
		};

		Sizer.prototype.setHeight = function () {
			var children = this.o.children();
			var height = 0;
			children.each(function () {
				height += Number($(this).outerHeight(true));
				//console.dir($(this));
			});
			this.o.height(String(height) + 'px');
			this.height = height;
			//console.log(height);
		};

		return this.each(function () {
			var $this = $(this);

			$this.scrollableSizer = new Sizer($this);
			$this.scrollableSizer.setWidth();
			$this.scrollableSizer.setHeight();
		});
	};
})(jQuery)
