/**
 *
 * jQuery.ScrollGallery - scroll elements
 * @author Profitroom
 *
 */

function ScrollGallery(options) {

	var $this = this;

	this.options = $.extend({
		unit : '.unit',
		paginate: false,
		direction: 'horizontal',
		autoscroll: true,
		visibleItems : 1,
		duration : 5000,
		speed: 500,
		easing: 'swing'
	}, options);

	this.unit = new Array();
	this.autoscroll = this.options.autoscroll;
	this.direction = 
	this.options.direction;
	this.duration = this.options.duration;
	this.speed = this.options.speed;
	this.easing = (jQuery.easing[this.options.easing] !== undefined) ? this.options.easing : 'swing';
	this.paginateClass = (this.options.paginate !== false) ? $(this.options.paginate) : this.options.paginate;
	
	$(this.options.unit).each(function(i, el) {
		if ($(el).css('display') !== 'none') {
			$this.unit.push($(el));
		}
	});
	
	this.firstUnit = (this.unit[0] !== undefined) ? this.unit[0] : jQuery('<div />');
	this.galleryContainer = this.firstUnit.parent();
	this.mainHolder = this.galleryContainer.parent();	
	
	this.scrollWidth = (this.direction === 'vertical') ? parseInt(this.firstUnit.css('height')) : parseInt(this.firstUnit.css('width'));
	this.amount = this.unit.length;
	this.fullWidth = this.scrollWidth * this.amount;
	this.currentPage = 0;
	this.currentWidth = 0;
	this.rows = this.options.visibleItems;
	this.paginationTab = new Array();
	this.paginationAmount = (this.rows > 1) ? Math.ceil(this.amount/this.rows)+1 : this.amount;
	this.scrollDirection = 0;
	this.interval;
	
	if (this.mainHolder.css('position') === 'static') this.mainHolder.css('position', 'relative');
	this.galleryContainer.css({
		'position': 'absolute',
		'left': '0px'
	});
	
	if (this.direction === 'vertical') {
		this.galleryContainer.css('height', this.fullWidth+'px');
	} else {
		this.galleryContainer.css('width', this.fullWidth+'px');
	}
	
	this.scroll = function(value) {
		if (this.direction === 'horizontal') {
			var scrollOptions = {
				'left': -value+'px'
			};
		} else {
			var scrollOptions = {
				'top': -value+'px'
			};
		}

		this.galleryContainer.stop().animate(scrollOptions, $this.speed, $this.easing);	
		$this.currentWidth = value;
	};
	
	this.scrollForward = function(click) {
		if (click !== undefined) {
			this.stopAutoScroll();
			this.scrollDirection = 0;
		}
		if (this.currentWidth+this.scrollWidth <= this.scrollWidth*(this.amount - this.rows)) {
			this.scroll(this.currentWidth+this.scrollWidth);
			this.currentPage++;
			if (this.paginateClass !== false) this.markPage(this.currentPage);
			if (click !== undefined) this.startAutoScroll();
		}
	};
	
	this.scrollBackward = function(click) {
		if (click !== undefined) {
			this.stopAutoScroll();
			this.scrollDirection = 1;
		}
		if (this.currentWidth-this.scrollWidth >= 0) {
			this.scroll(this.currentWidth-this.scrollWidth);
			this.currentPage--;
			if (this.paginateClass !== false) this.markPage(this.currentPage);
			if (click !== undefined) this.startAutoScroll();
		}
	};
	
	this.autoScroll = function() {
		if ((this.currentWidth == 0 || this.currentPage == 0) && this.scrollDirection == 1)
			this.scrollDirection = 0;
		if ((this.currentWidth == this.scrollWidth*(this.amount - this.rows) || this.currentPage == this.paginationAmount) && this.scrollDirection == 0)
			this.scrollDirection = 1;
			
		if (this.scrollDirection == 0)
			$this.scrollForward();
		if (this.scrollDirection == 1)
			$this.scrollBackward();
	};
	
	this.startAutoScroll = function() {
		if ($this.autoscroll) {
			$this.interval = setInterval(function() {
				$this.autoScroll()
			}, $this.duration);
		}
	};
	
	this.stopAutoScroll = function() {
		if ($this.autoscroll) {
			clearInterval($this.interval);
		}
	};
	
	this.openPage = function(pageNo) {
		this.localScrollWidth = pageNo*$this.scrollWidth;
		$this.scroll(this.localScrollWidth);
		$this.currentPage = pageNo;
		$this.markPage($this.currentPage);
		$this.startAutoScroll();
	};
	
	this.markPage = function(pageNo) {
		for(var i=0; i < $this.paginationTab.length; i++) {
			$this.paginationTab[i].removeClass('active');
		}
		if ($this.paginationTab.length > 0)
			$this.paginationTab[pageNo].addClass('active');
	};
	
	this.paginate = function() {
		this.paginer = $this.paginateClass;
		this.paginationParent = this.paginer.parent();
		this.cloneTab = new Array();
		for(var i=0; i < $this.paginationAmount; i++) {
			this.cloneTab[i] = this.paginer.clone();
			this.paginationParent.append(this.cloneTab[i]);
			this.cloneTab[i].data("iter", i);
			this.cloneTab[i].click(function(){
				$this.stopAutoScroll();
				$this.openPage($(this).data("iter"));
			});
		}
		this.paginer.remove();
		$this.paginationTab = this.cloneTab;
	};	
	
	this.onPageLoad = function() {
		if ($this.paginateClass !== false) {
			$this.paginate();
			$this.markPage(0);
		}	

		if ($this.autoscroll) {
			$this.startAutoScroll();
			$this.mainHolder.bind({
				mouseenter: function() {
					$this.stopAutoScroll();
				},
				mouseleave: function() {
					$this.startAutoScroll();
				}
			});
		}
	}();
	
}

