/// コンテンツ詳細画面

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

DETAIL.displayData = {
    contentID: "",
    contentTitle: "",
    contentDetail: "",
    contentShare: "",
    contentThumbnail: "",
    deliveryDate: (new Date()),
    pages: []
};


// Init function of page
$(document).ready(function () {

});

// Show content detail
DETAIL.openContentDetail = function() {
	if(!COMMON.isPdfContent(ClientData.contentInfo_contentType())){
		$('#sectionContentDetail').removeClass().addClass('sectiondetailnopdf');
	}
	else {
		$('#sectionContentDetail').removeClass().addClass('sectiondetail');
	}
	
	DETAIL.displayData = {
			contentID: "",
			contentTitle: "",
			contentDetail: "",
			contentShare: "",
			contentThumbnail: "",
			deliveryDate: (new Date()),
			pages: []
	};

	// Clear childs
	$('#book_list').html('');

    // Clear display info
	$("#imgContentThumbnail").attr('src', "img/data_loading.gif");
	DETAIL.resetLoadingImageSize();
    
	$("#txtContentTitle").text('');
	$("#txtPubDt2_Dsp").text('');
	$("#txtContentDetail").text('');

	$("#contentDetailClose").unbind("click");
	$("#contentDetailClose").click(DETAIL.contentDetailClose_Click);
	$("#contentdetail_dspRead").unbind("click");
	$("#contentdetail_dspRead").click(DETAIL.contentdetail_dspRead_Click);
	
	//if( ClientData.serviceOpt_content_share() != '2'){
	//$("#contentdetail_dspShare").click(SHARE.openContentShare);
	$("#contentdetail_dspShare").hide();
	//}
	
	COMMON.lockLayout();
	$("#contentDetail").css('z-index', 101);
	$("#sectionContentDetail").show();
	$("#contentDetail").show();
	$("#contentDetail").center();
	if ($("#contentDetail").height() > $(window).height()){
		$("#contentDetail").css('top', '0');
	}

	// Get contentid, thumbnail from list screen
	DETAIL.displayData.contentID = ClientData.contentInfo_contentId();
	DETAIL.displayData.contentThumbnail = ClientData.contentInfo_contentThumbnail();    

	var params = {
		sid: ClientData.userInfo_sid(),
		getType: '1',
		contentId: DETAIL.displayData.contentID
	};
	
	// Get content detail
	AVWEB.avwCmsApi(ClientData.userInfo_accountPath(), "webGetContent", "GET", params,
			function (data) {
				var contentType = ClientData.contentInfo_contentType();
				// Get content detail
				DETAIL.displayData.contentTitle = data.contentData.contentName;
				DETAIL.displayData.contentDetail = data.contentData.contentDetail;
				if( ClientData.serviceOpt_content_share() == '2'){
					if( data.contentData.readerShare ){
						//SHARE.contentId = DETAIL.displayData.contentID;
						DETAIL.displayData.contentShare = data.contentData.readerShare;
					}
				}
				DETAIL.displayData.deliveryDate = COMMON.convertToDate(data.contentData.deliveryStartDate);
				
				//Start Function : No.12 -- Editor : Viet Nguyen -- Date : 08/01/2013 -- Summary : Create new function to return content type of content.
				if(!COMMON.isPdfContent(contentType)){
					DETAIL.showContentNotPDF(DETAIL.displayData);
				}
				else{
					// Get pages
					AVWEB.avwCmsApiSync(ClientData.userInfo_accountPath(), "webContentPage", "GET", { contentId: ClientData.contentInfo_contentId(), sid: ClientData.userInfo_sid(), thumbnailFlg: 1, pageNos: '1,2,3,4,5,6'},
							function (data) {
								// Get pages
								for (var nIndex = 0; nIndex < data.pages.length; nIndex++) {
									if (nIndex < 6) {
										DETAIL.displayData.pages.push({ pageNo: data.pages[nIndex].pageNo, pageText: data.pages[nIndex].pageText, pageThumbnail: ("data:image/jpeg;base64," + data.pages[nIndex].pageThumbnail) });
									}
								}
								// Show to screen
								DETAIL.showContent(DETAIL.displayData);
							},
							null
					);
				}
				
				//End Function : No.12 -- Editor : Viet Nguyen -- Date : 08/01/2013 -- Summary : Create new function to return content type of content.
				
			},
			null
	);
};

// Close content detail
DETAIL.contentDetailClose_Click = function(e) {
	if( e != null ){
		e.preventDefault();
	}
	COMMON.unlockLayout();
	$("#contentDetail").hide();
	$("#sectionContentDetail").hide();
};

/*
----------------------------------------------------------------------------
Event groups [start]
----------------------------------------------------------------------------
*/

