var thumb_obj={
	maxSize:200,
	minSize:60,
	curSize:100,
	preVID:-1,
	isSizeChg:false,
	startDrag:0,
	startWidth:0,
	version:"2.1.0.0",
	listBnd:"",
	mainTable:"",
	isOn:false,
	bResize:true,
	bScroll:true,
	init:function(listBnd, mainTable, isFloat, isPopUp){
		thumb_obj.listBnd=listBnd;
		thumb_obj.mainTable=mainTable;
		if(typeof(isFloat)=="undefined") isFloat=true;
		if(typeof(isPopUp)=="undefined") isPopUp=false;
		frm_obj.init("frame_thumbnail", mainTable, listBnd, isFloat);
		if(window.addEventListener){
			window.addEventListener("load", this.initView, true);
		}else{
			window.onload=this.initView;
			if(isFloat){
				document.onselectstart=function(){
					window.event.cancelBubble = true;
					window.event.returnValue = false;
					return false;
				};
			}
		}
		if(!isFloat){
			window.onresize=function(evt){
				if(thumb_obj.bResize){
					thumb_obj.bResize=false;
					thumb_obj.resizeHandler();
					setTimeout("thumb_obj.bResize=true;",48);
				}
			};
		}
	},
	initView:function(){
		var bRetry=true;
		try{
			var thumbRect = thumb_obj.getThumbSize(thumb_obj.curSize);
			var viewRect=thumb_obj.getThumbViewSize(thumbRect);
			var epage=frm_obj.dBook().handle_getSlideTotal();
			var arr=frm_obj.dBook().handle_getViewersPageInfo();
			var listBndObj=document.getElementById(thumb_obj.listBnd);
			for(var i=0;i<arr.length;i++){
				var nLeft=arr[i].page_l;
				var nRight=arr[i].page_r;
				
				var nPage=(nLeft<1)?nRight:nLeft;
				
				var oDiv=listBndObj.appendChild(document.createElement("div"));
				oDiv.id="div_thumbBnd_"+i;
				oDiv.style.display="inline";
				
				var html='<table  border="0" cellpadding="0" cellspacing="0" style="display:inline">';
				html+='<tr><td height="10px" colspan="3"></td></tr>';
				html+='<tr>';
				html+='<td width="5"></td>';
				html+='<td style="width:'+viewRect.w+';height:'+viewRect.h+'" align="center" valign="middle" isLoad="false">';
				html+='<table id="tbl_thumbInner_'+i+'" border="0" cellpadding="0" cellspacing="0" style="border:1px solid #666666;cursor:pointer;" onclick="javascript:thumb_obj.click('+nPage+');" >';
				html+='<tr>';
				html+='<td id="td_thumbLeft_'+i+'" style="width:'+thumbRect.w+';height:'+thumbRect.h+';" align="right"></td>';
				if(frm_obj.parent().e_isDoublePage && !frm_obj.parent().e_isOneImgTwoPage){
					if(frm_obj.parent().e_isVerticalTurn) html+='</tr><tr>';
					html+='<td id="td_thumbRight_'+i+'" style="width:'+thumbRect.w+';height:'+thumbRect.h+';" align="right"></td>';
				}
				html+='</tr>';
				html+='</table>';
				html+='</td>';
				html+='<td width="5"></td>';
				html+='</tr>';
				html+='<tr><td height="2" colspan="3"></td></tr>';
				html+='<tr><td colspan="3" align="center">';
				html+='<table border="0" cellpadding="0" cellspacing="0"><tr><td  id="td_pageView_'+i+'" align="center" valign="middle" style="border:1px solid #666666;cursor:pointer;font-size:12px;padding-top:3px;padding-bottom:0px;padding-left:1px;padding-right:2px" onclick="javascript:thumb_obj.click('+nPage+');">';
				
				if(frm_obj.parent().e_isDoublePage && frm_obj.parent().e_isOneImgTwoPage){
					if(frm_obj.parent().e_bCoverSlide){
						if(frm_obj.parent().e_isOldTurn){
							nRight=nLeft*2-2;					
							nLeft=nLeft*2-1;
						}else{
							nRight=nLeft*2-1;					
							nLeft=nLeft*2-2;
						}
					}else{
						if(frm_obj.parent().ebk_isOldTurn){
							nRight=nLeft*2-1;
							nLeft=nLeft*2;
						}else{
							nRight=nLeft*2;
							nLeft=nLeft*2-1;
						}
					}
				}
				if(nLeft<1) nLeft=frm_obj.parent().MSG_FRONT_PAGE;
				else if(nLeft>epage)  nLeft=frm_obj.parent().MSG_REAR_PAGE;
				else nLeft=nLeft+frm_obj.parent().e_pageFix;
				
				if(nLeft<1) nLeft=frm_obj.parent().MSG_FRONT_PAGE;
				
				if(nRight<1) nRight=frm_obj.parent().MSG_FRONT_PAGE;
				else if(nRight>epage)  nRight=frm_obj.parent().MSG_REAR_PAGE;
				else nRight=nRight+frm_obj.parent().e_pageFix;
				
				if(nRight<1) nRight=frm_obj.parent().MSG_FRONT_PAGE;
				
				if(frm_obj.parent().e_isDoublePage) html+=nLeft+"-"+nRight;
				else  html+=nLeft;
				
				html+='</td></tr></table>';
				html+='</td></tr>';
				html+='<tr><td height="10" colspan="3"></td></tr>';
				html+='</table>';
				
				oDiv.innerHTML=html;
//				setTimeout("thumb_obj.load();thumb_obj.bScroll=true;",100);
			}
			listBndObj.onscroll=function(evt){
				if(thumb_obj.bScroll){
					thumb_obj.bScroll=false;
					setTimeout("thumb_obj.load();thumb_obj.bScroll=true;",100);
				}
			}
			if(epage>0 && arr.length>0) bRetry=false;
		}catch(e){}
		
		if(bRetry) setTimeout("thumb_obj.initView();", 100);
		else thumb_obj.toggle();

		if(frm_obj.isFloat) frm_obj.moveAndResize("init");
		else thumb_obj.resizeHandler();
	},
	toggle:function(){
		if(thumb_obj.isOn){
			thumb_obj.isOn=false;
			frm_obj.parent().ebook_removeEventListener("endFlip","frame_thumbnail.thumb_obj.flipHandler");
		}else{
			thumb_obj.isOn=true;
			frm_obj.parent().ebook_addEventListener("endFlip","frame_thumbnail.thumb_obj.flipHandler");
			thumb_obj.resizeHandler();
			thumb_obj.flipHandler();
		}
	},
	resizeHandler:function(){
		if(!thumb_obj.isOn || frm_obj.parent().e_isIEBrowser) return;
		var listBndObj=document.getElementById(thumb_obj.listBnd);
		var maxH;
		if(window.innerHeight){
			maxH=parseInt(window.innerHeight);				
		}else{
			maxH=parseInt(document.body.clientHeight);
		}
		if(parseInt(listBndObj.style.height)!=maxH){
			listBndObj.style.height=maxH;
			thumb_obj.load();
		}
	},
	flipHandler:function(){
		if(!thumb_obj.isOn) return;
		var vid=frm_obj.dBook().handle_getCurViewerID();
		var obj = document.getElementById("div_thumbBnd_"+vid);
		var listBnd=document.getElementById(thumb_obj.listBnd);
		var thumbRect = frm_obj.getElementRect(obj);
		var bndRect =  frm_obj.getElementRect(listBnd);
		
		if(thumbRect.h<20){
			var nextObj=document.getElementById("div_thumbBnd_"+(vid+1));
			if(nextObj!=null){
				var tmp= frm_obj.getElementRect(nextObj);
				thumbRect.h=tmp.y-thumbRect.y;
			}
		}
		try{
			if( (thumbRect.y-thumbRect.h)<bndRect.y ) 
				listBnd.scrollTop=listBnd.scrollTop-(bndRect.y-thumbRect.y)-thumbRect.h;
			else if( (thumbRect.y+2*thumbRect.h) >(bndRect.y+bndRect.h) )
				listBnd.scrollTop=listBnd.scrollTop+(thumbRect.y+thumbRect.h) -(bndRect.y+bndRect.h)+thumbRect.h;
		}catch(e){}
		try{
			document.getElementById("tbl_thumbInner_"+thumb_obj.preVID).style.border="1px solid #666666";
			document.getElementById("td_pageView_"+thumb_obj.preVID).style.backgroundColor="#ffffff";
			document.getElementById("td_pageView_"+thumb_obj.preVID).style.color = "#666666";
		}catch(e){}
		try{
			document.getElementById("tbl_thumbInner_"+vid).style.border="2px solid #1e90ff";
			document.getElementById("td_pageView_"+vid).style.backgroundColor="#1e90ff";
			document.getElementById("td_pageView_"+vid).style.color = "#ffffff";		
		}catch(e){}
		thumb_obj.preVID=vid;
		
		thumb_obj.load();
	},
	load:function(){
		if(!thumb_obj.isOn) return;
		var thumbRect = thumb_obj.getThumbSize(thumb_obj.curSize);
		var viewRect=thumb_obj.getThumbViewSize(thumbRect);

		var epage=frm_obj.dBook().handle_getSlideTotal();
		
		var arr=frm_obj.dBook().handle_getViewersPageInfo();
		var html="";
		
		var listBnd=document.getElementById(thumb_obj.listBnd);
		var listRect=frm_obj.getElementRect(listBnd);
		
		var chkIdx=Math.round(arr.length*(listRect.h/2 + listBnd.scrollTop)/listBnd.scrollHeight);
		if(chkIdx>=arr.length-1) chkIdx=arr.length-1;
		var baseURL=frm_obj.parent().e_absURL+frm_obj.parent().e_dataDirectory+"/thumb/";
		var blankURL=frm_obj.parent().e_absURL+frm_obj.parent().e_iconDirectory + "/blank.gif";
		
		var isDoublePage=frm_obj.parent().e_isDoublePage;
		var isOneImgTwoPage=frm_obj.parent().e_isOneImgTwoPage;
		for(var i=chkIdx;i>=0;i--){
			if(!thumb_obj.loadIdx(i, arr[i], thumbRect, listRect, baseURL, blankURL, isDoublePage, isOneImgTwoPage)) break;
		}
		for(var i=chkIdx+1;i<arr.length;i++){
			if(!thumb_obj.loadIdx(i, arr[i], thumbRect, listRect, baseURL, blankURL, isDoublePage, isOneImgTwoPage)) break;
		}
	},
	loadIdx:function(idx, oPage, thumbRect, listRect, baseURL, blankURL, isDoublePage, isOneImgTwoPage){
		var nLeft=oPage.page_l;
		var nRight=oPage.page_r;
		
		var thumbBnd=document.getElementById("div_thumbBnd_"+idx);
		var bndRect=frm_obj.getElementRect(thumbBnd);
		
		if( (bndRect.y+bndRect.w)>=listRect.y  && bndRect.y<(listRect.y+listRect.h)){
			var imgURL;
			var thumbLeft=document.getElementById("td_thumbLeft_"+idx);
			if(!thumbLeft.isLoaded){
				if(nLeft>0) imgURL=baseURL+nLeft+".jpg";
				else imgURL=blankURL; 
				thumbLeft.innerHTML='<img id="img_thumbRight_'+idx+'" src="'+imgURL+'" width="'+thumbRect.w+'" height="'+thumbRect.h+'" border="0"  onfocus="javascript:this.blur();" >';
				
				thumbLeft.isLoaded=true;
			}
			
			if(isDoublePage && !isOneImgTwoPage){
				var thumbRight=document.getElementById("td_thumbRight_"+idx);
				if(!thumbRight.isLoaded){
					if(nRight>0) imgURL=baseURL+nRight+".jpg";
					else imgURL=blankURL; 
					thumbRight.innerHTML='<img id="img_thumbRight_'+idx+'" src="'+imgURL+'" width="'+thumbRect.w+'" height="'+thumbRect.h+'" border="0"  onfocus="javascript:this.blur();" >';
					thumbRight.isLoaded=true;
				}
			}
			return true;
		}
		return false;
	},
	click:function(page){
		if(page<1) page=1;
		frm_obj.dBook().handle_gotoRealPage(page);
	},
	getThumbSize:function(nLimit){
		var ret={x:0,y:0,w:0,h:0};
		var cW=frm_obj.parent().e_canvasWidth;
		var cH=frm_obj.parent().e_canvasHeight;
		if(frm_obj.parent().e_isDoublePage && !frm_obj.parent().e_isOneImgTwoPage){
			if(frm_obj.parent().e_isVerticalTurn) cH=2*cH;
			else cW=2*cW;
		}
		ret.h=Math.floor(cH*nLimit/cW);
		ret.w=nLimit;
		if(frm_obj.parent().e_isDoublePage && !frm_obj.parent().e_isOneImgTwoPage){
			if(frm_obj.parent().e_isVerticalTurn) ret.h=Math.floor(ret.h/2);
			else ret.w=Math.floor(ret.w/2);
		}
		return ret;		
	},
	getThumbViewSize:function(rect){
		var ret={x:0,y:0,w:0,h:0};
		ret.w=rect.w+4;
		ret.h=rect.h+4;
		
		if(frm_obj.parent().e_isDoublePage && !frm_obj.parent().e_isOneImgTwoPage){
			if(frm_obj.parent().e_isVerticalTurn) ret.h=2*rect.h+4;
			else ret.w=2*rect.w+4;
		}
		return ret;		
	}
}

