(function ($) {
	
	$.slideShow = function (data, options) {
		return $.slideShow.impl.init(data, options);
	};
	
	/*
	 * Chained function to create a slideShow
	 * 
	 * @param {object} [options] An optional object containing options overrides
	 */
	$.fn.slideShow = function (options) {
		return $.slideShow.impl.init(this, options);
	};
	
	/*
	 * slideShow default options
	 */
	$.slideShow.defaults = {
	    index: 0
	};
	
	$.slideShow.impl = {
		
		/*
		 * slideShow options
		 */
		opts: null,
		
		/*
		 * Contains the slideShow data
		 */
		helper: {},
		
		/*
		 * Initialize the slideShow
		 */
		init: function (container, options) {
            
            // merge defaults and user options
            this.opts = $.extend({}, $.slideShow.defaults, options);
            
            $.slideShow.impl.helper = $(container).css('position', 'relative');
            
            $.slideShow.impl.helper.controls = $('<div></div>')
                .addClass('image-controls')
                .append('<div>Image</div>')
                .append('<ul></ul>')
                .insertAfter($.slideShow.impl.helper)
                ;
            
            $.slideShow.impl.helper.find('img').each(function(i, item){
                $(item).css('display', 'none');
                $(new Image()).load(function(){
                    $(item)
                        .css('position', 'absolute')
                        .css('left', '50%')
                        .css('top', '50%')
                        .css('margin-left', Math.floor($(item).width() / -2) + 'px')
                        .css('margin-top', Math.floor($(item).height() / -2) + 'px')
                        ;
                        if (i == $.slideShow.impl.opts.index)
                        {
                            $(item)
                                .css('display', 'block')
                                ;
                        }
                }).attr('src', $(item).attr('src'));
                
                $.slideShow.impl.helper.controls.find('ul')
                    .append('<li><a href="#">' + (i + 1) + '</a></li>')
                    ;
            });
            
            this.bindEvents();
            
			return this;
		},
		
		/*
		 * Change the image
		 */
		changeImage: function (i) {
		    $($.slideShow.impl.helper.find('img')[i])
		        .css('display', 'block')
		        .parent().siblings().children()
		        .css('display', 'none')
		        ;
		},
		
		/*
		 * Bind events
		 */
		bindEvents: function () {
		    $.slideShow.impl.helper.controls.find('a').click(function(e){
		        e.preventDefault();
		        $.slideShow.impl.changeImage(parseInt($(this).text()) - 1);
		    });
		}
		
	};
})(jQuery);