jQuery.noConflict();

 (function($j) {

    Gallery = {

        images: {
            source: '#Media img',
            count: 0,
            paginationID: '#Pagination',
            current: 0
        },

        init: function() {

            var self = this;
            self.processGallery();

        },

        processGallery: function() {

            var self = this;

            self.images.count = $j(self.images.source).size();

            if (self.images.count <= 1) {
                $j(self.images.source + ':first').show();
                return
            }

            var listToInsert = '';
            listToInsert += '<li class="button previous"><a href="#" title="Previous Image">Previous</a></li>';
            $j(self.images.source).each(function(i) {
                var item = i + 1;
                listToInsert += '<li class="numeric"><a href="#" title="Image ' + item + '">' + item + '</a></li>';
            });
            listToInsert += '<li class="button next"><a href="#" title="Next Image">Next</a></li>';
            $j(self.images.paginationID).empty().append(listToInsert);

            self.hideAll();
            self.addEvents();

            $j(self.images.paginationID + ' li.numeric:first').click();

        },

        hideAll: function() {

            var self = this;

            $j(self.images.source).each(function(i) {
                $j(this).hide();
                $j(self.images.paginationID + ' li').removeClass('current');
            });

        },

        addEvents: function() {

            var self = this;

            $j(self.images.paginationID + ' li.numeric').live('click',
            function() {

                var i = $j(self.images.paginationID + ' li').index(this);

                self.hideAll();
                $j(this).addClass('current');
                $j(self.images.source + ':nth-child(' + i + ')').show();
                self.images.current = i;

                return false;

            });

            $j(self.images.paginationID + ' li.next').live('click',
            function() {

                if (self.images.current != self.images.count) {
                    self.hideAll();
                    self.images.current++;
                    $j(self.images.source + ':nth-child(' + self.images.current + ')').show();
                    $j(self.images.paginationID + ' li:nth-child(' + (self.images.current + 1) + ')').addClass('current');
                }

                return false;

            });

            $j(self.images.paginationID + ' li.previous').live('click',
            function() {

                if (self.images.current > 1) {
                    self.hideAll();
                    self.images.current--;
                    $j(self.images.source + ':nth-child(' + self.images.current + ')').show();
                    $j(self.images.paginationID + ' li:nth-child(' + (self.images.current + 1) + ')').addClass('current');
                }
                return false;

            });

        }
    };

    $j().ready(function() {
        Gallery.init();
    });

})(jQuery);


