

/*$IG.GridColumn.prototype._get_sortIndicator = function()
{
	if(typeof(this._sortIndicator) != "undefined")
		return this._sortIndicator;
		
	for(var i=0; i<this._headerElement.childNodes.length; i++)
	{
		var childNode = this._headerElement.childNodes[i];
		if(childNode.getAttribute && childNode.getAttribute("img"))
		{
			this._sortIndicator = childNode;
			break;
		}
	}
	
	return this._sortIndicator;
}*/

$IG.Sorting = function(obj, objProps, control)
{
	/// <summary>
    /// Sorting behavior object of the grid. 
    /// </summary>
	$IG.Sorting.initializeBase(this, [obj, objProps, control]);

	this._sortedColumns=[];

	var sortedCols = this._get_value($IG.SortingProps.SortedColumns);
	if(sortedCols)
		for(var i = 0; i<sortedCols.length; i++)
		{
			var column = this._owner._columns.get_columnFromKey(sortedCols[i][0]);
			this._sortedColumns[this._sortedColumns.length] = column;
			column._sortDirection = sortedCols[i][1];
		}

	this._sortingMode = this._get_clientOnlyValue("sm");

	this._header = control._elements["header"];
}

$IG.Sorting.prototype =
{
	__getSortingColumnSettingByKey: function(key)
	{
		return this._sortingColumnSettings._getObjectByAdr(key);
	},
	get_sortingMode: function()
	{
		///<summary>
		///Returns the sorting mode. Corresponds to the server's SortingMode enumeration: 0 - Single, 1 - Multi.
		///</summary>
		return this._sortingMode;
	},

	dispose: function()
	{
		if (!this._grid)
			return;
		this._grid._removeElementEventHandler(this._header, "click", this.__headerMouseClickEventHandler);

		/*for (var i = 0; i < this._owner._columns._items.length; i++)
		{
			var column = this._owner._columns._items[i];
			//if (column._parkedSortIndicator)
			//	delete column._parkedSortIndicator;
			if (column._sortIndicator)
				delete column._sortIndicator;
		}*/
		this.__headerMouseClickEventHandler = null;
		/*
		DK 10 Oct 2008
		This was in a second dispose method that was at the bottom of this prototype, meaning this
		implementation up here was never run.  Removed that one, brought up code to here
		*/
		if (this._header)
			$clearHandlers(this._header);
		this._header = null;

		delete this._sortedColumns;
		this._sortingColumnSettings.dispose();
		$IG.Sorting.callBaseMethod(this, "dispose");
	},

	get_sortedColumns: function()
	{
		/*
		DK 30 Sept 2008
		Bug 8198 : No methods exposed on the client in the Sorting behavior to get the sorted columns.
		Added getter
		*/
		/// <summary>
		/// Returns an array with the columns that are currently sorted.
		/// </summary>
		return this._sortedColumns;
	},

	_onMouseClick: function(evnt)
	{
		var trgt = evnt.target;
		if (trgt.tagName == "IMG")
			trgt = trgt.parentNode;
		if (trgt.tagName == "TH")
		{
			var index = trgt.getAttribute("idx");
			var fieldKey = trgt.getAttribute("key");
			var column = (fieldKey ? this._owner._columns.get_columnFromKey(fieldKey) : this._owner._columns._items[parseInt(index, 10)]);
			if (column)
			{
				var sortingColumnSetting = this.__getSortingColumnSettingByKey(fieldKey);
				if (sortingColumnSetting != null && !sortingColumnSetting.get_sortable())
					return;

				var sortDirection = this.getSortDirection(column);
				sortDirection++;
				if (sortDirection > 2) sortDirection = 1;
				this._sortColumnInternal(column, sortDirection, !(this.get_sortingMode() == 1 && evnt.ctrlKey));
			}
		}
	},

	clear: function()
	{
		///<summary>
		///Clears the sorted columns collection and removes the sort indicators 
		///off the headers. The order of the rows remains unchanged.
		///</summary>
		
		// First parameter is assumed to be a no post back flag.
		// Not in the param list so it is not documented.
		var noPost = arguments[0];

		var i = this._sortedColumns.length - 1;
		while (i >= 0)
			this._removeSortedColumn(this._sortedColumns[i--]);

		this._owner._actionList.add_transaction(new $IG.SortingAction("Clear", this.get_name(), this));
		if (!noPost)
		{
			if (!this._owner._enableAjax)
				this._owner._postAction(1);
			else
			{
				var eventArgs = new $IG.SortingEventArgs(this, null, 0, true);
				eventArgs._props[1] = 2;
				this._owner._postAction(eventArgs);
			}
		}
	},

	_sortColumnInternal: function(column, sortDirection, clear)
	{
		var eventArgs = new $IG.SortingEventArgs(this, column, sortDirection, clear);
		this._owner._raiseSenderClientEventStart(this, this._clientEvents["ColumnSorting"], eventArgs);
		if (!eventArgs.get_cancel())
		{
			column = eventArgs.get_column();
			sortDirection = eventArgs.get_sortDirection();
			clear = eventArgs.get_clear();
			if (column != null && sortDirection > 0 && sortDirection < 3)
			{
				this.sortColumn(column, sortDirection, clear, true);
				this._owner._raiseClientEventEnd(eventArgs);
			}
		}
	},
	
	applySort: function()
	{
		///<summary>
		///Post back to the server to apply sorted columns.
		///</summary>
		if (!this._owner._enableAjax)
			this._owner._postAction(1);
		else
		{
			var eventArgs = new $IG.SortingEventArgs(this, null, 0, false);
			eventArgs._props[1] = 2;
			this._owner._postAction(eventArgs);
		}
	},
	
	addSortColumn: function(column, sortDirection)
	{
		///<summary>
		///Add a column to be sorted without posting back to the server.
		///</summary>
		///<param name="column">
		///Reference to the column that needs to be sorted.
		///</param>
		///<param name="sortDirection" type="Integer">
		///Sorting direction. Corresponds to the server's SortDirection enumeration: 0 - None, 1 - Ascending, 2 - Descending.
		///</param>
		this.sortColumn(column, sortDirection, false, true);
	},

	sortColumn: function(column, sortDirection, clear)
	{
		///<summary>
		///Sorts a column.
		///</summary>
		///<param name="column">
		///Reference to the column that needs to be sorted.
		///</param>
		///<param name="sortDirection" type="Integer">
		///Sorting direction. Corresponds to the server's SortDirection enumeration: 0 - None, 1 - Ascending, 2 - Descending.
		///</param>
		///<param name="clear" type="Boolean">
		///Determines if previously sorted columns must be cleared.
		///</param>

		// Fourth parameter is assumed to be a no post back flag.
		// Not in the param list so it is not documented.
		var noPost = arguments[3];

		var colSortDir = this.getSortDirection(column);

		if (typeof (sortDirection) == "undefined")
		{
			sortDirection = colSortDir + 1;
			if (sortDirection > 2)
				sortDirection = 1;
		}

		if (sortDirection < 1 || sortDirection > 2)
			return;

		if (clear)
			this.clear(true);

		/*	OK 9/25/2008 8049 - the sorting image was changing before the sorted column comes
		*	back from the server.  I am leaving this code here, in case we want to go back to 
		*	changing the image right away and add some other visual indicator that sorting is
		*	in progress
		*
		var si = column._get_sortIndicator();
		if (!si)
		{
		if (column._parkedSortIndicator)
		{
		si = column._parkedSortIndicator;
		delete column._parkedSortIndicator;
		column._headerElement.appendChild(si);
		}
		else
		{
		si = document.createElement("IMG");
		column._headerElement.appendChild(si);
		si.border = 0;
		si.style.marginLeft = "5px";
		}
		column._sortIndicator = si;
		}
		if (sortDirection == 2)
		si.src = this._descImage;
		else
		si.src = this._ascImage;
		*/
		if (clear || colSortDir == 0)
			this._sortedColumns[this._sortedColumns.length] = column;
		column._sortDirection = sortDirection;
		this._owner._actionList.add_transaction(new $IG.SortingAction("Sort", this.get_name(), this, this._sortedColumns, column.get_idPair()));
		if (!noPost)
		{
			if (!this._owner._enableAjax)
				this._owner._postAction(1);
			else
			{
				var eventArgs = new $IG.SortingEventArgs(this, column, sortDirection, clear);
				eventArgs._props[1] = 2;
				this._owner._postAction(eventArgs);
			}
		}
	},

	_removeSortedColumn: function(column)
	{
		/* SJZ - 9/30/08  8223 - Since we're changing this on the server
		* don't remove the image for the sort indicator This is related to Olga's change for 8049
		* var si = column._get_sortIndicator();
		* if (!si)
		*	return;
		* si.parentNode.removeChild(si); 
		* column._parkedSortIndicator = si;
		*/
		column._sortIndicator = null;
		column._sortDirection = 0;
		var i = this._sortedColumns.length - 1;
		while (i >= 0)
		{
			if (this._sortedColumns[i] == column)
				break;
			i--;
		}
		if (i == this._sortedColumns.length - 1)
			this._sortedColumns.pop();
		else if (i == 0)
			this._sortedColumns.shift();
		else if (i > 0)
			this._sortedColumns = this._sortedColumns.slice(0, i - 1).concat(this._sortedColumns.slice(i + 1, this._sortedColumns.length - 1));
	},

	getSortDirection: function(column)
	{
		///<summary>
		///Returns a columns sort direction. The method can be used to check if the column is sorted.
		///</summary>
		///<param name="column" type="Integer">A reference to the column to get the sort direction for. 
		///The returned value corresponds to the servr's SortDirection enumeration: 0 - None, 1 - Ascending, 2 - Descending.</param>
		if (typeof (column._sortDirection) == "undefined")
			return 0;
		return column._sortDirection;
	},


	/********************************OVERRIDES*************************************/
	_initializeComplete: function()
	{
		var isColumnSorted = this._get_clientOnlyValue("ics");

		if (this._clientEvents["ColumnSorted"] && isColumnSorted)
		{
			this.__raiseClientEvent("ColumnSorted");
		}
		this.__headerMouseClickEventHandler = Function.createDelegate(this, this._onMouseClick);

		if (this._header)
			this._grid._addElementEventHandler(this._header, "click", this.__headerMouseClickEventHandler);
	},

	_createCollections: function(collectionsManager)
	{
		this._sortingColumnSettings = collectionsManager.register_collection(0, $IG.SortingColumnSettings);
		var collectionItems = collectionsManager._collections[0];
		for (var columnKey in collectionItems)
			this._sortingColumnSettings._addObject($IG.SortingColumnSetting, null, columnKey);
	}
	/********************************END OVERRIDES*********************************/
}

