
/// <reference path="../common/js/jquery-ui-1.8.23.custom.min.js" />

/// <reference path="../common/js/common.js" />

/// <reference path="contentview_VarDef.js" />

//Call API
function abapi(name, param, method, callback) {
    avwCmsApiSync(ClientData.userInfo_accountPath(), name, method, param, callback, null);
};
//START TRB00097 - Editor: Long - Date: 09/30/2013 - Summary : Get All Page size of content
/* get Json stored content info */
function getJsonContentInfo() {
	
	//ファイルシステムが有効であればキャッシュ確認
	if( CONTENTVIEW_FILESYSTEM.fs == null ){
		//通常
		avwGrabContentPageImage(
				ClientData.userInfo_accountPath(),
				{ contentId: contentID, sid: ClientData.userInfo_sid(), pageNo: 1 },
				function (data) { 
					pageImages = data;
					webGetContentData();
					getSearchDataFromJson();
					getJsonDataPageTitle();
					getJsonDataType4();
					getJsonDataType5();
					getDataJsonFile();
					webGetContentPageSize();
				},
				function (xmlHttpRequest, txtStatus, errorThrown) {
					showErrorScreen();
				}
		);
	} else {
		// 1ページ目のファイル取得
		var fileName = contentID + "/page_1.dat";
		CONTENTVIEW_FILESYSTEM.fs.root.getFile('/abook/' + fileName, { create: false },
				function(fileEntry){
					fileEntry.file(function(file){
						var reader = new FileReader();
						reader.onloadend = function(e) {
							AVWEB.avwLog("read FileSystem");
							pageImages = e.target.result;
							webGetContentData();
							getSearchDataFromJson();
							getJsonDataPageTitle();
							getJsonDataType4();
							getJsonDataType5();
							getDataJsonFile();
							webGetContentPageSize();
						};
						//reader.readAsBinaryString(fileEntry);
						reader.readAsText(file);
						//reader.readAsArrayBuffer(fileEntry);
					});
				},
				function(err){
					// 失敗時のコールバック関数
					AVWEB.avwLog("NotRead FileSystem");
					avwGrabContentPageImage(
							ClientData.userInfo_accountPath(),
							{ contentId: contentID, sid: ClientData.userInfo_sid(), pageNo: 1 },
							function (data) {
								pageImages = data;
								webGetContentData();
								getSearchDataFromJson();
								getJsonDataPageTitle();
								getJsonDataType4();
								getJsonDataType5();
								getDataJsonFile();
								webGetContentPageSize();
							},
							function (xmlHttpRequest, txtStatus, errorThrown) {
								CONTENTVIEW.showErrorScreen();
							}
					);
				}
		);
	}
}
//END TRB00097 - Editor: Long - Date: 09/30/2013 - Summary : Get All Page size of content

function webGetPageImageContentSize() {
    avwCmsApi(ClientData.userInfo_accountPath(),
	     "webContentPageSize",
	     "GET",
	     { contentId: contentID, sid: ClientData.userInfo_sid() },
	     function (data) {
	         widthContentImage = data.width;
	         heightContentImage = data.height;
	     },
	     null);
};

//START TRB00097 - Editor: Long - Date: 09/30/2013 - Summary : Get All Page size of content
function webGetContentPageSize(){
   avwCmsApi(ClientData.userInfo_accountPath(),
     "webGetContent",
     "GET",
     { contentId: contentID, sid: ClientData.userInfo_sid(), getType: 6 },
     function (data) {
         if( data.contentData.pageInfoData.pagesInfo ){
             $.each(data.contentData.pageInfoData.pagesInfo, function(i, n){
                 contentPageSizeArr.push(n);
             });
             
             //Get Page size of firstPage
             getPageSizeByPageNo(1);
         } else {
             showErrorScreen();
         }
     },
     null); 
};

//Get Pagesize by pageNo
function getPageSizeByPageNo(pageNo){
    for(var i = 0; i < contentPageSizeArr.length; i++){
        var page = contentPageSizeArr[i];
        
        if(page.pageNo == pageNo){            
            widthContentImage = page.pageWidth;
            heightContentImage = page.pageHeight;            
        }
    }
};
//END TRB00097 - Editor: Long - Date: 09/30/2013 - Summary : Get All Page size of content

