﻿//Copyright WebMail Wou3, Inc. 2007-2010
Type.registerNamespace('WebMail2');

WebMail2.FoldersCollection=function()
{
	this._folderArray=new Array();
	this._sequentialIndex=0;
	this._lastUpdateTime=3;
	this._EmptyTrashID=null;
	this._EmptySpamID=null;
	
	//_nextFolderIndex will store the next folder to check for latest by index.
	//each time a folder is checked,  increment until equal to or greater than
	//count.  Inbox is not checked.
	this._nextFolderIndex=1;
	
	this.ShowSubFolderImage='/images/icon_minus.gif';
	this.HideSubFolderImage='/images/icon_plus.gif';
}
WebMail2.FoldersCollection.prototype=
{   
cancelPending:function()
{
	for (var i=0; i< this._folderArray.length; i++)
	{
		this._folderArray[i].set_Cancel();
	}
},
add_Folder:function(folderObject)
{
	var index=this.get_FolderIndex(folderObject.get_FolderPath());
	if(index < 0 )
	{
		//adds the folderPath to the end of the folderIndexHeader array.
		//then adds the array of folderIndexHeader info to the array of folders
		this._folderArray.push(folderObject);
	}
	else
	{
		this._folderArray[index]=folderObject;
	}
},
get_Count:function(){return this._folderArray.length;},
get_FolderByPath:function(folderPath){return this.get_FolderByIndex(this.get_FolderIndex(folderPath));},
get_FolderIndex:function(folderPath)
{
	for (var i=0; i< this._folderArray.length; i++)
	{
		if(this._folderArray[i].get_FolderPath().toLowerCase()==folderPath.toLowerCase())
		{
			return i;
		}
	}
	return -1;
},
get_FolderByIndex:function(index)
{
	if( (index < this._folderArray.length) && (index >= 0) )
		return this._folderArray[index];
	else
		return null;
},
UpdateFolderByPath:function(FolderPath,recursive,forceIndexRebuild,forceMessageListUpdate,timeSpanSeconds)
{
	var folder=this.get_FolderByPath(FolderPath);
	if(folder!=null)
	{
		folder.UpdateFolder(recursive,forceIndexRebuild,forceMessageListUpdate,timeSpanSeconds);
	}
},
CancelUpdate:function(FolderPath)
{
	var folder=this.get_FolderByPath(FolderPath);
	if(folder!=null)
	{
		folder.CancelUpdate();
	}
},
get_FolderName:function(FolderPath)
{
	var folder=this.get_FolderByPath(FolderPath);
	if(folder!=null)
	{
		return folder.get_FolderName();
	}
},
get_IsComplete:function(FolderPath)
{
	var folder=this.get_FolderByPath(FolderPath);
	if(folder!=null)
	{
		return folder.get_IsComplete();
	}
},
get_TimeStamp:function(FolderPath)
{
	var folder=this.get_FolderByPath(FolderPath);
	if(folder!=null)
	{
		return folder.get_TimeStamp();
	}
},
get_TotalBytes:function(FolderPath)
{
	var folder=this.get_FolderByPath(FolderPath);
	if(folder!=null)
	{
		return folder.get_TotalBytes();
	}
},
get_TotalMessages:function(FolderPath)
{
	var folder=this.get_FolderByPath(FolderPath);
	if(folder!=null)
	{
		return folder.get_TotalMessages();
	}
},
get_TotalUnread:function(FolderPath)
{
	var folder=this.get_FolderByPath(FolderPath);
	if(folder!=null)
	{
		return folder.get_TotalUnread();
	}
},
//folderIndexHeader is the object returned from the store driver.
//hideWhileUpdating==false means the message list remains visible while updating
set_FolderIndexHeader:function(folderIndexHeader)
{
	if(folderIndexHeader==null)
		return;
	var folder=foldersArray.get_FolderByPath(folderIndexHeader.FolderPath);
	if(folder!=null)
		folder.set_FolderIndexHeader(folderIndexHeader);
	folder=null;
},
//folderIndexHeaders == a collection of folderIndexHeader objects (could be null)
//for each header, call set_FolderIndexHeader 
set_FolderIndexHeaders:function(folderIndexHeaders)
{
	if(folderIndexHeaders==null)
		return;
	for(i=0; i<folderIndexHeaders.length; i++)
	{
		foldersArray.set_FolderIndexHeader(folderIndexHeaders[i]);
	}
},

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//shows the immedate child folder menu items
//if the immediate icon 
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
showChildFolders:function(parentFolderPath)
{
	for (var i=0; i< this._folderArray.length; i++)
	{
		//this method is called by a parent folder to find all immediate child folders
		//Sets each child folder's display style to 'list-item'
		if(this._folderArray[i].get_ParentFolderPath()==parentFolderPath)
		{
			this._folderArray[i]._ListItem.style.display='list-item';
			if(this._folderArray[i]._ShowFoldersImage!=null)
			{
				if(this._folderArray[i]._ShowFoldersImage.src.indexOf(this.ShowSubFolderImage)>=0)
				{
					this.showChildFolders(this._folderArray[i].get_FolderPath());
				}
				else
				{
					this.hideChildFolders(this._folderArray[i].get_FolderPath());
				}
			}
		}
	}
},
hideChildFolders:function(parentFolderPath)
{
	for (var i=0; i< this._folderArray.length; i++)
	{
		//this method is called by a parent folder to find all immediate child folders
		//Sets each child folder's display style to 'list-item'
		if(this._folderArray[i].get_ParentFolderPath().indexOf(parentFolderPath)==0)
		{
			this._folderArray[i]._ListItem.style.display='none';
		}
	}
},
/**********************************************************************************************
set_EmptySpamID and set_EmptyTrashID are for the ManageFolderPage.aspx only
These methods are used to update the main page's spam and trash contents on the main 
portion of the page.  Not update the left side menu.
**********************************************************************************************/
set_EmptySpamID:function(EmptySpamID)
{this._EmptySpamID=EmptySpamID;},
set_EmptyTrashID:function(EmptyTrashID)
{this._EmptyTrashID=EmptyTrashID;},

/////////////////////////////////////////////////////////////////////////////////////////////////////////
emptyTrash:function()   
{
	//public static Dictionary<string, object> EmptyFolder(string FolderPath, bool DeletePermanent, string CurrentFolderPath, int? PageNo)
	if (confirm("Are you sure?"))
	{
		if(this._EmptyTrashID==null)
			PageMethods.EmptyFolder(FOLDER_DELETED,true, page.get_FolderPath(),g_objPageNo.value,clientPageMethods.onReturnBuildReturnFolderObjects,onError);
		else
		{
			//used the hardcoded value FOLDER_INBOX for the current folder.  
			//when this method is called from ManageFolders, the current 
			//message list never needs to be updated at this time
			PageMethods.EmptyFolder(FOLDER_DELETED,true, null,1,this.onEmptyTrashComplete,onError);
		}
	}
	return false;
},
/////////////////////////////////////////////////////////////////////////////////////////////////////////
emptySpam:function()   
{
	//public static Dictionary<string, object> EmptyFolder(string FolderPath, bool DeletePermanent, string CurrentFolderPath, int? PageNo)
	if (confirm("Are you sure?"))
	{
		if(this._EmptySpamID==null)
			PageMethods.EmptyFolder(FOLDER_SPAM,true, page.get_FolderPath(),g_objPageNo.value,clientPageMethods.onReturnBuildReturnFolderObjects,onError);
		else
		{
			//used the hardcoded value FOLDER_INBOX for the current folder.  
			//when this method is called from ManageFolders, the current 
			//message list never needs to be updated at this time
			PageMethods.EmptyFolder(FOLDER_SPAM,true, null,1,this.onEmptySpamComplete,onError);
		}
	}
	return false;
},
emptyFolder:function(folderPath)
{
	if (confirm("Are you sure?"))
	{
		if(page.pageName().toLowerCase() == "default.aspx")
			PageMethods.EmptyFolder(folderPath,true, page.get_FolderPath(),g_objPageNo.value,clientPageMethods.onReturnBuildReturnFolderObjects,onError);
		else
		{
			//used the hardcoded value FOLDER_INBOX for the current folder.  
			//when this method is called from ManageFolders, the current 
			//message list never needs to be updated at this time
			PageMethods.EmptyFolder(folderPath,true, null,g_objPageNo.value,clientPageMethods.onReturnBuildReturnFolderObjects,onError);
		}
	}
	return false;
},
onEmptyTrashComplete:function(results)
{
	setObjectInnerHTML(foldersArray._EmptyTrashID,"0 (0)");
	clientPageMethods.onReturnBuildReturnFolderObjects(results);
},
onEmptySpamComplete:function(results)
{
	setObjectInnerHTML(foldersArray._EmptySpamID,"0 (0)");
	clientPageMethods.onReturnBuildReturnFolderObjects(results);
},
/////////////////////////////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////////////////////////////
//autoRefresh is called by the timer.  It loops through the list of folders 
//and calls GetFolderInfo on the web service to update the 
/////////////////////////////////////////////////////////////////////////////////////////////////////////
autoRefresh:function()
{
	if(this._nextFolderIndex >= this.get_Count())
		this._nextFolderIndex=1;
	
	var folder = this.get_FolderByIndex(this._nextFolderIndex++);
	
	if(folder!=null)
		folder.UpdateFolder(true,false,false,5);
	
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
}
WebMail2.FoldersCollection.registerClass("WebMail2.FoldersCollection");
