﻿Type.registerNamespace('Infragistics.Web.UI');

/******************************************DialogWindowProps ENUM************************************/

$IG.DialogWindowProps = new function()
{
    var count = $IG.LayoutControlProps.Count;
    this.WindowState = [count++, 0];
    this.Left = [count++, ''];
    this.Top = [count++, ''];
    this.Width = [count++, ''];
    this.Height = [count++, ''];
    this.InitialLocation = [count++, 0];
    this.Moveable = [count++, 1];
    this.MaintainLocationOnScroll = [count++, 0];
    this.Modal = [count++, 0];
    this.AsyncUpdate = [count++, 1];
    this.LastWindowState = [count++, 0];
    this.Count = count;
};

$IG.HeaderProps = new function()
{
    var count = $IG.ControlObjectProps.Count;
    this.CaptionText = [count++, ''];
    this.ImageUrl = [count++, ''];
    this.MinimizedWidth = [count++, 100];
    this.Count = count;
};
/******************************************END DialogWindowProps ENUM********************************/

/*****************************************ENUMS******************************/
$IG.DialogWindowState = function () {}
$IG.DialogWindowState.prototype = 
{
   Normal:0, 
   Minimized:1,
   Maximized:2,
   Hidden:3
};
$IG.DialogWindowState.registerEnum("Infragistics.Web.UI.DialogWindowState");
/******************************************END ENUMS**************************/


/**********************************************   WebDialogWindow  ****************************************/
$IG.WebDialogWindow = function(element)
{
	$IG.WebDialogWindow.initializeBase(this, [element]);
}

