/* Copyright 2009 Palm, Inc.  All rights reserved. */

var Attachments = Class.create({
	initialize: function() {
	}
});

Attachments.normalizeFileSize = function(size) {
	// Size to megabytes or kilobytes
	if (size > 1048510) {
		size = Math.floor(size / 1048510) + "mb";
	} else if (size > 1024) {
		size = Math.floor(size / 1024) + "k";
	}
	return size;
};

/*
 * Sets the iconType, extension, and displayName properties.
 */
Attachments.processFileObject = function(file) {
	var extensionIndex = file.displayName.lastIndexOf('.');
	file.iconType = Attachments.getIconTypeFromMimeType(file.mimeType);
	if (extensionIndex > 0) {
		var extensionNoDot = file.displayName.substring(extensionIndex + 1).toLowerCase();
		file.extension = '.' + extensionNoDot;
		file.displayNameNoExtension = file.displayName.substring(0, extensionIndex).escapeHTML();
		if (!file.iconType) {
			file.iconType = Attachments.getIconTypeFromExtension(extensionNoDot);
			// Fixup the mimetype for audio attachments so the app treats all audio consistently
			if (!file.mimeType || file.mimeType === "application/octet-stream" || file.mimeType === "eas/eas") {
				if (file.iconType === "type-music") {
					var newMimeType = "audio/" + extensionNoDot;
					Mojo.Log.info("Changing mimetype from %s to %s", file.mimeType, newMimeType);
					file.mimeType = newMimeType;
				}
			}
		}
	} else {
		file.displayNameNoExtension = file.displayName.escapeHTML();
		// The extension needs to be something so layout doesn't get wacky
		file.extension = "&nbsp;";
	}

	if (file.size !== undefined) {
		file.size = Attachments.normalizeFileSize(file.size);
	}
}

Attachments.getIconTypeFromMimeType = function(mimeType) {
	if (!mimeType) {
		return null;
	}
	
	var result = null;
	try {
		var mimePieces = mimeType.split("/",2);
		switch (mimePieces[0]) {
			case 'audio':
				result = "type-music";
				break;
			case 'image':
				result = "type-image";
				break;
			case 'video':
				result = "type-video";
				break;
			case 'text':
				if (mimePieces[1] === "plain") {
					result = "txt";
				}
				else if (mimePieces[1] === 'x-vcard') {
					result = "vcard";
				}
				break;
			case 'application':
				switch (mimePieces[1]) {
					case 'msword':
						result = "type-word";
						break;
					case 'pdf':
						result = "pdf";
						break;
					case 'excel':
					case 'x-excel':
					case 'x-msexcel':
					case 'vnd.ms-excel':
						result = "xls";
						break;
					case 'powerpoint':
					case 'mspowerpoint':
					case 'x-mspowerpoint':
					case 'vnd.ms-powerpoint':
						result = "ppt";
						break;
				}
		}
	}
	catch (e) {
		Mojo.Log.error("Email.getIconTypeFromMimeType:", e.message, "in", e.sourceURL, "(", e.line, ")" );
	}
	
	return result;
};

Attachments.getIconTypeFromExtension = function(extension) {
	switch (extension) {
		case 'bmp':
		case 'gif':
		case 'jpg':
		case 'jpe':
		case 'jpeg':
		case 'png':
			return "type-image";

		case 'mp3':
		case 'm4a':
		case 'wav':
		case 'ogg':
			return "type-music";

		case 'mpg':
		case 'mp4':
		case 'avi':
		case 'wmv':
		case '3gp':
		case '3g2':
			return "type-video";

		case 'doc':
		case 'docx':
		case 'dcx':
		case 'odt':
			return "type-word";

		case 'xls':
		case 'xlsx':
		case 'ods':
			return "xls";

		case 'pdf':
			return "pdf";

		case 'txt':
			return "txt";
		
		case 'vcf':
			return "vcard";

		case 'ppt':
		case 'pptx':
		case 'ppp':
		case 'odp':
			return "ppt";
		
		default:
			return "generic";
	}
};

