// jquery.pjax.js
// copyright chris wanstrath
// https://github.com/defunkt/jquery-pjax
(function(a){a.fn.pjax=function(b,c){c?c.container=b:c=a.isPlainObject(b)?b:{container:b};if(typeof c.container!="string")throw"pjax container must be a string selector!";return this.live("click",function(b){if(b.which>1||b.metaKey)return!0;var d={url:this.href,container:a(this).attr("data-pjax"),clickedElement:a(this)};a.pjax(a.extend({},d,c)),b.preventDefault()})},a.pjax=function(b){var c=a(b.container),d=b.success||a.noop;delete b.success;if(typeof b.container!="string")throw"pjax container must be a string selector!";var e={timeout:650,push:!0,replace:!1,data:{_pjax:!0},type:"GET",dataType:"html",beforeSend:function(a){c.trigger("start.pjax"),a.setRequestHeader("X-PJAX","true")},error:function(){window.location=b.url},complete:function(){c.trigger("end.pjax")},success:function(e){if(!a.trim(e)||/<html/i.test(e))return window.location=b.url;c.html(e);var f=document.title,g=a.trim(c.find("title").remove().text());g&&(document.title=g);var h={pjax:b.container,timeout:b.timeout},i=a.param(b.data);i!="_pjax=true"&&(h.url=b.url+(/\?/.test(b.url)?"&":"?")+i),b.replace?window.history.replaceState(h,document.title,b.url):b.push&&(a.pjax.active||(window.history.replaceState(a.extend({},h,{url:null}),f),a.pjax.active=!0),window.history.pushState(h,document.title,b.url)),(b.replace||b.push)&&window._gaq&&_gaq.push(["_trackPageview"]);var j=window.location.hash.toString();j!==""&&(window.location.hash="",window.location.hash=j),d.apply(this,arguments)}};b=a.extend(!0,{},e,b),a.isFunction(b.url)&&(b.url=b.url());var f=a.pjax.xhr;return f&&f.readyState<4&&(f.onreadystatechange=a.noop,f.abort()),a.pjax.xhr=a.ajax(b),a(document).trigger("pjax",a.pjax.xhr,b),a.pjax.xhr};var b="state"in window.history,c=location.href;a(window).bind("popstate",function(d){var e=!b&&location.href==c;b=!0;if(e)return;var f=d.state;if(f&&f.pjax){var g=f.pjax;a(g+"").length?a.pjax({url:f.url||location.href,container:g,push:!1,timeout:f.timeout}):window.location=location.href}}),a.inArray("state",a.event.props)<0&&a.event.props.push("state"),a.support.pjax=window.history&&window.history.pushState,a.support.pjax||(a.pjax=function(b){window.location=a.isFunction(b.url)?b.url():b.url},a.fn.pjax=function(){return this})})(jQuery)
