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

// Start Declare Variables
// ----Constant-----------//
HISTORY.DEFAULT_SORT_TYPE = '4';
HISTORY.DEFAULT_SORT_ORDER = '2';
HISTORY.DEFAULT_SEARCH_DIVISION = 0;

// Thumbnail array
HISTORY.thumbnailArr = [];
// Contdent type array.
HISTORY.contentTypeArr = [];
HISTORY.contentIdArray = [];

HISTORY.totalPage;
HISTORY.contentViewData = [];
HISTORY.noRecordFlg = false;
HISTORY.home_isMove = false;
HISTORY.history_contentTitleKana = [];
// スクロール復帰
HISTORY.scrollTop = 0;

$(document).ready(function() {

	if (!AVWEB.avwCheckLogin(COMMON.ScreenIds.Login)) {
		return;
	}

	// ソート条件デフォルト設定
	if (AVWEB.avwSysSetting().sortTypeHistory != undefined) {
		HISTORY.DEFAULT_SORT_TYPE = AVWEB.avwSysSetting().sortTypeHistory;
	}
	if (AVWEB.avwSysSetting().sortOrderHistory != undefined) {
		HISTORY.DEFAULT_SORT_ORDER = AVWEB.avwSysSetting().sortOrderHistory;
	}

	if (ClientData.serviceOpt_encryption() != 'Y') {
		COMMON.LockScreen();
	}

	document.title = I18N.i18nText('dspViewHistory') + ' | ' + I18N.i18nText('sysAppTitle');

	ClientData.BookmarkScreen(COMMON.ScreenIds.History);

	if (ClientData.requirePasswordChange() != 1) {
		//if (ClientData.ReadingContentIds() == null || ClientData.ReadingContentIds() == 'undefined' || ClientData.ReadingContentIds().length == 0) {
		//} else {
		//	HISTORY.syncReadingContent();
		//}

		// remove hover effect when is touch device
		HISTORY.removeHoverCss();

		// Render Grid
		HISTORY.renderGridView();

		// Go To Details Page
		$('#main-ws canvas').live('click', HISTORY.canvasClickFunction);
		$('#main-ws canvas').live('touchend', HISTORY.canvasClickFunction);
		$('#main-ws canvas').live('touchmove', function() {
			HISTORY.home_isMove = true;
		});

		// Open dialog
		$('.dialog').live('click', HISTORY.titleClickFunction);
		// $('.dialog').live('touchstart', HISTORY.titleClickFunction);
		$('.dialog').live('touchend', HISTORY.titleClickFunction);
		$('.dialog').live('touchmove', function() {
			HISTORY.home_isMove = true;
		});

		// Sort Title
		$('#control-sort-title').click(HISTORY.sortByTitleFunction);

		// Sort by title kana
		$('#control-sort-titlekana').click(HISTORY.sortByTitleKanaFunction);

		// sort by release date
		$('#control-sort-releasedate').click(HISTORY.sortByReleaseDateFunction);

		$('#control-sort-viewdate').click(HISTORY.sortByViewDateFunction);

		// Go To Details Page
		$('.button-details').live('click', HISTORY.readSubmenuFunction);
		// $('.button-details').live('touchstart', HISTORY.readSubmenuFunction);
		$('.button-details').live('touchend', HISTORY.readSubmenuFunction);
		$('.button-details').live('touchmove', function() {
			HISTORY.home_isMove = true;
		});

		// Show Share Dialog
		$('.button-share').live('click', HISTORY.showContentShareDlgFunction);
		$('.button-share').live('touchend', HISTORY.showContentShareDlgFunction);
		$('.button-share').live('touchmove', function() {
			HISTORY.home_isMove = true;
		});
		
		//PDF download
        $('.button-download').live('click', HISTORY.downloadFunction);
        $('.button-download').live('touchend', HISTORY.downloadFunction);
        $('.button-download').live('touchmove', function () { HOME.home_isMove = true; });

		$(window).resize(function() {
			if ($("#contentDetail").css("display") != "none") {
				// Refresh panel of detail to center.
				$("#contentDetail").center();
				if ($("#contentDetail").height() > $(window).height()) {
					$("#contentDetail").css('top', '0');
				}
			}
		});
	} else {
		// Check if Force Change password
		HEADER.checkForceChangePassword();
	}

	if (COMMON.isAnonymousLogin()) {
		// プッシュメッセージ隠す
		$('#dspPushMessage').hide();
	} else {
		if ((ClientData.serviceOpt_apns() == 'Y') || (ClientData.serviceOpt_usable_push_message() == 'Y')) {
			$('#dspPushMessage').show();
		} else {
			$('#dspPushMessage').hide();
		}
	}

	// 詳細、共有表示パーツ読み込み
	$("#inc_detail").load("./inc_detail.html?__UPDATEID__", function(myData, myStatus, xhr) {
		// 読み込み完了時の処理
		I18N.i18nReplaceText();
		$('.datepicker').pickadate({
			format : 'yyyy-mm-dd'
		});
	});

	// ビューア、共有表示パーツ読み込み
	$("#viewer").load("./inc_contentview.html?__UPDATEID__", function(myData, myStatus, xhr) {
		// 読み込み完了時の処理
		I18N.i18nReplaceText();
	});

});

HISTORY.makeContentIds = function(){

	//並び替え
	var sortArr = ClientData.ReadingContentIds();
	if(sortArr.length <= 0 ){
		return "-1";
	}

	var temp;
	for ( var i = 0; i < sortArr.length; i++) {
		for ( var j = sortArr.length - 1; j > i; j--) {
			if (sortArr[j].originviewdate > sortArr[j - 1].originviewdate) {
				temp = sortArr[j];
				sortArr[j] = sortArr[j - 1];
				sortArr[j - 1] = temp;
			}
		}
	}

	var contentIds = "";
	var cnt = 0;
	for ( var i = 0; i < sortArr.length; i++) {
		//console.log("contentId:" + sortArr[i].contentid + " viewdate:" + sortArr[i].originviewdate)
		if(sortArr[i].originviewdate != null && sortArr[i].originviewdate != ''){
			cnt = cnt + 1;
			if(cnt == 1){
				contentIds = sortArr[i].contentid;
			} else {
				contentIds = contentIds + "," + sortArr[i].contentid;
			}
			if(cnt == 50){
				break;
			}
		}
	}
	
	return contentIds;
};

