/**
 * dataswitch.js
 * DataSwitch Object is a wrapper around the jQuery ajax function.
 * It intercepts an ajax call and retrieves data from the server via an
 * Ajax call if in online mode, or from the local store if in offline mode.
 * 
 */

var DataSwitch = (function(){
	function ajaxCall(params){
		var url;
		if (typeof Offline != "undefined" && Offline && Offline.isOffline()) {
            // remove the sub_id param 
            url = removeQueryString(params['url'], 'sub_id');
            if(url.indexOf("Page.action") != -1 && url.indexOf("#pg") != -1) {
                url = url.substring(0, url.indexOf("#pg"));
            }
		} else {
			// add modified param and pass ajax dataType
            url = addLmParam(params['url']);
            if (DocumentProperties.getSubIdCookieHack() === true) {
                url = addSubId(url);
            }
            if (typeof params['dataType'] != "undefined") {
                url += "&ajaxDataType=" + params['dataType'];
            }
		}
        params['url'] = url;
		var success = params['success'];
		params['error'] = ViewHelper.dataSwitchError;
		return $.ajax(params);
	};
	function addLmParam(url) {
		if (typeof Offline != "undefined" && Offline && Offline.isOffline()) {
			// do not append parameter if offline
			return url;
		}
		if (url.indexOf('lm=') > 0) return url; 
		// add last modified param
        if (DocumentProperties.modified) {
        	if (url.indexOf('?') >= 0) {
        		url += '&';
        	} else {
        		url += '?';
        	}
        	url += 'lm=' + DocumentProperties.modified;
        }
        return url;
	};

	/**
	 * Add the subscriber id to the Ajax call if the
	 * subscriber_id Cookie is set.
	 * This was added for the IE Cookie problem, but can be used
	 * by anyone.
	 * See SECTION E in that documentHtmlFooter.ftl for an explanation
	 * of why this might be necessary
	 * @param url the url to pass to the Ajax call
	 * @return url + sub_id if set or original url if not
	 */
	function addSubId(url) {
		if (typeof Offline != "undefined" && Offline && Offline.isOffline()) {
			// do not append parameter if offline
			return url;
		}
		if (url.indexOf('sub_id=') > 0) return url; 
		// add sub_id if subscriber_id Cookie is set
        if (CookieManager.get("subscriber_id") !== null) {
        	if (url.indexOf('?') >= 0) {
        		url += '&';
        	} else {
        		url += '?';
        	}
        	url += 'sub_id=' + CookieManager.get("subscriber_id");
        }
        return url;
	};

	function removeQueryString(url,param) {
        var re = new RegExp("([?|&])" + param + "=.*?(&|$)","i");
        if (url.match(re))
            return url.replace(re,'');
        else
            return url;
    };
	return {
		get: function(params){
			return ajaxCall(params);
		},
		post: function(params){
			params['type'] = 'POST';
			return ajaxCall(params);
		},
		addLmParam : function(url) {
			return addLmParam(url);
		},
		addSubId : function(url) {
			return addSubId(url);
		}
	};
})();


