ZoomAssistant.prototype.testStart = false;
ZoomAssistant.prototype.zoomInComplete = false;
ZoomAssistant.prototype.ZoomInTarget1 = {x:"180", y:"140"};
ZoomAssistant.prototype.ZoomInTarget2 = {x:"100", y:"200"};
ZoomAssistant.prototype.ZoomOutTarget1 = {x:"220", y:"100"};
ZoomAssistant.prototype.ZoomOutTarget2 = {x:"40", y:"240"};

ZoomAssistant.prototype.ZoomInArrows1 = {x:"212", y:"123"};
ZoomAssistant.prototype.ZoomInArrows2 = {x:"81", y:"234"};
ZoomAssistant.prototype.ZoomOutArrows1 = {x:"73", y:"223"};
ZoomAssistant.prototype.ZoomOutArrows2 = {x:"200", y:"135"};

/************************************************************************
 * Create the class, initialize things
 ************************************************************************/
function ZoomAssistant()
{
}

ZoomAssistant.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();
	}
}

ZoomAssistant.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.commandMenuModel = {
			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.commandMenuModel.items = commandMenuItems;

	
    this.controller.setupWidget(Mojo.Menu.commandMenu, {}, this.commandMenuModel);
	//this.controller.get('Skip').observe(Mojo.Event.tap, this.handleSkip.bindAsEventListener(this));
	this.controller.get('NoResponse').observe(Mojo.Event.tap, this.handleNoResponse.bindAsEventListener(this));

    this.Initialize();
}

ZoomAssistant.prototype.Initialize = function()
{
    this.controller.get('arrows1').show();
    this.controller.get('arrows2').show();

    // Set these up here so there is no flicker upon activation
    this.controller.get('target1').style.left = this.ZoomInTarget1.x + "px";
    this.controller.get('target1').style.top = this.ZoomInTarget1.y + "px";
    this.controller.get('target2').style.left = this.ZoomInTarget2.x + "px";
    this.controller.get('target2').style.top = this.ZoomInTarget2.y + "px";
    this.controller.get('arrows1').style.left = this.ZoomInArrows1.x + "px";
    this.controller.get('arrows1').style.top = this.ZoomInArrows1.y + "px";
    this.controller.get('arrows2').style.left = this.ZoomInArrows2.x + "px";
    this.controller.get('arrows2').style.top = this.ZoomInArrows2.y + "px";
}

ZoomAssistant.prototype.handleSkip = function()
{
    if (this.zoomInComplete)
        this.controller.stageController.pushScene("backGesture");
    else
        this.goToZoomOut();
}

ZoomAssistant.prototype.handleNoResponse = function()
{
    var str = this.zoomInComplete ? "Zoom Out" : "Zoom In";
    Logging.log( str, "No Response" );
    this.handleSkip();
}

ZoomAssistant.prototype.handleGestureStart = function(event){
	//console.log("GESTURE: handleGestureStart called: pageX=" + event.pageX + ", pageY=" + event.pageY + ",  event.centerX=" + event.centerX + ",  event.centerY=" + event.centerY + ", rotation=" + event.rotation + ", scale=" + event.scale);
	
	var target1 = {x: parseInt(this.ZoomInTarget1.x),
				   y: parseInt(this.ZoomInTarget1.y) };
	var target2 = {x: parseInt(this.ZoomInTarget2.x),
				   y: parseInt(this.ZoomInTarget2.y) };
	if (this.zoomInComplete) {

		target1 = {x: parseInt(this.ZoomOutTarget1.x),
				   y: parseInt(this.ZoomOutTarget1.y) };
		target2 = {x: parseInt(this.ZoomOutTarget2.x),
				   y: parseInt(this.ZoomOutTarget2.y) };
	}
	this.targetCenter = { x: (target1.x + target2.x) / 2,
						  y: (target1.y + target2.y) / 2 };
	this.diff = { x: (target1.x - target2.x) / 2,
				  y: (target1.y - target2.y) / 2 };
	     
	this.testStart = true;
	this.controller.get('arrows1').hide();
	this.controller.get('arrows2').hide();
}
   