// /Render Content
HISTORY.renderContent = function(id, text, division, type, order) {

	var contentIds = HISTORY.makeContentIds();

	var params = {
		sid : id,
		searchText : text,
		searchDivision : division,
		sortType : type,
		sortOrder : order,
		recordFrom: 0,
		recordTo: 99999,
		categoryId : '',
		groupId : '',
		contentIds : contentIds
	};

	AVWEB.avwCmsApiSync(ClientData.userInfo_accountPath(), 'webContentList', 'POST', params, function(data) {

		$('#content-grid').html('');
		var readingContentIds = ClientData.ReadingContentIds();
		for ( var i = 0; i < data.contentList.length; i++) {
			post = data.contentList[i];

			// save alert message level
			LIMIT_ACCESS_CONTENT.messageLevel[post.contentId] = {
				alertMessageLevel : post.alertMessageLevel,
				alertMessage : post.alertMessage
			};

			var viewdate = HISTORY.renderViewDate(post.contentId, readingContentIds);
			if (viewdate != null && viewdate != 'undefined' && viewdate != '') {

				// assign thumbnail to array
				var formatThumbnail = post.contentThumbnail;
				if ((formatThumbnail != null) && (formatThumbnail != 'undefined') && (formatThumbnail != '')) {
					formatThumbnail = COMMON.formatStringBase64(formatThumbnail);
				}

				HISTORY.thumbnailArr.push({
					contentId : post.contentId,
					thumbnail : formatThumbnail
				});

				HISTORY.contentTypeArr.push({
					contentId : post.contentId,
					contentType : post.contentType
				});

				// assign version to array
				COMMON.resourceVersionArr.push({
					contentid : post.contentId,
					resourceversion : post.resourceVersion
				});

				// assign meta version to array
				COMMON.metaVersionArr.push({
					contentid : post.contentId,
					metaversion : post.metaVersion
				});

				// Check if content has marking or memo
				HISTORY.checkContentMarkingMemoOption(post.contentId);

				var originalViewDate = HISTORY.formatDate(HISTORY.returnOriginalViewDate(post.contentId,readingContentIds));
				HISTORY.addReadContentToArray(post.contentId, post.resourceVersion, post.metaVersion, post.contentThumbnail, post.contentTitle, post.contentNameKana,
						post.contentDeliveryDate, post.contentType, post.readerShare, originalViewDate, post.printFlg);

			}
		}

		if (data.recordFrom) {
			ClientData.searchCond_recordFrom(data.recordFrom);
		}
		if (data.recordTo) {
			ClientData.searchCond_recordTo(data.recordTo);
		}
		HISTORY.totalPage = data.totalRecord;
		// Render Page number
		HISTORY.reRenderPageNumber(HISTORY.totalPage, HISTORY.totalPage);

	}, null);
};

// Handle language
HISTORY.handleLanguage = function() {

	if (I18N.getCurrentLanguage() == COMMON.Consts.ConstLanguage_En || I18N.getCurrentLanguage() == COMMON.Consts.ConstLanguage_Ko) {
		$('#control-sort-titlekana').css('display', 'none');
		$('#control-sort-titlekana-off').css('display', 'none');
		$('#label-sort-titlekana').css('display', 'none');
		$('#separate').css('display', 'none');
		$("#titlekana-sorttype").html('');
	} else {
		if (ClientData.searchCond_sortOrder() != null && ClientData.searchCond_sortOrder() != 'undefined' || ClientData.searchCond_sortType() != '') {
			var typeSort = ClientData.searchCond_sortType();
			var orderSort = ClientData.searchCond_sortOrder();
			HEADER.setStatusSort('#' + $('#menu_sort li.current a').attr('id'), orderSort == COMMON.Consts.ConstOrderSetting_Asc);
		}

		if (HISTORY.noRecordFlg) {
			$('#label-sort-titlekana').css('display', 'block');
			$('#separate').css('display', 'block');
			$('#control-sort-titlekana-off').css('display', 'block');
			$('#content-grid').html("<div id='msgHistoryNotExist'>" + I18N.i18nText('msgHistoryNotExist') + "</div>");
		} else {
			$('#control-sort-titlekana').css('display', 'block');
			$('#separate').css('display', 'block');
		}
	}

};

// Initial Screen
HISTORY.renderGridView = function() {

	var sortType = HISTORY.DEFAULT_SORT_TYPE;
	var sortOrder = HISTORY.DEFAULT_SORT_ORDER;
	var searchText = '';
	var searchDivision = HISTORY.DEFAULT_SEARCH_DIVISION;
	var sid = ClientData.userInfo_sid();

	ClientData.searchCond_recordFrom(0);
	ClientData.searchCond_recordTo(0);
	ClientData.searchCond_sortType(sortType);
	ClientData.searchCond_sortOrder(sortOrder);
	ClientData.searchCond_searchDivision(searchDivision);

	// Handle display sort
	HISTORY.handleSortDisp();

	// Display user name
	$('#login-username').html(ClientData.userInfo_loginId_session());

	// Refresh GridView
	HISTORY.refreshGrid();

	var readingContentIds = ClientData.ReadingContentIds();
	if (readingContentIds.length == 0) {
		HISTORY.displayResultNoRecord();
		HISTORY.noRecordFlg = true;
		HISTORY.reRenderPageNumber(0, 0);
	} else {
		// Render Gridview
		HISTORY.renderContent(sid, searchText, searchDivision, 3, sortOrder);

		var sortType = ClientData.searchCond_sortType();
		if (sortType == '1') {
			HISTORY.sortByTitleFunction();
		} else if (sortType == '2') {
			HISTORY.sortByTitleKanaFunction();
		} else if (sortType == '3') {
			HISTORY.sortByReleaseDateFunction();
		} else {
			HISTORY.sortByViewDateFunction();
		}

	}

	// Language Handle
	HISTORY.handleLanguage();
};

