jQuery.fn.extend({
	silSlideShow: function(options) {
		return this.each(function() {
			new jQuery.silSlideShow(this, options);
		});
	}
});

jQuery.silSlideShow = function(obj, opt) {

 	var opt = opt || {};
	 opt.changeTime = opt.changeTime || 3000;
	 opt.fadeTime = opt.fadeTime || 1000;
	 opt.autoPlay = opt.autoPlay || 0;
	 opt.picTable = opt.picTable || [];
	 opt.changeBg = opt.changeBg || true;
         opt.txtTable = opt.txtTable || [];
         opt.private = {Count:0,Busy:0,Loading:0};
         var $select = $(obj);
         tmp = [];
  
  //Przygotuj warstwy zawarte w tej warstwie do rotacji
  opt.tmp=0;
  $select.children("div").map(function() {
    $(this).attr("count",opt.tmp).hide();
    opt.tmp++;
  });
  
  //Wstaw playera
  $select.append("<div id=silSlideShow_Menu></div>");
  $("#silSlideShow_Menu").append("<ul id=silSlideShow_Buttons></ul>");
  $("#silSlideShow_Buttons").append("<div id=silSlideShow_Txt></div>");


  if(opt.private.Count == 0 && opt.picTable.length) {
    $select.children("div[count='0']").show();
  }

  $("#silSlideShow_Next").click(function() {
    opt.private.Count++;
    $select.children("div[count]").fadeOut(opt.fadeTime, function() {
      if(opt.private.Count >= $select.children("div[count]").size()) opt.private.Count = 0;
      $select.children("div[count='"+opt.private.Count+"']").show();
    });
  });

  $("#silSlideShow_Previous").click(function() {
    opt.private.Count--;
    $select.children("div[count]").fadeOut(opt.fadeTime, function() {
      if(opt.private.Count < 0) opt.private.Count = $select.children("div[count]").size()-1;
      $select.children("div[count='"+opt.private.Count+"']").show();
    });
  });

  $("#silSlideShow_Play").click(function() {
    var test = setInterval(play, opt.changeTime);
    $select.children("div[count]:last").attr("handle", test);
    opt.private.Busy = 1;
  });

  function play() {
    $select.children("div[count]").hide();
    opt.private.Count++;
    if(opt.private.Count >= $select.children("div[count]").size()) opt.private.Count = 0;
    $select.children("div[count='"+opt.private.Count+"']").show();
//    //$("#silSlideShow_Picture").fadeOut(opt.fadeTime, function() {
//    $("#silSlideShow_Block_wrap").fadeOut(opt.fadeTime, function() {
//      if(opt.private.Count > opt.picTable.length-1) opt.private.Count = 0;
//      $("#silSlideShow_Picture").attr("src", opt.picTable[opt.private.Count]);
//      //$("#silSlideShow_Picture").fadeIn(opt.fadeTime);
//      $("#silSlideShow_Block_wrap").fadeIn(opt.fadeTime);
//      $("#silSlideShow_Txt").html(opt.txtTable[opt.private.Count])
//    });
  }

  $("#silSlideShow_Pause").click(function() {
    var test = $select.children("div[count]:last").attr("handle");
    opt.private.Busy = 0;
    clearInterval(test);
    $("#silSlideShow_Picture").css({"filter":"alpha(opacity=0)", "opacity":"1.0", "-moz-opacity":"1"});
  });
  
  if(opt.autoPlay == 1) {
    var test = setInterval(play, opt.changeTime);
    $select.children("div[count]:last").attr("handle", test);
  }

}
