var pages_tot = 12;
var act_page = 0;
var pages = new Array();
var pages_full = new Array();
/*
 * Plugin Katalog se zoomem
 */
var dur = 500;
var dur_resize = 500;
var width_cont = $("#left-cont").width();		//ziskam sirku. Base ji Vzdy urcuje
var width_page = $("#left-base").width();		//ziskam sirku. Base ji Vzdy urcuje
var dif_pg_cnt = width_cont - width_page;		//offet pro full page
var height_page = $("#left-base").height();	//ziskam sirku. Base ji Vzdy urcuje
var anime = 0; //neanimujeme 
var first = 0;
var last = 0;
var shadow_width = 35;
var DBG_ON = false;
var cache_dbg = '';
var mode = 'pagelist';
var zoomin_pg = -1;	//zvetsena strana 1-prava, 0- leva  -1- zadna
var or = $("#right-base").position();
var ol = $("#left-base").position();
var img_last_x = 0;
var img_last_y = 0;
var img_width = 0;
var img_height = 0;
var drag_img = false;
var img_offset = 0;
var img_offset_from_top = $("#img-base").offset().top;

function BookInit(duration,duration_resize)
{
	CreateList($("#dir").text(),$("#count").text(),$("#type").text());
	PreloaderImages(pages,pages_full);
	
	dur = duration;
	dur_resize = duration_resize;
	$("#right-page").attr('src', pages[act_page]);
	
	//asign events
	ePrev();
	eNext();
	eFirst();
	eLast();
	eZoomIn();
	eZoomOut();
	eImgBase();
	ePages();
	
	setPaginator();
	
	putDbg("sirka stranky: ",width_page);
}

function CreateList(dir,count,type){
	for(i=0; i<count; i++){
		pages[i] = '/book_data/'+dir+'/nah/n_'+(i+1)+'.'+type;
		pages_full[i]= '/book_data/'+dir+'/ful/f_'+(i+1)+'.'+type;
	}
	pages_tot = count;
}

function PreloaderImages(image_nah,image_full){
	anime=1;
	var cnt = count(image_nah); 
	$("#book").append('<div id="loading" style="position: absolute; width:100%;margin:0;padding: 0; text-align: center; top:200px; left:0; height: 50px;  font: bolder 20px Verdana, san-serif; color: black; "><span style="display:inline-block; height: 30px; padding-top: 5px; background-color: silver; padding-left: 20px; padding-right: 20px;">Nahrávam obsah. Vyčkejte...</span></div>');
	for(i=0;i<cnt;i++){
		$("#right-base").append('<img style="display:none;" id ="n_'+i+'" src="'+pages[i]+'" />');
		$("#right-base").append('<img style="display:none;" id ="f_'+i+'" src="'+pages_full[i]+'" />');
	}
	
	var pom = "#f_"+(cnt-1);

	$(pom).load(function(){
		img_width = $(pom).width();
		img_height = $(pom).height();
		$("#img-base-img").width(img_width);
		$("#img-base-img").css("margin-left",(width_cont-(img_width/2))+"px");
		$("#img-base-img").height(img_height);
		dif_pg_cnt=(width_cont-(img_width/2));
		$("#img-base").css("left",(width_cont-(img_width/2))+"px");
		anime=0;

		putDbg("img_width: ",img_width);
		putDbg("img_height: ",img_height);
		putDbg("pom: ",pom);
		$("#loading").remove();
		return (true);
	});
}

function count(array)
{
	var c = 0;
	for(i in array) // in returns key, not object
		if(array[i] != undefined)
			c++;
	return c;
}
var fl=0;
function eImgBase(){
	$("#img-base-img").mousedown(function(e){
		//putDbg("mouse down fired",e.clientX);
		drag_img = true;
		img_last_x = e.clientX;
		img_last_y = e.clientY;
	});
	
	$("#img-base-img").mouseup(function(){
		//putDbg("mouse up fired","");
		drag_img = false;
		fl=0;
	});
	$("#img-base-img").mouseleave(function(){
		//putDbg("mouse leave fired","");
		drag_img = false;
	});
	
	$("#img-base-img").mousemove(function(e){
		if(drag_img == true){
			fl=1;
			var dif_x = img_last_x - e.clientX;
			var dif_y = img_last_y - e.clientY;
			
			img_last_x = e.clientX;
			img_last_y = e.clientY;

			img_offset = img_offset + (-1*dif_y);
			
			if(img_offset > 0)
				img_offset = 0;
			if(img_offset < (-1*((img_height-height_page)-img_offset_from_top)))
				img_offset = (-1*((img_height-height_page)-img_offset_from_top));
			
			//alert("img_offset: "+img_offset+" dif: "+(-1*dif_y)+" img_offset_from_top: "+img_offset_from_top);
			$("#img-base-img").css("top",(img_offset_from_top+img_offset)+"px");//.delay(200);
		}
	});
	
}

