(function($){$.fn.pjax=function(container,options){if(options)
options.container=container
else
options=$.isPlainObject(container)?container:{container:container}
if(options.container&&typeof options.container!=='string'){throw"pjax container must be a string selector!"
return false}
return this.live('click',function(event){if(event.which>1||event.metaKey)
return true
var defaults={url:this.href,container:$(this).attr('data-pjax'),clickedElement:$(this),fragment:null}
$.pjax($.extend({},defaults,options))
event.preventDefault()})}
$.pjax=function(options){var $container=$(options.container),success=options.success||$.noop
delete options.success
if(typeof options.container!=='string')
throw"pjax container must be a string selector!"
var defaults={timeout:650,push:true,replace:false,data:{_pjax:true},type:'GET',dataType:'html',beforeSend:function(xhr){$container.trigger('start.pjax')
xhr.setRequestHeader('X-PJAX','true')},error:function(){window.location=options.url},complete:function(){$container.trigger('end.pjax')},success:function(data){if(options.fragment){var $fragment=$(data).find(options.fragment)
if($fragment.length)
data=$fragment.children()
else
return window.location=options.url}else{if(!$.trim(data)||/<html/i.test(data))
return window.location=options.url}
$container.html(data)
var oldTitle=document.title,title=$.trim($container.find('title').remove().text())
if(title)document.title=title
var state={pjax:options.container,fragment:options.fragment,timeout:options.timeout}
var query=$.param(options.data)
if(query!="_pjax=true")
state.url=options.url+(/\?/.test(options.url)?"&":"?")+query
if(options.replace){window.history.replaceState(state,document.title,options.url)}else if(options.push){if(!$.pjax.active){window.history.replaceState($.extend({},state,{url:null}),oldTitle)
$.pjax.active=true}
window.history.pushState(state,document.title,options.url)}
if((options.replace||options.push)&&window._gaq)
_gaq.push(['_trackPageview'])
var hash=window.location.hash.toString()
if(hash!==''){window.location.href=hash}
success.apply(this,arguments)}}
options=$.extend(true,{},defaults,options)
if($.isFunction(options.url)){options.url=options.url()}
var xhr=$.pjax.xhr
if(xhr&&xhr.readyState<4){xhr.onreadystatechange=$.noop
xhr.abort()}
$.pjax.xhr=$.ajax(options)
$(document).trigger('pjax',$.pjax.xhr,options)
return $.pjax.xhr}
var popped=('state'in window.history),initialURL=location.href
$(window).bind('popstate',function(event){var initialPop=!popped&&location.href==initialURL
popped=true
if(initialPop)return
var state=event.state
if(state&&state.pjax){var container=state.pjax
if($(container+'').length)
$.pjax({url:state.url||location.href,fragment:state.fragment,container:container,push:false,timeout:state.timeout})
else
window.location=location.href}})
if($.inArray('state',$.event.props)<0)
$.event.props.push('state')
$.support.pjax=window.history&&window.history.pushState&&window.history.replaceState&&!navigator.userAgent.match(/(iPod|iPhone|iPad|WebApps\/.+CFNetwork)/)
if(!$.support.pjax){$.pjax=function(options){window.location=$.isFunction(options.url)?options.url():options.url}
$.fn.pjax=function(){return this}}})(jQuery);
