(function($){  

$.fn.SKSlider = function(vars) {       
    
  var element     = this;
  var timeOut     = (vars.timeOut != undefined) ? vars.timeOut : 1000;
  var current     = null;
  var last	= null;
  var timeOutFn   = null;
  var faderStat   = true;
  var mOver       = false;
  var Item	= $("#" + element[0].id);
  var items       = $("#" + element[0].id + ">ul>li");
  var itemsSpan   = [];
  var width	= $(element[0]).width();
  var height	= $(element[0]).height();

  items.each(function(i) {

    $this=$(this);
    $this.css({
      'width':width+'px',
      'height':height+'px',
      'padding':'0px',
      'margin':'0px',
      'border-radius':'15px',
      'display':'none',
      'position':'absolute',
      '-moz-border-radius':'15px'

    });

    $this.wrapInner ('<div class="span"></div>');
    itemsSpan[i] = $('.span',$this);

    if (!i) {
      $this.show();
    }

    if ($('.span',$this).html().length)
      itemsSpan[i].wrapInner('<div style="padding:10px"></div>');

    $this.mouseover(function() {
      mOver = true;
    });
    
    $this.mouseout(function() {
      mOver   = false;
      fadeElement(true);
    });

  });
  
  var fadeElement = function() {

    if (timeOutFn)
      return false;

    if(items.length > 1) {
        timeOutFn = setTimeout(makeSlider, timeOut);
    }
  }
  
  var makeSlider = function() {
    timeOutFn = null;
    if(!mOver) {
      if (last == null) {
        current = items[0];
        last = items[items.length-1];
        $(itemsSpan[0]).fadeIn((timeOut/3), function() {
          fadeElement();
        });
        return;

      } else {
        var currNo = jQuery.inArray(current, items)+1;
        currNo = (currNo == items.length) ? 0 : currNo;
      }

      next = items[currNo];

      $(last).hide();
      $(current).css('z-index',1);
      $(next).css('z-index',12);
      $(itemsSpan[currNo]).hide();


      $(next).fadeIn((timeOut/2),function() {
        $(itemsSpan[currNo]).fadeIn((timeOut/3), function() {
          if(!mOver) {
              fadeElement();
          }
        });
      });
      last = current;
      current = next;
    }
  }
  
  makeSlider();

};  

})(jQuery);  

