var MimetypeAssistant = Class.create({
	
	initialize: function(mimeTypeObj) {
		this.mimeTypeObj = mimeTypeObj;
		this.dataDirty = false;
		this.currentHandler = {};
	},
	
	setup: function() {
				
		this.controller.get('sceneHeader').innerHTML = $L("Open #{fileExt} With").interpolate(this.mimeTypeObj);
		this.parseMimeTypeObj();
		
		this.FileTypeHandlerModel = {
            items: this.fileTypeHandlers,
        };
        
        this.controller.setupWidget('handlerList', {
            itemTemplate: 'mimetype/list-item',
			listTemplate: 'mimetype/list-container'
        }, this.FileTypeHandlerModel);
		
		Mojo.Event.listen(this.controller.get('handlerList'), Mojo.Event.listTap, this.handleListTap.bindAsEventListener(this));
		
		Mojo.Event.listen(this.controller.document, Mojo.Event.deactivate, this.saveChanges.bind(this), false);
		
		var appMenuModel = {
	        visible: true,
	        items: [Mojo.Menu.editItem,Mojo.Menu.helpItem]
	        };
		this.controller.setupWidget(Mojo.Menu.appMenu, {omitDefaultItems:true}, appMenuModel);
		
	},
	
	cleanup : function() {
		this.saveChanges();	
	},
	
	parseMimeTypeObj: function() {
			
		this.fileTypeHandlers.clear();
		this.controller.modelChanged(this.FileTypeHandlerModel,this);
		
		var obj = {};
		obj.appName = this.mimeTypeObj.appName;
		obj.checkmark = "checkmark";
		obj.index = this.mimeTypeObj.activeHandler.index;
		obj.mime = this.mimeTypeObj.activeHandler.mime;
		this.fileTypeHandlers.push(obj);
		
		//Save the current handler index
		this.currentHandler = {index:obj.index, mime: this.mimeTypeObj.activeHandler.mime};
				
		//Parse the alternates
		for(var i = 0; i< this.mimeTypeObj.alternates.length; i++) {
			obj = {};
			
			obj.appName = this.palmAppNames.get(this.mimeTypeObj.alternates[i].appId) || this.mimeTypeObj.alternates[i].appName;
			if(!obj.appName)
				continue;
				
			obj.index = this.mimeTypeObj.alternates[i].index;
			obj.mime = this.mimeTypeObj.alternates[i].mime;
			obj.checkmark = '';
			this.fileTypeHandlers.push(obj);
		}
		
		this.controller.modelChanged(this.FileTypeHandlerModel,this);
	},
	
	handleListTap: function(event) {
		
		if(!event || !event.item)
			return;
		
		var item = event.item;
		
		if(this.currentHandler.index === item.index)
			return;
		
		this.currentHandler = {index:item.index, mime: item.mime};
		
		//Remove Checkmark from currently selected row.
		for(var i = 0; i< this.fileTypeHandlers.length; i++) {
			if(this.fileTypeHandlers[i].index === item.index) 
				this.fileTypeHandlers[i].checkmark = "checkmark";
			else
				this.fileTypeHandlers[i].checkmark = '';
		}
		this.controller.modelChanged(this.FileTypeHandlerModel,this);
		this.dataDirty = true;
	},
	
	saveChanges: function() {
		if(!this.dataDirty)
			return;
		this.dataDirty = false;
		
		this.controller.serviceRequest('palm://com.palm.applicationManager', {
			method: 'swapResourceHandler', 
			onSuccess: this.handleSwapResourceResponse.bind(this),
			onFailure: this.handleSwapResourceResponse.bind(this),
			parameters: {"mimeType": this.currentHandler.mime, "index":this.currentHandler.index}
	    });
	},
	
	handleSwapResourceResponse: function(response) {
		Mojo.Log.error("Error Swaping the resource handler "+ Object.toJSON(response));
	},
	
	handleCommand: function(event) {
		if(event.type == Mojo.Event.command) {
			if (event.command == Mojo.Menu.helpCmd) {
				Event.stop(event);
				var openParams = {
					target: 'http://help.palm.com/device_info/index.html'
				};
				this.controller.serviceRequest('palm://com.palm.applicationManager', {
					method: 'open',
					parameters: {
						id: 'com.palm.app.help',
						params: openParams
					}
				});
			}
		} else if (event.type == Mojo.Event.commandEnable && 
		         (event.command == Mojo.Menu.helpCmd)) {
			event.stopPropagation();
		} else if (event.type == Mojo.Event.back) {	
			this.saveChanges();								
		}
		
	},
	
	fileTypeHandlers: [],
	palmAppNames:$H({"com.palm.app.streamingmusicplayer":$L("Music"), "com.palm.app.videoplayer": $L("Videos"), "com.palm.app.certificate": $L("Certificate Manager")})
});