DETAIL.contentdetail_dspRead_Click = function(e) {
	e.preventDefault();
	var outputId = ClientData.contentInfo_contentId();
	LIMIT_ACCESS_CONTENT.checkLimitContent(outputId,
			function () {
				DETAIL.contentdetail_dspRead_Click_callback(outputId);
			},
			function(){
			},
			1
	);
};

DETAIL.contentdetail_dspRead_Click_callback = function(outputId) {

	var date = new Date();
	var month = date.getMonth() + 1;
	var day = date.getDate();
	var outputDate = COMMON.formatNormalDate(day, month, date.getFullYear());
	var contentIdArray = [];
	var checkflag = false;

	//Store Content id that user has read
	if (ClientData.ReadingContentIds().length > 0) {
		contentIdArray = ClientData.ReadingContentIds();
		
		for (var nIndex = 0; nIndex < contentIdArray.length; nIndex++) {
			if (contentIdArray[nIndex].contentid == outputId) {
				checkflag = true;
				if (contentIdArray[nIndex].viewdate == '' || contentIdArray[nIndex].viewdate == null || contentIdArray[nIndex].viewdate == 'undefined') {
					contentIdArray[nIndex].viewdate = outputDate;
					contentIdArray[nIndex].originviewdate = date;
				}
				break;
			}
			else {
				checkflag = false;
			}
		}
		
		if (!checkflag) {
			contentIdArray.push({ contentid: outputId, viewdate: outputDate, originviewdate: date });
		}
	}
	else {
		contentIdArray.push({ contentid: outputId, viewdate: outputDate, originviewdate: date });
	}
	
	//Renew ReadingContentID
	var newArray = [];
	ClientData.ReadingContentIds(newArray);
	
	//Set data for readingcontentid
	ClientData.ReadingContentIds(contentIdArray);
	
	//Set ResouceVersion for content
	COMMON.setResourceVersionData(outputId);
	
	//Set MetaVersion for content
	COMMON.setMetaVersionData(outputId);
	
	// Redirect to screen: contentview     
	//$('body,html').animate({ scrollTop: 0 }, 0);
	ClientData.IsRefresh(false);
	
	if (ClientData.contentInfo_contentType() == COMMON.ContentTypeKeys.Type_Others) {
		// Get content detail
		HEADER.downloadResourceById(ClientData.contentInfo_contentId());
	}
	else if(ClientData.contentInfo_contentType() == COMMON.ContentTypeKeys.Type_Link){
		// Get content detail
		HEADER.viewLinkContentById(ClientData.contentInfo_contentId());
	}
	else {
		//AVWEB.avwScreenMove(COMMON.ScreenIds.ContentView);
    	//試験
		//詳細閉じる
		DETAIL.contentDetailClose_Click(null);
		if( $('#bookshelf').length || $('#list').length ){
			//ビューア表示
			HOME.showContentView();
		} else if( $('#bookmark').length ){
			//ビューア表示
			BOOKMARK.showContentView();
		} else if( $('#history').length ){
			//ビューア表示
			HISTORY.showContentView();
		} else if( $('#contentsearch').length ){
			//ビューア表示
			CONTENTSEARCH.showContentView();
		}
	}
};

//Start Function : No.12 -- Editor : Viet Nguyen -- Date : 08/01/2013 -- Summary : Create new function to return content type of content.

DETAIL.showContentNotPDF = function(displayData) {
    $("#txtPubDt2_Dsp").text(displayData.deliveryDate.jpDateString() + " " + displayData.deliveryDate.jpShortTimeString());
    $("#txtContentDetail").text(displayData.contentDetail);
    //$("#txtContentTitle").text(COMMON.truncate(displayData.contentTitle, 20));
    $("#txtContentTitle").text(displayData.contentTitle);
    
    //コンテンツ共有
    if( displayData.contentShare == '1' ){
        SHARE.contentId = displayData.contentID;
        SHARE.contentTitle = displayData.contentTitle;
        $("#contentdetail_dspShare").show();
    	$("#contentdetail_dspShare").unbind("click");
        $("#contentdetail_dspShare").click(SHARE.openContentShare);
    }
    
    var tempContentType = ClientData.contentInfo_contentType();
    
    if(displayData.contentThumbnail == '' || displayData.contentThumbnail == null || displayData.contentThumbnail == 'undefined'){
        if(!COMMON.isPdfContent(tempContentType)){
            var src = HEADER.getThumbnailForOtherType(contentType);
            if( src != '' ){
            	displayData.contentThumbnail = src;
            }
        }
        //}else{
        //contentThumbnail = contentThumbnail;
    }
   
    var imgTemp = new Image();
    $("#imgContentThumbnail").attr("src", displayData.contentThumbnail);
    imgTemp.onload = function () {

        //resize Image
        if (imgTemp.width > imgTemp.height) {

            $("#imgContentThumbnail").attr('height', '');
            $("#imgContentThumbnail").removeAttr('height');
            $("#imgContentThumbnail").attr('width', '120');
            var realHeight = (120 * imgTemp.height) / imgTemp.width;
            $("#imgContentThumbnail").css('padding-top', (145 - realHeight)/2 + "px");
        }
        else {
            $("#imgContentThumbnail").attr('width', '');
            $("#imgContentThumbnail").removeAttr('width');
            $("#imgContentThumbnail").attr('height', '120');
            
            $("#imgContentThumbnail").css('padding-top', "12px");
        }

    };
    	
    imgTemp.src = displayData.contentThumbnail;
};

