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

var FilepickerBehaviors = {
	sanitizeSystemPath: function(systemPath){
		var newPath = systemPath.replace(/^\s*/, '').replace(/\s*$/, '');
		if (newPath.indexOf("file://") == 0) 
			newPath = newPath.substring(7);
		return newPath;
	},
	
	attachRingtonePicker: function(event){
		if(this.person.ringtoneLoc){
			this.controller.popupSubmenu( {
				onChoose:function(c){
					if(c == "CHANGE"){
						this.pushRingtonePicker();
					} else if(c == "DELETE"){
						this.clearRingtone();
					}
				}.bind(this),
				placeNear:event.target,
				items:[
					{label:$L("Change Ringtone"), command:'CHANGE'},
					{label:$L("Delete Ringtone"), command:'DELETE'}
				]
			})			
		} else {
			this.pushRingtonePicker();
		}
		
	},
	
	clearRingtone:function(){
		this.person.ringtoneLoc = "";
		this.person.ringtoneName = "";
		this.person.ringtoneDisplay = $L("Set a ringtone");
		this.person.ringtoneSet = "";
		this.person.dirty = true;
		this.controller.get("RingtoneBox").update(Mojo.View.render({
		        object:this.person,
		        template:"edit/ringtones"
		}));
	},
	
	pushRingtonePicker:function(){
			var params = {
				actionType: 'attach',
				kinds: ['ringtone'],
				defaultKind: 'ringtone',
				filePath:this.person.ringtoneLoc,
				onSelect: function(file){
					this.attachRingtone(this.sanitizeSystemPath(file.fullPath), file.name);
				}.bind(this)
			};
			//Mojo.Log.info("ContactMulti Picking a ringtone for " + this.contact.firstName + " " + this.contact.lastName);
			Mojo.FilePicker.pickFile(params, this.controller.stageController);

	},
	
	attachFilePicker: function(event){
		if(this.contact.readOnly){
			return;
		}
		if (this.contact.pictureLoc || this.contact.imAvatarLoc) {
			this.controller.popupSubmenu( {
				onChoose:function(c){
					if(c == "CHANGE"){
						this.pushPhotoPicker();
					} else if(c == "DELETE"){
						this.clearPhoto();
					}
				}.bind(this),
				placeNear:event.target,
				items:[
					{label:$L("Change Photo"), command:'CHANGE'},
					{label:$L("Delete Photo"), command:'DELETE'}
				]
			})
		} else {
			this.pushPhotoPicker(event);
		}
	},
	
	clearPhoto: function(){
		var picPath = this.contact.pictureLoc;
		
		Contact.setAllPicturePaths(this.person, "", "");
		Contact.setAllPicturePaths(this.contact, "", "");
		this.rerenderHeader();
		// notify the contacts service so it can clean up the file on disk
		if(!this.contact.id)return;
		Mojo.assert(this.contact.id); // shouldn't be able to get here without previously having saved as part of the 'attach photo' flow
		AppAssistant.contactsService.deleteContactPicture(this.controller, this.contact.id, picPath, function(response){
			Mojo.Log.info("Contacts file picker deleted contact picture! success=" + response.returnValue);
		});		
	},
	
	pushPhotoPicker: function(event){
		var params = {
			actionType: 'attach',
			actionName: $L('Assign To Contact'),
			kinds: ['image'],
			crop: {
				width: 256,
				height: 256,
				background: "#000",
				hideIcon: true
			},
			defaultKind: 'image',
			onSelect: function(file){
				this.attachFile(this.sanitizeSystemPath(file.fullPath), file.cropInfo);
			}.bind(this),
		};
		Mojo.FilePicker.pickFile(params, this.controller.stageController);
	},
	
	attachFile: function(filePath, cropInfo){
		this.contact.pictureLoc = filePath;
		this.contact.pic = filePath;
		this.contact.cropInfo = cropInfo;
		this.contact.listPic = filePath;
		
		//TODO do we need to set the person pic?
		this.person.pic = filePath;
		this.person.pictureLoc = filePath;
		if (this.contact.id) {
			var cb = this.saveContactPictureCallback.bind(this);
			this.saveContactRequest = AppAssistant.contactsService.saveContactPicture(this.contact.id, this.contact.pic, this.contact.cropInfo, cb);
			this.rerenderHeader();
		}
		else {
			this.contact.pictureDirty = true;
			this.contact.dirty = true;
			this.rerenderHeader();
		}
	},
	
	saveContactPictureCallback: function(resp){
		Contact.setAllPicturePaths(this.contact, resp.pictureLoc, resp.pictureLocBig);
		Contact.setAllPicturePaths(this.person, resp.pictureLoc, resp.pictureLocBig);
		this.rerenderHeader();
	},	
	
	attachRingtone: function(ringtonePath, name){
		if (ringtonePath) {
			this.person.dirty = true;
			this.person.ringtoneName = name;
			this.person.ringtoneDisplay = name;
			this.person.ringtoneSet = "ringtone-set";
			this.person.ringtoneLoc = ringtonePath;
			this.controller.get("RingtoneBox").update(Mojo.View.render({
		        object:this.person,
		        template:"edit/ringtones"
		      }));
		}
	}
}

EditAssistant.addMethods(FilepickerBehaviors);

