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

//Call API
CONTENTVIEW_CALLAPI.abapi = function(name, param, method, callback) {
    AVWEB.avwCmsApiSync(ClientData.userInfo_accountPath(), name, method, param, callback, null);
};

/* get Json stored content info */
CONTENTVIEW_CALLAPI.getJsonContentInfo = function( doneFunc ) {

	//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.getJsonContentInfoDone(doneFunc);
	     },
	     function (xmlHttpRequest, txtStatus, errorThrown) {
	    	 CONTENTVIEW.showErrorScreen();
	     }
	);
};

CONTENTVIEW_CALLAPI.getJsonContentInfoDone = function(doneFunc){

    var ajax1 = CONTENTVIEW_CALLAPI.webGetContentDataWhen();
    var ajax2 = CONTENTVIEW_CALLAPI.getSearchDataFromJsonWhen();
    var ajax3 = CONTENTVIEW_CALLAPI.getJsonDataPageTitleWhen();
    var ajax4 = CONTENTVIEW_CALLAPI.getJsonDataType4When();
    var ajax5 = CONTENTVIEW_CALLAPI.getJsonDataType5When();
    var ajax6 = CONTENTVIEW_CALLAPI.getDataJsonFileWhen();
    var ajax7 = CONTENTVIEW_CALLAPI.webGetContentPageSizeWhen();
    $.when(
         ajax1,ajax2,ajax3,ajax4,ajax5,ajax6,ajax7
    ).done(function(data1, data2, data3, data4, data5, data6, data7) {
        //console.log("done:data1:" + data1);
        //console.log("done:data2:" + data2);
        //console.log("done:data3:" + data3);
        //console.log("done:data4:" + data4);
        //console.log("done:data5:" + data5);
        //console.log("done:data6:" + data6);
        //console.log("done:data7:" + data7);
        if(data1){
            CONTENTVIEW_CALLAPI.webGetContentDataDone(data1[0]);
        }
        if(data2){
            CONTENTVIEW_CALLAPI.getSearchDataFromJsonDone(data2[0]);
        }
        if(data3){
            CONTENTVIEW_CALLAPI.getJsonDataPageTitleDone(data3[0]);
        }
        if(data4){
            CONTENTVIEW_CALLAPI.getJsonDataType4Done(data4[0]);
        }
        if(data5){
            CONTENTVIEW_CALLAPI.getJsonDataType5Done(data5[0]);
        }
        if(data6){
            CONTENTVIEW_CALLAPI.getDataJsonFileDone(data6[0]);
        }
        if(data7){
            CONTENTVIEW_CALLAPI.webGetContentPageSizeDone(data7[0]);
        }
        if(doneFunc){
            doneFunc();
        }
    });

};
//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);
};

CONTENTVIEW_CALLAPI.webGetContentPageSizeWhen = function(){

    var params = {
            contentId: CONTENTVIEW_GENERAL.contentID,
            sid: CONTENTVIEW.getSessionId(),
            getType: 6,
            isStreaming: ClientData.isStreamingMode()
    };

    return AVWEB._callCmsApiWhen(
            ClientData.userInfo_accountPath(),
            "webGetContent",
            "GET",
            params
    );

};

CONTENTVIEW_CALLAPI.webGetContentPageSizeDone = 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();
    }
};

//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;
        }
    }
};

CONTENTVIEW_CALLAPI.webGetContentDataWhen = function() {

    var params = {
            contentId: CONTENTVIEW_GENERAL.contentID,
            sid: CONTENTVIEW.getSessionId(),
            getType: 1,
            isStreaming: ClientData.isStreamingMode()
    };

    return AVWEB._callCmsApiWhen(
            ClientData.userInfo_accountPath(),
            "webGetContent",
            "GET",
            params
    );

};

CONTENTVIEW_CALLAPI.webGetContentDataDone = function(data) {
     CONTENTVIEW_GENERAL.totalPage = data.contentData.allPageNum;
};

/* get Json stored page title */
CONTENTVIEW_CALLAPI.getJsonDataPageTitleWhen = function() {

    var params = {
            contentId: CONTENTVIEW_GENERAL.contentID,
            sid: CONTENTVIEW.getSessionId(),
            getType: 3,
            isStreaming: ClientData.isStreamingMode()
    };

    return AVWEB._callCmsApiWhen(
            ClientData.userInfo_accountPath(),
            "webGetContent",
            "GET",
            params
    );

};

