jQuery.noConflict();
jQuery.ajaxPrefilter(function(options, originalOptions, jqXHR) { jqXHR.RqOptions = originalOptions; });
jQuery.ajaxSetup({ 
	dataType: 'json', 
	cache: false, 
	async: true,
});

/*add token*/
(function(){
	if(typeof jQuery == "undefined") return;
	
	var ajax_tmp = jQuery.ajax;	
	jQuery.ajax = function( url, options ) {
		//console.log('jQuery.ajax', url, options, '_______');
		var options = ( typeof url === "object" )? url : options || {} ;
		
		var tmp_beforeSend = options.beforeSend || null;
		var tmp_success = options.success || null;
		
		options.beforeSend = function(xhr, settings){
			
			if(typeof(xhr.RqOptions)!='undefined' && typeof(xhr.RqOptions.data)!='undefined'){
				if(xhr.RqOptions.data instanceof FormData ){
					if(typeof xhr.RqOptions.data.has == "undefined"){
						if(typeof xhr.RqOptions.data._token == "undefined") xhr.RqOptions.data.append('_token', jQuery._tourister_token);
					}else{
						if(!xhr.RqOptions.data.has('_token')) xhr.RqOptions.data.append('_token', jQuery._tourister_token);
					}
				}else{
					if(typeof(xhr.RqOptions.data._token)=='undefined'){
			        	xhr.RqOptions.data._token = jQuery._tourister_token;
			        	
			        	if(xhr.RqOptions.type=='POST'){
				    		settings.data += '&_token='+encodeURIComponent(xhr.RqOptions.data._token);
				    	}else{
								var rx = new RegExp('router\.project\-osrm\.org');
								if (!rx.test(settings.url)) {
					    		settings.url += '&_token='+encodeURIComponent(xhr.RqOptions.data._token);
								}  
				    	}
					}
				}
	        }
	        // допилить разбор функции в success
			
			if(tmp_beforeSend!=null) tmp_beforeSend();
		}
		
		
		options.success = function(data, status, xhr){
			if(data){
				if(data['error_code']==='not_valid_token'){
					if(document.getElementById('token_error_div')==null){
						var error_div = createElementFromHTML('<div id="token_error_div" width="100%" align="center"><span style="z-index:100001; position:fixed; bottom: 30px; background-color: red; color: white; font-size: 14px; padding: 10px 5px; font-weight: bold; width: 50%; margin-left: -25%;">'+data['error']+'</span></div>');
						document.body.appendChild(error_div);
					}
					delete(data['error_code']);
					delete(data['error']);
				}
			}
			if(tmp_success!=null) tmp_success(data, status, xhr);
		}
		
		
		return ajax_tmp(url, options);
	}
})();

(function(){
	if(typeof Ajax == "undefined") return;
	
	var ajax_tmp = Ajax.Request;
	
	Ajax.Request = function() {
		var getLocation = function(href) {
		    var l = document.createElement("a");
		    l.href = href;
		    return l;
		};
		
		var tmp_search = '';
		if(arguments[0]!=null){
			var l = getLocation(arguments[0]);
			if(l.search!=''){
				if(typeof URLSearchParams != "undefined" ){
					var sp = new URLSearchParams(l.search);
					if(!sp.has('_token')){
						sp.set('_token', jQuery._tourister_token);
					}
					var tmp = decodeURIComponent(sp.toString());
					if(tmp[0]=='?') tmp = tmp.substr(1);
					l.search = tmp;
				}else{
					var param_arr = new RegExp('[\?&]_token=([^&#]*)').exec(l.search);
					if(param_arr==null){
						l.search += ((l.search=="")? '?_token=' : '&_token=')+jQuery._tourister_token ;
					}
					
				}
				arguments[0] = ((l.pathname[0]=='/')? '' : '/')+l.pathname+l.search;
			}
		}
		
		if((typeof arguments[1] != "undefined") && (typeof arguments[1].onComplete != "undefined")){
			var tmp_onComplete = arguments[1].onComplete;
			
			arguments[1].onComplete = function(response){
				if(typeof response.responseText != "undefined"){
					var data = null;
					try { data = JSON.parse(response.responseText); } catch (e) { }
					if(data!=null){
						if(data['error_code']==='not_valid_token'){
							if(document.getElementById('token_error_div')==null){
								var error_div = createElementFromHTML('<div id="token_error_div" width="100%" align="center"><span style="z-index:100001; position:fixed; bottom: 30px; background-color: red; color: white; font-size: 14px; padding: 10px 5px; font-weight: bold; width: 50%; margin-left: -25%;">'+data['error']+'</span></div>');
								document.body.appendChild(error_div);
							}
							delete(data['error_code']);
							delete(data['error']);
						}
					}
				}
				
				tmp_onComplete(response);
			}
		}
		
		return new ajax_tmp(arguments[0], arguments[1]);
	}
	Ajax.Request.Events = ajax_tmp.Events;
})();