(function($) {
    $.fn.slideshow = function(options) {
        var settings = {
            autoSlide:'Y',
            autoSlideSpeed:12,
            fadeSpeed:12
        };

        return this.each(function() {
            if (options) {
                o = $.extend(settings, options);
            }

            var wrapper = $(this);
            var wrapperUl = $(this).children('ul');
            var slideCount = wrapperUl.children('li').length;
            var autoSlideSpeedConstant = 300;
            var fadeSpeedConstant = 150;
            var timerHandle = null;
            var working = false;

            //construct controls
            var controlWrapper = $('<div>', {id:'controlWrapper'}).addClass('controlWrapper');
			var controls = $('<div>', {id:'controls'}).addClass('controls');
            var controlWidth = 40;
            var leftArrow = $('<div>').addClass('leftArrow').click(
                    function() {
                        clearTimeout(timerHandle);
                        goToPage(current - 1);
                    }).appendTo(controls);

            //extract info
            wrapperUl.children('li').each(function(i) {
                var _src = $(this).children('span').text();
                var _title = $(this).children('h5').text();
                var _link = $(this).children('a').attr('href');

                $(this).children('*').remove();

                //add image back to it
                var e = null;
                if (_link.length) {
                    e = $('<a>', {href:_link,title:_title});
                    e.append($('<img>', {src:_src,alt:_title}));
                }
                else {
                    e = $('<img>', {src:_src,alt:_title});
                }
                $(this).append(e);

                //contstruct control
                $('<div>', {'index':i}).addClass('numberControl').click(
                        function() {
                            clearTimeout(timerHandle);
                            goToPage($(this).prevAll('.numberControl').length);
                        }).appendTo(controls);
                controlWidth += 22;
            });

            //construct controls
            var rightArrow = $('<div>').addClass('rightArrow').click(
                    function() {
                        clearTimeout(timerHandle);
                        goToPage(current + 1);
                    }).appendTo(controls);

			controlWrapper.append(controls.width(controlWidth));
            wrapper.append(controlWrapper);
            wrapperUl.show();

            //initialize slideshow
            var current = 0;

            if (settings.autoSlide == 'Y') {
                timerHandle = setTimeout(function() {
                    goToPage(current + 1);
                }, settings.autoSlideSpeed * autoSlideSpeedConstant)
            }

            controls.children('.numberControl:eq(' + current + ')').addClass('active');

            function goToPage(next) {
                if (!working) {
                    working = true;
                    if (next != current) {
                        next = next < slideCount ? next : 0;
                        var currentSlide = wrapperUl.children('li:eq(' + current + ')');
                        var nextSlide = wrapperUl.children('li:eq(' + next + ')');

                        currentSlide.fadeOut(settings.fadeSpeed * fadeSpeedConstant, function() {
                            controls.children('.numberControl').removeClass('active');
                            controls.children('.numberControl:eq(' + next + ')').addClass('active');

                            nextSlide.fadeIn(settings.fadeSpeed * fadeSpeedConstant, function() {
                                current = next;

                                if (settings.autoSlide == 'Y') {
                                    timerHandle = setTimeout(function() {
                                        goToPage(current + 1);
                                    }, settings.autoSlideSpeed * autoSlideSpeedConstant)
                                }
                                working = false;
                            });
                        });

                    }
                }
            }
        });
    };
})(jQuery);
