// config (imgCount must be equal Math.floor)
var _keyWidth = 25;
var _imgCount = 5;

// variables
var _w = 0;
var _wImg = 0;
var _gallerySize = 0;
var _gallerySizeChild = new Array();
var _galleryOpenChild = new Array();
var _gallerySlideBlock = false;

// resize
function _govResize(){
	for(_g = 0; _g < _gallerySize; _g++){
		_objGallery = $('div[@gallery="' + _g + '"]');
		_w = $(_objGallery).width() - (_keyWidth * 2);
		_wImg = Math.round(_w / _imgCount);

		$(_objGallery).children('div.sb_right').css({left: _w + _keyWidth});

		_iCount = 0;
		for(_i = _galleryOpenChild[_g]; _i < (_galleryOpenChild[_g] + _imgCount); _i++){
			$(_objGallery).children('span').eq(_i).css({left: (_iCount * _wImg) + _keyWidth, width: _wImg});
			_iCount++;
		}
	}
}

// slide
function _govSlide(_obj, _slide){
	if(_gallerySlideBlock) return false;

	_gallerySlideBlock = true;
	_num = $(_obj).parent().attr('gallery');
	_objGallery = $('div[@gallery="' + _num + '"]');

	switch(_slide){
		case 0:
			for(_i = _galleryOpenChild[_num]; _i < (_galleryOpenChild[_num] + _imgCount); _i++){
				$(_objGallery).children('span').eq(_i).animate({left: _w + _wImg}, 1000);
			}
			break;
		case 1:
			for(_i = _galleryOpenChild[_num]; _i < (_galleryOpenChild[_num] + _imgCount); _i++){
				$(_objGallery).children('span').eq(_i).animate({left: 0 - _wImg}, 1000);
			}
			break;
	}

	setTimeout('_gallerySlideBlock = false;', 1100);
	_govSlideMove(_num, _slide);
}

// slide move
function _govSlideMove(_num, _slide){
	_objGallery = $('div[@gallery="' + _num + '"]');

	switch(_slide){
		case 0:
			_open = _galleryOpenChild[_num] - _imgCount;
			if(_open < 0) _open = Math.floor(_gallerySizeChild[_num] / _imgCount) * 5;
			_galleryOpenChild[_num] = _open;
			_iCount = 0;
			for(_i = _galleryOpenChild[_num]; _i < (_galleryOpenChild[_num] + _imgCount); _i++){
				$(_objGallery).children('span').eq(_i).css({left: 0 - _wImg, width: _wImg});
				$(_objGallery).children('span').eq(_i).animate({left: (_iCount * _wImg) + _keyWidth}, 1000);
				_iCount++;
			}
			break;
		case 1:
			_open = _galleryOpenChild[_num] + _imgCount;
			if(_open > _gallerySizeChild[_num]) _open = 0;
			_galleryOpenChild[_num] = _open;
			_iCount = 0;
			for(_i = _galleryOpenChild[_num]; _i < (_galleryOpenChild[_num] + _imgCount); _i++){
				$(_objGallery).children('span').eq(_i).css({left: _w + _wImg, width: _wImg});
				$(_objGallery).children('span').eq(_i).animate({left: (_iCount * _wImg) + _keyWidth}, 1000);
				_iCount++;
			}
			break;
	}
}

// ready
$(document).ready(function(){
	_gallerySize = $('div.sb_gallery_list').length;
	for(_g = 0; _g < _gallerySize; _g++){
		_objGallery = $('div.sb_gallery_list').eq(_g);
		_gallerySizeChild[_g] = $(_objGallery).children('span').length;
		_galleryOpenChild[_g] = 0;

		$(_objGallery).attr('gallery', _g);

		for(_i = 0; _i < _gallerySizeChild[_g]; _i++){
			_img = $(_objGallery).children('span').eq(_i).html();
			$(_objGallery).children('span').eq(_i).html('<div align="center">' + _img + '</div>');
		}

		if(_gallerySizeChild[_g] <= _imgCount){
			$(_objGallery).children('div').css({cursor: 'default'});
			$(_objGallery).children('div').html('&#160;');
		}else{
			$(_objGallery).children('div.sb_left').click(function(){
				_govSlide(this, 0);
			});
			$(_objGallery).children('div.sb_right').click(function(){
				_govSlide(this, 1);
			});
		}
	}

	//arrow buttons opacity
	$('div.sb_gallery_list div.sb_left').css({opacity: 0.8});
	$('div.sb_gallery_list div.sb_right').css({opacity: 0.8});

	//browser fix
	if($.browser.opera) $('div.sb_gallery_list div').css({position: 'absolute'});
	if($.browser.opera || $.browser.safari) $('div.sb_gallery_list span').css({zIndex: 0});

	_govResize();
});

// resize
$(window).resize(function(){
	_govResize();
});
