/**
 * DWR retrieves the session id from the cookies associated with the current document object.  This
 * does not work in Ride Planner since the document is produced by a different web application than
 * the one hosting DWR.
 * 
 * This method fixes the problem by overriding the default DWR session retrieval to return the id
 * generated by the web application hosting DWR.  It does this by retrieving the cookies from a document
 * owned by a frame whose source references the DWR web application.  If this frame does not exist, 
 * this method default to the standard DWR behavior.
 * 
 * @method _getJSessionId
 * @return {String} the session id
 */
HD.util.DWR = {
	/** Initializes the DWR engine, creating a subdomain session if necessary. */
	initSession : function() {
		try{
		if (typeof dwr != "undefined" && typeof dwr.engine != "undefined") {
			dwr.engine._getJSessionId =  function() {
				// Retrieve and cache the frame document.
				var dwrDocument = HD.util.DWR.document;
				
				if (!dwrDocument) {
				    var dwrFrame = document.getElementById('dwr');
				    
				    if (dwrFrame) {
						// Retrieve the frame document.  The cookies on this document will have been set by the
						// web application hosting DWR.
						dwrDocument = dwrFrame.contentWindow || dwrFrame.contentDocument;
						
						if (dwrDocument.document) {
						    dwrDocument = dwrDocument.document;
						}
				    } else {
						// No frame so just use the parent document (standard DWR behavior).
						dwrDocument = document;
					}
					
					// Cache the document reference.	
					HD.util.DWR.document = dwrDocument;
				}
			    
			    
			    var sessionId = HD.util.Common.getCookie(dwr.engine._sessionCookieName, dwrDocument);
				if(sessionId == null) {
					sessionId = "";
				}
			    return sessionId;
			};
		}
		} catch(e) {
			//alert(e);
		}
	},
	
	/**
	 * Retrieves the default error handler
	 * @returns {function}
	 */
	getDefaultErrorHandler : function() {
		return function(message, exception) {
			var errorText = [];
			for(var a in exception) {
				errorText.push(a + " : " + exception[a] + "\n");
			}
			
			var stackText = [];
			var stackTrace = exception.stackTrace;
			if(stackTrace != null) {
				for(var i = 0, len = stackTrace.length; i < len; i++) {
					var stackTraceElement = stackTrace[i];
					stackText.push(stackTraceElement.methodName);
					stackText.push(" in ");
					stackText.push(stackTraceElement.fileName);
					stackText.push(" [");
					stackText.push(stackTraceElement.lineNumber);
					stackText.push("] ");
					stackText.push("\n");
				}
			}
			
			//alert(errorText.join('') + '\n' + stackText.join(''));
		}
	},
	
	/**
	 * Sets the error handler
	 * @param {function} errorHandler The function to handle errors with
	 */
	setErrorHandler : function(errorHandler) {
		var errorHandler = !!errorHandler ? errorHandler : this.getDefaultErrorHandler();
		dwr.engine.setErrorHandler(errorHandler);
	}
};

HD.register('hd_dwr', HD.util.DWR, {version: "1.0", build: "1"});