function webGetContentData() {
    avwCmsApi(ClientData.userInfo_accountPath(),
	     "webGetContent",
	     "GET",
	     { contentId: contentID, sid: ClientData.userInfo_sid(), getType: 1 },
	     function (data) {
	         totalPage = data.contentData.allPageNum;	       
	     },
	     null);
};

/* get Json stored page title */
function getJsonDataPageTitle() {
    avwCmsApi(ClientData.userInfo_accountPath(),
	     "webGetContent",
	     "GET",
	     { contentId: contentID, sid: ClientData.userInfo_sid(), getType: 3 },
	     function (data) {
	         dataPageTitle = [];
	         for (var nIndex = 0; nIndex < totalPage; nIndex++) {
	             dataPageTitle.push("");
	         }
	         if (data.contentData) {

	             if (data.contentData.titleInfo) {
	                 dataPageTitle = data.contentData.titleInfo;
	             }
	         }
	     },
	     null);
};

/* get Json webGetContent4 */
function getJsonDataType4() {
    avwCmsApi(ClientData.userInfo_accountPath(),
	     "webGetContent",
	     "GET",
	     { contentId: contentID, sid: ClientData.userInfo_sid(), getType: 4 },
	     function (data) {
	         dataJsonType4 = data.contentData.linkData;
	     },
	     null);
};

/* get Json webGetContent5 */
function getJsonDataType5() {
    avwCmsApi(ClientData.userInfo_accountPath(),
	     "webGetContent",
	     "GET",
	     { contentId: contentID, sid: ClientData.userInfo_sid(), getType: 5 },
	     function (data) {
	         dataJsonType5 = data.contentData.outlineData;
	     },
	     null);
};

/* read file Json -> get page objects */
function getDataJsonFile() {
    //get content from JSON
    avwCmsApi(ClientData.userInfo_accountPath(),
		     "webGetContent",
		     "GET",
		     { sid: ClientData.userInfo_sid(), contentId: contentID, getType: 2 },
		     function (data) {
		         var JsonFile = data.contentData;
		         pageObjectsData = [];
		         if (JsonFile.vertical) {
		             if (JsonFile.vertical.pages) {
		                 pageObjectsData = JsonFile.vertical.pages;
		                 
		                 //Start Function : No.9 - Editor : Long - Date : 08/16/2013 - Summary : 
                         if(data.contentDataSub != null && data.contentDataSub.length > 0){                          
                             for(var i = 0; i < pageObjectsData.length; i++){
                                 var obj = pageObjectsData[i];
                                 obj["contentDataSub"] = data.contentDataSub;
                             }                                              
                         }
                         //End Function : No.9 - Editor : Long - Date : 08/16/2013 - Summary :
		             }
		         }
		         else if (JsonFile.horizontal) {
		             if (JsonFile.horizontal.pages) {
		                 pageObjectsData = JsonFile.horizontal.pages;
		                 //Start Function : No.9 - Editor : Long - Date : 08/16/2013 - Summary : 
		                 if(data.contentDataSub != null && data.contentDataSub.length > 0){		                     
		                     for(var i = 0; i < pageObjectsData.length; i++){
		                         var obj = pageObjectsData[i];
		                         obj["contentDataSub"] = data.contentDataSub;
		                     }		                     		            
		                 }
		                 //End Function : No.9 - Editor : Long - Date : 08/16/2013 - Summary : 	                 
		             }
		         }
		         //Start : Function : No.12 - Editor : Long - Date: 08/27/2013 - Summary : Get Page Object for content type none
		         else{
		             pageObjectsData = JsonFile.content.pages;
		             
		             if(data.contentDataSub != null && data.contentDataSub.length > 0){                            
                         for(var i = 0; i < pageObjectsData.length; i++){
                             var obj = pageObjectsData[i];
                             obj["contentDataSub"] = data.contentDataSub;
                         }                                              
                     }
		         }
		         
		         //Get Data Of page to draw page
		         if(contentType == ContentTypeKeys.Type_NoFile){
		             
		             var pageDataInfo = [];
		             pageDataInfo["height"] = JsonFile.height;
		             pageDataInfo["width"] = JsonFile.width;
		             pageDataInfo["alpha"] = JsonFile.backgroundAlpha;
		             pageDataInfo["color"] = JsonFile.backgroundColor;
		             
		             totalPage = parseInt(JsonFile.totalPageNum);
		             
		             pageImages = returnImageString(pageDataInfo);
		             widthContentImage = JsonFile.width;
                     heightContentImage = JsonFile.height;
		         }	 
		                 		        
		         //End : Function : No.12 - Editor : Long - Date: 08/27/2013 - Summary : Get Page Object for content type none

		         getPageObjectsByPageIndex(pageObjectsData, 0);
		     },
		     null);
};
function loadDataBookmark(lstPageNo) {
    if (isSendingData == true) {

        avwCmsApi(ClientData.userInfo_accountPath(),
		     "webContentPage",
		     "GET",
		     { sid: ClientData.userInfo_sid(), contentId: contentID, pageNos: lstPageNo[0], thumbnailFlg: 1 },
		     function (data) {
		         getDataLoaded(data.pages);

		         //Resize Image
		         var imgTemp = new Image();
		         $('#img_bookmark_' + data.pages[0].pageNo).attr('src', formatStringBase64(data.pages[0].pageThumbnail));

		         imgTemp.onload = function () {

		             if (imgTemp.width > imgTemp.height) {

		                 $("img.imgbox").attr('height', '');
		                 $("img.imgbox").removeAttr('height');
		                 $("img.imgbox").attr('width', '43');
		             }
		             else {
		                 $("img.imgbox").attr('width', '');
		                 $("img.imgbox").removeAttr('width');
		                 $("img.imgbox").attr('height', '43');
		             }
		         };

		         imgTemp.src = formatStringBase64(data.pages[0].pageThumbnail);

		         lstPageNo = jQuery.grep(lstPageNo, function (value) {
		             return value != lstPageNo[0];
		         });

		         if (lstPageNo.length > 0) {
		             loadDataBookmark(lstPageNo);
		         } else {
		             isSendingData = false;
		         }
		     },
		     null);
    }
};

