
/**
 * Class Resizeable 
 *
 * Claudio Gamboa <gamboa AT_NO_SPAM co.sapo.pt>
 */


/*var Resizeable = function(element, elmHandle, options)
{
    this.init(element, elmHandle, options);
}*/

var Resizeable = Class.create();

Resizeable.prototype = {

    initialize: function(element, elmHandle)
    {
        var options = Object.extend({
                containers: false,
                constrain: 'both',
                debug:false
                }, arguments[2] || {});

        this.element = $(element);
        this.handle = $(elmHandle);

        this.options = options;

        if(this.options.containers && this.options.containers.length > 0) {
            this.elmContainers = [];
            for(var i=0; i < this.options.containers.length; i++) {
                this.elmContainers.push(document.getElementsByClassName(this.options.containers[i], this.element)[0]); 
            }
        }

        this.elmPosition = false;
        this.active = false; 

        Event.observe(this.handle, 'mousedown', this.onStart.bindAsEventListener(this));
        Event.observe(document, 'mousemove', this.onDrag.bindAsEventListener(this));
        Event.observe(document, 'mouseup', this.onEnd.bindAsEventListener(this));
    },

    onStart: function(e)
    {
        this.elmDims = [Element.getWidth(this.element), Element.getHeight(this.element)];
        this.elmPosition = Position.cumulativeOffset(this.element); 

        this.active = true; 
    },

    onDrag: function(e)
    {
        if(this.active) {

            if(this.options.constrain == 'both' || this.options.constrain == 'horizontal') {
                var mPosX = parseInt(Event.pointerX(e));
                var newX = (mPosX - parseInt(this.elmPosition[0]));
                this.element.style.width = newX+'px';
                var variationX = newX - this.elmDims[0];

                //for(var i=0; i < this.elmContainers.length; i++) {
                //    this.elmContainers[i].style.width = (Element.getWidth(this.elmContainers[i]) + variationX)+'px';
                //}
            }
            if(this.options.constrain == 'both' || this.options.constrain == 'vertical') {
                var mPosY = parseInt(Event.pointerY(e));
                var newY = (mPosY - parseInt(this.elmPosition[1]));
                this.element.style.height = newY+'px';
                var variationY = newY - this.elmDims[1];
                
                //for(var i=0; i < this.elmContainers.length; i++) {
                //    this.elmContainers[i].style.height = (Element.getHeight(this.elmContainers[i]) + variationY)+'px';
                //}
            }
            //this.elmDims = [Element.getWidth(this.element), Element.getHeight(this.element)];
        }
    },

    onEnd: function(e)
    {
        this.active = false;
    },


    debug: function()
    {
    }
};

