(function($) {

    var methods = {
        init: function(options) {

            var settings = {
                'link': 'slideshow.xml',
                'delay': '1000',
                'autoplay': 'on'
            };

            $.op = $.extend(settings, options);
            var that = $(this);
            $.currentSlide = 0;
            $.slideshow = $(that);
            $.nav = $("#nav");
            $.totalSlides = 0;
            $.rotator = [];

            return this.each(function() {
                // If options exist, lets merge them
                // with our default settings
                $.ajax({
                    url: '' + $.op.link + '',
                    type: 'GET',
                    dataType: 'xml',
                    async: false,
                    timeout: 1000,
                    error: function() {
                        $(this).html("error loading data");
                    },
                    success: function(data) {
                        $(data).find("slide").each(function(i, item) {
                            $(that).append($('<div class="slide"><div class="content-wrapper"><div class="content"><h3><a href="' + $(item).find('link').text() + '">' + $(item).find('title').text() + '</a></h3><p>' + $(item).find('description').text() + '</p></div></div></div>').css("background-image", "url(" + $(item).find('img').attr('src') + ")").click(function(){window.location=$(item).find('link').text()}));
                            $.rotator.push("<li id='" + (i + 1) + "'><a href='#'>" + (i + 1) + "</a></li>");
                        });
                    },
                    complete: function() {
                        // Variables
                        $.slides = $.slideshow.children();
                        $.totalSlides = $.slides.length;

                        if ($.totalSlides > 1) {

                            $.each($.rotator, function(i) {
                                var sid = "#" + (1 + i);
                                $(sid).click(function() {
                                    $.slideTo(i);
                                });
                            });

                            // Build Slider and Nav
                            $(that).append('<div class="rotator"><ul id="nav">' + $.rotator.join('') + '</ul></div>');
                            $.slides.wrapAll('<div class="slideshow-slider" />');
                            $.slider = $('div.slideshow-slider', $.slideshow);

                            $.slides.wrapAll('<div class="slideshow-content" />');
                            $.content = $('div.slideshow-content', $.slideshow);
                            $.setNavState();

                            // CSS
                            $.slides.css({
                                'display': 'inline',
                                'float': 'left'
                            });

                            $.slideshow.css({
                                'width': $.slides.width(),
                                'position': 'relative'
                            });

                            $.slider.css({
                                'overflow': 'hidden',
                                'width': $.slides.width(),
                                'position': 'relative'
                            });

                            $.content.css({
                                'width': ($.slideshow.width() * $.totalSlides),
                                'height': $.slides.height()
                            });

                            // Mouse Events
                            $.slideshow.hover(

                            function() {
                                $.pause();
                            }, function() {
                                $.play();
                            });

                            $.nav.hover(

                            function() {
                                $.pause();
                            }, function() {
                                $.play();
                            });

                            $('#nav a').click(function(e) {
                                $.slideTo($(this).parent().index());
                                e.preventDefault();
                                this.blur();
                            });

                            // Start
                             $(document).ready(function() {
                            $.play();
                             });
                        }
                    }
                });

            });



        }



    };

    $.extend({

        slideTo: function(number) {
            $.currentSlide = number;
            $.slider.stop(true).animate({
                'scrollLeft': number * $.slides.width()
            }, 'slow');
            $.setNavState();
        },

        play: function() {
            clearInterval($.intervalId);
            $.intervalId = setInterval($.nextSlide, $.op.delay);
        },

        pause: function() {
            clearInterval($.intervalId);
        },
        nextSlide: function() {
            $.currentSlide++;
            if ($.currentSlide == $.totalSlides) {
                $.currentSlide = 0;
            }
            $.slideTo($.currentSlide);
        },
        previousSlide: function() {
            $.currentSlide--;
            if ($.currentSlide === 0) {
                $.currentSlide = $.totalSlides;
            }
            $.slideTo($.currentSlide);
        },

        setNavState: function() {
            $('#nav li').removeClass('selected');
            $('#nav li:eq(' + $.currentSlide + ')').addClass('selected');
        }
    });


    $.fn.slideShow = function(method) {

        if (methods[method]) {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        } else if (typeof method === 'object' || !method) {
            return methods.init.apply(this, arguments);
        } else {
            $.error('Method ' + method + ' does not exist on jQuery.slideshow');
        }


    };
})(jQuery);