//End Function : No.12 -- Editor : Viet Nguyen -- Date : 08/01/2013 -- Summary : Create new function to return content type of content.

// Show detail content
DETAIL.showContent = function(displayData) {
    $("#txtPubDt2_Dsp").text(displayData.deliveryDate.jpDateString() + " " + displayData.deliveryDate.jpShortTimeString());
    $("#txtContentDetail").text(displayData.contentDetail);
    $("#txtContentTitle").text(COMMON.truncate(displayData.contentTitle, 20));
    
    //コンテンツ共有
    if( displayData.contentShare == '1' ){
        SHARE.contentId = displayData.contentID;
        SHARE.contentTitle = displayData.contentTitle;
        $("#contentdetail_dspShare").show();
    	$("#contentdetail_dspShare").unbind("click");
        $("#contentdetail_dspShare").click(SHARE.openContentShare);
    }
    
    $("#imgContentThumbnail").attr("src", displayData.contentThumbnail);
    var imgTemp = new Image();

    imgTemp.onload = function () {

        //resize Image
        if (imgTemp.width > imgTemp.height) {

            $("#imgContentThumbnail").attr('height', '');
            $("#imgContentThumbnail").removeAttr('height');
            $("#imgContentThumbnail").attr('width', '120');
            var realHeight = (120 * imgTemp.height) / imgTemp.width;
            $("#imgContentThumbnail").css('padding-top', (145 - realHeight)/2 + "px");
        }
        else {
            $("#imgContentThumbnail").attr('width', '');
            $("#imgContentThumbnail").removeAttr('width');
            $("#imgContentThumbnail").attr('height', '120');

            $("#imgContentThumbnail").css('padding-top', "12px");
        }

    };
    	
    imgTemp.src = displayData.contentThumbnail;
    
    //resizeThumbnailContentDetail(contentThumbnail, 120, 160);
    // Show pages
    for (var nIndex = 0; nIndex < displayData.pages.length; nIndex++) {
        //insertRow(imgSample, pages[nIndex].pageText, pages[nIndex].pageNo);
    	DETAIL.insertRow(displayData.pages[nIndex].pageThumbnail, COMMON.truncate(COMMON.getLines(displayData.pages[nIndex].pageText, 3), 45), displayData.pages[nIndex].pageNo); //55
    }
};

DETAIL.insertRow = function(pageThumbnail, pageText, pageNo) {
    var newRow = "";
    newRow += "<ul>";
    newRow += '<li class="list_img"><img src="' + pageThumbnail + '" alt="" width="90" /></li>';
    newRow += '<li class="list_title"><a href="#">' + COMMON.htmlEncode(pageText) + '</a></li>';
    newRow += '<li class="page"><label id="Label2" class="lang" lang="txtPage">' + I18N.i18nText('txtPage') + '</label>' + pageNo + '</li>';
    newRow += "</ul>";

    $('#book_list').append(newRow);
    I18N.i18nReplaceText();
    
    //Resize Image
    var imgTemp = new Image();
    
    imgTemp.onload = function(){
    		
    	if(imgTemp.width > imgTemp.height) {
    			
    		$("li.list_img img").attr('height', '');
    		$("li.list_img img").removeAttr('height');
    		$("li.list_img img").attr('width', '90');
    	}
    	else {
    		$("li.list_img img").attr('width', '');
    		$("li.list_img img").removeAttr('width');
    		$("li.list_img img").attr('height', '90');
    	}
	};
    	
    imgTemp.src = pageThumbnail;
};

/*
----------------------------------------------------------------------------
Event groups [ end ]
----------------------------------------------------------------------------
*/

/*
----------------------------------------------------------------------------
Setting dialog [start]
----------------------------------------------------------------------------
*/
/*
----------------------------------------------------------------------------
Setting dialog [ end ]
----------------------------------------------------------------------------
*/

DETAIL.resetLoadingImageSize = function(){
	$("#imgContentThumbnail").attr('height','25px');
	$("#imgContentThumbnail").attr('width','25px');
};