$IG.Sorting.registerClass('Infragistics.Web.UI.Sorting', $IG.GridBehavior);

/******************************************SORTING PROPS ENUM************************************/
$IG.SortingProps = new function()
{
    var count = $IG.GridBehaviorProps.Count;
	this.SortedColumns = [count++, 0];
    this.Count = count;
};
/******************************************END SORTING PROPS ENUM************************************/

/******************************************SORTING ACTION************************************/
$IG.SortingAction = function(type, ownerName, object, value, tag)
{
	$IG.SortingAction.initializeBase(this, [type, ownerName, object, value, tag]);
}

$IG.SortingAction.prototype =
{
	get_value:function()
	{
		var sortedCols = $IG.SortingAction.callBaseMethod(this, 'get_value');
		var serverSortCols = [];
		for(var colIndex in sortedCols)
		{
			var col = sortedCols[colIndex];
			serverSortCols[serverSortCols.length] = {id:col.get_idPair(), sortDirection:this._object.getSortDirection(col)};
		}
		return serverSortCols;
	}
}

$IG.SortingAction.registerClass('Infragistics.Web.UI.SortingAction', $IG.GridAction);
/******************************************END SORTING ACTION************************************/

/******************************************SORTING EVENT ARGS************************************/
$IG.SortingEventArgs = function(sorting, column, sortDirection, clear)
{
	/// <summary>
    /// Event arguments object passed into the ColumnSorting event handler.
    /// </summary>
    
	// First parameter of the event args object associated with the 
	// behavior must be a reference to the behavior itself. This is essencial
	// for proper async call back handling.
	
	$IG.SortingEventArgs.initializeBase(this, [sorting]);
	this._column = column;
	if(column != null)
		this._props[2] = column.get_idPair();
	this._props[3] = sortDirection;
	this._props[4] = clear;
}
$IG.SortingEventArgs.prototype =
{
	get_column:function()
	{
		return this._column;
	},
	set_column:function(value)
	{
		if(typeof(value.get_key) == "undefined")
			throw "First parameter must be of the type 'Infragistics.Web.UI.GridColumn'.";
		this._column = value;
		this._props[2] = value.get_key();
	},

	get_sortDirection:function()
	{
		return this._props[3];
	},
	set_sortDirection:function(value)
	{
		this._props[3] = value;
	},

	get_clear:function()
	{
		return this._props[4];
	},
	set_clear:function(value)
	{
		this._props[4] = value;
	}
}
$IG.SortingEventArgs.registerClass('Infragistics.Web.UI.SortingEventArgs', $IG.CancelBehaviorEventArgs);
/******************************************END SORTING EVENT ARGS************************************/


