if(window.RG === undefined) window.RG = {};

RG.initContextHelp = function(){
	RG.ContextHelp = function(config){
		config = config || {};
		Ext.apply(this, config);
		if(!RG.ContextHelp.ref)
			RG.ContextHelp.ref = {};
		
		// this.addEvents(...);
		NotificationCenter.addObserver(this, "didLoadHelp", "ContextHelpDidLoad");
	}

	RG.ContextHelp.showHelp = function(file, config){
		if(!RG.ContextHelp._instance)
			RG.ContextHelp._instance = new RG.ContextHelp();
		RG.ContextHelp._instance.showHelp(file, config);
	}
	
	RG.ContextHelp.setConfig = function(config){
		if(!RG.ContextHelp._instance)
			RG.ContextHelp._instance = new RG.ContextHelp();
		config = config || {};
		Ext.apply(RG.ContextHelp._instance, config);
	}

	RG.ContextHelp.setDefaults = function(config){
		if(config) for(name in config){
			RG.ContextHelp.prototype[name] = config[name];
		}
	}
	
	RG.ContextHelp.prototype = {
		title: "Context Help",
		width: 360,
		helpDirectory: "help",
		container: null, // if static
		headerCls: "rg-context-help-header",
		bodyCls: "rg-context-help",
		errorCls: "rg-context-help-error",
		align: "c-c",
		
		getPanel: function() {
			if(!this.panel){
				if(this.container){
					this.panel = new Ext.Panel({
						plain:true,
						footer:false,
						autoHeight: true,
						border:false
					});
					this.panel.render(this.container);
				}else{
					this.panel = new Ext.Window({
						autoCreate:true,
						title: this.title,
						resizable:true,
						constrain:true,
						constrainHeader:true,
						minimizable:false,
						maximizable:false,
						stateful:false,
						modal:false,
						shim:false, // true does not work on OSX FF
						buttonAlign:"right",
						width: this.width,
						height:"auto",
						autoHeight:true,
						plain:true,
						footer:true,
						closable:true,
						closeAction:"hide"
					//	close: function(){ this.handleClose(); }
					});
					this.panel.render(document.body);
					this.panel.header.addClass(this.headerCls);
				}
			}
			return this.panel;
		},
			
		showHelp: function(file, config){
			// config
			// title - window title, default: 'Context Help'
			// width - window width, default 360
			// align - window align in container, default: "c-c", center to center
			// x, y - absolute position if both specified
			// el - align at the bottom of element el, default none
			// offX, offY - offset alignment by offX and/or offY 
			var p = this.getPanel();
			if(this.bodyEl){
				this.bodyEl.remove();
				this.bodyEl = null;
			}
			
			var conf = config || {}
			this.helpTitle = conf.title || this.title;
			if(this.helpTitle)
				p.setTitle(this.helpTitle);

			if(RG.ContextHelp.ref && RG.ContextHelp.ref[file]){
				this.bodyEl = p.body.createChild({html: RG.ContextHelp.ref[file]});
				if(this.bodyCls)
					this.bodyEl.addClass(bodyCls);
			}else
				this.loadContextHelp(file);
			
			var align = conf.align || this.align;
			var xy = p.el.getAlignToXY(this.panel.container, align);
			var width = conf.width || this.width;
			if(width)
				p.setWidth(width);
			if(conf.x != null && conf.y != null){
				p.setPosition(conf.x, conf.y);
			}else if(conf.el){
				var b = conf.el.getBox();
				xy[1] = b.y + b.height;
				var offX = conf.offX || this.offX;
				var offY = conf.offY || this.offY;
				if(offX)
					xy[0] += offX;
				if(offY)
					xy[1] += offY;
				p.setPosition(xy[0], xy[1]);
			}else{
				var offX = conf.offX || this.offX;
				var offY = conf.offY || this.offY;
				if(offX)
					xy[0] += offX;
				if(offY)
					xy[1] += offY;
				p.setPosition(xy[0], xy[1]);
			}
			p.show();
			return this;
		},

		hideHelp: function(){
			if(this.panel)
				this.panel.hide();
			return this;
		},
		
		renderHelp: function(content){
			var p = this.getPanel();
			if(this.bodyEl)
				this.bodyEl.remove();
			this.bodyEl = p.body.createChild({html: content});
			if(this.helpTitle)
				p.setTitle(this.helpTitle);
			if(this.bodyCls)
				this.bodyEl.addClass(this.bodyCls);
		},
		
		getContextHelp: function(name){
			if(RG.ContextHelp.ref && RG.ContextHelp.ref[name])
				this.renderHelp(RG.ContextHelp.ref[name]);
			else
				this.loadContextHelp(name);
		},
		
		loadContextHelp: function(name){
			var rq = Utils.getXMLHttpRequest();
			var obj = new Object();
			obj.sender = this;
			if(rq) 
				try{
					rq.onreadystatechange = function() {
						if(rq.readyState == 4){
							if(rq.status == 200){ // HTTP OK
								obj.text = rq.responseText;
								obj.success = 0;
								obj.name = name;
							}else{
								obj.text = "<h4>Context Help error:</h4><p>server returned status " + rq.status + "</p>";
								obj.success = 1;
								obj.name = name;
							}
							Notification.postNotification("ContextHelpDidLoad", this, obj);
						}
					}
					var location = this.helpDirectory + "/" + name + ".html";
					rq.open('GET', location, true);
					rq.send(null);
				}catch(e){
					obj.text = "<h4>Context Help request error:</h4><p>" + e + "</p>";
					obj.success = 2;
					obj.name = name;
					Notification.postNotification("ContextHelpDidLoad", this, obj);
				}
			else{
				obj.text = "<h4>Context Help request error:</h4><p>cannot create request</p>";
				obj.success = 3;
				obj.name = name;
				Notification.postNotification("ContextHelpDidLoad", this, obj);
			}
		},
		
		didLoadHelp: function(note){
			if(note.userInfo.sender == this){
				switch(note.userInfo.success){
					case 0:
						this.renderHelp(note.userInfo.text);
						break;
					default:
						this.renderHelp("Help not available");
						if(this.errorCls)
							this.bodyEl.addClass(this.errorCls);
						break;
				}
			}
		}
		
	}
}