/* get data using for search */
function getSearchDataFromJson() {
    //get data from JSON file

    var arrPageNo = '';
    for (var nIndex = 0; nIndex < totalPage; nIndex++) {
        if (nIndex == 0) {
            arrPageNo += (nIndex + 1);
        } else {
            arrPageNo += "," + (nIndex + 1);
        }
    }
    avwCmsApi(ClientData.userInfo_accountPath(),
     "webContentPage",
     "GET",
     { sid: ClientData.userInfo_sid(), contentId: contentID, thumbnailFlg: 0 },
     function (data) {
         contentName = data.contentTitle;
         dataWebContentPage = data;

     },
     null);

};

function loadDataSearch(lstPageNo) {
    if (isSendingData == true) {
        avwCmsApi(ClientData.userInfo_accountPath(),
		     "webContentPage",
		     "GET",
		     { sid: ClientData.userInfo_sid(), contentId: contentID, pageNos: lstPageNo[0], thumbnailFlg: 1 },
		     function (data) {
		         getDataLoaded(data.pages);

		         //Resize Image
		         var imgTemp = new Image();
		         $('#img_search_' + data.pages[0].pageNo).attr('src', formatStringBase64(data.pages[0].pageThumbnail));

		         imgTemp.onload = function () {

		             if (imgTemp.width > imgTemp.height) {

		                 $("img.imgbox").attr('height', '');
		                 $("img.imgbox").removeAttr('height');
		                 $("img.imgbox").attr('width', '43');
		             }
		             else {
		                 $("img.imgbox").attr('width', '');
		                 $("img.imgbox").removeAttr('width');
		                 $("img.imgbox").attr('height', '43');
		             }
		         };

		         imgTemp.src = formatStringBase64(data.pages[0].pageThumbnail);

		         lstPageNo = jQuery.grep(lstPageNo, function (value) {
		             return value != lstPageNo[0];
		         });

		         if (lstPageNo.length > 0) {
		             loadDataSearch(lstPageNo);
		         } else {
		             isSendingData = false;
		         }
		     },
		     null);
    }
};


