function AudioHeadsetJackAssistant()
{
}

AudioHeadsetJackAssistant.prototype.HeadsetJackSubscription = undefined;
AudioHeadsetJackAssistant.prototype.bInsertFirst = true;
AudioHeadsetJackAssistant.prototype.iCheckScreenTimeout = 2000;

AudioHeadsetJackAssistant.prototype.Initialize = function()
{
	this.showSpinner();
	
	this.controller.get('imageInsert').src = "images/headset_insertion.png";
	this.controller.get('imageRemove').src = "images/headset_removal.png";
}

AudioHeadsetJackAssistant.prototype.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 (typeof this[event.command] == "function") {
			this[event.command](event);
		}
	} else if (event.type == Mojo.Event.commandEnable && 
		 (event.command == Mojo.Menu.helpCmd)) {
		event.stopPropagation();
	}
}

AudioHeadsetJackAssistant.prototype.setup = function()
{
	this.appMenuModel = {
		visible: true,
		items: [Mojo.Menu.editItem, Mojo.Menu.helpItem]
	};
	this.controller.setupWidget(Mojo.Menu.appMenu, {omitDefaultItems: true}, this.appMenuModel);
	 this.cmdMenuModel = {
			visible:true,
			items: [{ }] 
    };  
	var commandMenuItems = new Array();
    
  
	var cmdmenuItemSkip = new Object();
    cmdmenuItemSkip.label = $L("Skip");
    cmdmenuItemSkip.command = "handleSkip";
    commandMenuItems.push(cmdmenuItemSkip);
	var cmdmenuItem1 = new Object();
    commandMenuItems.push(cmdmenuItem1);
	var cmdmenuItem2 = new Object();
    commandMenuItems.push(cmdmenuItem2);
	
	this.cmdMenuModel.items = commandMenuItems;

	
    this.controller.setupWidget(Mojo.Menu.commandMenu, {}, this.cmdMenuModel);	
	  
	this.controller.get('insertNoResponse').observe(Mojo.Event.tap, this.handleInsertNoResponse.bindAsEventListener(this));
	//this.controller.get('insertSkip').observe(Mojo.Event.tap, this.handleSkip.bindAsEventListener(this));

	this.controller.get('removeNoResponse').observe(Mojo.Event.tap, this.handleRemoveNoResponse.bindAsEventListener(this));
	//this.controller.get('removeSkip').observe(Mojo.Event.tap, this.handleSkip.bindAsEventListener(this));

	this.controller.setupWidget('spinnerDiv', 
		{ spinnerSize: Mojo.Widget.spinnerLarge },  
		{ spinning: true });

	this.Initialize();
}

AudioHeadsetJackAssistant.prototype.activate = function()
{
	this.subscribeMediaStatus();
}

AudioHeadsetJackAssistant.prototype.deactivate = function()
{
  	if (this.HeadsetJackSubscription != undefined)
		this.HeadsetJackSubscription.cancel();

	this.Initialize();	
}

AudioHeadsetJackAssistant.prototype.cleanup = function()
{
	SceneChoices.push( "audioHeadsetJack" );
}

AudioHeadsetJackAssistant.prototype.handleInsertNoResponse = function()
{
	Logging.log("Headset Jack", "headset insertion not detected");
	if(this.bInsertFirst)	this.showRemove();
	else			SceneChoices.pushNextScene(this.controller.stageController);
}

AudioHeadsetJackAssistant.prototype.handleRemoveNoResponse = function()
{
	Logging.log("Headset Jack", "headset removal not detected");
	if(this.bInsertFirst)	SceneChoices.pushNextScene(this.controller.stageController);
	else			this.showInsert();

}

AudioHeadsetJackAssistant.prototype.handleSkip = function()
{
	Logging.log("Headset Jack", "skipping headset jack test");
	SceneChoices.pushNextScene(this.controller.stageController);
}

AudioHeadsetJackAssistant.prototype.showCheck = function()
{
	this.controller.get('spinnerDiv').hide();
	this.controller.get('audioHeadsetJackInsert').hide();
	this.controller.get('audioHeadsetJackRemove').hide();
	this.controller.get('audioHeadsetJackCheck').show();
}


AudioHeadsetJackAssistant.prototype.showRemove = function()
{
	this.controller.get('audioHeadsetJackCheck').hide();
	this.controller.get('spinnerDiv').hide();
	this.controller.get('audioHeadsetJackInsert').hide();
	this.controller.get('audioHeadsetJackRemove').show();
}

AudioHeadsetJackAssistant.prototype.showInsert = function()
{
	this.controller.get('audioHeadsetJackRemove').hide();
	this.controller.get('audioHeadsetJackCheck').hide();
	this.controller.get('spinnerDiv').hide();
	this.controller.get('audioHeadsetJackInsert').show();
}

AudioHeadsetJackAssistant.prototype.showSpinner = function()
{
	this.controller.get('audioHeadsetJackInsert').hide();
	this.controller.get('audioHeadsetJackRemove').hide();
	this.controller.get('audioHeadsetJackCheck').hide();
	this.controller.get('spinnerDiv').show();
}

AudioHeadsetJackAssistant.prototype.subscribeMediaStatus = function()
{
  	this.HeadsetJackSubscription = new Mojo.Service.Request('palm://com.palm.audio/media/status', {
		//method: '',
		parameters: {"subscribe": true},
		onSuccess: this.handleSubscribeCallback.bind(this),
		onFailure: function() {
			console.log("Failed to subscribe to media status");
		}
	});
}

AudioHeadsetJackAssistant.prototype.handleSubscribeCallback = function(data)
{
	if (data.subscribed == true)
	{
		if (data.scenario === "media_back_speaker")
		{
			console.log( "No headset preset" );
			this.bInsertFirst = true;
			this.showInsert();
		}
		else if (data.scenario === "media_headset")
		{
			console.log( "Headset present" );
			this.bInsertFirst = true;
			this.showInsert();
		}
		else if (data.scenario === "media_headset_mic")
		{
			console.log( "Headset with mic present" );
			this.bInsertFirst = false;
			this.showRemove();
		}
		else
		{
			console.log("Unknown scenario returned: " + Object.toJSON(data) );
			this.bInsertFirst = true;
			this.showInsert();
		}
	}

	if (data.action === "enabled")
	{
		if (data.scenario === "media_headset")
		{
			console.log( "Headset inserted" );
		}
		else if (data.scenario === "media_headset_mic")
		{
			console.log( "Headset with mic inserted");
			this.showCheck();
			if(this.bInsertFirst)	setTimeout( this.showRemove.bind(this), this.iCheckScreenTimeout );
			else			setTimeout( SceneChoices.pushNextScene.bind(this, this.controller.stageController), this.iCheckScreenTimeout );
		}
		else
		{
			console.log( "Unknown enabled message");
		}
	}
	else if (data.action === "disabled")
	{
		if (data.scenario === "media_headset")
		{
			console.log( "Headset removed" );
		}
		else if (data.scenario === "media_headset_mic")
		{
			console.log( "Headset with mic removed");
			this.showCheck();
			if(this.bInsertFirst)	setTimeout( SceneChoices.pushNextScene.bind(this, this.controller.stageController), this.iCheckScreenTimeout );
			else			setTimeout( this.showInsert.bind(this), this.iCheckScreenTimeout );
		}
		else
		{
			console.log("Unknown disabled message");
		}
	}
}