Attachments.mimeTypeTable = {
		'.aif':	'audio/aiff',
		'.aifc':	'audio/aiff',
		'.aiff':	'audio/aiff',
		'.art':	'image/x-jg',
		'.asf':	'video/x-ms-asf',
		'.au':	'audio/basic',
		'.avi':	'video/avi',
		'.avs':	'video/avs-video',
		'.bin':	'application/octet-stream',
		'.bm':	'image/bmp',
		'.bmp':	'image/bmp',
		'.doc':	'application/msword',
		'.docx':	'application/msword',
		'.dot':	'application/msword',
		'.dv':	'video/x-dv',
		'.dvi':	'application/x-dvi',
		'.exe':	'application/octet-stream',
		'.gif':	'image/gif',
		'.gl':	'video/gl',
		'.gz':	'application/x-compressed',
		'.htm':	'text/html',
		'.html':	'text/html',
		'.ico':	'image/x-icon',
		'.jam':	'audio/x-jam',
		'.jfif':	'image/jpeg',
		'.jfif-tbnl':	'image/jpeg',
		'.jpe':	'image/jpeg',
		'.jpeg':	'image/jpeg',
		'.jpg':	'image/jpeg',
		'.kar':	'audio/midi',
		'.la':	'audio/nspaudio',
		'.log':	'text/plain',
		'.m1v':	'video/mpeg',
		'.m2a':	'audio/mpeg',
		'.m2v':	'video/mpeg',
		'.mid':	'audio/midi',
		'.midi':	'audio/midi',
		'.mjpg':	'video/x-motion-jpeg',
		'.mov':	'video/quicktime',
		'.movie':	'video/x-sgi-movie',
		'.mp2':	'audio/mpeg',
		'.mp3':	'audio/mpeg3',
		'.mpa':	'video/mpeg',
		'.mpe':	'video/mpeg',
		'.mpeg':	'video/mpeg',
		'.mpg':	'video/mpeg',
		'.mpga':	'audio/mpeg',
		'.mpp':	'application/vnd.ms-project',
		'.mv':	'video/x-sgi-movie',
		'.pct':	'image/x-pict',
		'.pcx':	'image/x-pcx',
		'.pdf':	'application/pdf',
		'.png':	'image/png',
		'.pot':	'application/vnd.ms-powerpoint',
		'.ppa':	'application/vnd.ms-powerpoint',
		'.pps':	'application/vnd.ms-powerpoint',
		'.ppt':	'application/vnd.ms-powerpoint',
		'.ppz':	'application/mspowerpoint',
		'.pwz':	'application/vnd.ms-powerpoint',
		'.rmi':	'audio/mid',
		'.rpm':	'audio/x-pn-realaudio-plugin',
		'.snd':	'audio/basic',
		'.text':	'text/plain',
		'.tif':	'image/tiff',
		'.tiff':	'image/tiff',
		'.txt':	'text/plain',
		'.uu':	'text/x-uuencode',
		'.uue':	'text/x-uuencode',
		'.vcf': 'text/x-vcard',
		'.vcs':	'text/x-vcalendar',
		'.vdo':	'video/vdo',
		'.viv':	'video/vivo',
		'.vivo':	'video/vivo',
		'.w6w':	'application/msword',
		'.wav':	'audio/wav',
		'.wiz':	'application/msword',
		'.word':	'application/msword',
		'.xlb':	'application/vnd.ms-excel',
		'.xlc':	'application/vnd.ms-excel',
		'.xll':	'application/vnd.ms-excel',
		'.xlm':	'application/vnd.ms-excel',
		'.xls':	'application/vnd.ms-excel',
		'.xlsx':	'application/vnd.ms-excel',
		'.xlw':	'application/vnd.ms-excel',
		'.xm':	'audio/xm',
		'.xml':	'text/xml',
		'.xmz':	'xgl/movie',
		'.xpm':	'image/x-xpixmap',
		'.xpm':	'image/xpm',
		'.x-png':	'image/png',
		'.zip':	'application/zip'
};
//application/x-binary