function ePages(){
	
	
	//	$("#right-base").dblclick(function(){
	//		PageNext();
	//	});
	
	$("#right-base").click(function(){
		if(act_page < (pages_tot)){
			ZoomIn(1);
		}
	});
	$("#left-base").click(function(){
		if(act_page > 0){
			ZoomIn(0);
		}
	});
	
//	$("#left-base").dblclick(function(){
//		PageNext();
//	});
}

function ePrev(){
	$("#prev").click(function(){
		if(anime == 0){
			PagePrev();
		}
	});
	$("#prev").mouseenter(function(){
		if(act_page != 0){
			$("#prev").css('background-image','url("/js/book/img/prev_h.png")');
		}
	});
	$("#prev").mouseleave(function(){
		if(act_page != 0){
			$("#prev").css('background-image','url("/js/book/img/prev.png")');
		}
	});
}

function eNext(){
	$("#next").click(function(){
		if(anime == 0){
			PageNext();
		}
	});
	$("#next").mouseenter(function(){
		if(act_page != pages_tot){
			$("#next").css('background-image','url("/js/book/img/next_h.png")');
		}
	});
	$("#next").mouseleave(function(){
		if(act_page != pages_tot){
			$("#next").css('background-image','url("/js/book/img/next.png")');
		}
	});
}

function eFirst(){
	$("#first").click(function(){
		if(anime == 0){
			PageFirst();
		}
	});
	$("#first").mouseenter(function(){
		if(act_page != 0){
			$("#first").css('background-image','url("/js/book/img/first_h.png")');
		}
	});
	$("#first").mouseleave(function(){
		if(act_page != 0){
			$("#first").css('background-image','url("/js/book/img/first.png")');
		}
	});
}

function eLast(){
	$("#last").click(function(){
		if(anime == 0){
			PageLast();
		}
	});
	$("#last").mouseenter(function(){
		if(act_page != pages_tot){
			$("#last").css('background-image','url("/js/book/img/last_h.png")');
		}
	});
	$("#last").mouseleave(function(){
		if(act_page != pages_tot){
			$("#last").css('background-image','url("/js/book/img/last.png")');
		}
	});
}

function eZoomIn(){
	$("#zoomin").click(function(){
		//putDbg("zoomIN fired","");
		setZoom();
		ResetHover();
		$("#zoomin").css('background-image','url("/js/book/img/zoomout_h.png")');
	//ZoomIn(1);
	});
	$("#zoomin").mouseenter(function(){
		if(zoomin_pg < 0){
			$("#zoomin").css('background-image','url("/js/book/img/zoomout_h.png")');
		}
	});
	$("#zoomin").mouseleave(function(){
		if(zoomin_pg < 0){	//act_page != pages_tot
			$("#zoomin").css('background-image','url("/js/book/img/zoomout.png")');
		}
	});
}
	
function eZoomOut(){
	$("#zoomout").click(function(){
		//putDbg("Pointer fired","");
		ZoomOut(zoomin_pg);
		
		setPagelist();
		ResetHover();
		$("#zoomout").css('background-image','url("/js/book/img/zoomin.png")');
		img_offset = 0;
	});
	$("#zoomout").mouseenter(function(){
		if(zoomin_pg >= 0){
			$("#zoomout").css('background-image','url("/js/book/img/zoomin_h.png")');
		}
	});
	$("#zoomout").mouseleave(function(){
		if(zoomin_pg >= 0){	//act_page != pages_tot
			$("#zoomout").css('background-image','url("/js/book/img/zoomin.png")');
		}
	});
}

function ResetHover(){
	$("#zoomin").css('background-image','url("/js/book/img/zoomout.png")');
	$("#pointer").css('background-image','url("/js/book/img/zoomin.png")');
}

function getMode(){
	return (mode);
}

function setZoom(){
	mode = 'zoom';
}

function setPagelist(){
	mode = 'pagelist';
}

