/* Copyright 2010 Christian Rick */

(function($) {
  $.fn.extend({
    ulslider: function(options) {
      var defaults = {  
        interval: 5000,  
        speed: 1000,  
        direction: 'left',
        easing: 'swing',
        callback: function(current) {},
        ids: {}
      };  
      var options = $.extend(defaults, options);  
    
    
      return this.each(function() {
        var element = $(this);
        if(element.children('li').length>1) {
          var xoffset = $(this).width();
          var yoffset = $(this).height();
          switch(options.direction.toLowerCase())
          {
            case 'down':
              element.css('position', 'relative').children().css('top', '-' + yoffset + 'px').css('position', 'absolute').css('overflow', 'hidden').width(xoffset).height(yoffset);
              element.children(':first').css('top', '0px').addClass('current');
              
              options.ids.id = setInterval(function() {
                element.children('.current').animate({top: yoffset+'px'}, options.speed,options.easing);
                element.children('.current + LI').animate({top: '0px'}, options.speed, options.easing, function() {options.callback(this); $(this).addClass('current').prev().removeClass('current').css('top', '-' + yoffset + 'px').remove().appendTo($(this).parent());});
              }, options.interval);
              break;
            case 'up':
              element.css('position', 'relative').children().css('top', yoffset + 'px').css('position', 'absolute').css('overflow', 'hidden').width(xoffset).height(yoffset);
              element.children(':first').css('top', '0px').addClass('current');
              
              options.ids.id = setInterval(function() {
                element.children('.current').animate({top: '-' + yoffset+'px'}, options.speed, options.easing);
                element.children('.current + LI').animate({top: '0px'}, options.speed, options.easing, function() {options.callback(this); $(this).addClass('current').prev().removeClass('current').css('top', yoffset + 'px').remove().appendTo($(this).parent());});
              }, options.interval);
              break;
            case 'right':
              element.css('position', 'relative').children().css('left', '-' + xoffset + 'px').css('position', 'absolute').css('overflow', 'hidden').width(xoffset).height(yoffset);
              element.children(':first').css('left', '0px').addClass('current');
              
              ids.id = setInterval(function() {
                element.children('.current').animate({left: xoffset+'px'}, options.speed, options.easing);
                element.children('.current + LI').animate({left: '0px'}, options.speed, options.easing, function() {options.callback(this); $(this).addClass('current').prev().removeClass('current').css('left', '-' + xoffset + 'px').remove().appendTo($(this).parent());});
              }, options.interval);
              break;
            case 'fade':
              var zindex=0;
              element.css('position', 'relative').children().css('top', '0px').css('left', '0px').css('position', 'absolute').css('overflow', 'hidden').css('z-index', '900').width(xoffset).height(yoffset);
              element.children(':last').addClass('current').css('z-index', '902');
              options.ids.id = setInterval(function() {
                element.children('.current').fadeOut(options.speed, function() {
                  var current = element.children('.current');
                  var next = current.prev();
                  current.css('z-index', '900').css('display', 'block').remove().prependTo(element).removeClass('current');
                  next.css('z-index', '902').addClass('current');
                  
                });
              }, options.interval+options.speed);
              break;
            default:
              element.css('position', 'relative').children().css('left', xoffset + 'px').css('position', 'absolute').css('overflow', 'hidden').width(xoffset).height(yoffset);
              element.children(':first').css('left', '0px').addClass('current');
              options.ids.id = setInterval(function() {
                element.children('.current').animate({left: '-'+xoffset+'px'}, options.speed, options.easing);
                element.children('.current + LI').animate({left: '0px'}, options.speed, options.easing, function() {$(this).addClass('current').prev().removeClass('current').css('left', xoffset + 'px').remove().appendTo($(this).parent());});
              }, options.interval);
              break;
          }
        }
      });
    }
  });
})(jQuery);
