$(function() {
  var size = 1000;
  var box  = $('#img');
  var btn  = $('#btn');
  var next = $('#btn .next');
  var prev = $('#btn .prev');
  var time = 10000;
  var slideImg = $('#slideimg');
  var firstImg = $('#firstimg');
  var text = $('#text img');
  var count = 0;
  var ie = navigator.userAgent.indexOf('MSIE 7') != -1 || navigator.userAgent.indexOf('MSIE 8') != -1 ? true : false;


    firstImg.hide();
    slideImg.show();
    text.eq(count).show();
    slideEvent();


  function slideEvent() {
    next.click(function() {
      btn.hide();
      text.hide();
      box.animate({
        marginLeft : parseInt(box.css('margin-left')) - size + 'px'
      }, 800, function() {
        box.css('margin-left', -size + 'px')
        $('img', box).first().appendTo(box);
        btn.fadeIn();
        count = count == 5 ? 0 : count + 1;
        if (ie == false) {
          text.eq(count).fadeIn(1500);
        }
        else {
          text.eq(count).show();
        }
      });
    });

    prev.click(function() {
      btn.hide();
      text.hide();
      box.css('margin-left', - size * 2 + 'px')
      $('img', box).last().prependTo(box);
      box.animate({
        marginLeft : parseInt(box.css('margin-left')) + size + 'px'
      }, 800, function() {
//        box.css('margin-left', - size + 'px')
//        $('img', box).last().prependTo(box);
        btn.fadeIn();
        count = count == 0 ? 5 : count - 1;
        if (ie == false) {
          text.eq(count).fadeIn(1500);
        }
        else {
          text.eq(count).show();
        }
      });
    });

    next.mouseup(function() {
      clearInterval(timerID);
      timerID = setInterval(function() {
        next.click();
      }, time);
    });
    prev.mouseup(function() {
      clearInterval(timerID);
      timerID = setInterval(function() {
        next.click();
      }, time);
    });
    timerID = setInterval(function() {
      next.click();
    }, time);
  }

});

