Number.prototype.between = function(range){	
		return range.include(this);	
		}

var SimpleWindow = Class.create();
SimpleWindow.prototype={
	initialize:function(element){
		
		this.element = $(element);		
		var defaults = {			
			tolerance : 5,
			minHeight : 5,
			maxHeight : 5000,
			minWidth : 5,
			maxWidth : 5000,
			dragOptions :{}
		}
		this.options = Object.extend(defaults,arguments[1] || {});
		this.draggable = new Draggable(this.element,this.options.dragOptions);
		Draggables.unregister(this.draggable);
		
		//Fixes IE bugs
		Draggables.deactivate();
		document.body.ondragstart = function() { return false; };
		document.body.ondrag = function(){return false;};
    //document.body.onselectstart = function() { return false; };
    //document.body.onselect = function(){return false;};
		
		this.onMouseOver = this.isOnResizeHandler.bindAsEventListener(this);
		this.onMouseDown = this.startDrag.bindAsEventListener(this);
		this.onMouseMove = this.onResize.bindAsEventListener(this);
		this.onMouseUp = this.onResizeEnd.bindAsEventListener(this);
		
		this.launchListeners();	
					
	},
	launchListeners : function(){
		Event.observe(this.element,'mousemove',this.onMouseOver);
		Event.observe(this.element,'mouseover',this.onMouseOver);
		Event.observe(this.element,'mousedown',this.onMouseDown);
	},
	stopListeners : function(){
		Event.stopObserving(this.element,'mousemove',this.onMouseOver);
		Event.stopObserving(this.element,'mouseover',this.onMouseOver);
		Event.stopObserving(this.element,'mousedown',this.onMouseDown);
	},
	show : function(){
		this.launchListeners();
		this.element.show();
	},
	hide : function(){
		this.stopListeners();
		this.element.hide();	
	},
	isOnResizeHandler : function(e){
		var right = Position.cumulativeOffset(this.element)[0]+this.element.getDimensions().width;
		var bottom = Position.cumulativeOffset(this.element)[1]+this.element.getDimensions().height;
		if(Event.pointerX(e).between($R(right-this.options.tolerance, right))&&Event.pointerY(e).between($R(bottom-this.options.tolerance, bottom))){
			this.element.setStyle({cursor:'se-resize'});
			this.overResize=true;			
		}else {
			this.element.setStyle({cursor:''});
			this.overResize = false;			
		}			
	},
	startDrag : function(e){		
		
		if(this.overResize){
			Draggables.unregister(this.draggable);
			this.mouseCoords =[Event.pointerX(e),Event.pointerY(e)];
			Event.stop(e);
			this.dimensions=this.element.getDimensions();
			Draggables.deactivate();
			Event.observe(document,'mousemove',this.onMouseMove);
			Event.observe(document,'mouseup',this.onMouseUp);
			
		}else{
			//Draggables.register(this.draggable);
		}		
	},
	onResize : function(e){
		Draggables.deactivate();//Fixes IE bug
		var moved = [Event.pointerX(e)-this.mouseCoords[0],Event.pointerY(e)-this.mouseCoords[1]];
		
		$(this.element).style.width= [this.options.minWidth,[this.options.maxWidth,this.dimensions.width+(moved[0]*2)].min()].max() + 'px';
		$(this.element).style.height = [this.options.minHeight,[this.options.maxHeight,this.dimensions.height+moved[1]].min()].max() + 'px';
		
		//$('main').style.width = $('main').style.width + 100;
		
	},
	onResizeEnd : function(e){	
		
		Event.stopObserving(document,'mouseup',this.onMouseUp);
		Event.stopObserving(document,'mousemove',this.onMouseMove);		
	}
}


