Ajax.Application.Event = function(){};
		
Object.extend(Object.extend(Ajax.Application.Event.prototype, Ajax.Application.Base.prototype), EventDispatcher.prototype);

Ajax.Service = {};
	
Ajax.Service.Base = function(){};

Ajax.Service.History = function(){};
	
Object.extend(Object.extend(Ajax.Service.Base.prototype, Ajax.Application.Event.prototype),
					{
						sendRequest : function(dto, cb){
							
							this.dispatchEvent("request", dto);
							
							Ajax.Application.Event.prototype.sendRequest.apply(this, [dto, cb]);
						
						},
						receiveRequest : function(cb, eAja){
							
							this.dispatchEvent("response", eAja);
							
							Ajax.Application.Event.prototype.receiveRequest.apply(this, [cb, eAja]);
						
						}
						
					}
				);


Object.extend(Object.extend(Ajax.Service.History.prototype, Ajax.Service.Base.prototype),
			{
				
					buildInterface : function(obj){
						
						this.historyArr = [];
						this.historyIndex = undefined;
						
						
						this.container = $(obj);
						this.historyFrame = this.container.down("iframe");
						this.form = this.container.down("form");
					
					},
					attachListener : function(){
						
						Event.observe(this.historyFrame, "load", this.reloadHandle);
											
					},
					createListener : function(){
														
						this.reloadHandle = this.handleReload.bindAsEventListener(this);
					
					},
					handleReload : function(e){
									
						var index = this.getHistoryIndex();
						
						var obj = this.historyArr[index];
						
						if(!obj)
							return true;
						
						this.historyIndex = index+1;
						
						this.dispatchEvent("reload", [obj, index]);
						this.dispatchEvent(obj.type, obj.arg);
						
					
					},
					
					getHistoryIndex : function(){
						
						return parseInt(this.getIndex(this.historyFrame.contentWindow.location.toString()));
						
					},
					getIndex : function(str){
						
						return str.replace(/.*index=/gi, "");
					
					},
					getQuery : function(str){
						
						return str.replace(/[^?]+?/gi, "");
					
					},
					
					registerRequest : function(type, eAja){
					
						if(this.historyIndex && this.historyIndex < this.historyArr.length)
							this.historyArr.length = this.historyIndex;
							
						this.form.index.value = this.historyArr.length;    
						
						this.historyArr.push({ type : type, arg : eAja});
														
						this.form.submit();
					
					}
			
			
			
			}
		);


Ajax.Service.Bookmark = Class.create();
		
Object.extend(Object.extend(Ajax.Service.Bookmark.prototype, Ajax.Service.Base.prototype),
				{
					getBookmarkHash : function(){
						
						if(!this.bookmarkHash)
							return this.bookmarkHash = $H();
						
						return this.bookmarkHash;
					
					
					},
					getOperation : function(name){
					
						return this.getBookmarkHash()[name];
													
					},
					setOperation : function(name, arg){
					
						this.getBookmarkHash()[name] = arg;
					
					},
					getOperationKeys : function(){
						
						return this.getBookmarkHash().keys();
					
					}
				
				}
			);

Ajax.Service.BookmarkHistory = Class.create();


Object.extend(Object.extend(Ajax.Service.BookmarkHistory.prototype, Ajax.Service.History.prototype),
				{
					
					buildInterface : function(){
					
						Ajax.Service.History.prototype.buildInterface.apply(this, arguments);
						this.historyRequestList = [];				
					
					},					
					sendRequest : function(dto, cb){
					
						Ajax.Application.Base.prototype.sendRequest.apply(this, arguments);
						
						this.registerRequest(dto, cb);
									
					},
					registerRequest : function(dto){
					
						this.historyRequestList.push(dto);					
					
					},
					registerResponse : function(){
					
						Ajax.Service.History.prototype.registerRequest.apply(this, arguments);
										
					},
					handleReload : function(e){
					
						var index = this.getHistoryIndex();
						
						var obj = this.getResponseHistory(index);
						
						if(!obj)
							return true;
						
						var reqObj = this.getRequestHistory(index);
						
						this.historyIndex = index+1;
						
						this.dispatchEvent("request", reqObj);
						this.dispatchEvent("response", obj);
						this.dispatchEvent(obj.type, obj.arg);					
					
					},
					getResponseHistory : function(num){
						
						return this.historyArr[num];
					
					},
					getRequestHistory : function(num){
					
						return this.historyRequestList[num];
					
					},
					getBookmarkHash : function(){
						
						if(!this.bookmarkHash)
							return this.bookmarkHash = $H();
						
						return this.bookmarkHash;
					
					
					},
					getOperation : function(name){
					
						return this.getBookmarkHash()[name];
													
					},
					setOperation : function(name, arg){
					
						this.getBookmarkHash()[name] = arg;
					
					},
					getOperationKeys : function(){
						
						return this.getBookmarkHash().keys();
					
					}
				
				}
			);