// Canvas Click function
HISTORY.canvasClickFunction = function(e) {
	if (e) {
		e.preventDefault();
	}
	if (HISTORY.home_isMove == true) {
		HISTORY.home_isMove = false;
		return;
	}

	var contentId = $(this).attr('id');
	var outputId = contentId.substring(17);

	// Set content id for screen: content detail
	ClientData.contentInfo_contentId(outputId);

	// Get image of selected image
	var base64String = HISTORY.returnThumbnail(outputId);
	ClientData.contentInfo_contentThumbnail(base64String);

	var contentType = HISTORY.returnContentType(outputId);
	ClientData.contentInfo_contentType(contentType);

	// Store Content id that user has read
	var checkflag = false;
	HISTORY.contentIdArray = ClientData.ReadingContentIds();
	if (HISTORY.contentIdArray.length > 0) {

		for ( var nIndex = 0; nIndex < HISTORY.contentIdArray.length; nIndex++) {
			if (HISTORY.contentIdArray[nIndex].contentid == outputId) {
				checkflag = true;
				break;
			}
		}

		if (!checkflag) {
			HISTORY.contentIdArray.push({
				contentid : outputId,
				viewdate : '',
				originviewdate : ''
			});
		}
	} else {
		HISTORY.contentIdArray.push({
			contentid : outputId,
			viewdate : '',
			originviewdate : ''
		});
	}

	// Renew ReadingContentID
	var newArray = [];
	ClientData.ReadingContentIds(newArray);

	// Set data for readingcontentid
	ClientData.ReadingContentIds(HISTORY.contentIdArray);

	// Set ResouceVersion for content
	COMMON.setResourceVersionData(outputId);

	// Set MetaVersion for content
	COMMON.setMetaVersionData(outputId);

	// Delete 'new' icon
	HISTORY.drawEditImage(outputId);

	// Open content Detail
	DETAIL.openContentDetail();
};

// Re-render page from and total record
HISTORY.reRenderPageNumber = function(dispRecord, dispTotal) {

	if (HISTORY.contentViewData.length) {
		$('#dispPage').html(HISTORY.contentViewData.length);
		$('#totalPage').html(HISTORY.contentViewData.length);
		$('#dispPage').css('visibility', 'visible');
		$('#totalPage').css('visibility', 'visible');
		$('.pageNumControl').css('visibility', 'visible');
	} else {
		$('#dispPage').css('visibility', 'hidden');
		$('#totalPage').css('visibility', 'hidden');
		$('.pageNumControl').css('visibility', 'hidden');
	}

};

// Sort By Title Function
HISTORY.sortByTitleFunction = function() {

	var sortOrder = ClientData.searchCond_sortOrder();
	var sortType = ClientData.searchCond_sortType();

	if (sortOrder == COMMON.Consts.ConstOrderSetting_Asc) {
		if (sortType == '1') {
			sortOrder = COMMON.Consts.ConstOrderSetting_Desc;
			HISTORY.sortByTitleDesc();
		} else {
			sortOrder = COMMON.Consts.ConstOrderSetting_Asc;
			HISTORY.sortByTitleAsc();
		}
		ClientData.searchCond_sortOrder(sortOrder);
	} else {
		sortOrder = COMMON.Consts.ConstOrderSetting_Asc;
		ClientData.searchCond_sortOrder(sortOrder);
		HISTORY.sortByTitleAsc();
	}

	HEADER.setStatusSort('#control-sort-title', sortOrder == COMMON.Consts.ConstOrderSetting_Asc);

	sortType = '1';
	ClientData.searchCond_sortType(sortType);

};

// Sort By Title Kana function
HISTORY.sortByTitleKanaFunction = function() {

	var sortOrder = ClientData.searchCond_sortOrder();
	var sortType = ClientData.searchCond_sortType();

	if (sortOrder == COMMON.Consts.ConstOrderSetting_Asc) {
		if (sortType == '2') {
			sortOrder = COMMON.Consts.ConstOrderSetting_Desc;
			HISTORY.sortByTitleKanaDesc();
		} else {
			sortOrder = COMMON.Consts.ConstOrderSetting_Asc;
			HISTORY.sortByTitleKanaAsc();
		}
		ClientData.searchCond_sortOrder(sortOrder);
	} else {
		sortOrder = COMMON.Consts.ConstOrderSetting_Asc;
		ClientData.searchCond_sortOrder(sortOrder);
		HISTORY.sortByTitleKanaAsc();
	}

	HEADER.setStatusSort('#control-sort-titlekana', sortOrder == COMMON.Consts.ConstOrderSetting_Asc);

	sortType = '2';
	ClientData.searchCond_sortType(sortType);

};

// Sort By Release Date
HISTORY.sortByReleaseDateFunction = function() {

	var sortOrder = ClientData.searchCond_sortOrder();
	var sortType = ClientData.searchCond_sortType();

	if (sortOrder == COMMON.Consts.ConstOrderSetting_Asc) {
		if (sortType == '3') {
			sortOrder = COMMON.Consts.ConstOrderSetting_Desc;
			HISTORY.sortByPublishDateDesc();
		} else {
			sortOrder = COMMON.Consts.ConstOrderSetting_Asc;
			HISTORY.sortByPublishDateAsc();
		}
		ClientData.searchCond_sortOrder(sortOrder);
	} else {
		sortOrder = COMMON.Consts.ConstOrderSetting_Asc;
		ClientData.searchCond_sortOrder(sortOrder);
		HISTORY.sortByPublishDateAsc();
	}

	HEADER.setStatusSort('#control-sort-releasedate', sortOrder == COMMON.Consts.ConstOrderSetting_Asc);

	sortType = '3';
	ClientData.searchCond_sortType(sortType);
};

// Sort By View Date
HISTORY.sortByViewDateFunction = function() {

	var sortOrder = ClientData.searchCond_sortOrder();
	var sortType = ClientData.searchCond_sortType();

	if (sortOrder == COMMON.Consts.ConstOrderSetting_Asc) {
		if (sortType == '4') {
			sortOrder = COMMON.Consts.ConstOrderSetting_Desc;
			HISTORY.sortByViewDateDesc();
		} else {
			sortOrder = COMMON.Consts.ConstOrderSetting_Asc;
			HISTORY.sortByViewDateAsc();
		}
		ClientData.searchCond_sortOrder(sortOrder);
	} else {
		sortOrder = COMMON.Consts.ConstOrderSetting_Asc;
		HISTORY.sortByViewDateAsc();
		ClientData.searchCond_sortOrder(sortOrder);
	}

	HEADER.setStatusSort('#control-sort-viewdate', sortOrder == COMMON.Consts.ConstOrderSetting_Asc);

	sortType = '4';
	ClientData.searchCond_sortType(sortType);

};

// Get Thumnail base on contentid
HISTORY.returnThumbnail = function(contentid) {
	for ( var i = 0; i < HISTORY.thumbnailArr.length; i++) {
		if (HISTORY.thumbnailArr[i].contentId == contentid) {
			return HISTORY.thumbnailArr[i].thumbnail;
		}
	}
};

// Get content type base on contentid
HISTORY.returnContentType = function(contentid) {
	// Array Length
	var iArrCnt = HISTORY.contentTypeArr.length;

	// Get contentType in array by contentId
	for ( var i = 0; i < iArrCnt; i++) {
		if (HISTORY.contentTypeArr[i].contentId == contentid) {
			return HISTORY.contentTypeArr[i].contentType;
		}
	}
};

