(function(j){
    j.fn.extend({
        slideGallery: function(o){
            var g = j(this), u = g.find('ul'), c = 0, l = u.find('li'), i = l.find('img'), sW = function(){
                if (0 == j(this).width()) {
                    var src = this.src;
                    j(this).removeAttr('width').removeAttr('height').css({
                        width: '',
                        height: ''
                    });
                    this.src = '';
                    this.src = src;
                }
                j(this).parents('li').css('width', gW(this));
                return [j(this).width(), j(this).height()];
            }, gW = function(obj){
                var ow = j(obj).outerWidth(), w = j(obj).width();
                return ow >= w && 0 < w ? ow : 'auto';
            }, init = function(){
                var w = l.outerWidth(true), m = l.length * l.outerWidth(true), pa = j('<a />').html('previous').click(function(){
                    u.stop(true, true);
										if (0 > parseInt(u.css('margin-left'))) {
                        c--;
                        u.animate({
                            'margin-left': '+=' + l.outerWidth(true)
                        });
                    }
                }), p = j('<span class="prevBtn slideCtrl" />').append(pa), na = j('<a />').html('next').click(function(){
										u.stop(true, true);
										if ((l.length * l.outerWidth(true)) > parseInt(g.find('.slider').innerWidth()) + Math.abs(parseInt(u.css('margin-left')))) {
                        c++;
                        u.animate({
                            'margin-left': '-=' + l.outerWidth(true)
                        });
                    }
                }), n = j('<span class="nextBtn slideCtrl" />').append(na), sl = g.find('div.slider');
                g.removeClass('no-js');
                if (!sl.length) {
                    u.wrap(j('<div class="slider" />'));
                }
                u.css('width', m);
                if (!g.find('span.slideCtrl').length) {
                    g.append(p).append(n);
                }
                else {
                    pa = g.find('span.prevBtn a');
                    na = g.find('span.nextBtn a');
                }
                var lH = l.outerHeight();
                g.css('height', '').find('.slider').css({
                    height: lH
                });
                pa.css('margin-top', lH / 2);
                na.css('margin-top', lH / 2);
                isScrollable();
            }, isScrollable = function(){
                if (!j(g).length) {
                    return;
                }
                var m = l.length * l.outerWidth(true);
                g.find('span.slideCtrl a').css('display', parseInt(g.innerWidth()) >= m ? 'none' : '');
            };
            i.each(function(){
                j(this).parents('a').removeAttr('title').parent('li').css('width', j(this).outerWidth());
            }).load(sW);
            init();
            $(window).load(init);
        }
    });
})(jQuery);
jQuery(document).ready(function($){
    $('.slide-gallery').slideGallery();
});

