$(function(){
	var nextImg = new Image();
	var prevImg = new Image();
	var Jimg = "";
	var Jtext = "";
	var Jnum = "";
	var jNum = Math.floor(Math.random()*14+1);

	json(jNum,function() {
		$(".bookImageBox").append(Jimg);
		$(".bookDataBox").html(Jtext);
		$(".dataNum").text(Jnum);
	});

	//---------------------flip
	flipSwitch = false;
	setFlipPage();
	resetEvent();
	
	function resetEvent(){
	$(".flipPoint-R").unbind();
	$(".flipPoint-L").unbind();
	
	$(".flipPoint-R").hover(function(){
		$(this).css("cursor","pointer");
	},function(){
		$(this).css("cursor","default");
	});

	$(".flipPoint-R").mouseover(function(){
		flipReady("R");
	});

	$(".flipPoint-R").mouseout(function(){
		setFlipPage();
		flipSwitch = false;
	});

	$(".flipPoint-R").click(function(){
		$(".bookDataBox").fadeOut();
		$(".dataNum").fadeOut();
		setFlipPage();
		flipAnimation("R");
	});

	$(".flipPoint-L").hover(function(){
		$(this).css("cursor","pointer");
	},function(){
		$(this).css("cursor","default");
	});

	$(".flipPoint-L").mouseover(function(){
		flipReady("L");
	});

	$(".flipPoint-L").mouseout(function(){
		setFlipPage();
		flipSwitch = false;
	});

	$(".flipPoint-L").click(function(){
		$(".bookImageBox").fadeOut();
		setFlipPage();
		flipAnimation("L");
	});
	};
	//---------------------flip//

	function json(n,callback){
		var jumpUrl = "/special/?page=" + n ;
		$.getJSON(jumpUrl, function(json){
			jNum = json.page;
			var img = $('<img />');
			img.attr('src',json.object.thumbnail_url);
			var pagelink = "/scribble/"+json.object.key+"/";
			var a = $('<a/>');
			a.attr('href',pagelink);
			a.append(img);
			Jimg = a;
			var tex = json.object.description.slice(0, 120)+"…";
			tex = tex.replace("『","<b>『");
			tex = tex.replace("』","』</b><br/>");
			tex = tex.replace("（","<span>（");
			tex = tex.replace("）","）</span><br/>");
			Jtext = tex;
			Jnum = json.page+"/"+json.total;

			if( json.previous){
				$(".bookPrev").show();
				jumpUrl = "/special/?page=" + (n - 1);
				$.getJSON(jumpUrl, function(json){
					prevImg.src = json.object.thumbnail_url;
				});
			}else{
				$(".bookPrev").hide();
			};

			if(json.next){
				$(".bookNext").show();
				jumpUrl = "/special/?page=" + (n + 1);
				$.getJSON(jumpUrl, function(json){
					nextImg.src = json.object.thumbnail_url;
				});
			}else{
				$(".bookNext").hide();
			};
			var imgObj = new Image();
			imgObj.src = json.object.thumbnail_url;

			if(callback){
				callback();
			};
		});
	};

	//---------------------flip//

	function setFlipPage(){
		$(".flipPage-R").css("margin-left","-0%");
		$(".flipPage-R").css("width","0%");
		$(".flipPage-R").show();
		$(".flipPage-L").css("margin-left","-100%");
		$(".flipPage-L").css("width","0%");
		$(".flipPage-L").show();
	}

	function flipReady(posi){
		if(posi == "R"){
		$(".flipPage-R").css("margin-left","-0%");
		$(".flipPage-R").css("width","0%");
		$(".flipPage-R").fadeTo(1,1);
		$(".flipPage-R").animate({
			marginLeft: "-4%",
			width:"2.8%"
		},100);
		};
		if(posi == "L"){
		$(".flipPage-L").css("margin-left","-100%");
		$(".flipPage-L").css("width","0%");
		$(".flipPage-L").fadeTo(1,1);
		$(".flipPage-L").animate({
			marginLeft: "-99%",
			width:"2.8%"
		},100);
		};
	}

	function flipCancel(){
		$(".flipPage-R").animate({
			marginLeft: "-0%",
			width:"0%"
		},50,function(){
			setFlipPage();
		});
		$(".flipPage-L").animate({
			marginLeft: "-100%",
			width:"0%"
		},50,function(){
			setFlipPage();
		});
	}

	function flipAnimation(posi){
		if(posi == "R"){
		$(".flipPoint-R").unbind();
		json(jNum+1, function() {
			$(".flipPage-R").animate({
			marginLeft: "-140%",
			width:"100%"
			},500,function(){
			$(".bookImageBox").empty();
			$(".bookImageBox").append(Jimg);
			$(".bookDataBox").html(Jtext);
			$(".bookDataBox").show();
			$(".dataNum").text(Jnum);
			$(".dataNum").show();
			$(".flipPage-R").fadeOut(100,function(){
				$('.flipPge-R').hide();
				setFlipPage();
				resetEvent();
			})
			});
		});
		}
		if(posi == "L"){
			json(jNum-1, function() {
				$(".flipPoint-L").unbind();
				$(".flipPage-L").animate({
				marginLeft: "-60%",
				width:"100%"
				},500,function(){
				$(".bookImageBox").empty();
				$(".bookImageBox").append(Jimg);
				$(".bookImageBox").show();
				$(".bookDataBox").html(Jtext);
				$(".dataNum").text(Jnum);
				$(".flipPage-L").fadeOut(100,function(){
					$('.flipPge-L').hide();
					setFlipPage();
					resetEvent();
				})
				});
			});
		}
	}

//---------------------flip//
});