// Dialog Read Button CLick
HISTORY.readSubmenuFunction = function(e) {
	if (e) {
		e.preventDefault();
	}
	if (HISTORY.home_isMove == true) {
		HISTORY.home_isMove = false;
		return;
	}

	var contentId = $(this).attr('contentid');

	// check limit of content
	LIMIT_ACCESS_CONTENT.checkLimitContent(contentId, function() {
		HISTORY.readSubmenuFunction_callback(contentId);
	}, function() {
	});

};

// read content callback
HISTORY.readSubmenuFunction_callback = function(contentId) {
	var contentThumbnail = HISTORY.returnThumbnail(contentId);
	var date = new Date();
	var month = date.getMonth() + 1;
	var day = date.getDate();

	var outputDate = COMMON.formatNormalDate(day, month, date.getFullYear());

	ClientData.contentInfo_contentId(contentId);
	ClientData.contentInfo_contentThumbnail(contentThumbnail);

	var contentType = HISTORY.returnContentType(contentId);
	ClientData.contentInfo_contentType(contentType);
	var checkflag = false;
	HISTORY.contentIdArray = ClientData.ReadingContentIds();
	// Store Content id that user has read
	if (HISTORY.contentIdArray.length > 0) {

		for ( var nIndex = 0; nIndex < HISTORY.contentIdArray.length; nIndex++) {
			if (HISTORY.contentIdArray[nIndex].contentid == contentId) {
				checkflag = true;
				if (HISTORY.contentIdArray[nIndex].viewdate == null || HISTORY.contentIdArray[nIndex].viewdate == 'undefined' || HISTORY.contentIdArray[nIndex].viewdate == '') {
					HISTORY.contentIdArray[nIndex].viewdate = outputDate;
					HISTORY.contentIdArray[nIndex].originviewdate = date;
				}
				break;
			}
		}

		if (!checkflag) {
			HISTORY.contentIdArray.push({
				contentid : contentId,
				viewdate : outputDate,
				originviewdate : date
			});
		}
	} else {
		HISTORY.contentIdArray.push({
			contentid : contentId,
			viewdate : outputDate,
			originviewdate : date
		});
	}

	// Renew ReadingContentID
	var newArray = [];
	ClientData.ReadingContentIds(newArray);

	// Set ResouceVersion for content
	COMMON.setResourceVersionData(contentId);

	// Set MetaVersion for content
	COMMON.setMetaVersionData(contentId);

	// Set data for readingcontentid
	ClientData.ReadingContentIds(HISTORY.contentIdArray);

	ClientData.IsRefresh(false);

	if (contentType == COMMON.ContentTypeKeys.Type_Others) {
		// Download content
		HEADER.downloadResourceById(contentId);
		// redraw content remove new icon
		HISTORY.drawEditImage(contentId);
	} else if (contentType == COMMON.ContentTypeKeys.Type_Link) {
		// link content
		HEADER.viewLinkContentById(contentId);
		// redraw content remove new icon
		HISTORY.drawEditImage(contentId);
	} else {
		// Go to Conten view page
		HISTORY.drawEditImage(contentId);
		HISTORY.showContentView();
	}
};

// Check if Content Has marking or memo
HISTORY.checkContentMarkingMemoOption = function(contentId) {
	// Check if contentid has marking
	if (ClientData.MarkingData().length == 0) {
		$('#imgBookMark' + contentId).css('visibility', 'hidden');
	} else {
		for ( var nIndex1 = 0; nIndex1 < ClientData.MarkingData().length; nIndex1++) {
			if (ClientData.MarkingData()[nIndex1].contentid == contentId) {
				$('#imgBookMark' + contentId).css('visibility', 'visible');
				break;
			} else {
				$('#imgBookMark' + contentId).css('visibility', 'hidden');
			}
		}
	}

	if (ClientData.MemoData().length == 0) {
		$('#imgMemo' + contentId).css('visibility', 'hidden');
	} else {
		// Check if contentid has memo
		for ( var nIndex1 = 0; nIndex1 < ClientData.MemoData().length; nIndex1++) {
			if (ClientData.MemoData()[nIndex1].contentid == contentId) {
				$('#imgMemo' + contentId).css('visibility', 'visible');
				break;
			} else {
				$('#imgMemo' + contentId).css('visibility', 'hidden');
			}
		}
	}
};

