
//名前空間用のオブジェクトを用意する
var CONTENTVIEW_CALLAPI = {};

//Call API
CONTENTVIEW_CALLAPI.abapi = function(name, param, method, callback) {
	AVWEB.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 */
CONTENTVIEW_CALLAPI.getJsonContentInfo = function() {
	
	//console.log("CONTENTVIEW_CALLAPI.getJsonContentInfo");
	AVWEB.avwGrabContentPageImage(ClientData.userInfo_accountPath(),
	     { contentId: CONTENTVIEW_GENERAL.contentID, sid: CONTENTVIEW.getSessionId(), pageNo: 1 },
	     function (data) {	  
	    	 CONTENTVIEW_GENERAL.pageImages = data;	        
	    	 CONTENTVIEW_CALLAPI.webGetContentData();
	    	 CONTENTVIEW_CALLAPI.getSearchDataFromJson();
	         CONTENTVIEW_CALLAPI.getJsonDataPageTitle();
	         CONTENTVIEW_CALLAPI.getJsonDataType4();
	         CONTENTVIEW_CALLAPI.getJsonDataType5();
	         CONTENTVIEW_CALLAPI.getDataJsonFile();
	         //CONTENTVIEW_CALLAPI.webGetPageImageContentSize();
	         CONTENTVIEW_CALLAPI.webGetContentPageSize();
	         
	     },
	     function (xmlHttpRequest, txtStatus, errorThrown) {
	    	 CONTENTVIEW.showErrorScreen();
	     });
};
//END TRB00097 - Editor: Long - Date: 09/30/2013 - Summary : Get All Page size of content

//未使用
CONTENTVIEW_CALLAPI.webGetPageImageContentSize = function() {
	
    var params = {
    		contentId: CONTENTVIEW_GENERAL.contentID,
    		sid: CONTENTVIEW.getSessionId(),
    		isStreaming: ClientData.isStreamingMode()
    };
	
	AVWEB.avwCmsApi(ClientData.userInfo_accountPath(),
	     "webContentPageSize",
	     "GET",
	     { contentId: CONTENTVIEW_GENERAL.contentID, sid: CONTENTVIEW.getSessionId() },
	     function (data) {
	    	 CONTENTVIEW_GENERAL.widthContentImage = data.width;
	    	 CONTENTVIEW_GENERAL.heightContentImage = data.height;
	     },
	     null);
};

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

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

CONTENTVIEW_CALLAPI.webGetContentData = function() {
	
    var params = {
    		contentId: CONTENTVIEW_GENERAL.contentID,
    		sid: CONTENTVIEW.getSessionId(),
    		getType: 1,
    		isStreaming: ClientData.isStreamingMode()
    };
	
	AVWEB.avwCmsApi(ClientData.userInfo_accountPath(),
	     "webGetContent",
	     "GET",
	     params,
	     function (data) {
	    	 CONTENTVIEW_GENERAL.totalPage = data.contentData.allPageNum;
	     },
	     null);
};

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

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

/* get Json webGetContent4 */
CONTENTVIEW_CALLAPI.getJsonDataType4 = function() {
	
    var params = {
    		contentId: CONTENTVIEW_GENERAL.contentID,
    		sid: CONTENTVIEW.getSessionId(),
    		getType: 4,
    		isStreaming: ClientData.isStreamingMode()
    };
	
	AVWEB.avwCmsApi(ClientData.userInfo_accountPath(),
	     "webGetContent",
	     "GET",
	     params,
	     function (data) {
	    	 CONTENTVIEW_GENERAL.dataJsonType4 = data.contentData.linkData;
	     },
	     null);
};

/* get Json webGetContent5 */
CONTENTVIEW_CALLAPI.getJsonDataType5 = function() {
	
    var params = {
    		contentId: CONTENTVIEW_GENERAL.contentID,
    		sid: CONTENTVIEW.getSessionId(),
    		getType: 5,
    		isStreaming: ClientData.isStreamingMode()
    };
	
	AVWEB.avwCmsApi(ClientData.userInfo_accountPath(),
	     "webGetContent",
	     "GET",
	     params,
	     function (data) {
	    	 CONTENTVIEW_GENERAL.dataJsonType5 = data.contentData.outlineData;
	     },
	     null);
};

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

		         CONTENTVIEW_GETDATA.getPageObjectsByPageIndex(CONTENTVIEW_GENERAL.pageObjectsData, 0);
		     },
		     null);
};

CONTENTVIEW_CALLAPI.loadDataBookmark = function(lstPageNo) {
    if (CONTENTVIEW_GENERAL.isSendingData == true) {

        var params = {
        		contentId: CONTENTVIEW_GENERAL.contentID,
        		sid: CONTENTVIEW.getSessionId(),
        		pageNos: lstPageNo[0],
        		thumbnailFlg: 1,
        		isStreaming: ClientData.isStreamingMode()
        };
    	
    	AVWEB.avwCmsApi(ClientData.userInfo_accountPath(),
		     "webContentPage",
		     "GET",
		     params,
		     function (data) {
		    	 CONTENTVIEW_GETDATA.getDataLoaded(data.pages);

		         //Resize Image
		         var imgTemp = new Image();
		         $('#img_bookmark_' + data.pages[0].pageNo).attr('src', COMMON.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 = COMMON.formatStringBase64(data.pages[0].pageThumbnail);

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

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

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

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

     },
     null);

};

CONTENTVIEW_CALLAPI.loadDataSearch = function(lstPageNo) {
    if (CONTENTVIEW_GENERAL.isSendingData == true) {
    	
        var params = {
        		contentId: CONTENTVIEW_GENERAL.contentID,
        		sid: CONTENTVIEW.getSessionId(),
        		pageNos: lstPageNo[0],
        		thumbnailFlg: 1,
        		isStreaming: ClientData.isStreamingMode()
        };
    	
    	AVWEB.avwCmsApi(ClientData.userInfo_accountPath(),
		     "webContentPage",
		     "GET",
		     params,
		     function (data) {
		    	 CONTENTVIEW_GETDATA.getDataLoaded(data.pages);

		         //Resize Image
		         var imgTemp = new Image();
		         $('#img_search_' + data.pages[0].pageNo).attr('src', COMMON.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 = COMMON.formatStringBase64(data.pages[0].pageThumbnail);

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

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

