var cHAAddItem = 1;
var cHARemoveItem = 2;
var cHAUpdateProgress = 3;

function initHugeAttachments(mailid){
	try{
		var iframeObj = document.getElementById('iframe_'+mailid);
		var qqAttachList = [];
		var foxAttachList = [];
		if (iframeObj){
			//check 
			var bInitAttachments = false;
			var InitAttachmentsState = $(iframeObj).attr('initState');
			if (InitAttachmentsState){
				if (InitAttachmentsState === 'Finish'){
					bInitAttachments = true;
				}
			}
			if (bInitAttachments) return;
			$(iframeObj).attr('initState', 'Finish');
			//
			var iframeContent = iframeObj.contentWindow.document;
			if (iframeContent){
				var foxBigAttach = iframeContent.getElementById('FoxmailClientBigAttach');
				var qqBigAttach = iframeContent.getElementById('QQMailBigAttach');
				if (foxBigAttach){
					var div = $('.FoxmailClientBigAttachEntry', foxBigAttach);
					for (var i=0; i<div.length; i++){
						foxAttachList[foxAttachList.length] = div[i].innerHTML;
					}
					if (div.length > 0){
						//hide attachment area
						foxBigAttach.style.display = 'none';
					}
				}
				if (qqBigAttach){
					var div = $('.bigatt_bt', qqBigAttach);
					for (var i=0; i<div.length; i++){
						qqAttachList[qqAttachList.length] = div[i].innerHTML;
					}
					//hide attachment area
					qqBigAttach.style.display = 'none';
				}
			}
		}
		invokeAppFunction('InitHugeAttachments', {mailid: mailid, qqBigAttach: qqAttachList, foxBigAttach: foxAttachList});
	}catch(err){
		WriteLog('initHugeAttachments', err.message);
	}
}
function releaseHugeAttachments(){
	try{
	invokeAppFunction('ReleaseHugeAttachments', {});
	}catch(e){
		WriteLog('releaseHugeAttachments', e.message);
	}
}
function updateNormalAttachmentTitle(mailid){
	var normalAttachmentTitle = $('#mail_normal_attachment_title_'+mailid);
	if (normalAttachmentTitle.hasClass('hide_ifzero_0')){
		normalAttachmentTitle.removeClass('hide_ifzero_0');
	}
}
function updateAttachmentBoxTitle(mailid){
	var ulElement = $('#huge_attachment_'+mailid);
	var hugeAttachmentCount = parseInt(ulElement.attr('attachNum'));
	var totalCount = parseInt($('#mail_attachment_total_count_'+mailid).attr('normalAttachment')) + hugeAttachmentCount;
	$('#mail_attachment_boxtitle_button_'+mailid).hide();
	$('#mail_attachment_total_count_'+mailid).text(totalCount);
}
function updateAttachmentContainer(mailid){
	var container = $('#mail_attachment_container_host_'+mailid);
	if (container.hasClass('hide_ifzero_0')){
		container.removeClass('hide_ifzero_0');
	}
}
function updateHugeAttachmentEntry(mailid, fileName, fileSize, fileIcon, downloadUrl){
	try{
		var hugeAttachmentArea = $('#mail_huge_attachment_area_'+mailid);
		if (hugeAttachmentArea.hasClass('hide_ifzero_0')){
			hugeAttachmentArea.removeClass('hide_ifzero_0');
		}
		var ulElement = $('#huge_attachment_'+mailid);
		var hugeAttachmentCount = parseInt(ulElement.attr('attachNum'));
		hugeAttachmentCount = hugeAttachmentCount + 1;
		ulElement.attr('attachNum', hugeAttachmentCount);
		var liEndItem = $('#huge_attachment_enditem_'+mailid);
		var liItemID = 'huge_attachment_'+mailid+'_'+hugeAttachmentCount;
		var liItemString = '<li class="mail_huge_attachment_entry" id="'+liItemID+'" title="'+fileName+'" downloadUrl="'+downloadUrl+'">';
		liItemString += '<div class="mail_huge_attachment_entry_container">';
		liItemString += '<div class="mail_huge_attachment_image_container">';
		liItemString += '<div class="mail_huge_attachment_image_wrap">';
		liItemString += '<img class="mail_huge_attachment_image" src="'+fileIcon+'"/>';
		liItemString += '</div></div>';
		liItemString += '<div class="mail_huge_attachment_info">';
		liItemString += '<div class="mail_huge_attachment_filename">'+fileName+'</div>';
		liItemString += '<div class="mail_huge_attachment_size">'+fileSize+'</div></div></div></li>';
		var liItem = $(liItemString);
		liItem.insertBefore(liEndItem);
	}catch(e){
		WriteLog('updateHugeAttachmentEntry', e.message);
	}
}
function getProgressNumber(curPos, totalPos){
	var percent = Math.round(curPos/totalPos * 100);
	return percent;
}
function getProgressString(curPos, totalPos){
	var percent = getProgressNumber(curPos, totalPos);
	var str = csUploadedDesc + ' ' + percent + '%';
	return str;
}
function updateHugeAttachmentEntryInDraftBox(mailid, fileId, fileName, fileIcon, state, curPos, totalPos){
	try{
		var hugeAttachmentArea = $('#mail_huge_attachment_area_'+mailid);
		if (hugeAttachmentArea.hasClass('hide_ifzero_0')){
			hugeAttachmentArea.removeClass('hide_ifzero_0');
		}
		var ulElement = $('#huge_attachment_'+mailid);
		var hugeAttachmentCount = parseInt(ulElement.attr('attachNum'));
		var liEndItem = $('#huge_attachment_enditem_'+mailid);
		var liItemID = 'huge_attachment_'+mailid+'_'+fileId;
		var liItemDOM = document.getElementById(liItemID);
		switch (state){
			case cHAAddItem:
			{
				hugeAttachmentCount = hugeAttachmentCount + 1;
				ulElement.attr('attachNum', hugeAttachmentCount);
				var uploadClass = '';
				if (getProgressNumber(curPos, totalPos) < 100){
					uploadClass = 'mail_huge_attachment_entry_uploading';
				}
				var liItemString = '<li class="mail_huge_attachment_entry" id="'+liItemID+'" title="'+fileName+'">';
				liItemString += '<div class="mail_huge_attachment_entry_container '+uploadClass+'">';
				liItemString += '<div class="mail_huge_attachment_image_container '+uploadClass+'">';
				liItemString += '<div class="mail_huge_attachment_image_wrap">';
				liItemString += '<img class="mail_huge_attachment_image" src="'+fileIcon+'"/>';
				liItemString += '</div></div>';
				liItemString += '<div class="mail_huge_attachment_info">';
				liItemString += '<div class="mail_huge_attachment_filename">'+fileName+'</div>';
				liItemString += '<div class="mail_huge_attachment_size">'+getProgressString(curPos, totalPos)+'</div></div></div></li>';
				var liItem = $(liItemString);
				liItem.insertBefore(liEndItem);
				break;
			}
			case cHARemoveItem:
			{
				hugeAttachmentCount = hugeAttachmentCount - 1;
				ulElement.attr('attachNum', hugeAttachmentCount);
				if (liItemDOM){
					var liItem = $(liItemDOM);
					liItem.remove();
				}
				break;
			}
			case cHAUpdateProgress:
			{
				if (liItemDOM){
					var liItem = $(liItemDOM);
					if (getProgressNumber(curPos, totalPos) >= 100){
						var container = $('.mail_huge_attachment_entry_container', liItem);
						if (container.hasClass('mail_huge_attachment_entry_uploading')){
							container.removeClass('mail_huge_attachment_entry_uploading');
						}
						var divImage = $('.mail_huge_attachment_image_container', liItem);
						if (divImage.hasClass('mail_huge_attachment_entry_uploading')){
							divImage.removeClass('mail_huge_attachment_entry_uploading');
						}
					}
					var progressDIV = $('.mail_huge_attachment_size', liItem);
					var progessString = getProgressString(curPos, totalPos);
					progressDIV.text(progessString);
				}
				break;
			}
			default:
				break;
		}
	}catch(e){
		WriteLog('updateHugeAttachmentEntryInDraftBox', e.message);
	}
}