// Check if User has read content
HISTORY.checkUserHasReadContent = function(contId, resourceVer, metaVer, versionArr, metaArr, readArr) {

	var imgThumb = new Image();
	var imgIconNew = new Image();
	var imgIconEdit = new Image();

	var c = document.getElementById('content-thumbnail' + contId);
	var ctx = c.getContext('2d');

	var readFlg = false;
	//var versionArr = ClientData.ResourceVersion();
	//var metaArr = ClientData.MetaVersion();
	//var readArr = ClientData.ReadingContentIds();

	var contentThumbnail = HISTORY.returnThumbnail(contId);
	var contentType = HISTORY.returnContentType(contId);

	if (readArr == null || readArr <= 0 || readArr == 'undefined') {
		imgThumb.onload = function() {
			var resizeImg = HISTORY.resizeResourceThumbnail(imgThumb, c.width, c.height);
			ctx.drawImage(imgThumb, c.width / 2 - resizeImg[0] / 2, 0, resizeImg[0], resizeImg[1]);
			imgIconNew.onload = function() {
				ctx.drawImage(imgIconNew, c.width / 2 - resizeImg[0] / 2, 0);
				HISTORY.showContentThumbnail(contId);
			};
			imgIconNew.src = COMMON.DEFAULT_IMG_CONTENT_NEW;
		};

		if (contentThumbnail == '' || contentThumbnail == null) {
			if (!COMMON.isPdfContent(contentType)) {
				var src = HEADER.getThumbnailForOtherType(contentType);
				if (src != '') {
					imgThumb.src = src;
				}
			}
		} else {
			imgThumb.src = contentThumbnail;
		}

	} else {
		// Check if user has read this content or not
		for ( var nIndex1 = 0; nIndex1 < ClientData.ReadingContentIds().length; nIndex1++) {
			if (ClientData.ReadingContentIds()[nIndex1].contentid == contId) {
				imgThumb.onload = function() {
					var resizeImg = HISTORY.resizeResourceThumbnail(imgThumb, c.width, c.height);
					ctx.drawImage(imgThumb, c.width / 2 - resizeImg[0] / 2, 0, resizeImg[0], resizeImg[1]);
					HISTORY.showContentThumbnail(contId);
				};
				if (contentThumbnail == '' || contentThumbnail == null) {
					if (!COMMON.isPdfContent(contentType)) {
						var src = HEADER.getThumbnailForOtherType(contentType);
						if (src != '') {
							imgThumb.src = src;
						}
					}
				} else {
					imgThumb.src = contentThumbnail;
				}
				readFlg = true;
				break;
			}
		}
	}

	// Check if resource version has change
	if (readFlg) {
		if (versionArr == null || versionArr <= 0 || versionArr == 'undefined') {
		} else {
			for ( var nIndex2 = 0; nIndex2 < versionArr.length; nIndex2++) {
				if (versionArr[nIndex2].contentid == contId) {
					if (versionArr[nIndex2].resourceversion != resourceVer) {
						imgThumb.onload = function() {
							var resizeImg = HISTORY.resizeResourceThumbnail(imgThumb, c.width, c.height);
							ctx.drawImage(imgThumb, c.width / 2 - resizeImg[0] / 2, 0, resizeImg[0], resizeImg[1]);
							HISTORY.showContentThumbnail(contId);
							imgIconEdit.onload = function() {
								ctx.drawImage(imgIconEdit, c.width / 2 - resizeImg[0] / 2, 0);
							};
							imgIconEdit.src = COMMON.DEFAULT_IMG_CONTENT_EDIT;
						};
						// thumbnail.
						if (contentThumbnail == '' || contentThumbnail == null) {
							if (!COMMON.isPdfContent(contentType)) {
								var src = HEADER.getThumbnailForOtherType(contentType);
								if (src != '') {
									imgThumb.src = src;
								}
							}
						} else {
							imgThumb.src = contentThumbnail;
						}
						// thumbnail.
						break;
					}
				}
			}
		}

		if (metaArr == null || metaArr <= 0 || metaArr == 'undefined') {
		} else {
			for ( var nIndex2 = 0; nIndex2 < metaArr.length; nIndex2++) {
				if (metaArr[nIndex2].contentid == contId) {
					if (metaArr[nIndex2].metaversion != metaVer) {
						imgThumb.onload = function() {
							var resizeImg = HISTORY.resizeResourceThumbnail(imgThumb, c.width, c.height);
							ctx.drawImage(imgThumb, c.width / 2 - resizeImg[0] / 2, 0, resizeImg[0], resizeImg[1]);
							HISTORY.showContentThumbnail(contId);
							imgIconEdit.onload = function() {
								ctx.drawImage(imgIconEdit, c.width / 2 - resizeImg[0] / 2, 0);
							};
							imgIconEdit.src = COMMON.DEFAULT_IMG_CONTENT_EDIT;
						};
						// thumbnail.
						if (contentThumbnail == '' || contentThumbnail == null) {
							if (!COMMON.isPdfContent(contentType)) {
								var src = HEADER.getThumbnailForOtherType(contentType);
								if (src != '') {
									imgThumb.src = src;
								}
							}
						} else {
							imgThumb.src = contentThumbnail;
						}
						// thumbnail.
						break;
					}
				}
			}
		}
	} else {
		imgThumb.onload = function() {
			var resizeImg = HISTORY.resizeResourceThumbnail(imgThumb, c.width, c.height);
			ctx.drawImage(imgThumb, c.width / 2 - resizeImg[0] / 2, 0, resizeImg[0], resizeImg[1]);
			HISTORY.showContentThumbnail(contId);
			imgIconNew.onload = function() {
				ctx.drawImage(imgIconNew, c.width / 2 - resizeImg[0] / 2, 0);
			};
			imgIconNew.src = COMMON.DEFAULT_IMG_CONTENT_NEW;
		};
		if (contentThumbnail == '' || contentThumbnail == null) {
			if (!COMMON.isPdfContent(contentType)) {
				var src = HEADER.getThumbnailForOtherType(contentType);
				if (src != '') {
					imgThumb.src = src;
				}
			}
		} else {
			imgThumb.src = contentThumbnail;
		}
	}
};

// draw Edit Image
HISTORY.drawEditImage = function(id) {
	var img = new Image();
	var imgSrc = HISTORY.returnThumbnail(id);

	if (imgSrc != null) {

	} else {
		var contentType = HISTORY.returnContentType(id);

		var src = HEADER.getThumbnailForOtherType(contentType);
		if (src != '') {
			imgSrc = src;
		}

	}

	var c = document.getElementById('content-thumbnail' + id);

	// use getContext to use the canvas for drawing
	var ctx = c.getContext('2d');
	ctx.clearRect(0, 0, c.width, c.height);
	img.onload = function() {
		var resizeImg = HISTORY.resizeResourceThumbnail(img, c.width, c.height);
		ctx.drawImage(img, (c.width / 2) - (resizeImg[0] / 2) + 4, c.height - resizeImg[1] + 4, resizeImg[0], resizeImg[1]);
		$("#loadingIcon" + id).fadeOut('slow', function() {
			$('#content-thumbnail' + id).fadeIn('slow');
		});
	};

	img.src = imgSrc;
};

// Render User view date
HISTORY.renderViewDate = function(id, readingContentIds) {
	for ( var i = 0; i < readingContentIds.length; i++) {
		if (readingContentIds[i].contentid == id) {
			return readingContentIds[i].viewdate;
		}
	}
};

HISTORY.returnOriginalViewDate = function(id, readingContentIds) {
	for ( var i = 0; i < readingContentIds.length; i++) {
		if (readingContentIds[i].contentid == id) {
			return readingContentIds[i].originviewdate;
		}
	}
};

// handle display sort direction
HISTORY.handleSortDisp = function() {

	$('#control-sort-title').removeClass('active_tops');
	$('#control-sort-titlekana').removeClass('active_tops');
	$('#control-sort-releasedate').removeClass('active_tops');
	$('#control-sort-viewdate').removeClass('active_tops');

	var typeSort;
	var orderSort;

	if (ClientData.searchCond_sortType() == null || ClientData.searchCond_sortType() == 'undefined' || ClientData.searchCond_sortType() == '') {
		$('#title-sorttype').html('');
		$('#title-sorttype').html('');
		$('#titlekana-sorttype').html('');
		$('#rDate-sorttype').html('');
		$('#vDate-sorttype').html('');
	} else {
		if (ClientData.searchCond_sortOrder() != null && ClientData.searchCond_sortOrder() != 'undefined' && ClientData.searchCond_sortType() != '') {
			typeSort = ClientData.searchCond_sortType();
			orderSort = ClientData.searchCond_sortOrder();

			if (typeSort == 1) {
				HEADER.setStatusSort('#control-sort-title', orderSort == COMMON.Consts.ConstOrderSetting_Asc);
			} else if (typeSort == 2) {
				HEADER.setStatusSort('#control-sort-titlekana', orderSort == COMMON.Consts.ConstOrderSetting_Asc);
			} else if (typeSort == 3) {
				HEADER.setStatusSort('#control-sort-releasedate', orderSort == COMMON.Consts.ConstOrderSetting_Asc);
			} else {
				HEADER.setStatusSort('#control-sort-viewdate', orderSort == COMMON.Consts.ConstOrderSetting_Asc);
			}
		}
	}
};

