/* 
 * jQuery.simpleGallery.js
 * © 2011 kumagaiyusuke
*/

$.simpleGallery = function (width, height, contentsList) {

  if (typeof width === "undefined" || typeof height === "undefined") {
    throw new Error("widthとheightの値は必須です。");
  }
  
  if (!$.isArray(contentsList)) {
    throw new Error("contentsListは配列で指定する必要があります。");
  }

  var $gallery = $("<div>", {
    "class": "simpleGallery"
  });

  var $container = $("<div>", {
    "class": "container"
  });
  
  var itemList = [];
  var currentItem = 0;
  var timer;
  
  var settings = $.simpleGallery.settings;
  
  var events = $.simpleGallery.events;
  
  var currentLeft, startX, moveX;

  
  // !初期化
  
  $gallery.css({
    position: "relative",
    width: width,
    height: height,
    overflow: "hidden"
  });
  
  $gallery.html($container);
  
  $container.css({
    position: "absolute",
    top: 0,
    left: 0,
    width: "100%",
    height: "100%"
  });
  
  $container.bind("touchstart", onTouchStart);
  
  for (var i = 0; i < contentsList.length; ++i) {
    addItem(contentsList[i]);
  }
  
  
  // !イベントハンドラ
  
  function onTouchStart(e) {
    
    var data = e.originalEvent;
    
    if (data.touches.length == 1) {
      $container.bind("touchmove", onTouchMove);
      $container.bind("touchend", onTouchEnd);
      $container.stop();
      currentLeft = $container.css("left").replace("px", "") * 1;
      startX = data.touches[0].pageX;
    }
  }
  
  function onTouchMove(e) {
    
    var data = e.originalEvent;

    e.preventDefault();
    
    moveX = data.touches[0].pageX - startX;
    
    $container.css({
      left: currentLeft + moveX
    });
  }
  
  function onTouchEnd(e) {
    
    var data = e.originalEvent;

    $container.unbind("touchmove", onTouchMove);
    $container.unbind("touchend", onTouchEnd);
    
    if (moveX > settings.TOUCH_MOVE_RANGE && currentItem > 0) {
      $gallery.slidePrev(settings.TOUCH_SLIDE_DURATION);
    }
    else if (moveX < -settings.TOUCH_MOVE_RANGE && currentItem < itemList.length - 1) {
      $gallery.slideNext(settings.TOUCH_SLIDE_DURATION);
    }
    else {
      $gallery.slideTo(currentItem, settings.TOUCH_SLIDE_DURATION);
    }
    
    moveX = 0;
  }
  
  
  // !プライベート
  
  function addItem(contents) {
    
    var $item = $("<div>", {
      "class": "item"
    });
    
    $item.html(contents);
  
    $item.css({
      position: "absolute",
      top: 0,
      left: itemList.length * 100 + "%",
      width: "100%",
      height: "100%"
    });
    
    itemList.push($item);
    
    $container.append($item);
  };
  
  
  // !パブリック
  
  $gallery.getCurrentItem = function () {
    return currentItem;
  };
  
  $gallery.getTotalItems = function () {
    return itemList.length;
  };
  
  $gallery.slideTo = function (i, duration, easing) {
    
    if (i < 0 || i >= itemList.length) {
      throw new Error("アイテム番号は0から" + (itemList.length - 1) + "の間で指定する必要があります。");
    }
    
    $gallery.trigger(events.SLIDE_START);
    
    currentItem = i;
    
    $container.stop();
    
    $container.animate({
      left: - i * $gallery.width()
    }, {
      duration: duration,
      easing: easing,
      complete: function () {
        $gallery.trigger(events.SLIDE_END);
      }
    });
  };
  
  $gallery.slidePrev = function (duration, easing) {
    
    var nextItem = currentItem - 1;
    
    if (nextItem < 0) {
      nextItem = itemList.length - 1;
    }
    
    $gallery.slideTo(nextItem, duration, easing);
  };
  
  $gallery.slideNext = function (duration, easing) {
    
    var nextItem = currentItem + 1;
    
    if (nextItem >= itemList.length) {
      nextItem = 0;
    }
    
    $gallery.slideTo(nextItem, duration, easing);
  };
  
  $gallery.resize = function (width, height) {
  
    if (typeof width === "undefined" || typeof height === "undefined") {
      throw new Error("widthとheightの値は必須です。");
    }

    $gallery.css({
      width: width,
      height: height
    });
    
    $container.css({
      left: -width * currentItem
    });

  };
  
  return $gallery;
};


// !スタティック

$.simpleGallery.settings = {
  TOUCH_MOVE_RANGE: 50,
  TOUCH_SLIDE_DURATION: 300
};

$.simpleGallery.events = {
  SLIDE_START: "slideStart",
  SLIDE_END: "slideEnd"
};

