/*
 * jQuery galleryScrol v1.2.0 
 *
 * Copyright (c) 2008 Taranets Aleksey
 * email: aleks_tar@ukr.net
 * Licensed under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 */
jQuery.fn.galleryScroll = function(_options){
	// defaults options
	var _options = jQuery.extend({
		btPrev: 'a.link-prev',
		btNext: 'a.link-next',
		holderList: 'div',
		scrollElParent: 'ul',
		scrollEl: 'li',
		slideNum: false,
		duration : 1000,
		step: false,
		circleSlide: true,
		disableClass: 'disable',
		funcOnclick: null
	},_options)

	return this.each(function(){
		var _this = $(this);

		var _gWidth = jQuery(_options.holderList,_this).get(0).offsetWidth;
		var _liWidth = jQuery(_options.scrollEl,_this).get(0).offsetWidth;
		var _liSum = jQuery(_options.scrollEl,_this).length * _liWidth;
		var _margin = 0;
		var f = 0;
		var _step = 0;
		if (!_options.step) _step = _gWidth; else _step = _options.step*_liWidth;
		
		if (!_options.circleSlide) {
			jQuery(_options.btPrev,_this).addClass(_options.disableClass);
		}
		if (_options.slideNum && !_options.step) {
			var _lastSection = 1;
			var _sectionWidth = 0;
			while(_sectionWidth < _liSum)
			{
				 _sectionWidth = _sectionWidth + _gWidth;
				 if(_sectionWidth > _liSum) {
					_lastSection = _sectionWidth - _liSum;
				 }
			}
		}
	
		// click button 'Next'
		jQuery(_options.btNext,_this).click(function(){
			jQuery(_options.btPrev,_this).removeClass(_options.disableClass);
			if (_liSum - _gWidth  <= _margin + _step) {
				if (f == 0) {
					_margin = _liSum - _gWidth;
					f = 1;
					if (!_options.circleSlide) 
						jQuery(this).addClass(_options.disableClass);
				} 
				else {
					if (_options.circleSlide) 
						_margin = 0;
					f = 0;
				}
			} else _margin = _margin + _step;
			
			jQuery(_options.scrollElParent,_this).animate({marginLeft: -_margin+"px"}, {queue:false,duration: _options.duration });
			
			if (_options.slideNum && !_options.step) jQuery.fn.galleryScroll.numListActive(_margin,_options.slideNum,_gWidth,_lastSection);		
			if (jQuery.isFunction(_options.funcOnclick)) {
				_options.funcOnclick.apply(_this);
			}
			return false;
		});
		// click button 'Prev'
		jQuery(_options.btPrev, _this).click(function(){
			jQuery(_options.btNext,_this).removeClass(_options.disableClass);
			if (_margin - _step == -_step ) {
				if (!_options.circleSlide) {
					jQuery(this).addClass(_options.disableClass);
					_margin = 0;
				} else 	_margin = _liSum - _gWidth;
			}
			else if (_margin - _step < 0 && _margin - _step > -_step) _margin = 0;
			else _margin = _margin - _step;
			if (!_options.circleSlide && _margin==0) jQuery(this).addClass(_options.disableClass);
			
			jQuery(_options.scrollElParent,_this).animate({marginLeft: -_margin + "px"}, {queue:false, duration: _options.duration});
			
			if (_options.slideNum && !_options.step) jQuery.fn.galleryScroll.numListActive(_margin,_options.slideNum,_gWidth,_lastSection);
			
			if (jQuery.isFunction(_options.funcOnclick)) {
				_options.funcOnclick.apply(_this);
			}
			return false;
		});
		// Number list
		if (_options.slideNum && !_options.step) {
			jQuery.fn.galleryScroll.numListCreate(_options.slideNum, _liSum, _gWidth,_lastSection);
			jQuery.fn.galleryScroll.numListActive(_margin, _options.slideNum,_gWidth,_lastSection);
			
			jQuery('a',_options.slideNum).click(function(){
				jQuery(_options.btPrev,_this).removeClass(_options.disableClass);
				jQuery(_options.btNext,_this).removeClass(_options.disableClass);
				
				var _indexNum = jQuery('a',_options.slideNum).index($(this));
				_margin = _step*_indexNum;
				if (_margin + _step > _liSum) {
					_margin = _margin - (_margin - _liSum) - _step;
					if (!_options.circleSlide) jQuery(_options.btNext, _this).addClass(_options.disableClass);
				}
				jQuery(_options.scrollElParent,_this).animate({marginLeft: -_margin + "px"}, {queue:false, duration: _options.duration});
				
				if (!_options.circleSlide && _margin==0) jQuery(_options.btPrev,_this).addClass(_options.disableClass);
				jQuery.fn.galleryScroll.numListActive(_margin, _options.slideNum,_gWidth,_lastSection);
				return false;
			});
		}
		jQuery.fn.galleryScroll.numListCreate = function(_elNumList, _liSumWidth, _width, _section){
			var _numListElC = '';
			var _num = 1;
			var _difference = _liSumWidth + _section;
			while(_difference > 0)
			{
			     _difference = _difference - _width;
				 _numListElC += '<li><a href="">'+_num+'</a></li>';
				 _num++;
			}
			$(_elNumList).html('<ul>'+_numListElC+'</ul>');
		};
		jQuery.fn.galleryScroll.numListActive = function(_marginEl, _slideNum, _width, _section){
			$('a',_slideNum).removeClass('active');
			var _activeRange = _width - _section-1;
			var _n = 0;
			if (_marginEl != 0) {
				while (_marginEl > _activeRange) {
					_activeRange = (_n * _width) -_section-1;
					_n++;
				}
			}
			var _a  = (_activeRange+_section+1)/_width - 1;
			$('a',_slideNum).eq(_a).addClass('active');
		};
	})
};