// function Open SubMenu Dialog
HISTORY.titleClickFunction = function(e) {
	if (e) {
		e.preventDefault();
	}
	if (HISTORY.home_isMove == true) {
		HISTORY.home_isMove = false;
		return;
	}
	var contentid = $(this).attr('contentid');
	// Get image of selected image
	var base64String = HISTORY.returnThumbnail(contentid);
	ClientData.contentInfo_contentThumbnail(base64String);
	ClientData.contentInfo_contentId(contentid);

	var contentType = HISTORY.returnContentType(contentid);
	ClientData.contentInfo_contentType(contentType);

	// Store Content id that user has read
	var checkflag = false;
	HISTORY.contentIdArray = ClientData.ReadingContentIds();
	if (HISTORY.contentIdArray.length > 0) {

		for ( var nIndex = 0; nIndex < HISTORY.contentIdArray.length; nIndex++) {
			if (HISTORY.contentIdArray[nIndex].contentid == contentid) {
				checkflag = true;
				break;
			}
		}

		if (!checkflag) {
			HISTORY.contentIdArray.push({
				contentid : contentid,
				viewdate : '',
				originviewdate : ''
			});
		}
	} else {
		HISTORY.contentIdArray.push({
			contentid : contentid,
			viewdate : '',
			originviewdate : ''
		});
	}

	// Renew ReadingContentID
	var newArray = [];
	ClientData.ReadingContentIds(newArray);

	// Set data for readingcontentid
	ClientData.ReadingContentIds(HISTORY.contentIdArray);

	// Set ResouceVersion for content
	COMMON.setResourceVersionData(contentid);

	// Set MetaVersion for content
	COMMON.setMetaVersionData(contentid);

	// Delete 'new' icon
	HISTORY.drawEditImage(contentid);

	// Open content Detail
	DETAIL.openContentDetail();
};

// refresh GridView
HISTORY.refreshGrid = function() {
	$('#content-grid').empty();
	$('.pageNumControl').css('visibility', 'hidden');
};

HISTORY.sortByViewDateAsc = function() {
	var sortArr = HISTORY.contentViewData;
	var t;
	for ( var i = 0; i < sortArr.length; i++) {
		for ( var j = 1; j < sortArr.length - i; j++) {
			if (sortArr[j - 1].originviewdate > sortArr[j].originviewdate) {
				t = sortArr[j - 1];
				sortArr[j - 1] = sortArr[j];
				sortArr[j] = t;
			}
		}
	}

	var resultArr = sortArr;
	HISTORY.renderContentAfterSort(resultArr);
};

HISTORY.formatDate = function(originDate) {
	var sourceDate = new Date(originDate);
	var year = sourceDate.getFullYear() + 1;
	var month = sourceDate.getMonth();
	var day = sourceDate.getDate();
	var hour = sourceDate.getHours();
	var minute = sourceDate.getMinutes();
	var second = sourceDate.getSeconds();
	var milisecond = sourceDate.getMilliseconds();

	var newDate = new Date(year, month, day, hour, minute, second, milisecond);
	return newDate;
};

HISTORY.sortByViewDateDesc = function() {
	var sortArr = HISTORY.contentViewData;

	var temp;
	for ( var i = 0; i < sortArr.length; i++) {
		for ( var j = sortArr.length - 1; j > i; j--) {
			if (sortArr[j].originviewdate > sortArr[j - 1].originviewdate) {
				temp = sortArr[j];
				sortArr[j] = sortArr[j - 1];
				sortArr[j - 1] = temp;
			}
		}
	}

	var resultArr = sortArr;
	HISTORY.renderContentAfterSort(resultArr);
};

HISTORY.addReadContentToArray = function(strContentId, strResourceVersion, strMetaVersion, strThumbnail, strTitle, strTitleKana, strDelivDate, contentType, readerShare, originalViewDate, printFlg) {
	if (HISTORY.contentViewData.length > 0) {
		var flag = false;
		for ( var j = 0; j < HISTORY.contentViewData.length; j++) {
			if (HISTORY.contentViewData[j].contentid == strContentId) {
				flag = true;
				break;
			}
		}

		if (!flag) {
			HISTORY.contentViewData.push({
				contentid : strContentId,
				originviewdate : originalViewDate,
				contenttitle : strTitle,
				contenttitlekana : strTitleKana,
				deliverydate : strDelivDate,
				resourceversion : strResourceVersion,
				metaversion : strMetaVersion,
				thumbnail : COMMON.formatStringBase64(strThumbnail),
				contenttype : contentType,
				readerShare : readerShare,
				printFlg : printFlg
			});
		}

	} else {
		HISTORY.contentViewData.push({
			contentid : strContentId,
			originviewdate : originalViewDate,
			contenttitle : strTitle,
			contenttitlekana : strTitleKana,
			deliverydate : strDelivDate,
			resourceversion : strResourceVersion,
			metaversion : strMetaVersion,
			thumbnail : COMMON.formatStringBase64(strThumbnail),
			contenttype : contentType,
			readerShare : readerShare,
			printFlg : printFlg
		});
	}
};

HISTORY.showContentThumbnail = function(conid) {
	$('img#imgloading' + conid).fadeOut('slow', function() {
		$('canvas#content-thumbnail' + conid).fadeIn('slow');
	});
};

HISTORY.syncReadingContent = function() {
	var readArr = ClientData.ReadingContentIds();
	var metaArr = ClientData.MetaVersion();
	var resourceArr = ClientData.ResourceVersion();

	for ( var i = readArr.length - 1; i >= 0; i--) {
		var readContent = readArr[i];

		if (!HISTORY.IsExistContent(readContent.contentid)) {
			readArr.splice(i, 1);
			metaArr.splice(i, 1);
			resourceArr.splice(i, 1);
		}
		// Do not process next
		if (AVWEB.avwHasError()) {
			return;
		}
	}

	ClientData.ReadingContentIds(readArr);
	ClientData.MetaVersion(metaArr);
	ClientData.ResourceVersion(resourceArr);
};