function ZoomIn(pg){
	anime = 1;
	var o;
	zoomin_pg = pg;
	if(pg == 1){
		o = or;
		$("#img-base-img").css("top",o.top+"px");//.delay(200);
		$("#img-base").attr("src",pages_full[act_page]);
		$("#img-base-img").css('background-image','url("'+pages_full[act_page]+'")');
	}else{
		o = ol;
		$("#img-base-img").css("top",o.top+"px");//.delay(200);
		$("#img-base").attr("src",pages_full[act_page-1]);
		$("#img-base-img").css('background-image','url("'+pages_full[act_page-1]+'")');
	}
	$("#img-base").ready(function(){
		setSizeBlock("#img-base","no" ,o.left, o.top, width_page, height_page, 1 );
	
		$("#img-base").show();
		$("#img-base").animate({
			left: dif_pg_cnt+"px",
			width: (img_width)+"px",
			height: (img_height)+"px"
		}, {
			queue:false, 
			duration: dur_resize, 
			complete:
			function (){
				$("#img-base-img").show();
				$("#img-base").hide();
				anime = 0;
		
			}
		});
	});
}

function setSizeBlock(id, name, x, y, w, h, aspect){
	$(id).css("position","absolute");
	$(id).css("left",x);
	$(id).css("top",y);
	$(id).css("width", w);
	$(id).css("height", h);
	
}

function ZoomOut(pg){
	$("#img-base").css("top",(img_offset_from_top+img_offset)+"px");//.delay(200);
	$("#img-base").show();
	$("#img-base-img").hide();
	var o;
	anime = 1;
	if(pg == 1){
		o = or;
	}else if(pg == 0){
		o = ol;
	}else{
		return (false);
	}
	putDbg("left:",o.left);
	putDbg("left:",o.top);
	putDbg("to page:",pg);
	$("#left-cont").show();
	$("#right-cont").show();

	$("#img-base").animate({
		top: o.top+"px",
		left: o.left+"px",
		width: (width_page)+"px",
		height: (height_page)+"px"
	}, {
		queue:false, 
		duration: dur_resize, 
		complete:
		function (){
			$("#img-base").hide();
			zoomin_pg = -1;
			anime = 0;
			
			$("#img-base").css('background-image','url("")');
			$("#img-base").hide();
			$("#img-base-img").hide();
			return(true);
		}
	});
}

function PageLast(){
	if(act_page < pages_tot-1){
		act_page = pages_tot-2;
		PageNext();
		setPaginator();
	}
}

function PageFirst(){
	if(act_page >= 2){
		act_page = 2;
		PagePrev();
		setPaginator();
	}
}

function setPaginator(){
	if(act_page == 0){
		$("#first").css('background-image', 'url("/js/book/img/first_sh.png")');
		$("#prev").css('background-image', 'url("/js/book/img/prev_sh.png")');
		$("#right-sh").width(shadow_width+"px");
		first = 1;	//prvni strana
		$("#pg_cont").text((act_page+1)+" / "+pages_tot);
		$("#left-page").hide();
		$("#left-page-base").hide();
	}else if((act_page+1)>pages_tot){
		$("#left-page").show();
		$("#left-page-base").show();
		$("#last").css('background-image', 'url("/js/book/img/last_sh.png")');
		$("#next").css('background-image', 'url("/js/book/img/next_sh.png")');
		last = 1;
		$("#pg_cont").text(act_page+" / "+pages_tot);
	}else{
		if(first == 1){
			$("#first").css('background-image', 'url("/js/book/img/first.png")');
			$("#prev").css('background-image', 'url("/js/book/img/prev.png")');
			first = 0;
		}
		if(last == 1){
			$("#last").css('background-image', 'url("/js/book/img/last.png")');
			$("#next").css('background-image', 'url("/js/book/img/next.png")');
			last = 0;
		}
		$("#pg_cont").text(act_page+" - "+(act_page+1)+" / "+pages_tot);
	}
}

