/*

Fluid App Userscript

FastMail beta web interface
URL pattern: *fastmail.fm/mail/*
Based on https://github.com/melomac/Fluid

*/

var readyStateCheckInterval = setInterval(function() {
    if (document.readyState === "complete") {
		setInterval(updateDockBadge, 1000);
        clearInterval(readyStateCheckInterval);
    }
}, 10);

INBOX_COUNT_ONLY = false;

FILTERED = [ "Drafts", "Trash", "Spam" ];

// How many folders to display in notification
MAX_FOLDERS_SHOWN = 6;


// Holds last list of folder counts
LAST_FOLDER_TOTALS = '';

function updateDockBadge()
{
	var count = 0;
	var folders = [];
	var counts = [];

	var tree = document.getElementsByClassName("FolderTree")[0];
	for (i = 0;  i < tree.childNodes.length; i++)
	{
		// Saved views were causing problems
		if(undefined != tree.childNodes[i].getElementsByClassName("name")[0])
		{
        name_obj = tree.childNodes[i].getElementsByClassName("name")[0];
        name = (name_obj) ? name_obj.innerText : '';
        
        badge_obj = tree.childNodes[i].getElementsByClassName("badge")[0];
        badge = (badge_obj) ? parseInt(badge_obj.innerText) : '';
        
        
        //console.log('folder: '+name+' ('+badge+')');

			if (badge > 0)
			{
				if (name == "Inbox")
				{
					if (FILTERED.indexOf(name) == -1)
					{
						count += badge;
						if(badge > 0)
						{
							folders.push(name);
							counts.push(badge);
						}
					} else {
						// console.log("filtered: " + name);
					}
				}
				else
				{
    				if(INBOX_COUNT_ONLY != true)
    				{
			    		if (FILTERED.indexOf(name) == -1)
			    		{
		    				count += badge;
	    					if(badge > 0)
    						{
						    	folders.push(name);
					    		counts.push(badge);
			    			}
				    	}
    				}
				}
			}
			
		}
	}
	console.log(count);

	if(count > 0)
	{
        // Added description of how many new messages in each folder
		ses = (count > 1) ? 's' : '';
		folder_count = folders.length;
		folder_totals = [];
		for(j=0;j<=folder_count;j++)
		{
			if(undefined != folders[j] && j < MAX_FOLDERS_SHOWN){
				folder_totals.push(folders[j] + ' (' + counts[j] + ')');
			} else {
				if(j >= MAX_FOLDERS_SHOWN)
					folder_totals.push('…');
					break;
			}
		}
		message_totals = '';
		if(folder_totals.length > 0)
			message_totals = folder_totals.join(', ');

        /*
         * Only shows notification if folder counts have changed
         * and total unread has increased.
         * Modify to suit your preferences
         */
		if(message_totals != LAST_FOLDER_TOTALS && count > window.fluid.dockBadge)
		{
			LAST_FOLDER_TOTALS = message_totals;

			notification = {
				title: count + " new FastMail message" + ses + ":",
				description: message_totals,
				priority: 3,
				sticky: false,
				identifier: "fastmail-beta",
				icon: window.fluid.resourcePath + 'app.icns'
			};
			console.log(notification);
			window.fluid.showGrowlNotification(notification);
			window.fluid.dockBadge = count;

		} else {
			window.fluid.dockBadge = count;
		}

	} else {
		window.fluid.dockBadge = "";
	}
}