﻿Type.registerNamespace("Telerik.Web.UI");

Telerik.Web.UI.ChangeLog = function ()
{
	this._opCodeInsert = 1;
	this._opCodeDelete = 2;
	this._opCodeClear = 3;
	this._opCodePropertyChanged = 4;
	
	this._logEntries = null;
}

Telerik.Web.UI.ChangeLog.prototype =
{
	initialize : function ()
	{
		this._logEntries = [];
		this._serializedEntries = null;
	},
	
	logInsert : function (item)
	{
		var logEntry = {};
		logEntry.Type = this._opCodeInsert;
		logEntry.Index = item._getHierarchicalIndex();
		logEntry.Data = item._getData();
		
		Array.add(this._logEntries, logEntry);
	},
	
	logDelete : function (item)
	{
		var logEntry = {};
		logEntry.Type = this._opCodeDelete;
		logEntry.Index = item._getHierarchicalIndex();
		
		Array.add(this._logEntries, logEntry);
	},
	
	logClear : function (item)
	{
		var logEntry = {};
		logEntry.Type = this._opCodeClear;
		
		if (item._getHierarchicalIndex)
		{
			logEntry.Index = item._getHierarchicalIndex();
		}
		
		Array.add(this._logEntries, logEntry);
	},
	
	logPropertyChanged : function (item, propertyName, propertyValue)
	{
		var logEntry = {};
		logEntry.Type = this._opCodePropertyChanged;
		logEntry.Index = item._getHierarchicalIndex();
		logEntry.Data = {};
		logEntry.Data[propertyName] = propertyValue;
		
		Array.add(this._logEntries, logEntry);
	},
	
	serialize : function ()
	{	
		if(this._logEntries.length == 0)
		{
			if(this._serializedEntries == null)
			{
				return "[]";
			}

			return this._serializedEntries;
		}
	
		var newEntries = Sys.Serialization.JavaScriptSerializer.serialize(this._logEntries);		
	
		if(this._serializedEntries == null)
		{
			this._serializedEntries = newEntries;
		}
		else
		{
			this._serializedEntries = this._serializedEntries.substring(0, this._serializedEntries.length - 1) + ',' + newEntries.substring(1);
		}
		
		this._logEntries = [];
		
		return this._serializedEntries;
	}	
}

Telerik.Web.UI.ChangeLog.registerClass("Telerik.Web.UI.ChangeLog");


if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();