///*
// * Get content title kana if it existed
// */
//HISTORY.getContentNameKana = function(strContentId) {
//	var strContentNameKana = null;
//	for ( var nIndex = 0; nIndex < HISTORY.history_contentTitleKana.length; nIndex++) {
//		if (HISTORY.history_contentTitleKana[nIndex].contentId == strContentId) {
//			strContentNameKana = HISTORY.history_contentTitleKana[nIndex].contentNameKana;
//			break;
//		}
//	}
//	return strContentNameKana;
//};

/*
 * Check content whether existed or not
 */
HISTORY.IsExistContent = function(strContentId) {

	if (strContentId == null || strContentId == "") {
		return false;
	}

	var isExisted = true;
	var params = {
		sid : ClientData.userInfo_sid(),
		getType : '1',
		contentId : strContentId
	};
	AVWEB.avwCmsApiSync(ClientData.userInfo_accountPath(), "webGetContent", 'GET', params, function(data) {
		isExisted = true;
		HISTORY.history_contentTitleKana.push({
			contentId : strContentId,
			contentNameKana : data.contentData.contentNameKana
		});
	}, function(xmlHttpRequest, txtStatus, errorThrown) {
		if (xmlHttpRequest.status == 404) {
			isExisted = false;
		} else {
			// Show system error
			isExisted = true; // Mark this flag to prevent bookmarks from deleting
			AVWEB.showSystemError();
		}
	});
	return isExisted;
};

HISTORY.displayResultNoRecord = function() {
	I18N.i18nReplaceText();
	$('#content-grid').html("<div id='msgHistoryNotExist'>" + I18N.i18nText('msgHistoryNotExist') + "</div>");
	$('#control-nextrecord').css('visibility', 'hidden');
	$('.control_sort_on').hide();
	$('.control_sort_off').show();
	$('#off-default').addClass('descending_sort');
};

HISTORY.sortByTitleAsc = function() {

	var sortArr = HISTORY.contentViewData;
	var t;
	for ( var i = 0; i < sortArr.length; i++) {
		for ( var j = 1; j < sortArr.length - i; j++) {
			if (sortArr[j - 1].contenttitle.toUpperCase() > sortArr[j].contenttitle.toUpperCase()) {
				t = sortArr[j - 1];
				sortArr[j - 1] = sortArr[j];
				sortArr[j] = t;
			}
		}
	}

	var resultArr = sortArr;
	HISTORY.renderContentAfterSort(resultArr);
};

HISTORY.sortByTitleDesc = function() {
	var sortArr = HISTORY.contentViewData;
	var temp;
	for ( var i = 0; i < sortArr.length; i++) {
		for ( var j = sortArr.length - 1; j > i; j--) {
			if (sortArr[j].contenttitle.toUpperCase() > sortArr[j - 1].contenttitle.toUpperCase()) {
				temp = sortArr[j];
				sortArr[j] = sortArr[j - 1];
				sortArr[j - 1] = temp;
			}
		}
	}

	var resultArr = HISTORY.contentViewData;
	HISTORY.renderContentAfterSort(resultArr);
};

HISTORY.sortByPublishDateAsc = function() {
	var sortArr = HISTORY.contentViewData;

	var t;
	for ( var i = 0; i < sortArr.length; i++) {
		for ( var j = 1; j < sortArr.length - i; j++) {
			if (HISTORY.formatOriginalPublishDate(sortArr[j - 1].deliverydate) > HISTORY.formatOriginalPublishDate(sortArr[j].deliverydate)) {
				t = sortArr[j - 1];
				sortArr[j - 1] = sortArr[j];
				sortArr[j] = t;
			}
		}
	}

	var resultArr = sortArr;
	HISTORY.renderContentAfterSort(resultArr);
};

HISTORY.sortByPublishDateDesc = function() {
	var sortArr = HISTORY.contentViewData;
	var temp;
	for ( var i = 0; i < sortArr.length; i++) {
		for ( var j = sortArr.length - 1; j > i; j--) {
			if (HISTORY.formatOriginalPublishDate(sortArr[j].deliverydate) > HISTORY.formatOriginalPublishDate(sortArr[j - 1].deliverydate)) {
				temp = sortArr[j];
				sortArr[j] = sortArr[j - 1];
				sortArr[j - 1] = temp;
			}
		}
	}

	var resultArr = sortArr;
	HISTORY.renderContentAfterSort(resultArr);
};

HISTORY.sortByTitleKanaAsc = function() {
	var sortArr = HISTORY.contentViewData;
	var t;
	for ( var i = 0; i < sortArr.length; i++) {
		for ( var j = 1; j < sortArr.length - i; j++) {
			if (sortArr[j - 1].contenttitlekana > sortArr[j].contenttitlekana) {
				t = sortArr[j - 1];
				sortArr[j - 1] = sortArr[j];
				sortArr[j] = t;
			}
		}
	}

	var resultArr = sortArr;
	HISTORY.renderContentAfterSort(resultArr);
};

HISTORY.sortByTitleKanaDesc = function() {
	var sortArr = HISTORY.contentViewData;
	var temp;
	for ( var i = 0; i < sortArr.length; i++) {
		for ( var j = sortArr.length - 1; j > i; j--) {
			if (sortArr[j].contenttitlekana > sortArr[j - 1].contenttitlekana) {
				temp = sortArr[j];
				sortArr[j] = sortArr[j - 1];
				sortArr[j - 1] = temp;
			}
		}
	}

	var resultArr = sortArr;
	HISTORY.renderContentAfterSort(resultArr);
};

