(function($){var _ajax=$.ajax,mockHandlers=[],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');}}catch(e){var msg=(e.name==undefined?e:e.name+': '+e.message);$(document).trigger('xmlParseError',[msg]);return undefined;}
return xmlDoc;}
function trigger(s,type,args){(s.context?jQuery(s.context):jQuery.event).trigger(type,args);}
function isMockDataEqual(mock,live){var identical=false;if(typeof live==='string'){return $.isFunction(mock.test)?mock.test(live):mock==live;}
$.each(mock,function(k,v){if(live[k]===undefined){identical =false;return identical;}else{identical=true;if(typeof live[k]=='object'){return isMockDataEqual(mock[k],live[k]);}else{if($.isFunction(mock[k].test)){identical=mock[k].test(live[k]);}else{identical=(mock[k]==live[k]);}
return identical;}}});return identical;}
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('*','.+')).test(requestSettings.url)){return null;}}
if(handler.data&&requestSettings.data){if(!isMockDataEqual(handler.data,requestSettings.data)){return null;}}
if(handler&&handler.type&&handler.type.toLowerCase()!=requestSettings.type.toLowerCase()){return null;}
return handler;}
function logMock(mockHandler,requestSettings){var c=$.extend({},$.mockjaxSettings,mockHandler);if(c.log&&$.isFunction(c.log)){c.log('MOCK '+requestSettings.type.toUpperCase()+': '+requestSettings.url,$.extend({},requestSettings));}}
function _xhrSend(mockHandler,requestSettings,origSettings){var process=(function(that){return function(){return(function(){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);}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;}
if($.isFunction(this.onreadystatechange)){if(mockHandler.isTimeout){this.status=-1;}
this.onreadystatechange(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,txt){mockHandler.responseXML=xhr.responseXML;mockHandler.responseText=xhr.responseText;mockHandler.status=xhr.status;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({},$.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);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,mockHandler);jsonpComplete(requestSettings,mockHandler);if(jQuery.Deferred){newMock=new jQuery.Deferred();if(typeof mockHandler.responseText=="object"){newMock.resolve(mockHandler.responseText);}
else{newMock.resolve(jQuery.parseJSON(mockHandler.responseText));}}
return newMock;}
function createJsonpCallback(requestSettings,mockHandler){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,mockHandler);jsonpComplete(requestSettings,mockHandler);window[jsonp]=undefined;try{delete window[jsonp];}catch(e){}
if(head){head.removeChild(script);}};}
function jsonpSuccess(requestSettings,mockHandler){if(requestSettings.success){requestSettings.success.call(callbackContext,(mockHandler.response?mockHandler.response.toString():mockHandler.responseText||''),status,{});}
if(requestSettings.global){trigger(requestSettings,"ajaxSuccess",[{},requestSettings]);}}
function jsonpComplete(requestSettings,mockHandler){if(requestSettings.complete){requestSettings.complete.call(callbackContext,{},status);}
if(requestSettings.global){trigger("ajaxComplete",[{},requestSettings]);}
if(requestSettings.global&&!--jQuery.active){jQuery.event.trigger("ajaxStop");}}
function handleAjax(url,origSettings){var mockRequest,requestSettings,mockHandler;if(typeof url==="object"){origSettings=url;url=undefined;}else{origSettings.url=url;}
requestSettings=jQuery.extend(true,{},jQuery.ajaxSettings,origSettings);for(var k=0;k<mockHandlers.length;k++){if(!mockHandlers[k]){continue;}
mockHandler=getMockForRequest(mockHandlers[k],requestSettings);if(!mockHandler){continue;}
logMock(mockHandler,requestSettings);if(requestSettings.dataType==="jsonp"){if((mockRequest=processJsonpMock(requestSettings,mockHandler,origSettings))){return mockRequest;}}
mockHandler.cache=requestSettings.cache;mockHandler.timeout=requestSettings.timeout;mockHandler.global=requestSettings.global;(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;}
return _ajax.apply($,[origSettings]);}
$.extend({ajax:handleAjax});$.mockjaxSettings={log:function(msg){window['console']&&window.console.log&&window.console.log(msg);},status:200,statusText:"OK",responseTime:500,isTimeout: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=[];}};$.mockjax.handler=function(i){if(arguments.length==1){return mockHandlers[i];}};})(jQuery);