CONTENTVIEW_CALLAPI.getJsonDataPageTitleDone = 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;
         }
     }
};

/* get Json webGetContent4 */
CONTENTVIEW_CALLAPI.getJsonDataType4When = function() {

    var params = {
            contentId: CONTENTVIEW_GENERAL.contentID,
            sid: CONTENTVIEW.getSessionId(),
            getType: 4,
            isStreaming: ClientData.isStreamingMode()
    };

    return AVWEB._callCmsApiWhen(
            ClientData.userInfo_accountPath(),
            "webGetContent",
            "GET",
            params
    );

};

CONTENTVIEW_CALLAPI.getJsonDataType4Done = function(data) {
     CONTENTVIEW_GENERAL.dataJsonType4 = data.contentData.linkData;
};

/* get Json webGetContent5 */
CONTENTVIEW_CALLAPI.getJsonDataType5When = function() {

    var params = {
            contentId: CONTENTVIEW_GENERAL.contentID,
            sid: CONTENTVIEW.getSessionId(),
            getType: 5,
            isStreaming: ClientData.isStreamingMode()
    };

    return AVWEB._callCmsApiWhen(
            ClientData.userInfo_accountPath(),
            "webGetContent",
            "GET",
            params
    );

};

CONTENTVIEW_CALLAPI.getJsonDataType5Done = function(data) {
     CONTENTVIEW_GENERAL.dataJsonType5 = data.contentData.outlineData;
};

/* read file Json -> get page objects */
CONTENTVIEW_CALLAPI.getDataJsonFileWhen = function() {

    var params = {
            contentId: CONTENTVIEW_GENERAL.contentID,
            sid: CONTENTVIEW.getSessionId(),
            getType: 2,
            isStreaming: ClientData.isStreamingMode()
    };

    return AVWEB._callCmsApiWhen(
            ClientData.userInfo_accountPath(),
            "webGetContent",
            "GET",
            params
    );

};

CONTENTVIEW_CALLAPI.getDataJsonFileDone = 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{
    	
    	//#25861 オーサリング機能のプレビュー機能が無反応となる
        if (JsonFile.content) {
        	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 = [];
        if(JsonFile.height){
        	pageDataInfo["height"] = JsonFile.height;
        } else {
        	pageDataInfo["height"] = "1004";
        }
        if(JsonFile.width){
        	pageDataInfo["width"] = JsonFile.width;
        } else {
        	pageDataInfo["width"] = "710";
        }
        if(JsonFile.backgroundAlpha){
        	pageDataInfo["alpha"] = JsonFile.backgroundAlpha;
        } else {
        	pageDataInfo["alpha"] = "1";
        }
        if(JsonFile.backgroundColor){
        	pageDataInfo["color"] = JsonFile.backgroundColor;
        } else {
        	pageDataInfo["color"] = "ffffff";
        }
        if(JsonFile.totalPageNum){
        	CONTENTVIEW_GENERAL.totalPage = parseInt(JsonFile.totalPageNum);
        } else {
        	CONTENTVIEW_GENERAL.totalPage = 1;
        }

        CONTENTVIEW_GENERAL.pageImages = CONTENTVIEW_CONTENTTYPENONE.returnImageString(pageDataInfo);
        CONTENTVIEW_GENERAL.widthContentImage = pageDataInfo["width"];
        CONTENTVIEW_GENERAL.heightContentImage = pageDataInfo["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);

};

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.getSearchDataFromJsonWhen = 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()
    };

    return AVWEB._callCmsApiWhen(
            ClientData.userInfo_accountPath(),
            "webContentPage",
            "GET",
            params
    );

};

CONTENTVIEW_CALLAPI.getSearchDataFromJsonDone = function(data) {
    CONTENTVIEW_GENERAL.contentName = data.contentTitle;
    CONTENTVIEW_GENERAL.dataWebContentPage = data;
};

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);
    }
};

CONTENTVIEW_CALLAPI.ready = function(){
};