var preChapter;


function switchChatpters()
{
	$("#chapter_bgr_"+preChapter,"#chapter_bgr").fadeOut("slow");
	$("#chapter_info_"+preChapter,"#chapter_info").animate({top : "-386"},500, function(){		
		$(this).css("top","386px");
	});
	$("#chapter_bgr_"+currChapter,"#chapter_bgr").fadeIn("slow");
	
	$("#chapter_info_"+currChapter,"#chapter_info").animate({top : "0"},500);
}
function createChapterPreview()
{
	if($("#chapter_bgr_"+currChapter,"#chapter_bgr").length==0)
	{
		$("#chapter_bgr").append('<div id="chapter_bgr_'+currChapter+'" class="chapter_bgr" chapter="'+currChapter+'" style="display:none"><div>');
		$("#chapter_bgr_"+currChapter).css("background","url("+pathChLevelUp+"chapters/"+currChapter+"/images/background.jpg) no-repeat");
		
		$("#chapter_info").append('<div id="chapter_info_'+currChapter+'" chapter="'+currChapter+'" style="position:absolute;top:386px;left:0px;"><div id="chapter_logo_'+currChapter+'" style="background:url('+pathCh+'images/Chapter_frame/Chapter_frame_Chapter_'+(currChapter<10?"0"+currChapter:currChapter)+'.png) no-repeat;width:224px;height:44px;"></div><div id="chapter_title_'+currChapter+'" style="width:224px;height:52px;"><img src="'+(folder.length>0?folder.replace("/"+pageChapter+"/","/"+currChapter+"/"):"../"+currChapter+"/")+'images/title_thumb.png" width="220" height="50" border="0"/></div><div id="chapter_text_1" style="width:220px;height:200px;padding:4px;" align="left">'+chapterDescArray[currChapter]+'</div></div>');
		$("[id^=chapter_info_"+currChapter+"],[id^=chapter_bgr_"+currChapter+"]").click(function(){
			window.location = pathChLevelUp+"chapters/"+$(this).attr("chapter");	
		});
	}
	
	if(preChapter!=currChapter)
	{
		switchChatpters();
	}
	
} 

$().ready(function($) {
	
	preChapter = currChapter;
	
	createChapterPreview();
	$("#chapter_bgr_"+currChapter,"#chapter_bgr").show();
	$("#chapter_info_"+currChapter,"#chapter_info").css("top","0px");
	
	chapter_listStr = "";
	for(i=chapterNum;i>0;i--)
	{
		chapter_listStr += '<div style="float:right;height:27px;width:33px;cursor:pointer;" '+((i==currChapter)?'class="chbtnSelected"':'')+' align="left" id="chapter_btn_'+i+'"><div style="background:url('+pathCh+'images/slide_chapters/numerals_'+(i<10?"0"+i:i)+'.png) no-repeat;width:33px;height:27px;cursor:pointer;font-size:20px;" >&nbsp;</div></div>';		
	}
	
	$("#chapter_list").append("<div style='padding-top:12px;height:33px;width:531px;'>"+chapter_listStr+"</div>");
	
		
	$("[id^=chapter_btn_]").click(function(){

		$(".chbtnSelected").removeClass("chbtnSelected");
		$(this).addClass("chbtnSelected");
		preChapter = currChapter;
		currChapter = this.id.replace("chapter_btn_","");
	
		if(preChapter!=currChapter)
		{createChapterPreview();}			
	});
	

	if(pageChapter>0)
	{
		$("#chSliderBtn").click(function(){
			$("#chSlider").toggle();
		});
		$("#chSliderBtn").mouseover(function(){
			$(this).addClass("chSliderBtn_over");
		});
		$("#chSliderBtn").mouseout(function(){
			$(this).removeClass("chSliderBtn_over");
		});
		
	}

	if(pageChapter>1)
	{
		$("#chPrev").click(function(){
			gotoChapter=pageChapter-1;
			window.location = pathChLevelUp+"chapters/"+gotoChapter;
		});
		$("#chPrev").mouseover(function(){
			$("#chPrev_bgr").addClass("chPrev_over");
		});
		$("#chPrev").mouseout(function(){
			$("#chPrev_bgr").removeClass("chPrev_over");
		});
	}
	else
	{
		$("#chPrev_bgr").addClass("chPrev_disable");
	}
	
	if(pageChapter<chapterNum&&pageChapter>0)
	{
		$("#chNext").click(function(){
			gotoChapter=pageChapter+1;
			window.location = pathChLevelUp+"chapters/"+gotoChapter;
		});
		
		$("#chNext").mouseover(function(){
			$("#chNext_bgr").addClass("chNext_over");
		});
		$("#chNext").mouseout(function(){
			$("#chNext_bgr").removeClass("chNext_over");
		});	
	}
	else
	{
		$("#chNext_bgr").addClass("chNext_disable");
	}
	//$(document).pngFix();	
	
});	
