dojo.provide("dojox.layout.Window");

dojo.require("dojox.layout.FloatingPane");

dojo.declare("dojox.layout.Window",
[dojox.layout.FloatingPane],
{
	typematicConnections: [],
	
	postCreate: function(){
		if(this.resizable){
			this.connectMoveModeKeyHandler();
			this.moveOverlay = this.createMoveOverlay();
		}
		dojo.connect(this.focusNode,'onfocus',this,'bringToTop');
		this.inherited(arguments);
	},
	
	connectMoveModeKeyHandler: function(){
		dojo.connect(this.focusNode,'onkeydown',this,'_handleFocusNodeKey');
		dojo.connect(this.focusNode,'onblur',this,'endMoveMode');
	},
	
	_handleFocusNodeKey: function(evt){
		if(evt.keyCode === 32){
			dojo.stopEvent(evt);
			this[ (this.moveMode ? "end" : "start") + "MoveMode"]();
		}
		if(evt.keyCode === 27){
			this.endMoveMode();
		}
	},
	
	startMoveMode: function(){
		if(this.moveMode){
			return;
		}
		this.showMoveOverlay();
		this.enableTypematicConnections();
		this.moveMode = true;
	},
	
	endMoveMode: function(){
		if(!this.moveMode){
			return;
		}
		this.hideMoveOverlay();
		this.disableTypematicConnections();
		this.moveMode = false;
	},
	
	createMoveOverlay: function(){
		return dojo.create('div',{'class': 'dojoxFloatingPaneMoveOverlay'},this.canvas,'first');
	},
	
	showMoveOverlay: function(){
		this._toggleMoveOverlay(true);
	},
	
	hideMoveOverlay: function(){
		this._toggleMoveOverlay(false);
	},
	
	_toggleMoveOverlay: function(isVisible){
		dojo.toggleClass(this.moveOverlay,'visible',isVisible);
	},
	
	enableTypematicConnections: function(){
		dojo.forEach([37,38,39,40],function(code){
			this.typematicConnections.push(dijit.typematic.addKeyListener(this.focusNode, {charOrCode: code, shiftKey: false}, this, "_handleKeyMove", 25, 0));
			this.typematicConnections.push(dijit.typematic.addKeyListener(this.focusNode, {charOrCode: code, shiftKey: true}, this, "_handleKeyResize", 25, 0));
		},this);
	},
	
	disableTypematicConnections: function(){
		dojo.forEach(this.typematicConnections,function(connSet){
			dojo.forEach(connSet,function(conn){dojo.disconnect(conn);});
		},this);
	},
	
	_handleKeyMove: function(count, obj, e) {
		if(count === -1){
			return;
		}
		var coords = dojo.marginBox(this.domNode),
			dest = {},
			prop = e.charOrCode % 2 === 0 ? 't' : 'l',
			dir = e.charOrCode > 38 ? 'plus' : 'minus';
		
		dest[prop] = coords[prop] + 5 * ( dir === 'plus' ? 1 : -1 );
		
		/*
		switch(e.charOrCode){
			case 37:
				dest.l = coords.l - 5;
				break;
			case 38:
				dest.t = coords.t - 5;
				break;
			case 39:
				dest.l = coords.l + 5;
				break;
			case 40:
				dest.t = coords.t + 5;
				break;
		}
		*/
		
		dojo.marginBox(this.domNode,dest);
	},
	
	_handleKeyResize: function(count, obj, e){
		if(count === -1){
			return;
		}
		var coords = dojo.contentBox(this.domNode),
			dest = {},
			prop = e.charOrCode % 2 === 0 ? 'h' : 'w',
			dir = e.charOrCode > 38 ? 'plus' : 'minus';
			
		dest[prop] = coords[prop] + 5 * ( dir === 'plus' ? 1 : -1 );
		
		/*
		switch(e.charOrCode){
			case 37:
				dest.w = coords.w - 2;
				break;
			case 38:
				dest.h = coords.h - 2;
				break;
			case 39:
				dest.w = coords.w + 2;
				break;
			case 40:
				dest.h = coords.h + 2;
				break;
		}
		*/
		this.resize(dest);
	}
});