/* Copyright (c) 2006 Yahoo! Inc. All rights reserved. */

/**
 * @extends YAHOO.util.DragDrop
 * @constructor
 * @param {String} handle the id of the element that will cause the resize
 * @param {String} panel id of the element to resize
 * @param {String} sGroup the group of related DragDrop items
 */
YAHOO.example.DDResize = function(parentObj, panelElId, handleElId, sGroup, config) {
    if (panelElId) {
        this.init(panelElId, sGroup, config);
        this.handleElId = handleElId;
        this.setHandleElId(handleElId);
        this.logger = this.logger || YAHOO;
		this.parentObj = parentObj;
        this.dragStartedEvent = new YAHOO.util.CustomEvent("dragStartedEvent", this);
    }
};

// YAHOO.example.DDResize.prototype = new YAHOO.util.DragDrop();
YAHOO.extend(YAHOO.example.DDResize, YAHOO.util.DragDrop);

YAHOO.example.DDResize.prototype.dragStarted = false;

YAHOO.example.DDResize.prototype.onMouseDown = function(e) {
    var panel = this.getEl();
    this.startWidth = panel.offsetWidth;
    this.startHeight = panel.offsetHeight;
    this.lastGoodWidth = null;

    this.startPos = [YAHOO.util.Event.getPageX(e),
                     YAHOO.util.Event.getPageY(e)];
	
	var targ = YAHOO.util.Event.getTarget(e);
	this.className = targ.className;

};

YAHOO.example.DDResize.prototype.onDrag = function(e) {
    this.dragStartedEvent.fire();
    
    var newPos = [YAHOO.util.Event.getPageX(e),
                  YAHOO.util.Event.getPageY(e)];
	var minDimension = [0,0];
	
	var panel = this.getEl();
	var parentDimensions = [568, 400];
	var maxDimension = getMaxDimension(panel, parentDimensions);
	
	var offsetX, offsetY;	
	var classname = this.className;
	var keepRatio = this.parentObj.keepResizeRatio;
	var proportion = parseInt(this.parentObj.initialDimensions[0])/parseInt(this.parentObj.initialDimensions[1]);
	
	var hasClass = function(direction)
	{
		if (classname.indexOf(direction)!=-1)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	if (hasClass("NE"))
	{
		offsetX = newPos[0] - this.startPos[0];
   		offsetY = this.startPos[1] - newPos[1];
	}
	else if (hasClass("NW"))
	{
		offsetX = this.startPos[0] - newPos[0];
   		offsetY = this.startPos[1] - newPos[1];
	}
	else if (hasClass("SW"))
	{
		offsetX = this.startPos[0] - newPos[0];
   		offsetY = newPos[1] - this.startPos[1];
	}
	else if (hasClass("SE"))
	{
		offsetX = newPos[0] - this.startPos[0];
   		offsetY = newPos[1] - this.startPos[1];
	}
    
    var oldWidth = panel.offsetWidth + "px";
    if (!this.lastGoodWidth) this.lastGoodWidth = oldWidth; 
    var newWidth = Math.max(this.startWidth + offsetX, 10);
    var newHeight = Math.max(this.startHeight + offsetY, 10);
    if (keepRatio) newHeight=newWidth*(1/proportion);
	
	if (keepRatio && (newWidth>maxDimension[0] || newHeight>maxDimension[1])) return;
	if (newWidth>minDimension[0] && newWidth<=maxDimension[0])
	{
		if ((hasClass("NW")) || hasClass("SW"))
		{
			YAHOO.util.Dom.setX(panel, newPos[0]);
		}
		
		panel.style.width = newWidth + "px";
		if (panel.offsetHeight>maxDimension[1])
		{
		    panel.style.width = this.lastGoodWidth;
		}
		else{
		    this.lastGoodWidth = newWidth + "px";
		}
	}
	
	if (keepRatio && newHeight>minDimension[1] && newHeight<=maxDimension[1])
	{
		if (hasClass("NW") ||hasClass("NE"))
		{
			YAHOO.util.Dom.setY(panel, newPos[1]);
		}
		panel.style.height = newHeight + "px";
	}
	
	function getMaxDimension(panel, parentDimensions){	    
		var maxWidth = parentDimensions[0] - (parseInt(Dom.getStyle(panel, "left")));
		var maxHeight = parentDimensions[1] - (parseInt(Dom.getStyle(panel, "top")));
        return [maxWidth,maxHeight];
    }			
    
    this.dragStarted = true;
};

YAHOO.example.DDResize.prototype.onMouseUp = function(e)
{
	this.parentObj.setConstraint();
}