// BEGIN RESIZEPANEL SUBCLASS //
YAHOO.widget.ResizePanel = function(el, userConfig) {
    if (arguments.length > 0) {
        YAHOO.widget.ResizePanel.superclass.constructor.call(this, el, userConfig);
    }
}
YAHOO.widget.ResizePanel.CSS_PANEL_RESIZE = "yui-resizepanel";
YAHOO.widget.ResizePanel.CSS_RESIZE_HANDLE = "resizehandle";
YAHOO.extend(YAHOO.widget.ResizePanel, YAHOO.widget.Panel, {
    init: function(el, userConfig) {
        YAHOO.widget.ResizePanel.superclass.init.call(this, el);
        this.beforeInitEvent.fire(YAHOO.widget.ResizePanel);
        var Dom = YAHOO.util.Dom,
            Event = YAHOO.util.Event,
            oInnerElement = this.innerElement,
            oResizeHandle = document.createElement("DIV"),
            sResizeHandleId = this.id + "_resizehandle";
         oResizeHandle.id = sResizeHandleId;
         oResizeHandle.className = YAHOO.widget.ResizePanel.CSS_RESIZE_HANDLE;
        Dom.addClass(oInnerElement, YAHOO.widget.ResizePanel.CSS_PANEL_RESIZE);
        this.resizeHandle = oResizeHandle;
        function initResizeFunctionality() {
            var me = this,
                oHeader = this.header,
                oBody = this.body,
                oFooter = this.footer,
                nStartWidth,
                nStartHeight,
                aStartPos,
                nBodyBorderTopWidth,
                nBodyBorderBottomWidth,
                nBodyTopPadding,
                nBodyBottomPadding,
                nBodyOffset;
            oInnerElement.appendChild(oResizeHandle);
            this.ddResize = new YAHOO.util.DragDrop(sResizeHandleId, this.id);
            this.ddResize.setHandleElId(sResizeHandleId);
            this.ddResize.onMouseDown = function(e) {
                nStartWidth = oInnerElement.offsetWidth;
                nStartHeight = oInnerElement.offsetHeight;
                if (YAHOO.env.ua.ie && document.compatMode == "BackCompat") {
                    nBodyOffset = 0;
                } else {
                    nBodyBorderTopWidth = parseInt(Dom.getStyle(oBody, "borderTopWidth"), 10),
                    nBodyBorderBottomWidth = parseInt(Dom.getStyle(oBody, "borderBottomWidth"), 10),
                    nBodyTopPadding = parseInt(Dom.getStyle(oBody, "paddingTop"), 10),
                    nBodyBottomPadding = parseInt(Dom.getStyle(oBody, "paddingBottom"), 10),
                    nBodyOffset = nBodyBorderTopWidth + nBodyBorderBottomWidth + nBodyTopPadding + nBodyBottomPadding;
                }
                me.cfg.setProperty("width", nStartWidth + "px");
                aStartPos = [Event.getPageX(e), Event.getPageY(e)];
            };
            this.ddResize.onDrag = function(e) {
                var aNewPos = [Event.getPageX(e), Event.getPageY(e)],
                    nOffsetX = aNewPos[0] - aStartPos[0],
                    nOffsetY = aNewPos[1] - aStartPos[1],
                    nNewWidth = Math.max(nStartWidth + nOffsetX, 10),
                    nNewHeight = Math.max(nStartHeight + nOffsetY, 10),
                    nBodyHeight = (nNewHeight - (oFooter.offsetHeight + oHeader.offsetHeight + nBodyOffset));
                me.cfg.setProperty("width", nNewWidth + "px");
                me.cfg.setProperty("height", nNewHeight + "px");
                if (nBodyHeight < 0) {
                    nBodyHeight = 0;
                }
                oBody.style.height =  nBodyHeight + "px";
            };
        }
    
        function onBeforeShow() {
           initResizeFunctionality.call(this);
           this.unsubscribe("beforeShow", onBeforeShow);
        }
    
        function onBeforeRender() {
            if (!this.footer) {
                this.setFooter("");
            }
            if (this.cfg.getProperty("visible")) {
                initResizeFunctionality.call(this);
            } else {
                this.subscribe("beforeShow", onBeforeShow);
            }
            this.unsubscribe("beforeRender", onBeforeRender);
        }
    
        this.subscribe("beforeRender", onBeforeRender);
        if (userConfig) {
            this.cfg.applyConfig(userConfig, true);
        }
        this.initEvent.fire(YAHOO.widget.ResizePanel);
    
    },
    
    toString: function() {
        return "ResizePanel " + this.id;
    }
});

