
$(function(){
	$("#twitter_box").twitterBox('pigmybank');
	$("#news_list").pagenation('/internal/get_posts_as_list/news/publish/5');
	$("#side_news_list").pagenation('/internal/get_posts_as_list/news/publish/5');
	$("#hitokuchi").slider();
});

(function($) {
    $.fn.slider = function() {
    	var loading_num = loaded_num = 8;
    	var offset		= 0;
    	return this.each(function() {
    		var $$		= $(this);
    		
    		function slideToLeft() {
    			var baseX = parseInt($("#hitokuchi_slider").css("marginLeft").replace("px", ""));
    			if(baseX <= -390*(loaded_num-1)) {
    				offset++;
    				$.get("/internal/get_works_as_raw/0/hitokuchi/unique_id DESC/publish/"+loading_num+"/"+offset,
    					function(response){
    						var obj = $.evalJSON(response);
    						if(obj.length == 0) {
    							offset--;
    							return;
    						}
    						loaded_num = obj.length;
    						//alert(loaded_num)
    						var html = "";
    						for(var i=0;i<obj.length;i++) {
    							html += "<li>"+obj[i].description+"</li>";
    						}
    						$("#hitokuchi_slider").html(html);
    						$("#hitokuchi_slider").css({marginLeft:0});
    					}
    				);
    				return;
    			}
    			if(baseX % 390 == 0) {
    				baseX -= 390;
    			} else {
    				var slide = -baseX;
    				baseX -= 390*(parseInt(slide/390)+1) - slide;
    			}
    			$("#hitokuchi_slider").animate({ marginLeft : baseX }, 200);
    		}
    		function slideToRight(){
    			var baseX = parseInt($("#hitokuchi_slider").css("marginLeft").replace("px", ""));
    			if(baseX >= 0) {
    				offset--;
    				if(offset < 0) {
    					offset = 0;
    					return;
    				}
    				$.get("/internal/get_works_as_raw/0/hitokuchi/unique_id DESC/publish/"+loading_num+"/"+offset,
    					function(response){
    						var obj = $.evalJSON(response);
    						if(obj.length == 0) return;
    						loaded_num = obj.length;
    						var html = "";
    						for(var i=0;i<obj.length;i++) {
    							html += "<li>"+obj[i].description+"</li>";
    						}
    						$("#hitokuchi_slider").html(html);
    						$("#hitokuchi_slider").css({marginLeft:-390*(loaded_num-1)});
    					}
    				);
    				return;
    			}
    			if(baseX % 390 == 0) {
    				baseX += 390;
    			} else {
    				var slide = -baseX;
    				baseX += slide - 390*parseInt(slide/390);
    			}
    			$("#hitokuchi_slider").animate({ marginLeft : baseX }, 200);
    		}
    		function init() {
    			offset = 0;
    			$("#hitokuchi_left").click(slideToRight)
    			$("#hitokuchi_right").click(slideToLeft)
    		}
    		init();
    	});
    };
}
)(jQuery);