$IG.WebDialogWindow.prototype =
{
	_thisType:'dialog',

	/* function call in response on async postback */
	_responseComplete:function(cb, response)
	{
		/* response.context contains array [0]-props for script, [1]-html */
		var cont = response ? response.context : null;
		if(!cont || cont.length < 2)
			return;
		/* temporary container for new element */
		var div = document.createElement('DIV');
		div.innerHTML = cont[1];
		var id = this.get_id(), name = this.get_name(), i = div.childNodes.length;
		while(i-- > 0)
		{
			var node = div.childNodes[i];
			/* find element which represents whole control */
			if(node.id == id)
			{
				/* find DIV which represents body */
				var body = this._get_asyncUpdate() ? null : this._findBody(node);
				if(body)
				{
					var elem, old = null, elems = body.childNodes;
					i = elems ? elems.length : 0;
					/* do not do any moving if there is a ContentUrl */
					if(i != 1 || elems[0].nodeName != 'IFRAME')
					{
						/* remove possible &nbsp; */
						while(i-- > 0)
							body.removeChild(elems[i]);
						elems = this._body2.childNodes;
						i = elems.length;
						/* move children from old body to new body */
						while(i-- > 0)
						{
							this._body2.removeChild(elem = elems[i]);
							body.insertBefore(elem, old);
							old = elem;
						}
					}
				}
				/* swap whole control */
				this._element.swapNode(node);
				break;   
			}
		}
		/* recreate script objects */
		this.dispose();
		$create($IG.WebDialogWindow, {'id':id, 'name':name, 'props':eval(cont[0])}, null, null, $get(id));
	},
	
	_findBody:function(elem)
	{
		if(elem.id && elem.id.indexOf(':mkr:16') > 0)
			return elem;
		var elems = elem.childNodes;
		var i = elems ? elems.length : 0;
		while(i-- > 0)
			if((elem = this._findBody(elems[i])) != null)
				return elem;
		return null;
	},
	
	initialize:function()
	{
		$IG.WebDialogWindow.callBaseMethod(this, 'initialize');
		/* In case of rounded corners container for child controls is deep inside of table. */
		/* In order to support LayoutManager functionality getBody should return correct element */
		/* without any delay triggered by _onTimer and waiting for resize-image. */
		this._body2 = this._elements[16];
		this._ie = Sys.Browser.agent == Sys.Browser.InternetExplorer;
		this._safari = Sys.Browser.agent === Sys.Browser.Safari;
		
		this._modalCss = this._get_clientOnlyValue("mbc");
		
		if(this._contentPane)
			this._contentPane._DIV = this._body2;
		if(this._onTimer(true))/* no need to process first paint */
			delete this._onTimer;
		else/* enable processing first paint */
			ig_ui_timer(this);				
	},
	
	_get_modal:function(){return this._get_value($IG.DialogWindowProps.Modal);},
	_get_asyncUpdate:function(){return this._get_value($IG.DialogWindowProps.AsyncUpdate);},
	get_enabled:function(){return this._getFlags().getEnabled();},
	
	_onTimer:function(init, play)
	{
		if(this._painted)
			return true;		
		var elem = this._element;
		var width = elem ? elem.offsetWidth : 0;
		if(width == 0)
			return false;
        this._painted = true;		
		if(!this._header)/* if header is hidden, then create a dummy-header */
			this._header = new $IG.Header(null, null, null, this);
		/* values related to buttons */
		/* this._elements: */
		/* [0]:IMG-icon, [1]:SPAN-caption, [2]:IMG-pin, [3]:IMG-minimize, [4]:IMG-maximize, [5]:IMG-close */
		/* [7]:TD-header content, [8]:TD-header left edge, [9]:TD-header right edge */
		/* [10]:TD-body left edge, [11]:TD-body center, [12]:TD-body right edge */
		/* [13]:TD-body left bottom corner, [14]:TD-body bottom center, [15]:TD-body bottom right corner */
		/* [16]:DIV-body content */
		this._header._init(this._elements, elem);		
		var rp = this._get_clientOnlyValue('rp');
		if(rp && rp.length > 1)
		{
			rp = this._resizer = $create($IG.ResizeBehavior,{'pairs':rp,'id':this.get_id()+'.r','control':this},null,null,elem);
			rp._prefix = 'Resizer';
			if(rp.get_minimumWidth() == 0)
				rp.set_minimumWidth(this._header._minWidth);
			if(rp.get_minimumHeight() == 0)
				rp.set_minimumHeight(this._header.get_height() + (this._elements[15] ? 25 : 15));
		}
		else if(!this._get_isMinimized() && !this._get_isMaximized() && !this._get_isClosed())
			this.onResize(2, null, play);/* 2: works only if control was never resized */
			
		this._headerMoveable(true);
		if(!this._get_isClosed())
		    this.set_windowState(this.get_windowState())
		    
		this._inModal = false;
		this._ready = true;				
		if(this._thisType == 'dialog')
			this._raiseClientEvent('Initialize');
		return true;			
	},
	_play:function()
	{
		/* TODO: Add animations in future release */
	},

	_addHandlers:function() 
	{
		$IG.WebDialogWindow.callBaseMethod(this, '_addHandlers');
	},

	_createObject:function(element, obj)
	{
		if(obj == 'header')
		{
			this._header = new $IG.Header(obj, element, this._objectsManager.get_objectProps(0), this);
			this._objectsManager.register_object(0, this._header);
		}
		if(obj == 'body')
		{
			this._body = element;
			this._contentPane = new $IG.LayoutPane(obj, element, this._objectsManager.get_objectProps(1), this);
			this._objectsManager.register_object(1, this._contentPane);
		}
	},
	
	/* returns reference to the Infragistics.Web.UI.LayoutPane */
	get_contentPane:function(){return this._contentPane;},
	
	_get_resizeAreaElem:function()
	{
	    var pane = this.get_contentPane();
	    if(pane != null)
	        return pane._DIV;
	
	}, 
	
	get_header:function()
	{
	     ///<summary>
	    /// Returns a reference to the Infragistics.Web.UI.Header
	    ///</summary>
	    return this._ready ? this._header : null;
	},

	
	get_resizer:function()
	{
        ///<summary>
	    /// Returns a reference to the Infragistics.Web.UI.ResizeBehavior if resizing is enabled
	    ///</summary>		    
	    return this._resizer;
	},
	
    /* this is used by the resizer control*/
	canResize:function()
	{
	    return (this.get_windowState() == $IG.DialogWindowState.Normal)
	},	
	
	set_initialLocation:function(val)
	{
		this._set_value($IG.DialogWindowProps.InitialLocation, val);
		if(val == 1 && this._painted && this._header._mover &&  !this._get_isMaximized())
			this._header._mover.setAbsPos(this.get_left(), this.get_top(), true, this.get_maintainLocationOnScroll(), this._get_modal());
	},
	get_initialLocation:function()
	{ 
	    ///<summary>
	    /// Gets/sets the initial location of WebDialogWindow relative to window of browser.
	    ///</summary>	
		return this._get_value($IG.DialogWindowProps.InitialLocation);
	},

	set_moveable:function(value)
	{
		this._set_value($IG.DialogWindowProps.Moveable, value);
		this._headerMoveable(value);
	},
	get_moveable:function()
	{ 
	    ///<summary>
	    /// Gets/sets the ability to drag control by mouse.
	    ///</summary>	
		return this._get_value($IG.DialogWindowProps.Moveable, true) && this.get_enabled();
	},

	set_maintainLocationOnScroll:function(value)
	{
		this._set_value($IG.DialogWindowProps.MaintainLocationOnScroll, value);
		this._headerMoveable(value);
	},
	get_maintainLocationOnScroll:function()
	{ 
	    ///<summary>
	    /// Gets/sets the ability to keep relative location of dialog while window is resized or scrolled.
	    ///</summary>
		return this._get_value($IG.DialogWindowProps.MaintainLocationOnScroll) == 1;
	},

	get_width:function()
	{
	    ///<summary>
	    /// Gets/sets the width of the control.
	    ///</summary>
		return this._get_value($IG.DialogWindowProps.Width);
	},
	set_width:function(val)
	{
		this.setSize(val);
	},
	get_height:function()
	{
	    ///<summary>
	    /// Gets/sets the height of the control.
	    ///</summary>
		return this._get_value($IG.DialogWindowProps.Height);
	},
	set_height:function(val)
	{
		this.setSize(null, val);
	},
	setSize:function(width, height, evt)
	{
		if(width)
			this._set_value($IG.DialogWindowProps.Width, width);
		if(height)
			this._set_value($IG.DialogWindowProps.Height, height);
		if(evt)
			return;
		var elem = this._element;
		if(width)
			elem.style.width = width;
		if(height)
			elem.style.height = height;
		if(!this._painted)
			return;
		if(this._resizer)
			this._resizer.setSize(elem.offsetWidth, elem.offsetHeight);
		else
			this.onResize();
		if(this._header._mover)
			this._header._mover._onScrollSize();
	},
	get_left:function()
	{
	    ///<summary>
	    /// Gets/sets the location of the left edge of control.
	    ///</summary>
		return this._get_value($IG.DialogWindowProps.Left);
	},
	set_left:function(val, mouse)
	{
		this._set_value($IG.DialogWindowProps.Left, val);
		if(!mouse)
			this._element.style.left = val;
	},
	get_top:function()
	{
	    ///<summary>
	    /// Gets/sets the location of the top edge of control.
	    ///</summary>
		return this._get_value($IG.DialogWindowProps.Top);
	},
	set_top:function(val, mouse)
	{
		this._set_value($IG.DialogWindowProps.Top, val);
		if(!mouse)
			this._element.style.top = val;
	},

	/* IResizable */
	/* if(e==2) is called only if control was never resized */
	onResize:function(e, height, play)
	{
		var body = this._body, elem = this._element;
		if(!body || !elem)
			return;
		var v, dif, width, style = elem.style;
		/* if height is defined then it is called by _header._onResize when dialog is maximized and window is resized */
		/* in this situation e is the value for width */
		if(height)
		{
			style.width = e;
			style.height = height;
			e = null;
		}
		if(e == 2 && this._clientWidth != null)
			return;
		height = elem.offsetHeight;
		if(height == 0)
			return;
		/* new width and height coming from the ResizeBehavior */
		if(e)
			this.setSize(style.width, style.height, true);
		var fire = this._shift1h == null;
		/* extra reduction of height because of borders, paddings, margins, etc */
		if(fire)
		{
			this._ready = true;
			style = $util.getRuntimeStyle(elem);
			/* reduction of width and height by main element */
			this._shift1w = $util.getOffset(style, true);
			this._shift1h = $util.getOffset(style);
			this._shift2h = this._shift2w = 0;
			/* elems: [10]-body left edge TD, [11]-body center TD, [12]-body right edge TD */
			/* [13]-body left bottom corner TD, [14]-body bottom center TD, [15]-body bottom right corner TD */
			var i = 9, elems = this._elements;
			while(++i < 16)
			{
				style = $util.getRuntimeStyle(elem = this._elements[i]);
				/* center TD where body DIV is located */
				if(i == 11)
				{
					this._bodyTD = elem;
					continue;
				}
				if(!style)
					continue;
				elem.style.margin = '0px';
				/* top-left and top-right edge TDs */
				if(i == 10 || i == 12)
				{
					v = $util.toIntPX(style, 'width');
					/* (i+3) == bottom-left and bottom-right corner TDs */
//					elem.style.width = elems[i + 3].style.width = v + 'px';
					this._shift2w += v;
					this._shift2w += $util.getOffset(style, true);
				}
				/* bottom-left corner TD */
				if(i == 13)
				{
					v = $util.toIntPX(style, 'height');
					if(v < 5 && this._safari)
						v = elem.offsetHeight;
					/* (i+1) == bottom-center TD, (i+2) == bottom-right corner TD */
//					elem.style.height = elems[i + 1].style.height = elems[i + 2].style.height = v + 'px';
					this._shift2h += v;
					this._shift2h += $util.getOffset(style);
				}
			}
			/* DIV container for child controls */
			elem = this._body2;
			style = $util.getRuntimeStyle(elem);
			this._shift3h = $util.toIntPX(style, 'marginTop') + $util.toIntPX(style, 'marginBottom');
			this._shift3w = $util.toIntPX(style, 'marginLeft') + $util.toIntPX(style, 'marginRight');
			this._shift4h = $util.getOffset(style);
			this._shift4w = $util.getOffset(style, true);
		}
		/* this._shift1h - height-shift of main control element */
		height -= this._shift1h + this._header.get_height();
		if(height < 0)
			height = 0;
		/* this._shift1w - width-shift of main control element */
		if((width = elem.offsetWidth - this._shift1w) < 0)
			width = 0;
		body.style.height = height + 'px';
		/* rounded corners */
		elem = this._bodyTD;
		body = this._body2;
		/* adjust height of center TD which contains body */
		if((height -= this._shift2h) < 0)
			height = 0;
		elem.style.height = height + 'px';
		/* adjust height of body */
		if((height -= this._shift3h) < 0)
			height = 0;
		v = this._shift4h;
		var heightS = height - v;
		if(heightS < 0)
			heightS = 0;
		body.style.height = heightS + 'px';
		/* that may happen in IE under HTML DOCTYPE  */
		if(v > 0 && body.offsetHeight < height)
		{
			body.style.height = height + 'px';
			if((dif = elem.offsetHeight - body.offsetHeight) > 0)
			{
				body.style.height = (height += dif) + 'px';
			}
		}
		height = body.offsetHeight - v;
		/* adjust width of center TD which contains body */
		if((width -= this._shift2w) < 0)
			width = 0;
		elem.style.width = width + 'px';
		var widthTD = elem.offsetWidth;
		/* adjust width of body */
		v = this._shift4w;
		body.style.width = '';
		if((dif = body.offsetWidth - widthTD) > 0)
		{
			body.style.width = '100%';
			if((dif = body.offsetWidth - widthTD) > 0)
				body.style.width = Math.floor(100 - dif / widthTD * 100) + '%';
		}
		/* that may happen in IE under HTML DOCTYPE  */
		if(v > 0 && (body.offsetWidth + this._shift3w) < width)
		{
			body.style.width = width + 'px';
		}
		width = body.offsetWidth - v;
		if(height < 0)
			height = 0;
		if(width < 0)
			width = 0;
		/* check if new values are different from old values  */
		if(this._clientWidth === width && this._clientHeight === height)
			return;
		this._clientWidth = width;
		this._clientHeight = height;
		if(fire)
			this._contentPane._onInit();
		if(play)
			this._play();
		/* notify listeners about size-change */
		$util.raiseLayoutEvent(this);
	},

	/* LayoutManager: get layout manager which resizes children */
	getLayoutManager:function(){ return this; },
	/* LayoutManager: get width available for children */
	getClientWidth:function(){ return this._clientWidth; },
	/* LayoutManager: get height available for children */
	getClientHeight:function(){ return this._clientHeight; },
	/* LayoutManager: get reference to element which contains children, which should be resized */
	getBody:function(){var v = this._body2; return v ? v : this._body;},
	
	/* returns reference to a child element which id ends-up with "id" */
	findChild:function(id)
	{
		return $util.findChild(this.getBody(), id);
	},
	
	_get_isMinimized:function(){return (this.get_windowState() == $IG.DialogWindowState.Minimized);},
	_get_isMaximized:function(){return (this.get_windowState() == $IG.DialogWindowState.Maximized);},
	_get_isClosed:function(){return (this.get_windowState() == $IG.DialogWindowState.Hidden);},
	get_windowState:function()
	{
	    ///<summary>
	    /// Gets/sets the current state of the Dialog window.
	    ///</summary>
	    return this._get_value($IG.DialogWindowProps.WindowState);
	},	    
	set_windowState:function(state, fire)
	{   
	    var lastState = this._get_lastWindowState(), newState = state, restoreMax = restoreMin = false;
	    var currentState = this._get_value($IG.DialogWindowProps.WindowState);	    
	    if(state == $IG.DialogWindowState.Normal)
	    {
	        if(currentState ==  $IG.DialogWindowState.Hidden)
	        {
	            if(lastState == $IG.DialogWindowState.Maximized)
	            {
                    state = $IG.DialogWindowState.Maximized;		        
                    restoreMax = true;
                }
                else if(lastState == $IG.DialogWindowState.Minimized)
                {
                    restoreMin = true;
                    state = $IG.DialogWindowState.Minimized;	
                }
            }
	        else if(currentState == $IG.DialogWindowState.Minimized && lastState == $IG.DialogWindowState.Maximized)
	            state = $IG.DialogWindowState.Maximized;		        
	    }
	    
	    if(state != currentState)   
	    {
	        if(fire)
	        {
	            var args =  this._raiseClientEvent('WindowStateChanging', 'WindowStateCancel', null, null, currentState, state);
	            if(args == null || !args.get_cancel())
	            {
	                this._updateWindowState(state, currentState, lastState, restoreMax, restoreMin);	            
	                this._raiseClientEvent("WindowStateChanged", "PostBack", null, null);
	            }
	            return;
	        }	            
	    }
	    this._updateWindowState(state, currentState, lastState, restoreMax, restoreMin);	            
	},
	
	show:function()
	{
		///<summary>
	    /// Displays the WebDialogWindow by adjusting the WindowState to $IG.DialogWindowState.Normal
	    ///</summary>
	    this.set_windowState($IG.DialogWindowState.Normal);
	},
	hide:function()
	{
		///<summary>
	    /// Hides the WebDialogWindow by adjusting the WindowState to $IG.DialogWindowState.Hidden
	    ///</summary>
	    this.set_windowState($IG.DialogWindowState.Hidden);
	},	
	_updateWindowState:function(state, currentState, lastState, restoreMax, restoreMin)
	{
	    if(restoreMax || restoreMin)
            this._setNormal(state, restoreMax, restoreMin);
        
        this._set_value($IG.DialogWindowProps.WindowState, state);
        
        if(state == $IG.DialogWindowState.Maximized)
            this._setMaximized(currentState);
        else if(state == $IG.DialogWindowState.Minimized)
            this._setMinimized(currentState);
        else if(state == $IG.DialogWindowState.Hidden)
            this._setClosed(); 
        else if(state == $IG.DialogWindowState.Normal)
        {		        
            if(currentState == $IG.DialogWindowState.Maximized)
                this._setRestoreMax();
            else if(currentState == $IG.DialogWindowState.Minimized)
                this._setRestoreMin(lastState);
            else
                this._setNormal(state);
        }
        
        if(state != currentState)
            this._set_lastWindowState(currentState)
	    
        for(var i = 0; i < this._header._buttons.length; i++)
            this._header._buttons[i]._ensureState();
	    
        if(this._resizer)
            this._resizer.enabled = (state == $IG.DialogWindowState.Normal);
	},
	
	_get_lastWindowState:function(){return this._get_value($IG.DialogWindowProps.LastWindowState);},	
	_set_lastWindowState:function(val){this._set_value($IG.DialogWindowProps.LastWindowState, val);},	
	 
	_setRestoreMax:function(isTemp)
	{
	    var max = this._maxOld;
	    if(max)
		{
		    var htm = max.htm;
		    this._header._resizeLsnr(false);
		    var style = this._element.style;
		    style.width = max.w;
		    style.height = max.h;
		    var pos = max.pos;
		    if(pos == null)
			    pos = 'absolute';
		    style.position = pos;
		    style.left = max.x;
		    style.top = max.y;
		    style.zIndex = max.zi;
		    htm.scrollLeft = max.x2;
		    htm.scrollTop = max.y2;
		    if(htm.style)
			    htm.style.overflow = max.over;
			if(!isTemp)
		        this._maxOld = null;
		    this.onResize();
		    this._headerMoveable(true);
		}
		
		this._hideResizer(false);		
	},
	
	_setRestoreMin:function(lastState)	
	{
	    $util.display(this._body);
	    var style = this._element.style;
	    	        
	    if(lastState != $IG.DialogWindowState.Maximized)
	    {
	        style.width = this._header._ownerWidth;
		    style.height = this._header._ownerHeight;		    
		    this._hideResizer(false);	    
		    this.onResize();
	        this._header._ownerSize();	  	        
		    this._headerMoveable(true);
		}
	},
	
	_setMaximized:function(currentState)
	{   
	    if(currentState == $IG.DialogWindowState.Minimized)
	        this._setRestoreMin();
	    var body = this._body;
	    if(!body)
			return;
		var style = this._element.style;
		this._hideResizer(true);

        this._headerMoveable(false);
			
		var htm = $util.getHTML();
		
		if(!this._maxOld)
		{
		    if(currentState == $IG.DialogWindowState.Minimized)
		        this._maxOld = max = {x:this.get_left(), y:this.get_top(), w:this.get_width(), h:this.get_height(), pos:null, zi:style.zIndex, htm:htm, x2:htm.scrollLeft, y2:htm.scrollTop};
		    else
		        this._maxOld = max = {x:style.left, y:style.top, w:style.width, h:style.height, pos:style.position, zi:style.zIndex, htm:htm, x2:htm.scrollLeft, y2:htm.scrollTop};
		 }   
		
		htm.scrollLeft = htm.scrollTop = 0;
		if(htm.style)
		{
			max.over = htm.style.overflow;
			htm.style.overflow = 'hidden';
		}
		style.position = 'absolute';
		style.left = style.top = '0px';
		style.zIndex = 100000;
		var point = $util.getPosition(this._element);
		if(point.x != 0)
			style.left = (-point.x) + 'px';
		if(point.y != 0)
			style.top = (-point.y) + 'px';
		this._header._onResize();
		this._header._resizeLsnr(true);		
	},
	
	_setMinimized:function(currentState)
	{   
	    if(currentState == $IG.DialogWindowState.Maximized)
	        this._setRestoreMax(true)
	        
	    var body = this._body;
		this._header._ownerSize();
		$util.display(body, true);
		var style = this._element.style;
		this._hideResizer(true);
		style.width = this._header._minWidth + 'px';
		style.height = this._header.get_height() + 'px';				
	},
	
	_setClosed:function()
	{
		$util.display(this._element, true);		
		this._headerMoveable(false);	
	},
	
	_setNormal:function(newState, restoreMax, restoreMin)
	{ 
	    $util.display(this._element);	       
	    
	    this._onTimer(true, true)
	    
	    if(restoreMax)
	        this._setRestoreMax();
	    if(restoreMin)
	        this._setRestoreMin();
		
        if(newState == $IG.DialogWindowState.Normal) 
        {
            this.onResize();
            this._headerMoveable(true);
            this._hideResizer(false);	
        }
	    
	    this._play();
	},
		
	
	_hideResizer:function(hide)
	{   
	    if(this._resizer)
			$util.display(this._resizer._handDad, hide);
	},
	
	_headerMoveable:function(moveable)
	{
	    this._header._moveable(moveable, this._get_isMaximized());
	},
	
	/* allow ContentPane to save its scroll */
	_onSubmitOtherHandler:function()
	{
		if(this._contentPane)
			this._contentPane._onSubmit();
		$IG.WebDialogWindow.callBaseMethod(this, '_onSubmitOtherHandler');
	},

	dispose:function()
	{
		if(this._header)
			this._header.dispose();
		if(this._resizer)
			this._resizer.dispose();
		if(this._contentPane)
			this._contentPane.dispose();
		this._header = this._resizer = this._contentPane = null;
		$IG.WebDialogWindow.callBaseMethod(this, 'dispose');
	}
}