// BEGIN RESIZEDIALOG SUBCLASS //
YAHOO.widget.ResizeDialog = function(el, userConfig) {
    if (arguments.length > 0) {
        YAHOO.widget.ResizeDialog.superclass.constructor.call(this, el, userConfig);
    }
}
YAHOO.widget.ResizeDialog.CSS_PANEL_RESIZE = "yui-resizepanel";
YAHOO.widget.ResizeDialog.CSS_RESIZE_HANDLE = "resizehandle";
YAHOO.extend(YAHOO.widget.ResizeDialog, YAHOO.widget.Dialog, {
    init: function(el, userConfig) {
        YAHOO.widget.ResizeDialog.superclass.init.call(this, el);
        this.beforeInitEvent.fire(YAHOO.widget.ResizeDialog);
        var Dom = YAHOO.util.Dom,
            Event = YAHOO.util.Event,
            oInnerElement = this.innerElement,
            oResizeHandle = document.createElement("DIV"),
            sResizeHandleId = this.id + "_resizehandle";
         oResizeHandle.id = sResizeHandleId;
         oResizeHandle.className = YAHOO.widget.ResizeDialog.CSS_RESIZE_HANDLE;
        Dom.addClass(oInnerElement, YAHOO.widget.ResizeDialog.CSS_PANEL_RESIZE);
        this.resizeHandle = oResizeHandle;
        function initResizeFunctionality() {
            var me = this,
                oHeader = this.header,
                oBody = this.body,
                oFooter = this.footer,
                nStartWidth,
                nStartHeight,
                aStartPos,
                nBodyBorderTopWidth,
                nBodyBorderBottomWidth,
                nBodyTopPadding,
                nBodyBottomPadding,
                nBodyOffset;
            oInnerElement.appendChild(oResizeHandle);
            this.ddResize = new YAHOO.util.DragDrop(sResizeHandleId, this.id);
            this.ddResize.setHandleElId(sResizeHandleId);
            this.ddResize.onMouseDown = function(e) {
                nStartWidth = oInnerElement.offsetWidth;
                nStartHeight = oInnerElement.offsetHeight;
                if (YAHOO.env.ua.ie && document.compatMode == "BackCompat") {
                    nBodyOffset = 0;
                } else {
                    nBodyBorderTopWidth = parseInt(Dom.getStyle(oBody, "borderTopWidth"), 10),
                    nBodyBorderBottomWidth = parseInt(Dom.getStyle(oBody, "borderBottomWidth"), 10),
                    nBodyTopPadding = parseInt(Dom.getStyle(oBody, "paddingTop"), 10),
                    nBodyBottomPadding = parseInt(Dom.getStyle(oBody, "paddingBottom"), 10),
                    nBodyOffset = nBodyBorderTopWidth + nBodyBorderBottomWidth + nBodyTopPadding + nBodyBottomPadding;
                }
                me.cfg.setProperty("width", nStartWidth + "px");
                aStartPos = [Event.getPageX(e), Event.getPageY(e)];
            };
            this.ddResize.onDrag = function(e) {
                var aNewPos = [Event.getPageX(e), Event.getPageY(e)],
                    nOffsetX = aNewPos[0] - aStartPos[0],
                    nOffsetY = aNewPos[1] - aStartPos[1],
                    nNewWidth = Math.max(nStartWidth + nOffsetX, 10),
                    nNewHeight = Math.max(nStartHeight + nOffsetY, 10),
                    nBodyHeight = (nNewHeight - (oFooter.offsetHeight + oHeader.offsetHeight + nBodyOffset));
                me.cfg.setProperty("width", nNewWidth + "px");
                me.cfg.setProperty("height", nNewHeight + "px");

                if (nBodyHeight < 0) {
                    nBodyHeight = 0;
                }
                oBody.style.height =  nBodyHeight + "px";
            };
        }
    
        function onBeforeShow() {
           initResizeFunctionality.call(this);
           this.unsubscribe("beforeShow", onBeforeShow);
        }
    
        function onBeforeRender() {
            if (!this.footer) {
                this.setFooter("");
            }
            if (this.cfg.getProperty("visible")) {
                initResizeFunctionality.call(this);
            } else {
                this.subscribe("beforeShow", onBeforeShow);
            }
            this.unsubscribe("beforeRender", onBeforeRender);
        }
    
        this.subscribe("beforeRender", onBeforeRender);
        if (userConfig) {
            this.cfg.applyConfig(userConfig, true);
        }
        this.initEvent.fire(YAHOO.widget.ResizeDialog);
    
    },
    
    toString: function() {
        return "ResizeDialog " + this.id;
    }
});


YAHOO.namespace("ks");

