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

var DroppedcallAssistant = Class.create({
    initialize: function(displayName, displayNumber, displayLabel, picture, message, contact) {
    	QDLogger.log( "DroppedcallAssistant::initialize", displayName);
		this.appControl = Mojo.Controller.getAppController();
		this.appAssistant = this.appControl.assistant;
		this.updateFields(displayName, displayNumber, displayLabel, picture, message);
		this.windowName = "dropped";
		this.contacts = this.appAssistant.contacts;
		this.contact = contact
		
    },
    
    setup: function(){
		QDLogger.log( "DroppedcallAssistant::setup");
		if (this.contacts.isNumberValid(this.number)) {
			this.controller.get('redial_button').addEventListener(Mojo.Event.tap, this.onRedial.bindAsEventListener(this));
		} else {
			this.controller.get('redial_button').hide();
		}
		this.controller.get('cancel_button').addEventListener(Mojo.Event.tap, this.onCancel.bindAsEventListener(this));
		this.controller.get('dropped_message').addEventListener(Mojo.Event.tap, this.onContactTap.bindAsEventListener(this));
		this.renderScene();
    },
    
	updateFields: function(name, number, label, picture, message) {
		QDLogger.log( "DroppedcallAssistant::updateFields", number);
		if (name == undefined || name == "unknown")
			name = "";
		this.displayName = name;
		this.number = number;
		this.displayNumber = this.formatNumber(number);
		this.displayLabel = (label ? label :"");
		this.picture = this.formatPicture(picture);
		this.message = message;
		QDLogger.log( "DroppedcallAssistant::updateFields", "end");
	},
	
	renderScene: function(){
        QDLogger.log( "DroppedcallAssistant::renderScene");
					
        var content = Mojo.View.render({
            object: this,
            template: "droppedcall/dropped"
        });
        this.controller.get('dropped_message').update(content);
    },
	
	
    
    onCancel: function() {
		this.closeWindow();
	},
    
    onRedial: function(){
        QDLogger.log( "DroppedcallAssistant::onRedial");
        this.appAssistant.launchDial(this.number);
        
        this.closeWindow();
    },
    
	closeWindow: function() {
		this.appControl.closeStage(this.windowName);
	},
	
	formatNumber: function(number){
		if (number)
			return FormatPhoneNumber(number);
		else
			return "";
	},
	
	formatPicture: function(picture) {
		if (picture)
			return "<img src=\""+picture+"\">";
		else
			return "";
	},
	
	// launches the contact app with contact card or pseudocard depending
	// on if the passed line has a contact book entry or not
	onContactTap: function(event){
		event.stop();
		
		if (this.contact && this.contact.id) {
			TelephonyCommands.contactsLaunchWithId(this.contact.id);
		} else if (this.contacts.isNumberValid(this.number)) {
			TelephonyCommands.contactsLaunchWithPseudocard(this.number);
		}
		
		this.closeWindow();
	},
    
});

