﻿Type.registerNamespace('Infragistics.Web.UI');
$IG.ResizeBehavior = function(element)
{
	$IG.ResizeBehavior.initializeBase(this, [element]);
/*
	0: handleClass = '';
	1: handleImage = '';
	2: resizeBorder = '';
	3: minimumWidth = 0;
	4: minimumHeight = 0;
	5: maximumWidth = 2000;
	6: maximumHeight = 2000;
	7: offsetLeft = 0;
	8: offsetTop = 0;
	9: offsetRight = 0;
	10:offsetBottom = 0;
	11:parentType = 0;
	12:toolTip = '';
	13:resize = '';
	14:resizing = '';
	15:mouseover = '';
	16:mouseout = '';
	17:initialize = '';
*/
	this._props = ['', '', '', 0, 0, 2000, 2000, 0, 0, 0, 0, 0, '', '', '', '', '', ''];
}
$IG.ResizeBehavior.prototype =
{
	/* internal flag used by WebDialogWindow to temporary disable resize */
	enabled:true,
	/* $IG.WebDialogWindow support */
	get_pairs:function(){},
	/* expected string in format "#|val|#|val...", where #=index of property, val=value of property */
	set_pairs:function(vals)
	{
		vals = vals.split('|');
		for(var i = 0; i < vals.length; i += 2)
		{
			var id = parseInt(vals[i]), val = vals[i + 1];
			this._props[id] = (id > 2 && id < 12) ? parseInt(val) : $util.replace(val, '&pipe;', '|');
			if(id == 2)
				this._border = parseInt(val.substring(0, 1));
		}
	},
	/**/
	get_handleClass: function(){ return this._get_(0); },
	set_handleClass: function(val)
	{
		this._set_(0, val);
		if(this._hand) this._hand.className = val;
	},
	/**/
	get_handleImage: function(){ return this._get_(1); },
	set_handleImage: function(val)
	{
		this._set_(1, val);
		if(this._hand) this._hand.src = val;
	},
	/**/
	get_resizeBorder: function(){ return this._get_(2); },
	set_resizeBorder: function(val)
	{
		this._set_(2, val);
		this._border = $util.isEmpty(val) ? null : parseInt(val.substring(0, 1));
	},
	/**/
	get_minimumWidth: function(){ return this._get_(3); },
	set_minimumWidth: function(val){ this._set_(3, val); },
	/**/
	get_minimumHeight: function(){ return this._get_(4); },
	set_minimumHeight: function(val){ this._set_(4, val); },
	/**/
	get_maximumWidth: function(){ return this._get_(5); },
	set_maximumWidth: function(val){ this._set_(5, val); },
	/**/
	get_maximumHeight: function(){ return this._get_(6); },
	set_maximumHeight: function(val){ this._set_(6, val); },
	/**/
	get_offsetLeft: function(){ return this._get_(7); },
	set_offsetLeft: function(val){ this._set_(7, val); },
	/**/
	get_offsetTop: function(){ return this._get_(8); },
	set_offsetTop: function(val){ this._set_(8, val); },
	/**/
	get_offsetRight: function(){ return this._get_(9); },
	set_offsetRight: function(val){ this._set_(9, val); },
	/**/
	get_offsetBottom: function(){ return this._get_(10); },
	set_offsetBottom: function(val){ this._set_(10, val); },
	/**/
	get_parentType: function(){ return this._get_(11); },
	set_parentType: function(val){ this._set_(11, val); },
	/**/
	get_toolTip: function(){ return this._get_(12); },
	set_toolTip: function(val, v)
	{
		if(!v) this._set_(12, val);
		v = this._hand;
		if(v) v.alt = v.title = val;
	},
	/**/
	get_resize: function(){ return this._get_(13); },
	set_resize: function(val){ this._set_(13, val, 'Resize'); },
	add_resize: function(handler){ $util.addClientEvent(this, 'Resize', handler);},
	remove_resize: function(handler){ $util.removeClientEvent(this, 'Resize', handler);},
	/**/
	get_resizing: function(){ return this._get_(14); },
	set_resizing: function(val){ this._set_(14, val, 'Resizing'); },
	add_resizing: function(handler){ $util.addClientEvent(this, 'Resizing', handler);},
	remove_resizing: function(handler){ $util.removeClientEvent(this, 'Resizing', handler);},
	/**/
	get_mouseover: function(){ return this._get_(15); },
	set_mouseover: function(val){ this._set_(15, val, 'MouseOver'); },
	add_mouseover: function(handler){ $util.addClientEvent(this, 'MouseOver', handler);},
	remove_mouseover: function(handler){ $util.removeClientEvent(this, 'MouseOver', handler);},
	/**/
	get_mouseout: function(){ return this._get_(16); },
	set_mouseout: function(val){ this._set_(16, val, 'MouseOut'); },
	add_mouseout: function(handler){ $util.addClientEvent(this, 'MouseOut', handler);},
	remove_mouseout: function(handler){ $util.removeClientEvent(this, 'MouseOut', handler);},
	/**/
	get_initialize: function(){ return this._get_(17); },
	set_initialize: function(val){ this._set_(17, val, 'Initialize'); },
	/**/
	getFrameElement: function(){ return this._handDad; },
	getHandleElement: function(){ return this._hand; },
	initialize: function()
	{
		$IG.ResizeBehavior.callBaseMethod(this, 'initialize');
		this._target = this.getTargetElement();
		if(!this._target)
		{
			throw 'Target element for ResizeBehavior not found';
			return;
		}
		if(this._onTimer(true))/* no need to process first paint */
			delete this._onTimer;
		else/* enable processing first paint */
			ig_ui_timer(this);
	},
	_shift: function()/* shift resizer relative to target */
	{
		this._s.marginLeft = (this._shiftX + this._divX - this._bdr) + 'px';
		this._s.marginTop = (this._shiftY + this._divY - this._bdr) + 'px';
	},
	_onTimer: function(init)
	{
		if(this._width)
			return true;
		var elem = this._target, ctl = this._control;
		var val = elem ? elem.offsetWidth : 0;
		if(!val || val == 0)
			return false;
		if(ctl && ctl.canResize && !ctl.canResize())
			return false;
		this._width = val;
		this._height = elem.offsetHeight;
		var tag = elem.nodeName;
		/* extra top/left shifts of resizer relative to the location of target when target is not DIV */
		this._bdr = this._divX = this._divY = 0;
		val = this.get_parentType();
		/* not-DIV flag */
		this._div = (val == 2) || !(tag == 'DIV' || tag == 'SPAN');
		var td = (tag == 'TABLE' && val != 2) ? elem.rows[0] : null;
		if(td) td = td.cells[0];
		else if(val == 1) this._div = false;
		if(tag == 'TD') td = elem;
		/* if target is INPUT (or similar) and it has absolute position then z-index is required  */
		var zi = 0, style = $util.getRuntimeStyle(elem);
		if(this._div && !td)
		{
			val = $util.getStyleValue(style, 'position');
			if(elem.type == 'hidden')
				alert('Can not attach resizer to a hidden element ' + elem.id);
		}
		if(val == 'absolute' || val == 'relative')
		{
			zi = $util.getStyleValue(style, 'zIndex');
			if(!zi || zi < 1) zi = 99999;
		}
		/* calculate margins between target and resizer */
		this._shiftX = $util.getOffset(style, true, true);
		this._shiftY = $util.getOffset(style, false, true);
		this._widthFix = this._shiftX + $util.getOffset(style, true, false, true);
		this._heightFix = this._shiftY + $util.getOffset(style, false, false, true);
		this._shiftHandX = $util.toIntPX(style, 'borderRightWidth');
		this._shiftHandY = $util.toIntPX(style, 'borderBottomWidth');
		/* offsets of resizer relative to bounds of target element */
		val = this.get_offsetLeft();
		this._shiftX = -this._shiftX + val;
		this._shiftWidth = val - this.get_offsetRight();
		val = this.get_offsetTop();
		this._shiftY = -this._shiftY + val;
		this._shiftHeight = val - this.get_offsetBottom();
		val = this.get_stateValue();
		if(val)
		{
			val = val.split(',');
			if(!$util.isEmpty(val[0]))
				this._width = $util.toInt(val[0]);
			if(!$util.isEmpty(val[1]))
				this._height = $util.toInt(val[1]);
		}
		/* _handDad: element which contains handle (_hand) and draws resize border */
		this._handDad = document.createElement('DIV');
		this._s = style = this._handDad.style;
		/* continue not-DIV logic */
		if(td)/* TABLE */
			elem = td;
		if(!td && this._div)/* not-DIV and not-TABLE */
		{
			td = elem;
			elem = elem.parentNode;
			if(zi > 0) style.zIndex = zi + 1;
		}
		else/* DIV or TABLE */
			td = elem.firstChild;
		/* shift for resize element: offset this._handDad compare to this._target */
		style.width = style.height = '0px';
		this._shift();
		style.position = 'absolute';
		elem.insertBefore(this._handDad, td);
		/* element which will represent this._hand */
		val = this.get_handleImage();
		if(val && val.length > 0)
		{
			this._im = elem = document.createElement('IMG');
			elem.src = val;
			elem._me = this;
			$addHandlers(elem, {'readystatechange':this._resize, 'load':this._resize}, this);
		}
		else
			elem = document.createElement('DIV');
		elem.className = this.get_handleClass();
		elem.style.position = 'absolute';
		elem.unselectable = 'on';
		/* _hand: element which draw handle */
		this._hand = elem;
		this.set_toolTip(this.get_toolTip(), true);
		this._handDad.appendChild(elem);
		val = $util.getStyleValue(null, 'cursor', elem);
		if(!val || val == 'auto')
			elem.style.cursor = val = 'SE-resize';
		this._s.cursor = val;
		$addHandlers(this._hand, {'mousedown':this._onMouseDown, 'mouseover':this._onMouseOver, 'mouseout':this._onMouseOut}, this);
		this._onMouseMoveFn = Function.createDelegate(this, this._onMouseMove);
		this._onMouseUpFn = Function.createDelegate(this, this._onMouseUp);
		this._onSelectFn = Function.createDelegate(this, this._onSelectStart);
		this._resize();
		if(!ctl)
			this._raiseClientEvent('Initialize');
		return true;
	},
	_resize: function(e)
	{
		var im = this._im, hand = this._hand, elem = this._target;
		if(!hand)
			return;
		if(im && (im.complete || im.readyState == 'complete'))
			e = this._im = null;
		var width = this._width, height = this._height;
		if(e)
		{
			var b = e.button;
			if(b == 0 && e.rawEvent)
				b = e.rawEvent.button;
			if(b == 1)
				this._but = 1;
			if(this._but == 1 && this._drag && b != 1)/* mouse was released outside of browser */
			{
				this._onMouseUp(e);
				return;
			}
			if(!e.type || e.type.indexOf('m') != 0)
				return;
			var x = e.clientX, y = e.clientY;
			if(this._x == null)
			{
				this._x = x;
				this._y = y;
				this._widthOld = width;
				this._heightOld = height;
			}
			x -= this._x;
			y -= this._y;
			if(x == 0 && y == 0)
				return;
			width = this._widthOld + x;
			height = this._heightOld + y;
			/* 0-evtName, 1-'Resize'EventArgs, 2(0)-browserEvent, 3(1)-postbackAction, 4(2)-new width, 5(3)-new height, 6(4)-old width, 7(5)-old height */
			var args = this._raiseClientEvent('Resizing', 'Resize', e, null, width, height, this._widthOld, this._heightOld);
			if(args && args.get_cancel())
				return;
		}
		else if(this._im)
			return;
		var handWidth = hand.offsetWidth, handHeight = hand.offsetHeight;
		width = Math.min(Math.max(width, Math.max(this.get_minimumWidth(), handWidth) + this._widthFix), this.get_maximumWidth());
		height = Math.min(Math.max(height, Math.max(this.get_minimumHeight(), handHeight) + this._heightFix), this.get_maximumHeight());
		var style = elem.style;
		var width0 = width - this._widthFix, height0 = height - this._heightFix;
		if(this.enabled)
		{
			style.width = width0 + 'px';
			style.height = height0 + 'px';
			width0 = elem.offsetWidth;
			height0 = elem.offsetHeight;
			if(width0 > width)
			{
				width = width0;
				style.width = (width - this._widthFix) + 'px';
			}
			if(height0 > height)
			{
				height = height0;
				style.height = (height - this._heightFix) + 'px';
			}
			/* notify owner about resizing (adjust layout or similar) */
			if(this._control && this._control.onResize)
				this._control.onResize(e);
		}
		this._width = width;
		this._height = height;
		/* target it not DIV */
		if(this._div)
		{
			var p0 = $util.getPosition(elem), p1 = $util.getPosition(this._handDad);
			this._divY -= (p1.y - p0.y + this._bdr - this.get_offsetTop());
			this._divX -= (p1.x - p0.x + this._bdr - this.get_offsetLeft());
			if(!this._drag)
				this._shift();
		}
		if(this._drag)
			this._onMouseOver(e, true);
		hand.style.left = (elem.offsetWidth - handWidth - this._shiftWidth - this._shiftHandX) + 'px';
		hand.style.top = (elem.offsetHeight - handHeight - this._shiftHeight - this._shiftHandY) + 'px';
	},
	setSize: function(width, height)
	{
		this._width = width;
		this._height = height;
		this._resize();
		this.set_stateValue(this._width + ',' + this._height, true);
	},
	dispose: function()
	{
		if(this._hand)
			$clearHandlers(this._hand);
		this._onMouseUp();
		$IG.ResizeBehavior.callBaseMethod(this, 'dispose');
	},
	_onMouseOver: function(e, noEvt)
	{
		if(e && noEvt !== true)
			this._mouseIn = true;
		if(this._drag && noEvt !== true)
			return;
		var elem = this._handDad;
		var width = this._target.offsetWidth - this._shiftWidth, height = this._target.offsetHeight - this._shiftHeight;
		this._s.width = ((width > 0) ? width : 0) + 'px';
		this._s.height = ((height > 0) ? height : 0) + 'px';
		if(this._drag && this._div)
		{
			this._shift();
			return;
		}
		if(noEvt === true)
			return;
		/* 0-evtName, 1-(default)CancelEventArgs, 2(0)-browserEvent */
		this._raiseClientEvent(this._prefix + 'MouseOver', null, e);
		if(!this._border)
			return;
		this._s.border = this.get_resizeBorder();
		this._bdr = this._border;
		this._shift();
	},
	_onMouseOut: function(e, noEvt)
	{
		if(e && noEvt !== true)
			this._mouseIn = false;
		if(this._drag)
			return;
		/* 0-evtName, 1-(default)CancelEventArgs, 2(0)-browserEvent */
		this._raiseClientEvent(this._prefix + 'MouseOut', null, e);
		this._s.border = this._s.width = this._s.height = '0px';
		this._bdr = 0;
		if(!this._border)
			return;
		this._shift();
	},
	_onMouseDown: function(e)
	{
	    if(e.button == 0)
	    {
		    this._x = null;
		    if(!e)if((e = window.event) == null)
			    return;
		    this._drag = true;
		    $util.cancelEvent(e);
		    $addHandler(document, 'mousemove', this._onMouseMoveFn);
		    $addHandler(document, 'mouseup', this._onMouseUpFn);
		    if(Sys.Browser.agent === Sys.Browser.Safari)
			    document.onselectstart = this._onSelectFn;
		    else
			    $addHandler(document, 'selectstart', this._onSelectFn);
		    this._showIframe(true);		
		}
	},
	
	_frameMouseMove:function(e)
	{
	    e.clientY += parseInt(this._control._element.style.top);	    
	    e.clientX += parseInt(this._control._element.style.left);
	    this._onMouseMove(e);
	},
	
	_showIframe:function(show)
	{
	    if(!this._transFrame)
	    {
	        if(this._control && this._control._get_resizeAreaElem)
	        {
	            this._mouseMoveHandler2 = Function.createDelegate(this, this._frameMouseMove);
	            this._transFrame = new $IG.TransparentFrame(this._control._get_resizeAreaElem(), this._mouseMoveHandler2, this._onMouseUpFn)
	        }
	    }
	    if(this._transFrame)
	        this._transFrame.showFrame(show, this._transFrame._parent.offsetHeight, this._transFrame._parent.offsetWidth);
	},
	
	_onMouseMove: function(e)
	{
		if(this._drag)
		{
			this._resize(e ? e : window.event);
			this._showIframe(true);
		}
	},
	_onMouseUp: function(e)
	{
		this._x = null;
		if(!this._drag)
			return;
		this._showIframe(false);
		
		if((this._widthOld == null || this._heightOld == null) || (this._widthOld == this._width && this._heightOld == this._height)) 
		    return;
		this.__dragResized = false; 
		var v, fix = null;
		/* 0-evtName, 1-'Resize'EventArgs, 2(0)-browserEvent, 3(1)-postbackAction, 4(2)-new width, 5(3)-new height, 6(4)-old width, 7(5)-old height */
		var args = this._raiseClientEvent('Resized', 'Resize', e, null, this._width, this._height, this._widthOld, this._heightOld);
		if(args)
		{
			if(args.get_cancel())
			{
				fix = this._width = this._widthOld;
				this._height = this._heightOld;
			}
			else
			{
				if((v = args._width) != null)
					fix = this._width = v;
				if((v = args._height) != null)
					fix = this._height = v;
			}
		}
		this._drag = false;
		if(fix != null || this._div)
			this._resize();
		/* notify owner about resizing (adjust layout or similar) */
		else if(this._control && this._control.onResize)
			this._control.onResize();
		this.set_stateValue(this._width + ',' + this._height, true);
		$removeHandler(document, 'mousemove', this._onMouseMoveFn);
		$removeHandler(document, 'mouseup', this._onMouseUpFn);
		if(Sys.Browser.agent === Sys.Browser.Safari)
			document.onselectstart = null;
		else
			$removeHandler(document, 'selectstart', this._onSelectFn);
		if(!this._mouseIn)
			this._onMouseOut(e, true);		
	},
	_onSelectStart: function(e)
	{
		return this._drag ? $util.cancelEvent(e) : true;
	}
}
$IG.ResizeBehavior.registerClass('Infragistics.Web.UI.ResizeBehavior', $IG.Behavior);

/* event used by ResizeBehavior */
$IG.ResizeEventArgs = function()
{
	$IG.ResizeEventArgs.initializeBase(this);
}
$IG.ResizeEventArgs.prototype =
{
	/* this._props: 0-event, 1-postBackAction, 2-new width, 3-new height, 4-old width, 5-old height */
	get_width: function(){return this._props[2];},
	get_height: function(){return this._props[3];},
	get_oldWidth: function(){return this._props[4];},
	get_oldHeight: function(){return this._props[5];},
	set_width: function(val){this._props[2] = this._width = val;},
	set_height: function(val){this._props[3] = this._height = val;}
}
$IG.ResizeEventArgs.registerClass('Infragistics.Web.UI.ResizeEventArgs', $IG.CancelEventArgs);

if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();