ZoomAssistant.prototype.handleGestureChange = function(event)
{
	var target1 = {x: parseInt(this.ZoomInTarget1.x),
				   y: parseInt(this.ZoomInTarget1.y) };
	var target2 = {x: parseInt(this.ZoomInTarget2.x),
				   y: parseInt(this.ZoomInTarget2.y) };
	if (this.zoomInComplete) {

		target1 = {x: parseInt(this.ZoomOutTarget1.x),
				   y: parseInt(this.ZoomOutTarget1.y) };
		target2 = {x: parseInt(this.ZoomOutTarget2.x),
				   y: parseInt(this.ZoomOutTarget2.y) };
	}
	
	
	if (!this.testStart)
	{
		this.targetCenter = { x: (target1.x + target2.x) / 2,
							  y: (target1.y + target2.y) / 2 };
		this.diff = { x: (target1.x - target2.x) / 2,
					  y: (target1.y - target2.y) / 2 };
	     
		this.testStart = true;
		this.controller.get('arrows1').hide();
		this.controller.get('arrows2').hide();
	}

	this.newDiff = { x: this.diff.x * event.scale,
					 y: this.diff.y * event.scale };

    this.controller.get('target2').style.left = (this.targetCenter.x - this.newDiff.x) + "px";
    this.controller.get('target2').style.top = (this.targetCenter.y - this.newDiff.y) + "px";
    this.controller.get('target1').style.left = (this.targetCenter.x + this.newDiff.x) + "px";
    this.controller.get('target1').style.top = (this.targetCenter.y + this.newDiff.y) + "px";
}

ZoomAssistant.prototype.activate = function()
{
    this.zoomInComplete = false;

    this.controller.listen(this.controller.document, 'gesturestart', this.handleGestureStart.bind(this));
    this.controller.listen(this.controller.document, 'gesturechange', this.handleGestureChange.bind(this));
    this.controller.listen(this.controller.document, 'gestureend', this.handleGestureEnd.bind(this));
}

ZoomAssistant.prototype.deactivate = function()
{
    this.controller.get('titleDiv').update( $L("Zoom in") );

    this.controller.stopListening(this.controller.document, 'gesturestart', this.handleGestureStart);
    this.controller.stopListening(this.controller.document, 'gesturechange', this.handleGestureChange);
    this.controller.stopListening(this.controller.document, 'gestureend', this.handleGestureEnd);

    // Do this upon deactivate so there is no flicker upon activation
    this.Initialize();
}

ZoomAssistant.prototype.goToZoomOut = function(event)
{
    this.controller.get('instructions').update( $L("Touch both dots and move your fingers together.") );
    this.controller.get('titleDiv').update( $L("Zoom out") );
    this.zoomInComplete = true;

    this.controller.get('arrows1').show();
    this.controller.get('arrows2').show();

    // Setup the "zoom out" test
    this.controller.get('target1').style.left = this.ZoomOutTarget2.x + "px";
    this.controller.get('target1').style.top = this.ZoomOutTarget2.y + "px";
    this.controller.get('target2').style.left = this.ZoomOutTarget1.x + "px";
    this.controller.get('target2').style.top = this.ZoomOutTarget1.y + "px";

    this.controller.get('arrows1').style.left = this.ZoomOutArrows1.x + "px";
    this.controller.get('arrows1').style.top = this.ZoomOutArrows1.y + "px";
    this.controller.get('arrows2').style.left = this.ZoomOutArrows2.x + "px";
    this.controller.get('arrows2').style.top = this.ZoomOutArrows2.y + "px";
}

ZoomAssistant.prototype.handleGestureEnd = function(event)
{
    //console.log("Zoom: handleGestureEnd called: centerX=" + event.centerX + ", centerY=" + event.centerY + ", rotation=" + event.rotation + ", scale=" + event.scale );

    if (this.testStart)
    {
        if (!this.zoomInComplete)
        {
            if (event.scale > 1)
            {
                Logging.log( "Zoom In", "Completed successfully" );
                this.goToZoomOut();
            }
            else
            {
                this.controller.get('target1').style.left = this.ZoomInTarget1.x + "px";
                this.controller.get('target1').style.top = this.ZoomInTarget1.y + "px";
                this.controller.get('target2').style.left = this.ZoomInTarget2.x + "px";
                this.controller.get('target2').style.top = this.ZoomInTarget2.y + "px";
            }
        }
        else
        {
            if (event.scale < 1)
            {
                Logging.log( "Zoom Out", "Completed successfully" );
                this.controller.stageController.pushScene("backGesture");
                this.zoomInComplete = false;
            }
            else
            {
                this.controller.get('target1').style.left = this.ZoomOutTarget2.x + "px";
                this.controller.get('target1').style.top = this.ZoomOutTarget2.y + "px";
                this.controller.get('target2').style.left = this.ZoomOutTarget1.x + "px";
                this.controller.get('target2').style.top = this.ZoomOutTarget1.y + "px";
            }
        }
        this.testStart = false;
    }
}



