//History Service
var HistoryService = Class.create();
Ajax.Application.Event = function(){};
Object.extend(Object.extend(Ajax.Application.Event.prototype, Ajax.Application.Base.prototype), EventDispatcher.prototype);
Object.extend(Object.extend(HistoryService.prototype, Ajax.Application.Event.prototype),
    {
        initialize : function(url, container){
            this.url = url;
			this.buildInterface(container);
            this.createListener();
			this.attachListener();
        },
		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);
		    //Custom regions
            this.loadStoreCompleteHandle = this._loadStoreComplete.bind(this);
        },
		handleReload : function(e){
			var index = parseInt(this.getHistoryIndex());
			var obj = this.historyArr[index];
			var ind = document.getElementById('historyindex');
			if(!obj && ind && ind.value.length > 0)
				 window.location.reload(true);
			else if (!obj)
			    return true;
		    else{
			    this.historyIndex = index+1;
			    this.dispatchEvent("reload", [obj, index]);
			    this.dispatchEvent(obj.type, obj.arg);
			}
		},
		getHistoryIndex : function(){
			return this.getIndex(this.historyFrame.contentWindow.location.toString());
		},
		getIndex : function(str){
			return str.replace(/.*historyindex=/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;
			
			var obj = ((eAja.responseXML.text) ? eAja.responseXML.text.evalJSON() : eAja.responseXML.lastChild.textContent.evalJSON());
			if (obj && obj.DynamicScreen)
			    this.historyFrame.contentWindow.document.title = obj.DynamicScreen.Title;
			
	        this.form.historyindex.value = this.historyArr.length;
	        this.historyArr.push({ type : type, arg : eAja});
	        this.form.submit();
		},
		//Custom Functions
		loadStore : function(params){
            this.sendRequest(params, this.loadStoreCompleteHandle);
        },
        _loadStoreComplete: function(eAja){
            var type = "LoadStore";
			this.registerRequest(type, eAja);
        }
    }
);
var ServiceListener = Class.create();
Object.extend(ServiceListener.prototype,
    {
        buildInterface : function(obj){
            this.container = $(obj);
        },
        createListener : function(){
            this.receiveUpdateHandle = this.receiveUpdate.bind(this);
        },
        receiveUpdate : function(eAja){
            try
            {
                if (eAja && eAja.responseXML == null)
                    var obj = eAja.responseText.evalJSON();
                else{
                    var obj = ((eAja.responseXML.text) ? eAja.responseXML.text.evalJSON() : eAja.responseXML.lastChild.textContent.evalJSON());
                }
                this.update(obj);
            }
            catch (err)
            {
                alert(err.message);
            }
        },
        update : function(obj){
        }
    }
);
var ShoppingArea = Class.create();
Object.extend(Object.extend(ShoppingArea.prototype, ServiceListener.prototype),
    { 
        initialize : function(obj){
            
            this.buildInterface(obj);
            this.createListener(obj);                                
        },
        update : function(json){                        
            __loadStoreComplete(json);
        }    
    }
);
var myService = new HistoryService('Shopping.asmx/CreateShoppingHTML','historyElement');
var myShop = new ShoppingArea();
myService.addEventListener("LoadStore", myShop.receiveUpdateHandle);
