Array.prototype.shuffle = function(){
    var i = this.length;
    while(i){
        var j = Math.floor(Math.random()*i);
        var t = this[--i];
        this[i] = this[j];
        this[j] = t;
    }
    return this;
}

var imgList = ["images/index/img1.jpg","images/index/img2.jpg","images/index/img3.jpg","images/index/img4.jpg","images/index/img5.jpg","images/index/img6.jpg","images/index/img7.jpg","images/index/img8.jpg","images/index/img9.jpg","images/index/img10.jpg","images/index/img11.jpg","images/index/img12.jpg","images/index/img13.jpg","images/index/img14.jpg","images/index/img15.jpg","images/index/img16.jpg","images/index/img17.jpg"];
imgList.shuffle();
var randNumbers = [0,1,2];
randNumbers.shuffle();

var target = $("#switch");
var str = "current";
var time = 1750;
var cnt = 0;
var flag = false;
var loop;

target.empty();
shuffleElementOfDiv(randNumbers);

window.onload = function(){
  loop = setInterval("slideSwitch()", time);
}

function slideSwitch(){
  $(".current img").fadeOut("slow",function(){
    imgList.push($(this).attr("src"));
    $(this).attr("src",imgList.shift());
	$(this).fadeIn("slow",function(){
	  $(this).parent().removeClass(str);
      cnt++;
      if(cnt == 3) {
        randNumbers.shuffle();
        cnt = 0;
	  }
      $("#img" + randNumbers[cnt]).addClass(str);
	});
  });
  if(!flag) {
    clearInterval(loop);
    time = 3300;
    loop = setInterval("slideSwitch()", time);
    flag = !flag;
  }
}

function shuffleElementOfDiv(ary){
  var len = ary.length;
  for(var i = 0;i < len; i++) {
    target.append('<div id="img' + ary[i] + '"><img src="' + imgList.shift() + '">');
    if(i == 0) $("#img"  + ary[i]).addClass(str);
  }
}
