/*
 * Simple draggable popup using Prototype
 * Copyright (C) 2007-2008 Aymeric Augustin
 * Released under the GPL
 */

var Draggable = Class.create();

Draggable.prototype = {
	initialize : function(element) {
		this.element = $(element);
		this.x = 0;
		this.y = 0;
		this.startDrag = this.start.bindAsEventListener(this);
		this.moveDrag = this.move.bindAsEventListener(this);
		this.endDrag = this.end.bindAsEventListener(this);
		Position.absolutize(this.element);
		Event.observe(this.element.getElementsBySelector('div.title_bar').first(), 'mousedown', this.startDrag);
	},
	start : function(ev) {
		this.x = Event.pointerX(ev);
		this.y = Event.pointerY(ev);
		Event.observe(document, 'mousemove', this.moveDrag);
		Event.observe(document, 'mouseup', this.endDrag);
	},
	move : function(ev) {
		dx =  Event.pointerX(ev) - this.x;
		dy =  Event.pointerY(ev) - this.y;
		this.x = Event.pointerX(ev);
		this.y = Event.pointerY(ev);
		this.element.setStyle({
			'left': (parseInt(this.element.getStyle('left')) + dx) + 'px',
			'top': (parseInt(this.element.getStyle('top')) + dy) + 'px'
		});
	},
	end : function(ev) {
		Event.stopObserving(document, 'mousemove', this.moveDrag);
		Event.stopObserving(document, 'mouseup', this.endDrag);
	},
	open : function () {
		this.element.show();
	},
	close : function () {
		this.element.hide();
	}
}
