$(document).ready(function(){
	initCufon();
	$('div#carusel').gallery({
		duration: 200,
		autoRotation: 10000,
		fadeEl: 'div.carusel-holder > ul > li',
		switcher: 'ul.swicher > li',
		next: 'span.link-next > a',
		prev: 'span.link-prev > a',
		circle: true 
	});
});
function initCufon() {
	Cufon.replace('#carusel ul .txt-box .sorting, .steps ul strong, .bungobox .box .see, .bungobox .box .more, .product-box .box .view, .facebook-block a, .nets-blocks .block a, #content .about-container h4.subtitle', { fontFamily: 'Archer-Bold'});
	Cufon.replace('.steps ul em', { fontFamily: 'Archer-Book'});
	Cufon.replace('.product-box .box .txt-box span, .moving, #content .sub-title, #content h2, #content h3, .text-holder h2, .reply h3, #sidebar h3 span', { fontFamily: 'Archer-Semibold'});
	Cufon.replace('.facebook-block span', { fontFamily: 'Archer-Medium'});
	Cufon.replace('.search-form label, .order-box strong, #carusel ul .txt-box .slogan, .steps ul .number, .bungobox h2, .product-box h2, .coll h3, #content h1, .styles strong, #sidebar .product-box .box .text-holder h3', { fontFamily: 'Ziggurat-HTF-Black'});
}
/* Gallery */
jQuery.fn.gallery = function(_options){ 
	// defaults options	
	var _options = jQuery.extend({
		duration: 700,
		autoRotation: false,
		slideElement: 1,
		effect: 'fade',
		fadeEl: 'ul',
		switcher: 'ul > li',
		disableBtn: false,
		next: 'a.link-next, a.btn-next, a.next',
		prev: 'a.link-prev, a.btn-prev, a.prev',
		circle: true,
		direction: false
	},_options);

	return this.each(function(){
		var _hold = $(this);
		var _speed = _options.duration;
		var _timer = _options.autoRotation;
		var _sliderEl = _options.slideElement;
		var _wrap = _hold.find(_options.fadeEl);
		var _el = _hold.find(_options.switcher);
		var _next = _hold.find(_options.next);
		var _prev = _hold.find(_options.prev);
		var _count = _el.index(_el.filter(':last'));
		var _w = _el.outerWidth(true);
		var _h = _el.outerHeight(true);
		if (!_options.direction) {
			var _wrapHolderW = Math.ceil(_wrap.parent().width() / _w);
			if (((_wrapHolderW - 1) * _w + _w / 2) > _wrap.parent().width()) _wrapHolderW--;
		}
		else{
			var _wrapHolderW = Math.ceil(_wrap.parent().height()/_h);
			if (((_wrapHolderW-1)*_h + _h/2) > _wrap.parent().height()) _wrapHolderW--;
		}
		if (_timer) var _t;
		var _active = _el.index(_el.filter('.active:eq(0)'));
		if (_active < 0) _active = 0;
		var _last = _active;
		if (!_options.effect) var rew = _count - _wrapHolderW + 1;
		else var rew = _count;
		
		if (!_options.effect) {
			if (!_options.direction) _wrap.css({marginLeft: -(_w * _active)})
			else _wrap.css({marginTop: -(_h * _active)})
		}
		else {
			_wrap.css({
				opacity: 0
			}).removeClass('active').eq(_active).addClass('active').css({
				opacity: 1
			}).css('opacity', 'auto');
			_el.removeClass('active').eq(_active).addClass('active');
		}
		if (_options.disableBtn) {
			if (_count < _wrapHolderW) _next.addClass(_options.disableBtn);
			_prev.addClass(_options.disableBtn);
		}
		
		function fadeElement(){
			if (_speed != 0) {
				_wrap.eq(_last).animate({
					opacity: 0
				}, {
					queue: false,
					duration: _speed
				});
				_wrap.eq(_active).animate({
					opacity: 1
				}, {
					queue: false,
					duration: _speed,
					complete: function(){
						$(this).css('opacity', 'auto');
					}
				});
			}
			else{
				_wrap.eq(_last).css({
					opacity: 0
				});
				_wrap.eq(_active).css({
					opacity: 'auto'
				});
			}
			_wrap.removeClass('active').eq(_active).addClass('active');
			_el.removeClass('active').eq(_active).addClass('active');
			_last = _active;
		}
		function scrollEl(){
			if (!_options.direction) _wrap.animate({marginLeft: -(_w * _active)}, {queue:false, duration: _speed})
			else _wrap.animate({marginTop: -(_h * _active)}, {queue:false, duration: _speed})
		}
		function toPrepare(){
			if ((_active == rew) && _options.circle) _active = -_sliderEl;
			for (var i = 0; i < _sliderEl; i++){
				_active++;
				if (_active > rew) {
					_active--;
					if (_options.disableBtn &&(_count > _wrapHolderW)) _next.addClass(_options.disableBtn);
				}
			};
			if (_active == rew) if (_options.disableBtn &&(_count > _wrapHolderW)) _next.addClass(_options.disableBtn);
			if (!_options.effect) scrollEl();
			else fadeElement();
		}
		function runTimer(){
			_t = setInterval(function(){
				toPrepare();
			}, _timer);
		}
		_next.click(function(){
			if(_t) clearTimeout(_t);
			if (_options.disableBtn &&(_count > _wrapHolderW)) _prev.removeClass(_options.disableBtn);
			toPrepare();
			if (_timer) runTimer();
			return false;
		});
		_prev.click(function(){
			if(_t) clearTimeout(_t);
			if (_options.disableBtn &&(_count > _wrapHolderW)) _next.removeClass(_options.disableBtn);
			if ((_active == 0) && _options.circle) _active = rew + _sliderEl;
			for (var i = 0; i < _sliderEl; i++){
				_active--;
				if (_active < 0) {
					_active++;
					if (_options.disableBtn &&(_count > _wrapHolderW)) _prev.addClass(_options.disableBtn);
				}
			};
			if (_active == 0) if (_options.disableBtn &&(_count > _wrapHolderW)) _prev.addClass(_options.disableBtn);
			if (!_options.effect) scrollEl();
			else fadeElement();
			if (_timer) runTimer();
			return false;
		});
		if (_options.effect) _el.click(function(){
			_active = _el.index($(this));
			if(_t) clearTimeout(_t);
			fadeElement();
			if (_timer) runTimer();
			return false;
		});
		if (_timer) runTimer();
	});
}
