WSCore = {
	/*
	 * Custom load function similar to jQuery.load.
	 * 
	 * Usage of jQuery.load breaks the widget in IE if the html response has
	 * more external scripts and the embedded scripts refers to the symbols
	 * defined in the external scripts. There are two problems 1. The external
	 * scripts are not downloaded in the same order leading to JS errors if
	 * there is some dependency. 2. Sometimes the embedded scripts are executed
	 * even before the external script is downloaded. This is a bug in
	 * jQuery.load.
	 * 
	 * To overcome the issue the below custom load function is written which
	 * strips the external and embedded scripts in the response, assigns raw
	 * html to the div element, then requests for the external scripts [using
	 * WSCore.getScripts] and evaluates the embedded scripts
	 */
	load : function(divId, url, callback) {
		// Load the url using jquery ajax
		jQuery.ajax({
					type : "GET",
					url : url,
					success : function(res, status) {
						// Manipulate response to strip scripts and assign to div element
						WSCore.domManip(divId, res, callback);
					},
					dataType : "html",
					cache : true
				});
	},

	/*
	 * Parses the response to gather all external and embedded script and
	 * replace it with blank. The raw html will be assigned to the div's
	 * innerHTML and then the external scripts are requested using
	 * WSCore.getScritps. The embedded scripts are evaluated in the callback of
	 * getScripts
	 */
	domManip : function(divId, response, mainCallback) {
		var scripts = new Array();

		/*
		 * Parse the response and store the external script urls and the inner
		 * script body in an array and strip the response of both the script
		 * types as we will be requesting each external script and executing the
		 * inner scripts separately
		 */
		response = response.replace(/<script([\d\D]*?)>([\d\D]*?)<\/script>/gi,
				function(fullScript, attributes, body) {
					var attributePieces = attributes.match(/src="(.*?)"/i);
					if (attributePieces) {
						scripts.push({
							type : "EXTERNAL",
							// External script "src" url
							data : attributePieces[1]
							});
					} else {
						scripts.push({
							type : "INNER",
							// Inner script body
							data : body
							});
					}
					return "";
				});

		/*
		 * Assign raw html to the div -- it is very important that 
		 * this be done using jQuery only
		 */
		jQuery("#" + divId).html(response);

		/*
		 * Collect all the "data" values from the "scripts" array that are of
		 * the same "type"
		 */
		var collectOfSameType = function(type) {
			var ret = new Array();
			if (!scripts) {
				return ret;
			}

			while (scripts.length > 0 && (scripts[0].type === type)) {
				ret.push(scripts.shift().data);
			}
			return ret;
		};

		/*
		 * Build the callback for getScripts - any inner scripts immediately
		 * following the downloaded external scripts are evaluated.
		 * Subsequently, external scripts following those inner scripts are
		 * attempted to be downloaded in recursive manner untill we have
		 * exhausted all our scripts at which time, the main callback is
		 * evaluated
		 */
		var buildCallback = function(innerScripts) {
			return function() {
				jQuery.each(innerScripts, function(index, scriptBody) {
							window.execScript
									? window.execScript(scriptBody)
									: jQuery.globalEval(scriptBody);
						});

				if (scripts.length > 0) {
					WSCore.getScripts(collectOfSameType("EXTERNAL"), true,
							buildCallback(collectOfSameType("INNER")));
				} else {
					/*
					 * If main (user provided) callback is defined and is a
					 * function, execute it
					 */
					if (mainCallback && jQuery.isFunction(mainCallback)) {
						mainCallback();
					}
				}
			};
		};

		WSCore.getScripts(collectOfSameType("EXTERNAL"), true,
				buildCallback(collectOfSameType("INNER")));
	},

	/*
	 * getScripts - Method similar to jQuery.getScript. jQuery.getScript takes
	 * just one script file as argument whereas the below getScripts method
	 * takes an array of scripts as argument and requests for each of them
	 * [downloads] using jQuery.ajax. The supplied callback function will be
	 * invoked only at the end after all the ajax requests for the input scripts
	 * have succeded
	 */
	getScripts : function(scripts, isCachingRequired, callBack) {
		if (scripts.length <= 0) {
			// If no external scripts are supplied just call the callBack and return
			callBack();
			return;
		}
		var state = {
			index : 0,
			maxIndex : scripts.length - 1
		};
		function getScript() {
			/*
			 * Set the callback to the same function [getScript] till it reaches
			 * the last script. For the last script set the callback to the
			 * original supplied callback
			 */
			var _callBack = state.index == state.maxIndex
					? callBack
					: getScript;
			jQuery.ajax({
						type : "GET",
						url : scripts[state.index++],
						dataType : "script",
						success : _callBack,
						cache : isCachingRequired
					});
		}
		getScript();
	},

	/*
	 * Checks the Browser type. If Browser is Internet Explorer 6.0, it returns
	 * true for any other Browser, returns false
	 */
	isIE6Browser : function() {
		var isIE6 = false;
		jQuery.browser.msie ? ((jQuery.browser.version < 7.0)
				? isIE6 = true
				: isIE6 = false) : isIE6 = false;
		return isIE6;
	},

	/*
	 * Checks the Browser type. If IE browser is an advanced version than IE 7,
	 * it returns true else returns false
	 */
	isIEVersionGreaterThanV7 : function() {
		var isIEAdvanced = false;
		jQuery.browser.msie ? ((jQuery.browser.version > 7.0)
				? isIEAdvanced = true
				: isIEAdvanced = false) : isIEAdvanced = false;
		return isIEAdvanced;
	}
};