﻿//Copyright WebMail Wou3, Inc. 2007-2010
Type.registerNamespace('WebMail2');
WebMail2.Folder=function
(
ListItemID, FolderPath, FolderName, FolderImageID, FolderLink, FolderLabelID,  
DefaultFolderImage, LoadingFolderImage, ShowFoldersImageID,
IsComplete, TimeStamp, TotalBytes, TotalMessages, TotalUnread
)
{
	this._IsComplete=IsComplete;
	this._TimeStamp=TimeStamp;
	this._LastTimeStamp=TimeStamp;
	this._TotalBytes=TotalBytes;
	this._TotalMessages=TotalMessages;
	this._TotalUnread=TotalUnread;
	this._DisplayFolderName=null;

	this._FolderPath=FolderPath;
	this._ParentFolderPath='';

	switch(FolderPath)
	{
		case FOLDER_DRAFTS:
		case FOLDER_DELETED:
		case FOLDER_SPAM:
		case FOLDER_SENT:
		case FOLDER_INBOX:
			break;
		default:
			var paths = FolderPath.substring(1).split('\\');
			for(i=0;i<(paths.length-1);i++)
			{
				if(paths[i].replace(' ','').length>0)
					this._ParentFolderPath=this._ParentFolderPath + '\\' + paths[i];
			}
			break;
	}

	this._FolderName=FolderName;
	this._ListItem=document.getElementById(ListItemID);
	
	this._ShowFoldersImage=document.getElementById(ShowFoldersImageID)
	
	this._FolderImage=document.getElementById(FolderImageID);
	this._FolderLink=FolderLink;
	this._FolderLabel=document.getElementById(FolderLabelID);

	this._DefaultFolderImage=DefaultFolderImage;
	this._LoadingFolderImage=LoadingFolderImage;
	this._Cancel=false;
}

