/*
* ContentLoader
* ----------------------
* @last_modified: 2007-12-13
* @author: Filatov Dmitry <alpha@design.ru>
*/

function ContentLoader() {

	ContentLoader.baseConstructor.call(this);

	this.aItems = [];
	this.sKeyCurrent = '';

}

ContentLoader.ITEM_STATE_LOADING = 0;
ContentLoader.ITEM_STATE_LOADED  = 1;

ContentLoader.EVENT_TYPE_ON_LOAD       = 'onload';

ContentLoader.inheritFrom(
	Observable,
	{

		getKey : function(aData) {

			var sResult = '';

			aData.foreach(
				function(sKey, sData) {
					sResult += sKey + sData;
				}
				);

			return sResult;

		},

		load : function(
			sUrl,
			aParams
			) {

			var sKey = sUrl + this.getKey(aParams);

			if(this.aItems[sKey]) {

				if(this.aItems[sKey].iState == ContentLoader.ITEM_STATE_LOADED) {
					return this.notify(
						ContentLoader.EVENT_TYPE_ON_LOAD,
						this.aItems[sKey].sContent
						);
				}
			
			}

			this.aItems[sKey] = {
				iState   : ContentLoader.ITEM_STATE_LOADING,
				sContent : ''
				};

			this.sKeyCurrent = sKey;

			var
				oThis = this,
				oAjaxHelper = new Common.Ajax.Helper(
					sUrl,
					function(oResponse) {

						oThis.onLoad(
							oResponse,
							sKey
							);

					},
					null,
					aParams
					)
				;

			oAjaxHelper.send();

		},

		onLoad : function(
			oResponse,
			sKey
			) {

			this.aItems[sKey].sContent = oResponse.responseText;
			this.aItems[sKey].iState = ContentLoader.ITEM_STATE_LOADED;

			if(sKey != this.sKeyCurrent) {
				return;
			}

			return this.notify(
				ContentLoader.EVENT_TYPE_ON_LOAD,
				this.aItems[sKey].sContent
				);

		}

	}
	
);