/*
jquery plugin

options
imgs: String or Array //("さしかえるファイル")
sb_cls: jsでanimateするボックスのクラス(省略可能)

*/
(function($){

var ver = 'ALPHA-0.1';
$.fn.click_roll = function(options){
//ここでObjectをoverrideしてオプションを設定する
  options = options || {};
  var opts = $.extend({}, $.fn.click_roll.defaults, options);
  opts.imgs = (opts.imgs instanceof Array)? opts.imgs : [opts.imgs];

//つかう画像をキャッシュしておくよ
  $.each(function(){
    $('<img/>')[0].src = this;
  });

  return this.each(function(idx,el){

/* 変数 */
//element
    var $el = $(el);
    var $pr_overflow = null;
    if($el.css("overflow")!="hidden"){
      $pr_overflow = $el.css("overflow");
      $el.css("overflow","hidden");
    }


//配列インデックス
    var $idx = 0;
    
    if(opts.imgs.length>1){
      $el.click(function(){next()});
    }

//さしかえるfn
    var next = function(){
      var crnt = $idx;
      var nxt = crnt+1;
      if(nxt>=opts.imgs.length) nxt=0;
      var img = $el.children().filter("img")[0] || null;
      
      if(img!=null){ 
        $(img).hide();
        $el.append([
          '<div id="_j_c_r_">',
            '<img src="'+opts.imgs[crnt]+'">',
            '<br/>', //dasai!!
            '<img src="'+opts.imgs[nxt]+'">',
          '</div>'
        ].join('')); 
        
        $("div#_j_c_r_").css({position:"relative", "z-index":0});
        if(opts.sb_cls!=null) $("div#_j_c_r_").addClass(opts.sb_cls);

        $("div#_j_c_r_").animate({top:"-"+$("div#_j_c_r_").height()/2+"px"},{
          duration: "slow",
          complete:function(){
            $("div#_j_c_r_").remove(); 
            $(img).attr("src",opts.imgs[nxt]);
            $(img).show()
          }
        });
      }
      
      $idx = nxt;
    }
  
  });
}


$.fn.click_roll.version = function(){return ver};
$.fn.click_roll.defaults = {
  imgs:[],
  sb_cls:null
};

})(jQuery);