WebMail2.Folder.prototype=
{
refresh:function(currentTimeStamp, hideWhileUpdating)
{
	if(page.pageName().toLowerCase()=='default.aspx')
	{
		if(this._FolderPath.toLowerCase()==page.get_FolderPath().toLowerCase())
		{
			//if messageListVisible() returns true, the message list is already hidden. 
			//keep it that way until the message list is complete 
			if(false==hideWhileUpdating)
				hideWhileUpdating=listObject.messageListVisible();
			if((this._LastTimeStamp!=currentTimeStamp) || (this._LastTimeStamp==0))
			{
				this._LastTimeStamp=currentTimeStamp;
				page.Nav.UpdateNavigateLabel();
				listObject.refresh(hideWhileUpdating);
			}
			else
				listObject.unHideMessageList();
		}
	}
},
/*
=====================================================================
folderIndexHeader.FolderPath;
folderIndexHeader.FolderName;
folderIndexHeader.IsComplete;
folderIndexHeader.TimeStampTicks;
folderIndexHeader.TotalBytes;
folderIndexHeader.TotalMessages;
folderIndexHeader.TotalUnread;
folderIndexHeader.EllipsisFolderName;
folderIndexHeader.Recursive;
folderIndexHeader.MaxWorkingTimeSeconds;
=====================================================================
*/
set_FolderIndexHeader:function(folderIndexHeader)
{
	if(folderIndexHeader.FolderPath.toLowerCase()!=this._FolderPath.toLowerCase())
		return;   
	if(folderIndexHeader._TimeStamp!=this._LastTimeStamp)
	{
		this._TimeStamp=folderIndexHeader.TimeStampTicks;
	}
	this._IsComplete=folderIndexHeader.IsComplete;
	this._TotalBytes=folderIndexHeader.TotalBytes;
	this._TotalMessages=folderIndexHeader.TotalMessages;
	this._TotalUnread=folderIndexHeader.TotalUnread;
	this._DisplayFolderName=folderIndexHeader.EllipsisFolderName;
	this._FolderLabel.innerHTML=folderIndexHeader.EllipsisFolderName;
	timeSpanSeconds = Number(folderIndexHeader.MaxWorkingTimeSeconds);
	
	if ((false==this._Cancel) && (false==this.get_IsComplete()) && (true==folderIndexHeader.Recursive))
	{
		if(timeSpanSeconds<30)
			timeSpanSeconds += 10;
		else
			timeSpanSeconds += 60;
		//function(recursive,forceIndexRebuild,forceMessageListUpdate,timeSpanSeconds)
		this.UpdateFolder(true,false,false,timeSpanSeconds)
	}
	else
	{
		this._Cancel=false;
		this.set_DefaultFolderImage();
	}

},
SelectFolder:function(recursive, forceIndexRebuild, timeSpanSeconds)
{
	if (page.pageName().toLowerCase()!='default.aspx')
	{
		g_objPageNo.value=1;
		g_objStartAt.value=0;
		clientPageMethods.redirect('default.aspx',this.get_FolderPath());
		return;
	}
	
	this._Cancel=false;
	//if the folder changed or the message list is not visible attempt to update the folder again
	//if the message list is not visible, it may mean the update panel is hung.
	if((page.get_FolderPath().toLowerCase()!=this.get_FolderPath().toLowerCase()) || !listObject.messageListVisible())
	{
		foldersArray.cancelPending();
		listObject.hideMessageList();
		this.set_LoadingFolderImage();
		g_objPageNo.value=1;
		g_objStartAt.value=0;
		
		
		PageMethods.GetFolderHeader
		(
			this.get_FolderPath(), forceIndexRebuild, false, recursive, 
			Number(timeSpanSeconds),                                            //max time to update
			this.get_FolderPath(),
			g_objPageNo.value,
			clientPageMethods.onReturnBuildReturnFolderObjects,                 //success callback
			onError                                                             //error callback
		);

	}
	else
		this.UpdateFolder(recursive,forceIndexRebuild,false,timeSpanSeconds);

	//if selecting the Inbox, clear the "you have new messages" display
	//if it is displayed.  Also will reset the total new message count
	if(this.get_FolderPath().toLowerCase()==FOLDER_INBOX.toLowerCase())
	{
		page.set_newMessagesCount(-1);
	}
	page.setSelectedTabIndex(1);
},
UpdateFolder:function(recursive,forceIndexRebuild,forceMessageListUpdate,timeSpanSeconds)
{
	this.set_LoadingFolderImage();
	PageMethods.GetFolderHeader
	(
		this.get_FolderPath(), forceIndexRebuild, forceMessageListUpdate, recursive, 
		Number(timeSpanSeconds),                                        //max time to update
		page.get_FolderPath(),
		g_objPageNo.value,
		clientPageMethods.onReturnBuildReturnFolderObjects,                 //success callback
		onError                                                             //error callback
	);
},
click_ChildFolders:function()
{
	if(this._ShowFoldersImage!=null)
	{
		if(this._ShowFoldersImage.src.indexOf(foldersArray.ShowSubFolderImage)<0)
		{
			this._ShowFoldersImage.src=foldersArray.ShowSubFolderImage;
			foldersArray.showChildFolders(this._FolderPath);
		}
		else 
		{
			this._ShowFoldersImage.src=foldersArray.HideSubFolderImage;
			foldersArray.hideChildFolders(this._FolderPath);
		}
		if(typeof calendarNavigator!='undefined')
			calendarNavigator.setCalendarLocation();
	}
},

get_Cancel:function(){return this._Cancel;},
set_Cancel:function(){this._Cancel=true;},
set_DefaultFolderImage:function(){this._FolderImage.src=this._DefaultFolderImage;},
set_LoadingFolderImage:function(){this._FolderImage.src=this._LoadingFolderImage;},
get_FolderPath:function(){return this._FolderPath;},
get_ParentFolderPath:function(){return this._ParentFolderPath;},

get_FolderName:function(){return this._FolderName;},
get_IsComplete:function()
{
	//in case it is not defined or null.  Shouldn't happen but just to make sure
	if(this._IsComplete!=true)
		return false;
	else
		return this._IsComplete;
},
get_TimeStamp:function()
{
	if(this._TimeStamp==null)
		return '';
	else
		return this._TimeStamp;
},
get_TotalBytes:function()
{
	if(this._TotalBytes==null)
		return 0;
	else
		return Number(this._TotalBytes);
},
get_TotalMessages:function()
{
	if(this._TotalMessages==null)
		return 0;
	else
		return Number(this._TotalMessages);
},
get_TotalUnread:function()
{
	if(this._TotalUnread==null)
		return 0;
	else
		return Number(this._TotalUnread);
},
get_FormattedFolderName:function()
{
	if(this._DisplayFolderName==null)
		return "";
	else
		return this._DisplayFolderName;
}
}
WebMail2.Folder.registerClass("WebMail2.Folder");