YAHOO.util.Event.onDOMReady(function () {

	// Define various event handlers for Dialog   
	YAHOO.ks.handleSubmit = function(dialogObj) {   
		if (! dialogObj.ksClassName ) {
			var dialogObj = this;
		}
		if (! dialogObj.ksPostData ) {
			dialogObj.ksPostData = "";
		}
		if (! dialogObj.ksFileUpload ) {
			dialogObj.ksFileUpload = false;
		}	
		//alert("do submit " + dialogObj.ksClassName + " form = " + dialogObj.ksFormName);
		var formObject = document.getElementById(dialogObj.ksFormName);
		YAHOO.util.Connect.setForm(formObject, dialogObj.ksFileUpload);
		var callback = {
			//handle upload case. This function will be invoked after file upload is finished.
			success: function(o) {
				var response = o.responseText;
				if( doRedirect = /\[REDIRECT\]\[(.*?)\]/.exec(response))
				{
					window.location.href=doRedirect[1];
	 			}else{
					var callerObj = o.argument;
					//alert(response.substr(1,2000));
					callerObj.setBody(response);
					
					//Execute any javacript directly
					//NOTE - This will not parse correctly in IE if you surround the javascript in comment
					//tags, i.e. <!-- and -->
					var ScriptFragment = "<\s*script.*?>((\n|\r|\r\n|\n\r|.)*?)<\s*\/script";
					var match    = new RegExp(ScriptFragment, "img");
					var scripts  = response.match(match);
				
					if(scripts) {
						var match = new RegExp(ScriptFragment, "im");	
						var js = "";
					    for(var s = 0; s < scripts.length; s++) {
					        js += scripts[s].match(match)[1];
					 	}
						//alert(js);
					    eval(js);
					}

					YAHOO.util.Dom.setStyle(callerObj.element.firstChild.id, "opacity", 1.0);  
					
					callerObj.focusFirst();
					callerObj.show();
					callerObj.render();
					document.body.style.cursor="default";
					callerObj.focusFirst(); //needs to be called a second time to work in IE..
	 			}
	 		},
				
			upload: function(o) {
				var response = o.responseText;
				var callerObj = o.argument;
				response=response.replace(/&amp;/g,'&').replace(/&lt;/g,'<').replace(/&gt;/g,'>');
				//alert(response.substr(0,4000));
				var r = YAHOO.lang.JSON.parse(response); 
				var ok=r.ok;
				var fail=r.fail;
				var message=r.message;
				
				if(r.update_top)
				{
					for(var i=0;i<r.update_top.length; i++)
					{
						eval('window.top.YAHOO.ks.'+ r.update_top[i].id +'_el.set(\''+ r.update_top[i].name +'\',\''+ r.update_top[i].value +'\');');
					}
				}
				
				YAHOO.util.Dom.setStyle(callerObj.element.firstChild.id, "opacity", 1.0);  
				document.body.style.cursor="default";
				if(fail)
				{
					alert(message);
					callerObj.focusFirst();
					callerObj.show();
					callerObj.render();
					callerObj.focusFirst(); //needs to be called a second time to work in IE..
				} else
				{
					callerObj.hide();
				}
	 		},
				
			failure: function(o) {
					alert("Submission failed: " + o.status);
				var callerObj = o.argument;
					YAHOO.util.Dom.setStyle(callerObj.element.firstChild.id, "opacity", 1.0);  
				document.body.style.cursor="default";	
			},
				 
			timeout: 60000,   
			argument: dialogObj  				
		};
		if(dialogObj.ksFileUpload)
		{
			var action=formObject.action;
		} else
		{
			var action="./index.php?page=" + dialogObj.ksClassName + "&action=kn_update";
		}
		var cObj = YAHOO.util.Connect.asyncRequest("POST", action, callback, dialogObj.ksPostData);
		YAHOO.util.Dom.setStyle(dialogObj.element.firstChild.id, "opacity", 0.7);  
		dialogObj.ksPostData = "";
		document.body.style.cursor="wait";
	};   

	YAHOO.ks.handleCancel = function() {   
		this.cancel();   
	}; 
	YAHOO.ks.manager = new YAHOO.widget.OverlayManager();
	
	YAHOO.ks.displayAndFocus = function(e, obj) {
		if(obj.hideAllOthers){	
			YAHOO.ks.manager.hideAll();
		}
		obj.show();
		obj.focus();
		document.body.style.cursor="default";
	}
	
	YAHOO.ks.displayAndFocusDialog = function(e, obj) {
		if(obj.hideAllOthers){	
			YAHOO.ks.manager.hideAll();
		}
		obj.show();
		obj.focus();
		document.body.style.cursor="default";
		obj.focusFirst();	
	}	
	
	YAHOO.ks.display = function(e, obj) {
		obj.show();
		document.body.style.cursor="default";
	}
	
	YAHOO.ks.showThisOnOk = function() {
		eval(this.ksLinkOkButtonTo + ".show()");
		document.body.style.cursor="default";
	}
	YAHOO.ks.submitThisOnOk = function() {
		this.cancel(); 
		eval('YAHOO.ks.handleSubmit(' + this.ksLinkOkButtonTo + ')');
	}
	
	
}
);