jQuery(document).ready(function($) {
/**
* Dispatcher class.
*
* @since 0.3
*/
function AjaxForAllDispatcher() {
this.completed = 0;
this.request = new AjaxForAllRequest;
this.tmp = '';
this.fire = function(href) {
if ( this.completed == 0 ) {
this.step(false);
div.hide();
this.content = this.request.get(href);
}
}
this.step = function(content) {
this.completed = this.completed + 1;
if ( content != false ) {
this.tmp = content;
}
if ( this.completed == 3 ) {
div.reveal(this.tmp);
this.completed = 0;
}
}
}
/**
* Div class. The div that we will replace.
*
* @since 0.3
*/
function AjaxForAllDiv() {
this.width = 0;
this.height = 0;
this.id = afa_id;
this.preserve = afa_preserve_height;
this.hide = function() {
if ( this.preserve == true ) {
this.height = $('#content').css( 'height' );
this.width = $('#content').css( 'width' );
$('#' + this.id ).wrapInner( '
' );
$('#afasize').wrapInner( '' );
$('#afasize').css( 'height', this.height );
$('#afasize').css( 'width', this.width );
}
else {
$('#' + this.id ).wrapInner( '' );
}
$('#' + this.id ).prepend( '
' );
$('#afaspinner').fadeIn();
$('html, body').animate({
scrollTop: 0
}, 1500);
$('#afacontent').slideUp(
2000,
function() {
dispatcher.step(false);
}
);
}
this.reveal = function(tmp) {
if ( typeof AjaxForAllCallback == 'function' ) {
AjaxForAllCallback(tmp); // callback that gets all the data
}
$('#' + this.id ).hide();
$('#' + this.id ).html(tmp.content);
$('#' + this.id ).slideDown(2000);
// The position seems to be accurate even when the slideDown isn't
// finished yet:
if ( tmp.jump ) {
anchor = $('a[name|=' + tmp.jumpto + ']');
offset = anchor.offset();
ytop = offset.top;
$('html, body').animate({
scrollTop: ytop
}, 1500);
}
}
}
/**
* Request class. Gets the content we will inject.
*
* @since 0.3
*/
function AjaxForAllRequest() {
this.ajaxurl = ajaxurl;
this.user = ajax_for_all_curl_user;
this.nonce = ajax_for_all_curl_nonce;
this.get = function(href) {
$.getJSON(
this.ajaxurl, {
action: 'ajax_for_all',
href: href,
user: this.user,
nonce: this.nonce
}, function(data) {
if (data.success) {
dispatcher.step(data);
}
else {
// just follow the link then
window.location = href;
}
}
);
}
}
/**
* Instantiate the unique objects
*/
dispatcher = new AjaxForAllDispatcher();
div = new AjaxForAllDiv();
/**
* Event binding
*/
$('a').live( 'click',
function() {
href = $(this).attr('href');
if ( href.indexOf('#') == 0 ) {
return true;
}
dispatcher.fire(href);
return false;
}
);
});