/// <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() {
    avwGrabContentPageImage(ClientData.userInfo_accountPath(),
	     { contentId: contentID, sid: ClientData.userInfo_sid(), pageNo: 1, pid: pid },
	     function (data) {	  
	         pageImages = data;
	         webGetContentData();
	         getSearchDataFromJson();
	         getJsonDataPageTitle();
	         getJsonDataType4();
	         getJsonDataType5();
	         getDataJsonFile();
	         //webGetPageImageContentSize();
	         webGetContentPageSize();
	     },
	     function (xmlHttpRequest, txtStatus, errorThrown) {
	         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(), pid: pid },
	     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, pid: pid },
     function (data) {    
         $.each(data.contentData.pageInfoData.pagesInfo, function(i, n){
             contentPageSizeArr.push(n);
         });
         
         //Get Page size of firstPage
         getPageSizeByPageNo(1);
     },
     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, pid: pid },
	     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, pid: pid },
	     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, pid: pid },
	     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, pid: pid },
	     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, pid: pid },
		     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, pid: pid },
		     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 , pid: pid},
     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, pid: pid },
		     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);
    }
};