/*
	JoystickControl Version 1.0.0 alpha
	
	A map navigation control for the Google Maps API
	http://googlemapsapi.martinpearman.co.uk/joystickcontrol
	
	Copyright Martin Pearman 2008
	Last updated 17th October 2008

	This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

	You should have received a copy of the GNU General Public License along with this program.  If not, see <http://www.gnu.org/licenses/>.
	
*/

function JoystickControl(options){
	if(!options){
		options={};
	}
	this.isVisible=(options.visible===false)?false:true;
	this.updateInterval=(options.timerDelay)?options.updateInterval:100;
}

JoystickControl.prototype=new GControl();

JoystickControl.prototype.initialize=function(map){
	var boundingBox=document.createElement('div');
	boundingBox.className='JoystickControl_boundingBox';
	
	var joystickControl=document.createElement('div');
	joystickControl.className='JoystickControl_joystickControl';
	joystickControl.title='Drag to pan the map';
	
	boundingBox.appendChild(joystickControl);
	map.getContainer().appendChild(boundingBox);
	
	var joystickControlHomeX=(boundingBox.offsetWidth/2)-(joystickControl.offsetWidth/2);
	var joystickControlHomeY=(boundingBox.offsetHeight/2)-(joystickControl.offsetHeight/2);
	var joystickControlHomePoint=new GPoint(joystickControlHomeX, joystickControlHomeY);
	
	var draggableJoystickControl=new GDraggableObject(joystickControl,{container:boundingBox});
	draggableJoystickControl.moveToHome=function(){
		draggableJoystickControl.moveTo(joystickControlHomePoint);
	};
	draggableJoystickControl.moveToHome();
	
	this.dragEndListener=GEvent.bind(draggableJoystickControl, 'dragend', this, function(){
		this.draggableJoystickControl.moveToHome();
		clearTimeout(this.timeout);	//	wrap in if this.timeout!==false?
		this.timeout=false;
	});
	this.dragListener=GEvent.bind(draggableJoystickControl, 'drag', this, function(){
		if(this.timeout===false){
			var j=this.joystickControl, b=this.boundingBox;
			var x=(b.offsetWidth/2)-(j.offsetLeft+(j.offsetWidth/2));
			var y=(b.offsetHeight/2)-(j.offsetTop+(j.offsetHeight/2));
			GEvent.trigger(this, 'drag', x, y);
			this.timeout=this.newTimeout();
		}
	});
	
	this.boundingBox=boundingBox;
	this.joystickControl=joystickControl;
	this.draggableJoystickControl=draggableJoystickControl;
	this.timeout=false;
	this.setVisible(this.isVisible);
	return boundingBox;
};
JoystickControl.prototype.getDefaultPosition=function(){
	return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(7,7));
};

JoystickControl.prototype.setVisible=function(bool){
	this.boundingBox.style.display=(bool)? 'block':'none';
	this.isVisible=bool;
};

JoystickControl.prototype.selectable=function(){
	return false;
};

JoystickControl.prototype.printable=function(){
	return false;
};


JoystickControl.prototype.timeoutCallback=function(){
	var j=this.joystickControl, b=this.boundingBox;
	var x=(b.offsetWidth/2)-(j.offsetLeft+(j.offsetWidth/2));
	var y=(b.offsetHeight/2)-(j.offsetTop+(j.offsetHeight/2));
	GEvent.trigger(this, 'drag', x, y);
	this.timeout=this.newTimeout();
};

JoystickControl.prototype.newTimeout=function(){
	var me=this;
	var f=function(){
		me.timeoutCallback();
	};
	return setTimeout(f, this.updateInterval);
};