$IG.WebDialogWindow.registerClass('Infragistics.Web.UI.WebDialogWindow', $IG.ControlMain);
/**************************************** End Dialog Window ********************************************/

$IG.Header = function(obj, element, props, control)
{
	var csm = obj ? new $IG.ObjectClientStateManager(props[0]) : null;
	$IG.Header.initializeBase(this, [obj, element, props, control, csm]);
	this._elems = this._buttons = [];
}

$IG.Header.prototype =
{
	_height:null,

	get_minimizedWidth:function()
	{
	    ///<summary>
	    /// Gets/sets the width of the header when it is minimized.
	    ///</summary>
		return this._get_value($IG.HeaderProps.MinimizedWidth);
	},
	set_minimizedWidth:function(val)
	{
		this._set_value($IG.HeaderProps.MinimizedWidth, val);
		this.__updateMinWidth(); 
		if(this._owner._get_isMinimized())
		    this._owner._setMinimized();
	},

	/* memorize old values of owner's size (before maximize, minimize) */
	_ownerSize:function()
	{
		var ctl = this._owner;
		var elem = ctl._element;
		var v = ctl.get_height();
		if(v == '' && !this._ownerHeight)
		{
			if((v = elem.offsetHeight) == 0)
				v = 300;
			v = v + 'px';
		}
		if(v != '')
			this._ownerHeight = v;
		v = ctl.get_width();
		if(v == '' && !this._ownerWidth)
		{
			if((v = elem.offsetWidth) == 0)
				v = 400;
			v = v + 'px';
		}
		if(v != '')
			this._ownerWidth = v;
	},
	
	get_height:function()
	{
	    ///<summary>
	    /// Gets the height of the header.
	    ///</summary>
		var height = this._height;
		if(height !== null)
			return height;
		height = this._element;
		if(!height)
			return this._height = 0;
		return ((height = height.offsetHeight) != 0) ? (this._height = height) : 0;
	},
	
	getCaptionText:function()
	{
	    ///<summary>
	    /// Gets/sets the text that will be displayed in the caption area of the header.
	    ///</summary>
		return this._get_value($IG.HeaderProps.CaptionText);
	},
	
	setCaptionText:function(val)
	{
		var elem = this._getElem(1);
		if(!elem)
		{
			if(!val)return;
			this._elems[7].appendChild(this._elems[1] = elem = document.createElement('SPAN'));
		}
		elem.innerHTML = val; 
		this._set_value($IG.HeaderProps.CaptionText, val);
	},
	
	getImageUrl:function()
	{
	    ///<summary>
	    /// Gets/sets the image url that will be displayed in the header.
	    ///</summary>
		return this._get_value($IG.HeaderProps.ImageUrl);
	},
	
	setImageUrl:function(val)
	{
		var elem = this._getElem(0);
		if(!elem)
		{
			if(!val)return;
			this._elems[7].appendChild(this._elems[0] = elem = document.createElement('IMG'));
		}
		elem.src = val; 
		this._set_value($IG.HeaderProps.ImageUrl, val);
	},
	
	_moveable:function(val, max)
	{
		var move = this._mover, ctl = this._owner;/* control-owner of ClientEvents */
		if(!move)
		{
			move = this._element;
			if(!val || !move) return;
			move = this._mover = $create($IG.DialogMoveBehavior,{"targetElement":ctl._element, "owner":this, "control":ctl}, null, null, move);
			move._ie = this._ie;
			move._prefix = 'Header';
		    move._img = this._elems[0];
		    move._lbl = this._elems[1];
		}
		move.enabled = val && ctl.get_moveable();
		if(!val)
		{
			move.restore(max);
		}
		if(val)
			move.setAbsPos(ctl.get_left(), ctl.get_top(), ctl.get_initialLocation() > 0, ctl.get_maintainLocationOnScroll(), ctl._get_modal(), max);
	},
	
	_getElem:function(i)
	{
		var elem = this._elems;
		if(elem) elem = elem[i];
		return (elem && elem.nodeName) ? elem : null;
	},
	
	_setupButtons:function()
	{ 
	    this._buttons = [];
	    this._closeButton = new $IG.DialogButton("Close", this._elems["Close"], this._objectManager.get_objectProps(0), this, null, $IG.DialogWindowState.Hidden);
		this._objectManager.register_object(0, this._closeButton);
		this._buttons.push(this._closeButton);
		
		this._maxButton = new $IG.DialogButton("Maximize", this._elems["Maximize"], this._objectManager.get_objectProps(1), this, null,  $IG.DialogWindowState.Maximized);
		this._objectManager.register_object(1, this._maxButton);
		this._buttons.push(this._maxButton);
		
		this._minButton = new $IG.DialogButton("Minimize", this._elems["Minimize"], this._objectManager.get_objectProps(2), this, null,  $IG.DialogWindowState.Minimized);
		this._objectManager.register_object(2, this._minButton);
		this._buttons.push(this._minButton);
	},

	/* initialize action buttons */
	/* elems: [] of IMG-elements */
	/* elem: owner._element */
	_init:function(elems, elem)
	{
	    this._elems = elems;   
	    if(this._objectManager)
	        this._setupButtons();
	    else
	        return;
		
		var ctl = this._owner;
		this._ownerSize();
				 
		this.__updateMinWidth();
	},
	
	__updateMinWidth:function()
	{
	    var width = 2;
	    var elem = this._element;
	    var elems = this._elems;
	    var td = null;
	    var buttonCount = this._buttons.length;
		for(var i = 0; i < buttonCount; i++)
		{
		    var img = this._buttons[i].get_element();
		    if(!img)continue;
			var ow = img.offsetWidth;
			width += ((ow == 0) ? 15 : ow) + $util.toIntPX(null, 'marginLeft', 0, img) + $util.toIntPX(null, 'marginRight', 0, img);
		    if(!td)
			    td = img.parentNode;
		}
	    
	    var minWidth = this.get_minimizedWidth();
		/* ensure that minimize width is enough to fit buttons */
		if(td)
		{
			/* adjust width of TD parent of buttons */
			td.style.width = width + 'px';
			/* adjust for borders and paddings of header and dialog elements */
			width += $util.getOffset($util.getRuntimeStyle(elem), true);
			width += $util.getOffset($util.getRuntimeStyle(this._element), true) + 1;
			/* elems: [8]-header left edge TD, [9]-header right edge TD */
			if(elems[8])
				width += elems[8].offsetWidth + elems[9].offsetWidth;
			/* verify minimum value */
			if(!minWidth || minWidth < width)
				minWidth = width;
		}
		this._minWidth = minWidth ? minWidth : 0;
	},
	
	/* called by window when maximized is on */
	_onResize:function(e)
	{
		var max = this._maxOld, ctl = this._owner;
		if(!max && !ctl)
			return;
		var win = $util.getWinRect();
		ctl.onResize((win.width - 2) + 'px', (win.height - 2) + 'px');
	},

	/* start/end processing window-resize event */
	_resizeLsnr:function(on)
	{
		if(!on && !this._resizeOn)
			return;
		if(!this._onResizeFn)
			this._onResizeFn = Function.createDelegate(this, this._onResize);
		if(on === this._resizeOn)
			return;
		this._resizeOn = on;
		if(on)
			$addHandler(window, 'resize', this._onResizeFn);
		else
			$removeHandler(window, 'resize', this._onResizeFn);
	},

	getImageElement:function(){ return this._getElem(0); },
	getCaptionElement:function(){ return this._getElem(1); },
	getContentElement:function(){ return this._getElem(7); },
	
	_createObjects:function(objectManager)
    {
        this._objectManager = objectManager;
        if(this._elems)
            this._setupButtons();
    },

	dispose:function()
	{
		/* constants defined in LayoutHeader will set following keys */
		/* 2:pin, 3:minimize, 4:maximize, 5:close */
		if(this._elems && this._owner.get_enabled()) for(var i = 2; i < 6; i++)
		{
			var elem = this._elems[i];
			if(elem && elem.nodeName)
				$clearHandlers(elem);
		}
		this._resizeLsnr(false);
		if(this._mover)
			this._mover.dispose();
		this._mover = null;
		$IG.Header.callBaseMethod(this, 'dispose');
	}
}
$IG.Header.registerClass('Infragistics.Web.UI.Header', $IG.UIObject);