/************************************** IG.SortingColumnSettings ************************************/
$IG.SortingColumnSettings = function(control, clientStateManager, index, manager)
{
    /// <summary>
    /// A collection of SortingColumnSetting objects.
    /// </summary>
    $IG.SortingColumnSettings.initializeBase(this, [control, clientStateManager, index, manager]);
}
$IG.SortingColumnSettings.prototype = 
{
    getItemFromColumnKey:function(columnKey)
    {
        ///<summary>
        ///Returns the SortingColumnSetting with the specified column key
        ///</summary>
        var sortingSetting = this._getObjectByAdr(columnKey);
        
        if (!sortingSetting && this._control._grid.get_columns().get_columnFromKey(columnKey))
        {
             sortingSetting= this._control.__createSortingSetting();
             
             var clientState = sortingSetting._csm.get_clientState();
             
             this._addExistingObject( sortingSetting,  columnKey , clientState);
        }                 
        return sortingSetting;
    }
}
$IG.SortingColumnSettings.registerClass('Infragistics.Web.UI.SortingColumnSettings', $IG.ObjectCollection);

/************************************** IG.SortingColumnSetting ************************************/
$IG.SortingColumnSetting = function(adr, element, props, owner, csm)
{
	/// <summary>
    /// SortingColumnSetting object of the grid. 
    /// </summary>
	$IG.SortingColumnSetting.initializeBase(this, [adr, element, props, owner, csm]);
	
	this.__isDirty=false;
}
$IG.SortingColumnSetting.prototype =
{
    __set_property:function(propName, value, fireEvent)
    {        
        if (fireEvent)
        {
            /* we will be using this in the singleton, so that we can be notified if the singleton
            is modified so we can de-singleton it
            */
            if (this.onPropertyChanging)
            {
                this.onPropertyChanging(propName);
            }
        }
        this._set_value(propName,value);
    },
    dispose:function()
	{
		$IG.SortingColumnSetting.callBaseMethod(this,"dispose");
	},
//    get_columnKey:function()
//    {
//        ///<summary>
//        /// Returns the column key value from the SortingColumnSetting object.
//        ///</summary>
//        return this._get_value($IG.SortingColumnSettingProps.ColumnKey,"");
//    },
//    _set_columnKey:function(value)
//    {
//        this.__set_property($IG.SortingColumnSettingProps.ColumnKey,value,false);
//    },
    get_sortable:function()
    {
        ///<summary>
        /// Returns the Sortable value from the SortingColumnSetting object.
        ///</summary>
        return this._get_value($IG.SortingColumnSettingProps.Sortable,true);
    },
    set_sortable:function(value)
    {
        ///<summary>
        /// Sets the Sortable value from the SortingColumnSetting object.
        ///</summary>
        this.__set_property($IG.SortingColumnSettingProps.Sortable,value,true);
    },
    onPropertyChanging:function(propName)
    {    
    }
}
$IG.SortingColumnSetting.registerClass('Infragistics.Web.UI.SortingColumnSetting',  $IG.ColumnSetting);

/******************************************SortingColumnSettingProps PROPS ENUM************************************/
$IG.SortingColumnSettingProps = new function()
{
    var count           = $IG.ColumnSettingProps.Count;
    //this.ColumnKey      = [count++, ""];
    this.Sortable		= [count++, true];
    this.Count          = count;
};
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();