(function($){var _ajax=$.ajax,mockHandlers=[],mockedAjaxCalls=[],CALLBACK_REGEX=/=\?(&|$)/,jsc=(new Date()).getTime();function parseXML(xml){if(window.DOMParser==undefined&&window.ActiveXObject){DOMParser=function(){};DOMParser.prototype.parseFromString=function(xmlString){var doc=new ActiveXObject('Microsoft.XMLDOM');doc.async='false';doc.loadXML(xmlString);return doc;};}
try{var xmlDoc=(new DOMParser()).parseFromString(xml,'text/xml');if($.isXMLDoc(xmlDoc)){var err=$('parsererror',xmlDoc);if(err.length==1){throw('Error: '+ $(xmlDoc).text());}}else{throw('Unable to parse XML');}
return xmlDoc;}catch(e){var msg=(e.name==undefined?e:e.name+': '+ e.message);$(document).trigger('xmlParseError',[msg]);return undefined;}}
function trigger(s,type,args){(s.context?$(s.context):$.event).trigger(type,args);}
function isMockDataEqual(mock,live){var identical=true;if(typeof live==='string'){return $.isFunction(mock.test)?mock.test(live):mock==live;}
$.each(mock,function(k){if(live[k]===undefined){identical=false;return identical;}else{if(typeof live[k]==='object'&&live[k]!==null){if(identical&&$.isArray(live[k])){identical=$.isArray(mock[k])&&live[k].length===mock[k].length;}
identical=identical&&isMockDataEqual(mock[k],live[k]);}else{if(mock[k]&&$.isFunction(mock[k].test)){identical=identical&&mock[k].test(live[k]);}else{identical=identical&&(mock[k]==live[k]);}}}});return identical;}
function isDefaultSetting(handler,property){return handler[property]===$.mockjaxSettings[property];}
function getMockForRequest(handler,requestSettings){if($.isFunction(handler)){return handler(requestSettings);}
if($.isFunction(handler.url.test)){if(!handler.url.test(requestSettings.url)){return null;}}else{var star=handler.url.indexOf('*');if(handler.url!==requestSettings.url&&star===-1||!new RegExp(handler.url.replace(/[-[\]{}()+?.,\\^$|#\s]/g,"\\$&").replace(/\*/g,'.+')).test(requestSettings.url)){return null;}}
if(handler.data){if(!requestSettings.data||!isMockDataEqual(handler.data,requestSettings.data)){return null;}}
if(handler&&handler.type&&handler.type.toLowerCase()!=requestSettings.type.toLowerCase()){return null;}
return handler;}
function _xhrSend(mockHandler,requestSettings,origSettings){var process=(function(that){return function(){return(function(){var onReady;this.status=mockHandler.status;this.statusText=mockHandler.statusText;this.readyState=4;if($.isFunction(mockHandler.response)){mockHandler.response(origSettings);}
if(requestSettings.dataType=='json'&&(typeof mockHandler.responseText=='object')){this.responseText=JSON.stringify(mockHandler.responseText);}else if(requestSettings.dataType=='xml'){if(typeof mockHandler.responseXML=='string'){this.responseXML=parseXML(mockHandler.responseXML);this.responseText=mockHandler.responseXML;}else{this.responseXML=mockHandler.responseXML;}}else{this.responseText=mockHandler.responseText;}
if(typeof mockHandler.status=='number'||typeof mockHandler.status=='string'){this.status=mockHandler.status;}
if(typeof mockHandler.statusText==="string"){this.statusText=mockHandler.statusText;}
onReady=this.onreadystatechange||this.onload;if($.isFunction(onReady)){if(mockHandler.isTimeout){this.status=-1;}
onReady.call(this,mockHandler.isTimeout?'timeout':undefined);}else if(mockHandler.isTimeout){this.status=-1;}}).apply(that);};})(this);if(mockHandler.proxy){_ajax({global:false,url:mockHandler.proxy,type:mockHandler.proxyType,data:mockHandler.data,dataType:requestSettings.dataType==="script"?"text/plain":requestSettings.dataType,complete:function(xhr){mockHandler.responseXML=xhr.responseXML;mockHandler.responseText=xhr.responseText;if(isDefaultSetting(mockHandler,'status')){mockHandler.status=xhr.status;}
if(isDefaultSetting(mockHandler,'statusText')){mockHandler.statusText=xhr.statusText;}
this.responseTimer=setTimeout(process,mockHandler.responseTime||0);}});}else{if(requestSettings.async===false){process();}else{this.responseTimer=setTimeout(process,mockHandler.responseTime||50);}}}
function xhr(mockHandler,requestSettings,origSettings,origHandler){mockHandler=$.extend(true,{},$.mockjaxSettings,mockHandler);if(typeof mockHandler.headers==='undefined'){mockHandler.headers={};}
if(mockHandler.contentType){mockHandler.headers['content-type']=mockHandler.contentType;}
return{status:mockHandler.status,statusText:mockHandler.statusText,readyState:1,open:function(){},send:function(){origHandler.fired=true;_xhrSend.call(this,mockHandler,requestSettings,origSettings);},abort:function(){clearTimeout(this.responseTimer);},setRequestHeader:function(header,value){mockHandler.headers[header]=value;},getResponseHeader:function(header){if(mockHandler.headers&&mockHandler.headers[header]){return mockHandler.headers[header];}else if(header.toLowerCase()=='last-modified'){return mockHandler.lastModified||(new Date()).toString();}else if(header.toLowerCase()=='etag'){return mockHandler.etag||'';}else if(header.toLowerCase()=='content-type'){return mockHandler.contentType||'text/plain';}},getAllResponseHeaders:function(){var headers='';$.each(mockHandler.headers,function(k,v){headers+=k+': '+ v+"\n";});return headers;}};}
function processJsonpMock(requestSettings,mockHandler,origSettings){processJsonpUrl(requestSettings);requestSettings.dataType="json";if(requestSettings.data&&CALLBACK_REGEX.test(requestSettings.data)||CALLBACK_REGEX.test(requestSettings.url)){createJsonpCallback(requestSettings,mockHandler,origSettings);var rurl=/^(\w+:)?\/\/([^\/?#]+)/,parts=rurl.exec(requestSettings.url),remote=parts&&(parts[1]&&parts[1]!==location.protocol||parts[2]!==location.host);requestSettings.dataType="script";if(requestSettings.type.toUpperCase()==="GET"&&remote){var newMockReturn=processJsonpRequest(requestSettings,mockHandler,origSettings);if(newMockReturn){return newMockReturn;}else{return true;}}}
return null;}
function processJsonpUrl(requestSettings){if(requestSettings.type.toUpperCase()==="GET"){if(!CALLBACK_REGEX.test(requestSettings.url)){requestSettings.url+=(/\?/.test(requestSettings.url)?"&":"?")+
(requestSettings.jsonp||"callback")+"=?";}}else if(!requestSettings.data||!CALLBACK_REGEX.test(requestSettings.data)){requestSettings.data=(requestSettings.data?requestSettings.data+"&":"")+(requestSettings.jsonp||"callback")+"=?";}}
function processJsonpRequest(requestSettings,mockHandler,origSettings){var callbackContext=origSettings&&origSettings.context||requestSettings,newMock=null;if(mockHandler.response&&$.isFunction(mockHandler.response)){mockHandler.response(origSettings);}else{if(typeof mockHandler.responseText==='object'){$.globalEval('('+ JSON.stringify(mockHandler.responseText)+')');}else{$.globalEval('('+ mockHandler.responseText+')');}}
jsonpSuccess(requestSettings,callbackContext,mockHandler);jsonpComplete(requestSettings,callbackContext,mockHandler);if($.Deferred){newMock=new $.Deferred();if(typeof mockHandler.responseText=="object"){newMock.resolveWith(callbackContext,[mockHandler.responseText]);}
else{newMock.resolveWith(callbackContext,[$.parseJSON(mockHandler.responseText)]);}}
return newMock;}
function createJsonpCallback(requestSettings,mockHandler,origSettings){var callbackContext=origSettings&&origSettings.context||requestSettings;var jsonp=requestSettings.jsonpCallback||("jsonp"+ jsc++);if(requestSettings.data){requestSettings.data=(requestSettings.data+"").replace(CALLBACK_REGEX,"="+ jsonp+"$1");}
requestSettings.url=requestSettings.url.replace(CALLBACK_REGEX,"="+ jsonp+"$1");window[jsonp]=window[jsonp]||function(tmp){data=tmp;jsonpSuccess(requestSettings,callbackContext,mockHandler);jsonpComplete(requestSettings,callbackContext,mockHandler);window[jsonp]=undefined;try{delete window[jsonp];}catch(e){}
if(head){head.removeChild(script);}};}
function jsonpSuccess(requestSettings,callbackContext,mockHandler){if(requestSettings.success){requestSettings.success.call(callbackContext,mockHandler.responseText||"",status,{});}
if(requestSettings.global){trigger(requestSettings,"ajaxSuccess",[{},requestSettings]);}}
function jsonpComplete(requestSettings,callbackContext){if(requestSettings.complete){requestSettings.complete.call(callbackContext,{},status);}
if(requestSettings.global){trigger("ajaxComplete",[{},requestSettings]);}
if(requestSettings.global&&!--$.active){$.event.trigger("ajaxStop");}}
function handleAjax(url,origSettings){var mockRequest,requestSettings,mockHandler;if(typeof url==="object"){origSettings=url;url=undefined;}else{origSettings=origSettings||{};origSettings.url=url;}
requestSettings=$.extend(true,{},$.ajaxSettings,origSettings);for(var k=0;k<mockHandlers.length;k++){if(!mockHandlers[k]){continue;}
mockHandler=getMockForRequest(mockHandlers[k],requestSettings);if(!mockHandler){continue;}
mockedAjaxCalls.push(requestSettings);$.mockjaxSettings.log(mockHandler,requestSettings);if(requestSettings.dataType&&requestSettings.dataType.toUpperCase()==='JSONP'){if((mockRequest=processJsonpMock(requestSettings,mockHandler,origSettings))){return mockRequest;}}
mockHandler.cache=requestSettings.cache;mockHandler.timeout=requestSettings.timeout;mockHandler.global=requestSettings.global;copyUrlParameters(mockHandler,origSettings);(function(mockHandler,requestSettings,origSettings,origHandler){mockRequest=_ajax.call($,$.extend(true,{},origSettings,{xhr:function(){return xhr(mockHandler,requestSettings,origSettings,origHandler);}}));})(mockHandler,requestSettings,origSettings,mockHandlers[k]);return mockRequest;}
if($.mockjaxSettings.throwUnmocked===true){throw('AJAX not mocked: '+ origSettings.url);}
else{return _ajax.apply($,[origSettings]);}}
function copyUrlParameters(mockHandler,origSettings){if(!(mockHandler.url instanceof RegExp)){return;}
if(!mockHandler.hasOwnProperty('urlParams')){return;}
var captures=mockHandler.url.exec(origSettings.url);if(captures.length===1){return;}
captures.shift();var i=0,capturesLength=captures.length,paramsLength=mockHandler.urlParams.length,maxIterations=Math.min(capturesLength,paramsLength),paramValues={};for(i;i<maxIterations;i++){var key=mockHandler.urlParams[i];paramValues[key]=captures[i];}
origSettings.urlParams=paramValues;}
$.extend({ajax:handleAjax});$.mockjaxSettings={log:function(mockHandler,requestSettings){if(mockHandler.logging===false||(typeof mockHandler.logging==='undefined'&&$.mockjaxSettings.logging===false)){return;}
if(window.console&&console.log){var message='MOCK '+ requestSettings.type.toUpperCase()+': '+ requestSettings.url;var request=$.extend({},requestSettings);if(typeof console.log==='function'){console.log(message,request);}else{try{console.log(message+' '+ JSON.stringify(request));}catch(e){console.log(message);}}}},logging:true,status:200,statusText:"OK",responseTime:500,isTimeout:false,throwUnmocked:false,contentType:'text/plain',response:'',responseText:'',responseXML:'',proxy:'',proxyType:'GET',lastModified:null,etag:'',headers:{etag:'IJF@H#@923uf8023hFO@I#H#','content-type':'text/plain'}};$.mockjax=function(settings){var i=mockHandlers.length;mockHandlers[i]=settings;return i;};$.mockjaxClear=function(i){if(arguments.length==1){mockHandlers[i]=null;}else{mockHandlers=[];}
mockedAjaxCalls=[];};$.mockjax.handler=function(i){if(arguments.length==1){return mockHandlers[i];}};$.mockjax.mockedAjaxCalls=function(){return mockedAjaxCalls;};})(jQuery);