/*************DIALOGBUTTON*******************/

$IG.DialogButton = function(obj, element, props, owner, csm, state)
{   
    this._state = state;    
    if(!csm)csm = new $IG.ObjectClientStateManager(props[0]);    
    $IG.DialogButton.initializeBase(this, [obj, element, props, owner, csm]);
    this._control = this._owner._owner;
    if(this._element)
        this._setupImage();   
}

$IG.DialogButton.prototype = 
{
    get_altText:function()
    {
        ///<summary>
	    /// Gets the alt text of the button.
	    ///</summary>
        var alt = this._get_clientOnlyValue("a");
        return (alt == null)? "" : alt;
    },
    
    get_restoreAltText:function()
    {
        ///<summary>
	    /// Gets the restore alt text of the button.
	    ///</summary>
        var alt = this._get_clientOnlyValue("ra");
        return (alt == null)? "" : alt;
    },
    _setupImage:function()
    {
        if(this._element.nodeName && this._control.get_enabled())
        {
			$addHandlers(this._element, {'mousedown':this._onMouseDown, 'mouseover':this._onMouseOver, 'mouseout':this._onMouseOut}, this);
			this._onMouseUpFn = Function.createDelegate(this, this._onMouseUp);
		    $addHandler(document, 'mouseup', this._onMouseUpFn);
		}
    },
    
    _ensureState:function()
    {
        this.setState((this._control.get_windowState() == this._state) ? "r" : $IG.ImageState.Normal);
    },
    _updateState:function(val1, val2)
    {
        var state = (this._control.get_windowState() == this._state);
        this._updateAlt(state);
		this.setState(state ? val2 : val1);
    },
    _updateAlt:function(state)
    {
        this._element.alt = state ? this.get_restoreAltText(): this.get_altText();
    },
    
    _changeState:function()
    { 
        this._control.set_windowState( (this._control.get_windowState() == this._state) ? $IG.DialogWindowState.Normal :this._state, true);
    },  
    
    _onMouseDown:function(e)
	{
	    if(e.button == 0)
	    {
		    $util.cancelEvent(e);
		    this._updateState($IG.ImageState.Pressed, "rp");
		}
	},
	_onMouseOver:function(e)
	{
	    this._updateState($IG.ImageState.Hover, "rh");
	},
	_onMouseOut:function(e)
	{
	    this._updateState($IG.ImageState.Normal, "r");
	},
	_onMouseUp:function(e)
	{
	    if(e.button == 0)
	    {
		    if(e.target == this._element)
		    {
		        this._changeState();
	            this._updateState($IG.ImageState.Hover, "rh");
	        }
	        else
	            this._updateState($IG.ImageState.Normal, "r");
	    }
	}
}
$IG.DialogButton.registerClass('Infragistics.Web.UI.DialogButton', $IG.ImageObject);