HISTORY.renderContentAfterSort = function(contentSortArr) {
	HISTORY.refreshGrid();

	var versionArr = ClientData.ResourceVersion();
	var metaArr = ClientData.MetaVersion();
	var readArr = ClientData.ReadingContentIds();

	// var htmlTemp = "";
	for ( var i = 0; i < contentSortArr.length; i++) {
		post = contentSortArr[i];

		var outputDeliveryDate = COMMON.formatDeliveryDate(post.deliverydate);

		var htmlTemp = '<section class="sectionhistory">'
			+ '	<div class="cnt_section_list">'
			+ '		<a class="img">'
			+ '			<canvas height="110" width="150" id="content-thumbnail' + post.contentid + '" contentid="' + post.contentid + '" style="display:none;"></canvas>'
			+ '	        <img id="imgloading' + post.contentid + '" src="./img/data_loading.gif" height="25px" width="25px" style="padding: 46px; "/>'
			+ '		</a>'
			+ '		<div class="text">'
			+ '			<a id="title' + post.contentid + '" class="name dialog" contentid="' + post.contentid + '">'
			+ '             <img class="listIcon" src="' + HEADER.getIconTypeContent(post.contenttype) + '" width="20" height="20" align="left">'
			+ '             <div class="history_title">' + COMMON.htmlEncode(post.contenttitle) + '</div>'
			+ '         </a>'
			+ '			<div class="info">'
			+ '				<ul class="date">'
			+ '					<li><span class="lang" lang="txtPubDt">' + I18N.i18nText("txtPubDt") + '</span>：' + outputDeliveryDate + '</li>'
			+ '					<li><span class="lang" lang="txtViewDt">' + I18N.i18nText("txtViewDt") + '</span>：<span id="lblVdate' + post.contentid + '"> </span></li>'
			+ '				</ul>'
			+ '				<ul class="pic">'
			+ '					<li><img src="' + COMMON.DEFAULT_IMG_OPTION_MEMO + '" id="imgMemo' + post.contentid + '" class="sticker"  /></li>'
			+ '					<li><img src="' + COMMON.DEFAULT_IMG_OPTION_MARKING + '" id="imgBookMark' + post.contentid + '" class="pen"  /></li>'
			+ '					<li><ul class="iconList">{download}{share}<li><a class="read lang button-details" contentid="' + post.contentid + '" lang="txtRead">' + I18N.i18nText("txtRead") + '</a></li></ul></li>'
			+ '				</ul>'
			+ '			</div>'
			+ '		</div>'
			+ '	</div>'
			+ '</section>';

		var shareHtml = "";
		if (post.readerShare == '1') {
			shareHtml = '<li><a class="read lang button-share" contentid="' + post.contentid + '" lang="txtShare">' + I18N.i18nText("txtShare") + '</a></li>';
		}
		htmlTemp = htmlTemp.replace(/\{share\}/g, shareHtml);
		var downloadHtml = "";
		if(post.contenttype == COMMON.ContentTypeKeys.Type_PDF && ClientData.serviceOpt_air_print() == 'Y' && post.printFlg == '1'){
			downloadHtml ='<li><a class="read lang button-download" contentid="' + post.contentid + '" lang="txtDownload">'+I18N.i18nText("txtDownload")+'</a></li>';
		}
		htmlTemp = htmlTemp.replace(/\{download\}/g, downloadHtml);
		$('#content-grid').append(htmlTemp);

		var viewdate = HISTORY.renderViewDate(post.contentid, readArr);
		$('#lblVdate' + post.contentid).html(viewdate);

		// Check if user has read this content or not.
		HISTORY.checkUserHasReadContent(post.contentid, post.resourceversion, post.metaversion, versionArr, metaArr, readArr);
		// Check if content has marking or memo
		HISTORY.checkContentMarkingMemoOption(post.contentid);

	}

	HISTORY.reRenderPageNumber(HISTORY.totalPage, HISTORY.totalPage);

};

HISTORY.formatOriginalPublishDate = function(date) {

	var day = date.date;
	var month = date.month + 1;
	var year = date.year + 1900;
	var hour = date.hours;
	var minute = date.minutes;
	var second = date.seconds;

	var resultDate = new Date(year, month, day, hour, minute, second);
	return resultDate;
};

//HISTORY.returnContentTitleKana = function(id) {
//	var titleKana;
//
//	// Get title kana from existed contents
//	titleKana = HISTORY.getContentNameKana(id);
//
//	if (titleKana != null) {
//		// Skip this case
//	} else {
//		var params = {
//			contentId : id,
//			sid : ClientData.userInfo_sid(),
//			getType : 1
//		};
//
//		// Get all pages of content
//		AVWEB.avwCmsApiSync(ClientData.userInfo_accountPath(), "webGetContent", 'GET', params, function(data) {
//			// Success
//			titleKana = data.contentData.contentNameKana;
//		}, null);
//
//	}
//	return titleKana;
//};

HISTORY.resizeResourceThumbnail = function(mg, width, height) {
	var newWidth;
	var newHeight;
	var delta = Math.min(width / mg.width, height / mg.height);

	newHeight = parseInt(delta * mg.height);
	newWidth = parseInt(delta * mg.width);
	var result = [ newWidth, newHeight ];

	return result;

};

HISTORY.removeHoverCss = function() {
	if (COMMON.isTouchDevice()) {
		$('#control-sort-title').removeClass('nottouchdevice');
		$('#control-sort-titlekana').removeClass('nottouchdevice');
		$('#control-sort-releasedate').removeClass('nottouchdevice');
		$('#control-sort-viewdate').removeClass('nottouchdevice');
	}
};

// Dialog Read Button CLick
HISTORY.showContentShareDlgFunction = function(e) {

	if (e) {
		e.preventDefault();
	}

	if (HISTORY.home_isMove == true) {
		HISTORY.home_isMove = false;
		return;
	}

	var contentId = $(this).attr('contentid');
	// check limit of content
//	LIMIT_ACCESS_CONTENT.checkLimitContent(contentId, function() {
		SHARE.contentId = contentId;
		SHARE.contentTitle = "";
		SHARE.openContentShare();

//	}, function() {
//		// console.log("checkLimitContent_bbb");
//	});

};

HISTORY.showContentView = function() {

	var y = 0;
	if (document.documentElement.scrollTop) {
		y = document.documentElement.scrollTop;
	}
	if (document.body.scrollTop) {
		if (y == undefined || y == 0) {
			y = document.body.scrollTop;
		}
	}
	if (y == undefined || y == null) {
		y = 0;
	}
	// console.log("scrollTop:" + y);
	HISTORY.scrollTop = y;
	window.scrollTo(0, 0);

	$("#header-ws").hide();
	$("#ws-body").hide();
	$("#topcontrol").hide();

	CONTENTVIEW_INITOBJECT.clearViewerComponent();
	CONTENTVIEW.cssInit();
	$("#viewer").show();

	CONTENTVIEW.ready();

};

HISTORY.cssInit = function() {
	$('html').css({
		'overflow-y' : 'scroll'
	});
	$('html,body').css({
		'height' : '100%',
		'margin' : '0',
		'padding' : '0'
	});
	$('html>body').css({
		'font-size' : '16px',
		'font-size' : '68.75%'
	});
	$('body').css({
		'font-family' : 'Verdana, helvetica, arial, sans-serif',
		'font-size' : '68.75%',
		'background' : '#fff',
		'color' : '#333'
	});
};

HISTORY.downloadFunction = function(e) {
	if (e) {
		e.preventDefault();
	}

	var contentId = $(this).attr('contentid');
	// Download content
	HEADER.downloadResourceById(contentId);

};