//Update inbox huge attachments
function updateHugeAttachments(params){
	try{
		var mailid = params.getValue('mailid');
		var fileName = params.getValue('fileName');
		var fileSize = params.getValue('fileSize');
		var fileIcon = params.getValue('fileIcon');
		var downloadUrl = params.getValue('downloadUrl');
		downloadUrl = encodeURI(downloadUrl);
		var state = params.getValue('state');
		//Update normal attachment title 
		updateNormalAttachmentTitle(mailid);
		//Update huge attachment entry
		updateHugeAttachmentEntry(mailid, fileName, fileSize, fileIcon, downloadUrl);
		//Update attachment boxtitle
		updateAttachmentBoxTitle(mailid);
		//Update attachment container
		updateAttachmentContainer(mailid);
	}catch(e){
		WriteLog('updateHugeAttachments', e.message);
	}
}
//Update draftbox huge attachments
function updateHugeAttachmentsInDraftBox(params){
	try{
		var mailid = params.getValue('mailid');
		var fileId = params.getValue('fileId');
		var fileName = params.getValue('fileName');
		var fileIcon = params.getValue('fileIcon');
		var state = parseInt(params.getValue('state'));
		var curPos = parseInt(params.getValue('curPos'));
		var totalPos = parseInt(params.getValue('totalPos'));
		//Update normal attachment title 
		updateNormalAttachmentTitle(mailid);
		//Update huge attachment entry
		updateHugeAttachmentEntryInDraftBox(mailid, fileId, fileName, fileIcon, state, curPos, totalPos);
		//Update attachment boxtitle
		updateAttachmentBoxTitle(mailid);
		//Update attachment container
		updateAttachmentContainer(mailid);
	}catch(e){
		WriteLog('updateHugeAttachmentsInDraftBox', e.message);
	}
}