var current_page=1;
//max pages needs to be set in HTML on #book.
var bind_status_flip_left=0;
var bind_status_flip_right=0;

function init(){
	window.location="#page:1";
/* 	bind_flip_left(); */ /*we start at the first page, no need to allow flip back...*/
	bind_flip_right();	
}
function bind_flip_left(){
	$("#book_overlay_l").bind("mouseenter", function(event){
		$("#footer_flip_btn_left").animate({ 
			opacity: 0.4,
		}, 250 )
	});
	$("#book_overlay_l").bind("mouseleave", function(event){
		$("#footer_flip_btn_left").animate({ 
			opacity: 0.1,
		}, 250 )
	});
	$("#footer_flip_btn_left").bind("mouseenter", function(event){
		$("#footer_flip_btn_left").animate({ 
			opacity: 1,
		}, 150 )
	});
	$("#footer_flip_btn_left").bind("mouseleave", function(event){
		$("#footer_flip_btn_left").animate({ 
			opacity: 0.4,
		}, 150 )
	});
	$("#footer_flip_btn_left").bind("click", function(event){
		page_flip(-1);
	});
	$("#footer_flip_btn_left").css("opacity",0.4);
	$("#footer_flip_btn_left").fadeIn("fast");
	bind_status_flip_left=1;
}
function unbind_flip_left(){
	$("#book_overlay_l").unbind("mouseenter").unbind("mouseleave");
	$("#footer_flip_btn_left").unbind("mouseenter").unbind("mouseleave").unbind("click");
	$("#footer_flip_btn_left").fadeOut("fast");
	bind_status_flip_left=0;
}
function bind_flip_right(){
	$("#book_overlay_r").bind("mouseenter", function(event){
		$("#footer_flip_btn_right").animate({ 
			opacity: 0.4,
		}, 250 )
	});
	$("#book_overlay_r").bind("mouseleave", function(event){
		$("#footer_flip_btn_right").animate({ 
			opacity: 0.1,
		}, 250 )
	});
	$("#footer_flip_btn_right").bind("mouseenter", function(event){
		$("#footer_flip_btn_right").animate({ 
			opacity: 1,
		}, 150 )
	});
	$("#footer_flip_btn_right").bind("mouseleave", function(event){
		$("#footer_flip_btn_right").animate({ 
			opacity: 0.4,
		}, 150 )
	});
	$("#footer_flip_btn_right").bind("click", function(event){
		page_flip(1);
	});
	$("#footer_flip_btn_right").css("opacity",0.4);
	$("#footer_flip_btn_right").fadeIn("fast");
	bind_status_flip_right=1;
}
function unbind_flip_right(){
	$("#book_overlay_r").unbind("mouseenter").unbind("mouseleave");
	$("#footer_flip_btn_right").unbind("mouseenter").unbind("mouseleave").unbind("click");
	$("#footer_flip_btn_right").fadeOut("fast");
	bind_status_flip_right=0;
}
/////////////////////////////////////////////////////////////////////
//_NAME_
// _NOTE_
function page_flip(op){
/*
	tsplit=window.location.hash.replace(/[^a-zA-Z ; 0-9.-_//]+/g,'');
	split=tsplit.split(":");
	curr_page=split[1];
*/
	curr_page=current_page;
//	var max_pages=$('#book').getAttribute('bookmaxpgs');
	var max_pages=document.getElementById('book').getAttribute('bookmaxpgs');
	if(op>0){
/* 		next_page=(split[1]*1)+1; */
		next_page=current_page+1;
	}else{
		if(curr_page==0){
			return;
		}else{
/* 			next_page=(split[1]*1)-1; */
			next_page=current_page-1;
		}
	}
	if(next_page==max_pages){
		//end of book... hide the next button.
		unbind_flip_right();
		document.getElementById('book_overlay').style.display="none";
	}else if(next_page<max_pages){
		//not end of book... show the next button.
		if(bind_status_flip_right==0){
			bind_flip_right();
			document.getElementById('book_overlay').style.display="";
		}
	}
	if(next_page==1 || next_page==0){
		//beginning of book... hide the prev button.
		unbind_flip_left();
	}else if(next_page>1){
		//not beginning of book... show the prev button.
		if(bind_status_flip_left==0){
			bind_flip_left();
		}
	}
	window.location="#page:"+next_page;
	setTimeout(function(){
		$('#page_l').removeAttr("class").addClass("page_l-"+next_page);
		/*use attr for pagel to change image instead.. save some CSS.*/
		$('#page_r').removeAttr("class").addClass("page_r-"+next_page);
		var content=$('#content_page_r_'+next_page).html();
		$('#content').html(content);
		$('#page_l').fadeIn('fast');
		$('#page_r').fadeIn('fast');
		current_page=next_page;
		},500);
}