function PageNext()
{
	if(act_page < pages_tot-1){
		//pokud nejsme na posledni otacime
		anime = 1;	//animace start
		$("#left-page").attr('src', pages[act_page+1]);
		
		$("#right-page").attr('src',pages[act_page]);	//natahneme list pro smrsteni
		$("#right-page").width(width_page+"px");
		
		if((act_page+2)>(pages_tot-1)){
			$("#right-page-base").hide();
		}else{
			$("#right-page-base").attr('src',pages[act_page+2]);	//dalsi obrazek
		}
		$("#right-page").animate({
			width: "0px"
		}, {
			queue:false, 
			duration: dur, 
			complete:
			function (){
				if((act_page+2)>(pages_tot-1)){
					$("#right-page").hide();	//dalsi list neni
				}else{
					$("#right-page").attr('src', pages[act_page+2]);	//dalsi barvu
				}
				$("#left-page").animate({
					left: "0px",
					width: width_page+"px"
				},{
					queue:false, 
					duration: dur,
					complete:
					function(){
						$("#left-page-base").attr('src', pages[act_page+1]);	//otocena barva
						$("#left-page-base").show();
						$("#left-page").width("0px");	//colapse left page
						$("#left-page").css('left',width_page+"px");	//move to zero
						act_page = act_page+2;
						setPaginator();
						anime = 0; //konec animace
					}
				});
			}
		});

		if(act_page < pages_tot-2){
			$("#right-sh-base").width(shadow_width+"px");
			
		}else{
			$("#right-sh-base").width(0+"px");	
		}
		$("#right-sh").width(shadow_width+"px");
		$("#right-sh").animate({
			//pravy prhuh  -> 0
			width: "0"+"px"
		}, {
			queue:false, 
			duration: dur, 
			complete:
			function (){
				//poloha leveho pruhu
				$("#left-sh").css("left",(width_page)+"px");
				$("#left-sh-base").css("left", (width_page - shadow_width)+"px");
				$("#left-sh").animate({
					left: (width_page - shadow_width)+"px",
					width: shadow_width+"px"
				},{
					queue:false, 
					duration: dur,
					complete:
					function(){
						$("#left-sh-base").width(shadow_width+"px");
						$("#left-sh-base").show();
						$("#left-sh").width("0"+"px");
					}
				});
			}
		});
	}
}

function PagePrev()
{
	if(act_page >= 2){
		anime = 1;	//animace start
		$("#left-page").attr('src', pages[act_page-1]);
		$("#left-page").width(width_page+"px");	//expand left page
		$("#left-page").css('left',0+"px");
		
		if((act_page-3)>=0){
			$("#left-page-base").attr('src', pages[act_page-3]);	//nova stranu jako podklad
		}else{
			$("#left-page-base").hide();	//nova stranu jako podklad
		}
		//<<
		
		$("#left-page").animate({
			left: width_page+"px",
			width: "0px"
		}, {
			queue:false, 
			duration: dur, 
			complete:
			function (){
				if(act_page < (pages_tot)){
					$("#right-page-base").attr('src', pages[act_page]);
					$("#right-page-base").show();
				}
				$("#right-page").width("0px");	//shrink right page
				$("#right-page").attr('src', pages[act_page-2]);
				//<<				
				$("#right-page").animate({
					width: width_page+"px"
				},{
					queue:false, 
					duration: dur,
					complete:
					function(){
						$("#right-page-base").attr('src', pages[act_page-2]);	//otocena barva
						$("#right-page-base").show();
						$("#right-page").width(0+"px");	//colapse left page
						act_page = act_page-2;
						setPaginator();
						anime = 0; //animace konec
					}
				});
			}
		});
	}

	if(act_page > 0){	
		$("#left-sh").width(shadow_width+"px");	//natahneme stin
		$("#left-sh").css("left",(width_page-shadow_width)+"px");	//obnov velikost strany
		if((act_page)<=2){
			$("#left-sh-base").hide();	//dalsi list neni
		}else{
			$("#left-sh-base").width(shadow_width+"px");	//dalsi barvu
			$("#left-sh-base").show();	
		}
		$("#right-sh").width(0+"px");
		$("#left-sh").animate({
			left: (width_page)+"px",
			width: "0px"
		}, {
			queue:false, 
			duration: dur, 
			complete:
			function (){
				//>>
				$("#right-sh").animate({
					left: "0px",
					width: shadow_width+"px"
				},{
					queue:false, 
					duration: dur,
					complete:
					function(){
						$("#right-sh-base").width(shadow_width+"px");
						$("#right-sh-base").show();
						$("#right-sh").width(0+"px");
					}
				});
			}
		});
	}

}

function writeDbg()
{
	if(DBG_ON == true){
		$("#dbg").html('<div style="font: normal 12px Arial; text-align:left;"><h1 style="font:bolder 14px Arial">Debug Data</h1>'+cache_dbg+'</div>');
	}
}

function putDbg(name,value)
{
	if(DBG_ON == true){
		cache_dbg += '<p style="padding:0; margin:0;">'+name+value+'</p>';
		writeDbg();
	}
}

$(document).ready(function(){
	if($("#book").is("*"))
	{
		BookInit(200,300);
	}
//	$("#head").catalog({
//		test: 50
//	});
});
