jQuery.fn.gallSlide = function (_options) {
    // defaults options	
    var _options = jQuery.extend({
        duration: 700,
        autoSlide: 5000
    }, _options);

    return this.each(function () {
        var _hold = $(this);
        var _speed = _options.duration;
        var _timer = _options.autoSlide;
        var _wrap = _hold.find('ul.slider');
        var _el = _hold.find('ul.slider > li');
        var _next = _hold.find('a.next');
        var _prev = _hold.find('a.prev');
        var _count = _el.index(_el.filter(':last'));
        var _w = _el.outerWidth();
        var _wrapHolderW = Math.ceil(_wrap.parent().width() / _w);
        var _active = 0;
        if ($(_el).length == 0) {
            $(_next).hide();
            $(_prev).hide();
        }
        function scrollEl() {
            _wrap.eq(0).animate({
                marginLeft: -(_w * _active) + "px"
            }, { queue: false, duration: _speed });
        }
        _next.click(function () {
            _active++;
            if (_active > (_count - _wrapHolderW + 1)) _active = 0;
            scrollEl();
            return false;
        });
        _prev.click(function () {
            _active--;
            if (_active < 0) _active = _count - _wrapHolderW + 1;
            scrollEl();
            return false;
        });
    });
}


jQuery.fn.btnSlide = function(_options){
	// defaults options	
	var _options = jQuery.extend({
		duration: 700,
		autoSlide: 5000
	},_options);

	return this.each(function(){
		var _hold = $(this);
		var _speed = _options.duration;
		var _timer = _options.autoSlide;
		var _wrap = _hold.find('ul.list');
		var _el = _hold.find('ul.list > li');
		var _next = _hold.find('a.next');
		var _prev = _hold.find('a.prev, a.back');
		var _count = _el.index(_el.filter(':last'));
		var _w = _el.outerWidth();
		var _wrapHolderW = Math.ceil(_wrap.parent().width()/_w);
		var _t;
		var _active = 0;
		var _btn = $('<ul class="switcher"></ul>');
		_hold.find('div.paging').append(_btn);
		_el.each(function(_i){
			_btn.append('<li><a href="#">'+(_i+1)+'</a></li>');
		});
		_btn = _btn.find('a');
		_btn.parent('li').removeClass('active');
		_btn.eq(_active).parent('li').addClass('active');
		function scrollEl(){
			_wrap.eq(0).animate({
				marginLeft: -(_w * _active) + "px"
			}, {queue:false, duration: _speed});
			_btn.parent('li').removeClass('active');
			_btn.eq(_active).parent('li').addClass('active');
		}
		function runTimer(){
			_t = setInterval(function(){
				_active++;
				if (_active > (_count - _wrapHolderW + 1)) _active = 0;
				scrollEl();
			}, _timer);
		}
		
		_btn.click(function(){
			_active = _btn.index($(this));
			if (_active > (_count - _wrapHolderW + 1)) _active = 0;
			scrollEl();
			return false;
		});
		
		_next.click(function(){
			_active++;
			if (_active > (_count - _wrapHolderW + 1)) _active = 0;
			scrollEl();
			return false;
		});
		_prev.click(function(){
			_active--;
			if (_active < 0) _active = _count - _wrapHolderW + 1;
			scrollEl();
			return false;
		});
		_hold.mouseenter(function(){
			if(_t) clearTimeout(_t);
		}).mouseleave(function(){
			//runTimer()
		});
	});
}
function initEdit(){
	var _box = $('form.editable');
	$('a.edit-btn').click(function(){
		if(!$(this).hasClass('editing')){
			$(this).addClass('editing');
			_box.removeClass('noeditable');
		}
		else{
			$(this).removeClass('editing');
			_box.addClass('noeditable');
		}
		return false;
	});
}

this.applyPagingNextPrev = function (container, replacefield) {
    var parent = $(container);
    var replacew = replacefield ? replacefield : "div:first";
    $(".paging A", $(parent)).live("click", function () {
        $.get($(this).attr("rel"), function (response) {
            var res = replacefield ? $(response).find(replacew) : $(response).filter(replacew);
            $(parent).find(replacew).empty().html(res).hide().fadeIn('slow');
        });

        return false;
    });
}

$(document).ready(function () {
    $('div.gallery').gallSlide({
        duration: 700,
        autoSlide: 4000
    });
    /*$('div.donations, div.add-list').btnSlide({
    duration: 700,
    autoSlide: 4000
    });
    initEdit();*/

    //paging for donations on event page
    applyPagingNextPrev("#donations-paging");

});


