(function($) {
    $.fn.tarsRotator = function(settings) {
        var config = {'autorotate': false, 'ajaxurl': '/ajax'};
        if (settings) $.extend(config, settings);
        this.each(function() {
            var resultData;
            var isComplete = true;
            var timed;
            $.getJSON(config.ajaxurl, function(data) {
                resultData = data;
                
                if(config.autorotate) {
                    timed = setInterval(function() {
                        var currentElem = $('#switchpages > li.current');
                        var nextElem = currentElem.next();
                        if(nextElem.length == 0) {
                            nextElem = currentElem.siblings().first();
                        }
                        var nextNum = parseInt(nextElem.text(), 10);
                        if(nextNum != NaN && isComplete) {
                            moveFromTo(currentElem, nextElem.find('a'), nextNum);
                        }
                    }, 10000);
                }
                
                $('#switchpages > li:not(.current) > a').live('click', function() {
                    if(timed) {
                        clearInterval(timed);
                        timed = null;
                    }
                    $this = $(this);
                    var currentElem = $('#switchpages > li.current');
                    var itemNum = parseInt($this.text(), 10);
                    if(itemNum != NaN && resultData[itemNum - 1] && isComplete) {
                        moveFromTo(currentElem, $this, itemNum);
                        return false;
                    } else {
                        return true;
                    }
                });
            });
            
            function moveFromTo(currentElem, nextElem, itemNum) {
                isComplete = false;
                var currentNum = parseInt(currentElem.text(), 10);
                var myData = resultData[itemNum - 1];
                var img = $("<img>");
                img.attr('src', myData.img);
                $('#image').prepend(img);
                var options;
                if(currentNum < itemNum) {
                    options = {'right': 525};
                } else {
                    options = {'left': 525};
                }
                $('#content').fadeOut(150, function() {
                    $('#content').html(myData.data);
                    $('#content').fadeIn(150);
                });
                $('#image img:last-child').animate(options, {
                    duration: 300,
                    complete: function() {
                        if(!isComplete) {
                            $("#image img:last-child").remove();
                            nextElem.parent().addClass('current');
                            nextElem.parent().siblings().removeClass('current');
                            isComplete = true;
                        }
                    }
                });
            }
        });
        return this;
    };
})(jQuery);
