/*  JavaScript Splitter Class, version 0.1
 *  Uses prototype framework
 *  (c) 2006 Tamcy
/*--------------------------------------------------------------------------*/

  Splitter = function() {};

  Splitter.prototype = {

     register: function (e, elementToResize, type, callback)
     {
        addLog('Registering '+e.id+' as splitter');
        this.element = e;
        addLog('Resizing element: '+elementToResize);
        this.resizingElement = elementToResize;
        this.horizontal = (type == 'h');
        this.vertical = (type == 'v');
        this.name = e.id;
        this.type = type;

		this.limitMin = false;
		this.limitMax = false;
		this.limitMinValue = 0;
		this.limitMaxValue = 0;

        this.resizedCallback = callback;

//        e.onmousedown = this.mousedown;
        Event.observe(this.element, 'mousedown', this.mousedown.bindAsEventListener(this), false);

        this.mouseMoveSignature = this.mousemove.bindAsEventListener(this);
        this.mouseUpSignature = this.mouseup.bindAsEventListener(this);
     },

     dump: function ()
     {  
        var s= '';
        s += 'Element name : '+this.name+'<br/>';
        s += 'Type : '+this.type+'<br/>';
        return s;
     },

     onclick: function()
     {
     },

     calculateNewSize : function()
     {
		var newSize = this.newSize;
		var valid = true;

        if (this.horizontal)
           newSize = this.originalSize + (this.y2 - this.y1);
        else
           newSize = this.originalSize + (this.x2 - this.x1);

		if (newSize < 0)
			valid = false;

		if (this.limitMin && newSize < this.limitMinValue)
			valid = false;

		if (this.limitMax && newSize > this.limitMaxValue)
			valid = false;
		
		if (valid)
			this.newSize = newSize;

        return valid;
     },

	 getNewSize: function()
	 {
        return this.newSize;
	 },

     initCloneElement : function()
     {
        //Create a new element
//        addLog(co[0]+', '+co[1]);

        this.cloneElement = this.resizingElement.cloneNode (false);

        this.cloneElement.style.backgroundColor='#00FF00';

        Position.clone(this.resizingElement, this.cloneElement);

        this.cloneElement.style.position = 'absolute';

        if (this.vertical)
         {
          this.cloneElement.style.left = this.x1 + 'px';
          this.cloneElement.style.width='3px';
         }
        else
         {
          this.cloneElement.style.top = this.y1 + 'px';
          this.cloneElement.style.height='3px';
         }

        this.cloneElement.style.zIndex = '100';

        addLog('res.ol='+this.resizingElement.scrollLeft);
          
        //this.cloneElement.style.top=this.resizingElement.offsetTop+'px';
//        alert(this.cloneElement);
        document.body.appendChild(this.cloneElement);
//        $('Panels').appendChild(this.cloneElement);

//        Position.absolutize(this.cloneElement);
//        Position.absolutize(this.resizingElement);
     },

     disposeCloneElement : function()
     {
        document.body.removeChild(this.cloneElement);
     },

     mousedown: function(e)
     {
        if (this.vertical)
          this.originalSize = this.resizingElement.offsetWidth;
        else
          this.originalSize = this.resizingElement.offsetHeight;

        this.newSize = this.originalSize;

        this.x1 = Event.pointerX(e);
        this.y1 = Event.pointerY(e)
        addLog(this.resizingElement);
        addLog('Old width of element : '+this.originalSize);
        addLog('mouseDown : '+this.name);
        addLog('mouseDown : '+this.type);
        Event.observe(document, 'mousemove', this.mouseMoveSignature, false);
        Event.observe(document, 'mouseup', this.mouseUpSignature, false);

        this.initCloneElement();

        return false;
     },

     mouseup: function()
     {
        addLog('mouseUp : '+this.name);
        Event.stopObserving(document, 'mousemove', this.mouseMoveSignature, false);

        this.disposeCloneElement();

        //  NewsReaderApp.onListviewResize();

        if (this.vertical)
          this.resizingElement.style.width = this.newSize + 'px';
        else
          this.resizingElement.style.height = this.newSize + 'px';

        this.resizedCallback(this.newSize);

        Event.stopObserving(document, 'mouseup', this.mouseUpSignature, false);

        return false;
     },

     mousemove: function(e)
     {
        this.x2 = Event.pointerX(e);
        this.y2 = Event.pointerY(e)

        addLog('mouseMove : '+this.name+'('+Event.pointerX(e)+','+Event.pointerY(e)+')');

		var valid = this.calculateNewSize();

		if (valid)
		{
			addLog('new size : '+this.getNewSize());
			
			if (this.vertical)
			  this.cloneElement.style.left = this.x2 + 'px';
			else
			  this.cloneElement.style.top = this.y2 + 'px';
		}
		else
			addLog('invalid size : ');

        return false;
     }
  }