/*************END DIALOGBUTTON **************/


/*****************DialogMoveBehavior*****************/
$IG.DialogMoveBehavior = function(element)
{
	$IG.DialogMoveBehavior.initializeBase(this, [element]);
}

$IG.DialogMoveBehavior.prototype =
{
	/* internal flag used by WebDialogWindow to temporary disable drag */
	enabled:true,
	_fixTabOn:false,
	_scrollOn:false,
	_keyOn:false,
	_isModal:false, 
		
	_addHandlers:function()
	{
		this._mouseMoveHandler = Function.createDelegate(this, this._onMouseMove);
		this._mouseUpHandler = Function.createDelegate(this, this._onMouseUp);
		this._mouseOutHandler = Function.createDelegate(this, this._onMouseOut);
		$addHandlers(this.get_element(), {'mousedown':this._onMouseDown, 'mouseover':this._onMouseOver, 'mouseout':this._onMouseOut}, this);
	},

	get_targetElement:function(){return this._targetElement;},
	set_targetElement:function(value){this._targetElement = value;},

	_onMouseOver:function(e)
	{
		if(this.enabled)
			/* 0-evtName, 1-(default)CancelEventArgs, 2(0)-browserEvent */
			this._raiseClientEvent(this._prefix + 'MouseOver', null, e);
	},
	_onMouseOut:function(e)
	{
		if(this.enabled)
			/* 0-evtName, 1-(default)CancelEventArgs, 2(0)-browserEvent */
			this._raiseClientEvent(this._prefix + 'MouseOut', null, e);
	},
	/* is called while processing mouse-drag of dialog */
	_onMouseDown:function(e)
	{
	    if(e.button == 0)
	    {
		    if(!this.enabled) return;
		    this._drag = 1;
		    var elem = e.target;
		    if(!elem) return;
		    if(elem != this._element && elem != this._img && elem != this._lbl && elem.nodeName != 'TD')
			    return;
		    e.target.unselectable = 'on';/* disable drag-selection */
		    $util.cancelEvent(e);
		    $addHandler(document, 'mousemove', this._mouseMoveHandler); 		    
		    $addHandler(document, 'mouseup', this._mouseUpHandler); 	
		    $addHandler(document, 'mouseout', this._mouseOutHandler);	    
		}
		/* 0-evtName, 1-(default)CancelEventArgs, 2(0)-browserEvent */
		this._raiseClientEvent(this._prefix + 'MouseDown', null, e);
	},
	
	_onMouseOut:function(e)
	{
	    var rect = $util.getWinRect(window);
	    var x = e.clientX;
	    var y = e.clientY;
	    if((x < 0 || y < 0) || x > rect.width || y > rect.height)
	        this._onMouseUp(e);
	}, 
	
	/* is called while processing mouse-drag of dialog */
	_onMouseMove:function(e)
	{
		if(!this.enabled || !this._drag) return;
		$util.cancelEvent(e);
		if(this._drag == 2)
		{
		    this.__moved = true;
			this._moveWindow(e);
			return;
		}
		this._drag = 2;
		this.setAbsPos();
		this._mouseX = e.clientX;
		this._mouseY = e.clientY;
	},
	/* is called while processing mouse-drag of dialog */
	_onMouseUp:function(e)
	{
		if(!this.enabled || !this._drag) return;
		this._drag = null;
		$removeHandler(document, 'mousemove', this._mouseMoveHandler);
		$removeHandler(document, 'mouseup', this._mouseUpHandler);
		$removeHandler(document, 'mouseout', this._mouseOutHandler);	    
		/* raise mouseup event */
		/* 0-evtName, 1-(default)CancelEventArgs, 2(0)-browserEvent */
		this._raiseClientEvent(this._prefix + 'MouseUp', null, e);
		/* (fix!=null) - flag to adjust location */
		var v, fix = null, x = this._x, y = this._y;
		/* raise end of drag event */
		/* 0-evtName, 1-'Move'EventArgs, 2(0)-browserEvent, 3(1)-postbackAction, 4(2)-new x, 5(3)-new y, 6(4)-old x, 7(5)-old y */
		if(x != null && y != null && (this.__moved))
		{
		    this.__moved =false ;
		    var args = this._raiseClientEvent('Moved', 'Move', e, null, x, y, this._xOld, this._yOld);
		    var cancel = args ? args.get_cancel() : false;
		    if(cancel)
		    {
			    fix = x = this._xOld;
			    y = this._yOld;
		    }
		    else if(args)/* check if client adjusted position by MoveEventArgs.set_x/y */
		    {
			    if((v = args._x) != null)
				    fix = x = v;
			    if((v = args._y) != null)
				    fix = y = v;
		    }
		    if(fix == null)/* event was not canceled and new location was not modified */
		    {
		        this._showIframe(false);
			    return;
		    }
		    this.setLocation(x, y, cancel);						
		}
	},
	
	_frameMouseMove:function(e)
	{
	    e.clientY += parseInt(this._control._element.style.top) + this._element.offsetHeight + this._shiftY;	    
	    e.clientX += parseInt(this._control._element.style.left);
	    this._onMouseMove(e);
	},
	
	_showIframe:function(show)
	{
	    var contentPane = this._control.get_contentPane();
	    if(!this._transFrame)
	    {
            this._mouseMoveHandler2 = Function.createDelegate(this, this._frameMouseMove);
            this._transFrame = new $IG.TransparentFrame(contentPane._DIV, this._mouseMoveHandler2, this._mouseUpHandler)
	        
	    }
	    if(this._transFrame)
	        this._transFrame.showFrame(show, contentPane.getClientHeight(), contentPane.getClientWidth());
   
	},
	
	
	/* part of modal functionality */
	/* scroll: start/end processing window-resize event */
	/* key: start/end supressing focus on tab key */
	_lsnr:function(scroll, key)
	{
		if(scroll != this._scrollOn)
		{
			this._scrollOn = scroll;
			if(!this._onScrollFn)
			{
				this._onScrollFn = Function.createDelegate(this, this._onScrollSize);
				this._onResizeFn = Function.createDelegate(this, this._onScrollSize);
			}
			if(scroll)
			{
				$addHandler(window, 'scroll', this._onScrollFn);
				$addHandler(window, 'resize', this._onResizeFn);
			}
			else
			{
				$removeHandler(window, 'scroll', this._onScrollFn);
				$removeHandler(window, 'resize', this._onResizeFn);
			}
		}
		if(key == this._keyOn)
			return;
		this._keyOn = key;
		if(!this._onKeyFn)
		{
			this._onKeyFn = Function.createDelegate(this, this._onKey);
			this._onFocusFn = Function.createDelegate(this, this._onFocus);
			this._onDeactFn = Function.createDelegate(this, this._onDeact);
		}
		if(key)
		{
			$addHandler(document, 'keydown', this._onKeyFn);
			$addHandler(document, 'focus', this._onFocusFn);
			$addHandler(document, 'beforedeactivate', this._onDeactFn);
		}
		else
		{
			$removeHandler(document, 'keydown', this._onKeyFn);
			$removeHandler(document, 'focus', this._onFocusFn);
			$removeHandler(document, 'beforedeactivate', this._onDeactFn);
		}
	},
	/* part of modal functionality */
	/* is called when dialog is centered (or relative to scroll) on resize/scroll events */
	/* flag: 2==initialization (skip fix of position) */
	_onScrollSize:function(flag)
	{
		/* ignore if dialog is in process of dragging by mouse */
		if(this._drag)
			return;
		/* _x/yFixed: latest position after mouse-drag, or initial position coming from Left/Top properties of dialog */
		if(this._scroll && flag != 2)
			this.setAbsPos(this._xFixed, this._yFixed, this._center, this._scroll, this._isModal, false);
		var elem = this._targetElement, lid = this._lid;
		if(!lid)
			return;
		var style = lid.style, rect = $util.getWinRect();
		style.width = Math.max(rect.width + rect.x - 6, rect.maxWidth - 2) + 'px';
		style.height = Math.max(rect.height + rect.y - 6, rect.maxHeight - 2) + 'px';
	},
	/* part of modal functionality */
	/* check if element is a child of dialog */
	_isChild:function(dad, elem)
	{
		if(!dad || !elem)
			return false;
		if(dad == elem)
			return true;
		var nodes = dad.childNodes;
		if(!nodes)
			return false;
		for(var i = 0; i < nodes.length; i++)
			if(this._isChild(nodes[i], elem))
				return true;
		return false;
	},
	/* part of modal functionality */
	/* try find correct tabbable child element according to shiftKey and set focus to it */
	/* e: event to cancel */
	/* delay: request to use delay while setting focus (used under Mozilla) */
	_tabFoc:function(e, delay)
	{
		if(e)
			$util.cancelEvent(e);
		/* check if final _min/_maxTab2 are already available */
		var elem = this._shiftKey ? this._maxTab2 : this._minTab2;
		/* check if _min/_maxTabI are available */
		if(!elem)
			elem = this._shiftKey ? this._maxTabI : this._minTabI;
		/* do not allow to set focus to same old-focused element */
		if(!elem || elem == this._lastTab)
			elem = this._anyTab1;
		/* do not allow to set focus to same old-focused element */
		if(elem == this._lastTab)
			elem = this._anyTab2;
		if(!elem)
			return;
		if(delay)
		{
			$util._tab_foc = elem;
			window.setTimeout('try{$util._tab_foc.focus();}catch(i){}',1);
		}
		else
			try{elem.focus();}catch(elem){}
	},
	/* part of modal functionality */
	/* check if elem can get focus on TAB key */
	_canTab:function(elem)
	{
		if(parseInt(elem.tabIndex) > 0)
			return true;
		for(var i = 0; i < 5; i++)
			if(elem.nodeName == this._tags[i])
				return true;
		return false;
	},
	/* part of modal functionality */
	/* ensure that elem is child, if not then set focus to a child */
	_setTabElem:function(elem, foc)
	{
		var tab = this._lastTab;
		/* check that action was triggered by TAB key */
		if((!tab && !foc) || this._lastTabTime + 100 < (new Date()).getTime())
			return;
		/* check elem is not child of dialog */
		if(this._isChild(this._targetElement, elem))
			return;
		/* at this point we should set focus to child of dialog */
		/* to establish tab-loop we need to know final references to first and last tabbable elements */
		/* TAB+Shift direction */
		if(this._shiftKey)
		{
			/* set first tabbable element */
			if(!this._minTab2)
				this._minTab2 = tab;
		}
		/* TAB without Shift direction */
		else if(!this._maxTab2)
			/* set last tabbable element */
			this._maxTab2 = tab;
		this._tabFoc(null, foc);
	},
	/* part of modal functionality */
	/* is called when element deactivated (used to stop tabbing while modal under IE) */
	_onDeact:function(e)
	{
		var to = e ? e.rawEvent : null;
		if(!to)
			return;
		this._deAct = true;
		/* event.toElement pointst to element which will be activated (IE only) */
		this._setTabElem(to.toElement);//this._setTabElem(to.toElement, e);
	},
	/* part of modal functionality */
	/* is called when element gets focus (used to stop tabbing while modal under Mozilla) */
	_onFocus:function(e)
	{
		var elem = (e && !this._deAct) ? e.target : null;
		if(!elem)
			return;
		this._setTabElem(elem, true);
		this._lastTab = null;
	},
	/* part of modal functionality */
	/* is called when element get keydown (used to stop tabbing while modal) */
	_onKey:function(e)
	{
		var div = this._targetElement, elem = e ? e.target : null;
		if(!elem || e.keyCode != 9)
			return;
		this._shiftKey = e.shiftKey;
		this._lastTabTime = (new Date()).getTime();
		if(!this._isChild(div, elem))
			return;
		if(!this._canTab(elem))
		{
			this._tabFoc(e);
			return;
		}
		this._lastTab = null;
		if(e.shiftKey)
		{
			/* tab is pressed on first tabbable element: move focus to last tabbable element */
			if(elem == this._minTab2)
			{
				this._tabFoc(e);
				return;
			}
			/* if elem is child of dialog, then memorize it: used by _onFocus/_onFoc/_onDeact to identify this._minTab2 */
			if(this._isChild(div, elem))
				this._lastTab = elem;
		}
		else
		{
			/* tab is pressed on last tabbable element: move focus to first tabbable element */
			if(elem == this._maxTab2)
			{
				this._tabFoc(e);
				return;
			}
			/* if elem is child of dialog, then memorize it: used by _onFocus/_onFoc/_onDeact to identify this._maxTab2 */
			if(this._isChild(div, elem))
				this._lastTab = elem;
		}
	},
	/* part of modal functionality */
	/* called when _stop gets focus: condition to shift focus to valid child in dialog */
	_onFoc:function(e)
	{
		if(this._me)
			this._me._setTabElem();
	},
	/* part of modal functionality */
	/* disable/restore tabIndex values for elements outside dialog */
	/* max: dialog maximized and not modal */
	/* doc: container of elements which tabs should be disabled */
	_fixTabs:function(on, max, doc)
	{
		var elem, i = -1, stop = this._stop, tabs = this._oldTabs, elem0 = this._targetElement;
		if(on == this._fixTabOn)
			return;
		this._fixTabOn = on;
		if(!on)
		{
			if(!tabs)
				return;
			for(var i = 0; i < tabs.length; i++)
				tabs[i].elem.tabIndex = tabs[i].i;
			if(stop)
				stop.style.display = 'none';
			return;
		}
		if(!tabs)
		{
			tabs = this._oldTabs = new Array();
			this._tags = ['INPUT','SELECT','TEXTAREA','BUTTON','A','IFRAME','AREA','DIV','EMBED','OBJECT'];
		}
		else
			Array.clear(tabs);
		if(!stop && !max)
		{
			this._stop = stop = document.createElement('INPUT');
			var style = stop.style;
			style.padding = style.border = style.width = style.height = '0px';
			style.position = 'absolute';
			stop.tabIndex = 1000;
			elem0.insertBefore(stop, elem0.firstChild);
			stop.onfocus = this._onFoc;
			stop._me = this;
		}
		if(stop)
			stop.style.display = '';
		if(!doc)
			doc = document;
		/* ti1: min tabIndex,  ti2: max tabIndex */
		var lenAll, lenMy, ti1 = 0, ti2 = 0, j = i = 0, tags = this._tags;
		/* find all active elements outside of dialog and remove their tabIndexes */
		/* also initialize _anyTab#, _first/lastTabI */
		for(var t = 0; t < tags.length; t++)
		{
			var elemsAll = doc.getElementsByTagName(tags[t]);
			if((lenAll = elemsAll ? elemsAll.length : 0) < 1)
				continue;
			var elemsMy = this._targetElement.getElementsByTagName(tags[t]);
			lenMy = elemsMy ? elemsMy.length : 0;
			while(lenAll-- > 0)
			{
				elem = elemsAll[lenAll];
				j = lenMy;
				while(j-- > 0) if(elem == elemsMy[j])
				{
					if(elem == stop || elem.disabled || elem.type == 'hidden')// || !elem.offsetWidth)
						break;
					var ti = elem.tabIndex;
					if(!ti)
						ti = 0;
					else if((ti = parseInt(ti)) < 0)
						break;
					if(t < 5 || ti > 0)
					{
						/* memorize any tabbable element */
						if(!this._anyTab1)
							this._anyTab1 = elem;
						/* memorize another tabbable element */
						/* that is required because browser fails to set focus() to same element */
						else if(!this._anyTab2)
							this._anyTab2 = elem;
						/* memorize tabbable element with min tabIndex */
						if(!this._minTabI)
						{
							this._minTabI = elem;
							ti1 = ti;
						}
						if(ti1 > ti) 
						{
							this._minTabI = elem;
							ti1 = ti;
						}
					}
					if(ti < 1)
						break;
					/* memorize tabbable element with max tabIndex */
					if(ti >= ti2)
					{
						ti2 = ti;
						this._maxTabI = elem;
					}
					break;
				}
				/* that is active element outside of dialog */
				if(j < 0)
				{
					/* memorize element and it old tabIndex */
					tabs[i++] = {elem:elem,i:elem.tabIndex};
					/* remove tabIndex */
					elem.tabIndex = -1;
				}
			}
		}
		/* validate that tabIndex of _stop is largest among other children */
		if(stop && ti2 >= stop.tabIndex)
			stop.tabIndex = ti2 + 1;
	},
	/* part of modal functionality */
	_modal:function(on)
	{
		if(!this._isModal)
			return;
		if(this._inModal == on)
			return;
		var style, elem = this._targetElement, lid = this._lid;
		if(!lid)
			lid = $util._modalLid;
		if(this._modalNest(lid, on))
			return;
		var css = this._owner._owner._modalCss;
		var elem2 = null;
		if(on && !lid)
		{
			this._lid = lid = document.createElement($util.IsIE ? 'IFRAME' : 'DIV');
			if(!$util._modalLid)
				$util._modalLid = lid;
			style = lid.style;
			style.position = 'absolute';
			if($util.IsIE)
			{
			    lid.frameBorder = 0; 
				lid.scrolling = 'no';
				lid.src = elem2 = 'javascript:new String("<html></html>")';	  	
			}
			else
				lid.innerHTML = '&nbsp;';
			if(css)
				lid.className = css;
			else
			{
				style.filter = 'alpha(opacity:30)';
				style.opacity = 0.3;
			}
			/* that does not work for IE */
			lid.unselectable = 'on';
			lid.tabIndex = -1;			
		}
		this._inModal = on;
		var div = elem.parentNode;
		this._fixTabs(on);
		style = lid.style;
		lid._first = on ? this : null;
		if(on)
		{
			div.insertBefore(lid, elem);
			if($util.IsIE)
			{
		        var doc = lid.contentWindow.document;
		        $util.addHandler(doc, 'mousemove', this._mouseMoveHandler);
                $util.addHandler(doc, 'mouseup', this._mouseUpHandler); 	
                if(this._control && this._control._resizer)
                {
                        
                    $util.addHandler(doc, 'mousemove', this._control._resizer._onMouseMoveFn);
                    $util.addHandler(doc, 'mouseup', this._control._resizer._onMouseUpFn); 	
                }
			}
			lid._z = this._doZ(lid, elem);
			style.display = '';
			style.visibility = 'visible';
			var pos = $util.getPosition(lid);
			style.marginLeft = -pos.x + 'px';
			style.marginTop = -pos.y + 'px';
			/* adjust width/height of lid on initialization (flag==2) */
			this._onScrollSize(2);
			this._lidStyle = style;
			/* mouse-click blocker-element (I can not find lid.contentWindow.document.body ??) */
			if(elem2)
			{
				elem2 = lid.contentWindow.document.createElement('DIV');
				lid.appendChild(elem2);
				/* can not apply _modalCss to elem2, because it is in a different window */
				var style2 = elem2.style;
				style2.width = style2.height = '100%';
				elem2.unselectable = 'on';
				elem2.tabIndex = -1;
				/* IE has nice feature: backgroundColor of IFRAME is always #ffffff and ignores any 'set' */
				/* so, to get around of that, and get value of background from css,- use a temporary div */
				if(css)
				{
					div = document.createElement('DIV');
					div.style.position = 'absolute';
					div.style.width = div.style.width = '1px';
					div.className = css;
					document.body.appendChild(div);
					css = $util.getStyleValue(null, 'backgroundColor', div);
					document.body.removeChild(div);
					if(css)
						style2.background = css;
				}
			}
		}
		else
		{
			style.display = 'none';
			style.visibility = 'hidden';
			style.marginLeft = style.marginTop = style.width = style.height = '0px';
			div.removeChild(lid);
			elem.style.zIndex = this._oldZ;
		}
	},
	/* set value of zIndex for element and lid-element used while modal dialog */
	/* initialize values of _oldZ and _newZ */
	_doZ:function(lid, elem, lidZ)
	{
		this._newZ = this._oldZ = $util.getStyleValue(null, 'zIndex', elem);
		if(!lidZ)
			lidZ = $util.toInt($util.getStyleValue(null, 'zIndex', lid), 0);
		if(lidZ < 10)
			lid.style.zIndex = lidZ = 99999;
		if($util.toInt(this._oldZ, 0) <= lidZ)
			elem.style.zIndex = this._newZ = lidZ + 1;
		return lidZ;
	},
	/* resolve situation when nested modal dialogs are used */
	/* if new dialog is added, then add it to lid._nest array (chain of modal dialogs) */
	/* if dialog is closed, then remove it from lid._nest array and activate previous modal dialog */
	/* if only one dialog remained, then delete lid._nest */
	/* return: false- continue default modal logic, true- skip default modal logic */
	_modalNest:function(lid, on)
	{
		var ctl = lid ? lid._first : null;
		if(!ctl)
			/* continue default logic for modal */
			return false;
		var nest = lid._nest;
		if(!nest && !on)
			/* continue default logic for modal */
			return false;
		/* create lid._nest with first modal dialog */
		if(!nest)
			lid._nest = nest = [ctl];
		var z, i = nest.length, elem = this._targetElement;
		if(on)
		{
			/* adjust zIndex of main element and lid */
			this._doZ(lid, elem, lid._z);
			/* append this to the lid._nest array */
			nest[i--] = this;
			z = this._oldZ;
		}
		else
		{
			/* remove/reset whole lid._nest array */
			if(--i < 2)
				lid._nest = null;
			/* remove last item from lid._nest array */
			else
				nest[i] = null;
			/* cut off last item from lid._nest array */
			nest.length = i--;
			z = nest[i]._newZ;
		}
		this._inModal = on;
		/* main element for previous dialog */
		elem = nest[i]._targetElement;
		/* adjust zIndex of main element for previous dialog */
		elem.style.zIndex = z;
		/* adjust tabs for this dialog */
		/* if dialog on, then remove tabs only for elements from previous dialog */
		/* because all other elements are already disabled */
		this._fixTabs(on, false, elem);
		/* skip default logic for modal */
		return true;
	},
	/* x: (value string) from WebDialogWindow.get_left() */
	/* y: (value string) from WebDialogWindow.getRight() */
	/* center: value true may come only from WebDialogWindow on initialization/unpin of move */
	/* modal: value true may come only from WebDialogWindow, means modal dialog */
	/* max: maximized */
	setAbsPos:function(x, y, center, scroll, modal, max)
	{
		var elem = this._targetElement;
		var style = elem.style;
		this._position0 = style.position;
		if(style.position != 'absolute')
			style.position = 'absolute';
		var point = $util.getPosition(elem);
		/* assume that is initial call from $IG.Header._moveable */
		if(!center && scroll && x === '')
		{
			x = point.x + 'px';
			y = point.y + 'px';
		}
		/* save initial values for canceled move */
		this._xFixed = this._leftOld = x;
		this._yFixed = this._topOld = y;
		/* relative/centered location */
		if(center || scroll || modal)
		{
			this._center = center;
			this._scroll = scroll;
			if(modal)
				this._modal(this._isModal = modal);
			else
				this._fixTabs(max, max);
			this._lsnr(scroll || modal, modal);
		}
		else if(max != null)
			this._fixTabs(max, max);
		if(center || (scroll && x && y))
		{
			var win = $util.getWinRect(), len = (x && !center) ? x.length : 0;
			/* size of browser */
			var w = win.width, h = win.height;
			/* if left is set, then use that value explicitly */
			if(len > 0)
			{
				var perc = x.indexOf('%') > 0;
				x = parseInt(x);
				/* convert percent into pixel value */
				if(perc)
					x = w * x / 100;
			}
			/* centered location */
			else if((x = (w - elem.offsetWidth) / 2) < 0)
				x = 0;
			len = (y && !center) ? y.length : 0;
			/* if top is set, then use that value explicitly */
			if(len > 0)
			{
				var perc = y.indexOf('%') > 0;
				y = parseInt(y);
				/* convert percent into pixel value */
				if(perc)
					y = Math.floor(h * y / 100);
			}
			/* centered location */
			else if((y = (h - elem.offsetHeight) / 2) < 0)
				y = 0;
			/* adjust for scroll */
			point.x = Math.floor(win.x + x);
			point.y = Math.floor(win.y + y);
		}
		/* validate if control really was moved where we wanted */
		if(this._shiftX == null)
		{
			style.left = point.x + 'px';
			style.top = point.y + 'px';
			var p = $util.getPosition(elem);
			/* adjust for shift (probably) created by absolute/relative position of container */
			this._shiftX = p.x - point.x;
			this._shiftY = p.y - point.y;
		}

	    this._control.set_left((point.x -= this._shiftX) + 'px', false);
	    this._control.set_top((point.y -= this._shiftY) + 'px', false);

		/* initial location on start-move (to calculate relative shift for new location) */
		this._xOld = point.x;
		this._yOld = point.y;
	},

	restore:function(max)
	{
		if(this._isModal)
			this._modal(false);
		else
			this._fixTabs(max, max);
		this._lsnr(false, max && this._isModal);
	},

	/* x: new left (string) */
	/* y: new top (string) */
	/* cancel: event canceled new position */
	setLocation:function(x, y, cancel)
	{
		var style = this._targetElement.style;
		var ctl = this._control;
		style.left = x;
		style.top = y;
		if(ctl && ctl.set_left)
		{
			/* restore initial values of left/top which were set on start of resizing */
			if(cancel)
			{
				x = this._leftOld;
				y = this._topOld;
			}
			/* relative/centered location */
			else if(this._center)
			{
				var win = $util.getWinRect();
				/* adjust new location for scrolls */
				x = (parseInt(x) - win.x + this._shiftX) + 'px';
				y = (parseInt(y) - win.y + this._shiftY) + 'px';
			}
			/* notify owner about new location (to persist on next move and postback) */
			ctl.set_left(this._xFixed = x, true);
			ctl.set_top(this._yFixed = y, true);
		}
		if(cancel)
			style.position = this._position0;
	},

	_moveWindow:function(e)
	{
		var x = e.clientX, y = e.clientY;
		if(x < 0 || y < 0)
			return;
		x += this._xOld - this._mouseX;
		y += this._yOld - this._mouseY;
		/* 0-evtName, 1-'Move'EventArgs, 2(0)-browserEvent, 3(1)-postbackAction, 4(2)-new x, 5(3)-new y, 6(4)-old x, 7(5)-old y */
		var args = this._raiseClientEvent('Moving', 'Move', e, null, x, y, this._xOld, this._yOld);
			
		if(args && args.get_cancel())
			return;
			
		this._showIframe(true);
		this._center = false;
		this.setLocation((this._x = x) + 'px', (this._y = y) + 'px');
	},

	dispose:function()
	{
		this._lsnr(false, false);
		$IG.DialogMoveBehavior.callBaseMethod(this, 'dispose');
	}
}
$IG.DialogMoveBehavior.registerClass('Infragistics.Web.UI.DialogMoveBehavior', $IG.MoveBehavior);

$IG.WindowStateCancelEventArgs = function()
{
	$IG.WindowStateCancelEventArgs.initializeBase(this);
}
$IG.WindowStateCancelEventArgs.prototype =
{
	get_currentWindowState: function()
	{
	    ///<summary>
	    /// Gets the current window state of the dialog.
	    ///</summary>
	    return this._props[2];
	},
	
	get_newWindowState: function()
	{
	    ///<summary>
	    /// Gets the new window state of the dialog. 
	    ///</summary>
	    return this._props[3];
	}
}
$IG.WindowStateCancelEventArgs.registerClass('Infragistics.Web.UI.WindowStateCancelEventArgs', $IG.CancelEventArgs);

if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();