(function ($) {
	
    $.certificates = function (el, options) {
        var self = this,
			node = $(el),
			holder = $('.holder', node),
			strip = $('ul', node),
			blocks = $('> li', strip);
			amount = blocks.size(),
			pages = Math.ceil(amount / 3),
			speed = $.browser.msie ? 0 : 'fast';
			
        node.data('certificates', this);
		
        this.init = function () {
            this.options = $.extend({}, $.certificates.defaultOptions, options);
			
			this.active = 0;
			this.width = blocks.width() + parseInt(blocks.css('padding-right')) + parseInt(blocks.css('margin-right'));
			
			if (amount > 0) {
				strip.width(this.width * amount);
				this.arrows = $('<a class="prev"></a><a class="next"></a>');
				this.arrows.prependTo(node).click(this._arrows);
				
				var navi = '',
					a;
					
				for (a = 0; a < pages; a++) {
					navi += '<a' + (a == 0 ? ' class="active"' : '') + '></a>';
				}
				
				navi = $('<div>' + navi + '</div>');
				navi.addClass('switch').appendTo(node);
				this.navi = $('a', navi);
				this.navi.each(function (i) {
					$(this).data('i', i);
				}).click(this._navi);
				
				blocks.each(function () {
					if ($('.inner', $(this)).size()) {
						$('a', $(this)).click(self._lightbox);
					}
				});
				
				this.check();
			}
        };
		
		this._lightbox = function () {
			if (self.off == 1) return;
			self.off = 1;
			
			self.lightbox = $('#lightbox');
			var target = $(this),
				inner = target.next().clone();
			
			if (!self.lightbox.size()) self.lightbox = $('<table class="lightbox" id="lightbox"><tr><td class="lightbox"></td></tr></table>').appendTo('body');
			
			$('td.lightbox', self.lightbox).html(inner);
			
			self.lightbox.fadeIn(speed, function () {
				self.off = 0;
				$(document).bind('click.lightbox', function (e) {
					var target = $(e.target);
					
					if (target.parent().parent().parent().is('#lightbox')) self.hide();
				});
				$(this).find('.closebutmain a').click(function(e){
					e.preventDefault();
					self.hide();
				});
			});
		};
		
		this.hide = function () {
			if (self.off == 1) return;
			self.off = 1;
			self.lightbox.fadeOut(speed, function () {
				self.off = 0;
				$(document).unbind('click.lightbox');
			});
		};
		
		this._navi = function () {
			var target = $(this),
				index = target.data('i');
			
			if (target.hasClass('active')) return;
			
			self.active = index;
			self.navi.removeClass('active').eq(index).addClass('active');
			self.check();
			strip.stop().animate({left: -index * self.width * 3});
		};
		
		this._arrows = function () {
			var target = $(this),
				page;
			
			if (target.hasClass('prev')) {
				self.active--;
			} else {
				self.active++;
			}
			
			self.navi.eq(self.active).trigger('click');
		};
		
		this.check = function () {
			this.arrows.hide();
			if (this.active > 0) this.arrows.eq(0).show();
			if (this.active < pages - 1) this.arrows.eq(1).show();
		};
		
        this.init();
    };

    $.certificates.defaultOptions = {
    };

    $.fn.certificates = function (options) {
        return this.each(function () {
            (new $.certificates(this, options));
        });
    };
	
})(jQuery);

$('#certificates').certificates();
