var autosaveLast = '';
var autosavePeriodical;

function autosave_start_timer() {
	autosaveLast = jQuery('#post #title').val()+jQuery('#post #content').val();
	// Keep autosave_interval in sync with edit_post().
	autosavePeriodical = jQuery.schedule({time: autosaveL10n.autosaveInterval * 1000, func: autosave, repeat: true, protect: true});

	//Disable autosave after the form has been submitted
	jQuery("#post #submit").submit(function() { jQuery.cancel(autosavePeriodical); });
	jQuery("#post #save").click(function() { jQuery.cancel(autosavePeriodical); });
	jQuery("#post #submit").click(function() { jQuery.cancel(autosavePeriodical); });
	jQuery("#post #publish").click(function() { jQuery.cancel(autosavePeriodical); });
	jQuery("#post #deletepost").click(function() { jQuery.cancel(autosavePeriodical); });
}
addLoadEvent(autosave_start_timer)

function autosave_cur_time() {
	var now = new Date();
	return "" + ((now.getHours() >12) ? now.getHours() -12 : now.getHours()) + 
	((now.getMinutes() < 10) ? ":0" : ":") + now.getMinutes() +
	((now.getSeconds() < 10) ? ":0" : ":") + now.getSeconds();
}

function autosave_update_post_ID(response) {
	var res = parseInt(response);
	var message;

	if(isNaN(res)) {
		message = autosaveL10n.errorText.replace(/%response%/g, response);
	} else if( res > 0 ) {
		message = autosaveL10n.saveText.replace(/%time%/g, autosave_cur_time());
		jQuery('#post_ID').attr({name: "post_ID"});
		jQuery('#post_ID').val(res);
		// We need new nonces
		jQuery.post(autosaveL10n.requestFile, {
			action: "autosave-generate-nonces",
			post_ID: res,
			autosavenonce: jQuery('#autosavenonce').val(),
			post_type: jQuery('#post_type').val()
		}, function(html) {
			jQuery('#_wpnonce').val(html);
		});
		jQuery('#hiddenaction').val('editpost');
	} else {
		message = autosaveL10n.failText;
	}
	jQuery('#autosave').html(message);
	autosave_enable_buttons();
}

function autosave_loading() {
	jQuery('#autosave').html(autosaveL10n.savingText);
}

function autosave_saved(response) {
	var res = parseInt(response);
	var message;

	if(isNaN(res)) {
		message = autosaveL10n.errorText.replace(/%response%/g, response);
	} else {
		message = autosaveL10n.saveText.replace(/%time%/g, autosave_cur_time());
	}
	jQuery('#autosave').html(message);
	autosave_enable_buttons();
}

function autosave_disable_buttons() {
	jQuery("#post #save:enabled").attr('disabled', 'disabled');
	jQuery("#post #submit:enabled").attr('disabled', 'disabled');
	jQuery("#post #publish:enabled").attr('disabled', 'disabled');
	jQuery("#post #deletepost:enabled").attr('disabled', 'disabled');
	setTimeout('autosave_enable_buttons();', 1000); // Re-enable 1 sec later.  Just gives autosave a head start to avoid collisions.
}

function autosave_enable_buttons() {
	jQuery("#post #save:disabled").attr('disabled', '');
	jQuery("#post #submit:disabled").attr('disabled', '');
	jQuery("#post #publish:disabled").attr('disabled', '');
	jQuery("#post #deletepost:disabled").attr('disabled', '');
}

function autosave() {
	var rich = ( (typeof tinyMCE != "undefined") && tinyMCE.activeEditor && ! tinyMCE.activeEditor.isHidden() ) ? true : false;
	var post_data = {
			action: "autosave",
			post_ID:  jQuery("#post_ID").val() || 0,
			post_title: jQuery("#title").val() || "",
			autosavenonce: jQuery('#autosavenonce').val(),
			tags_input: jQuery("#tags-input").val() || "",
			post_type: jQuery('#post_type').val() || ""
		};

	/* Gotta do this up here so we can check the length when tinyMCE is in use */
	if ( rich ) {
		// Don't run while the TinyMCE spellcheck is on.
		if ( tinyMCE.activeEditor.plugins.spellchecker && tinyMCE.activeEditor.plugins.spellchecker.active ) return;
		tinyMCE.triggerSave();
	} 
	
	post_data["content"] = jQuery("#content").val();

	if(post_data["post_title"].length==0 || post_data["content"].length==0 || post_data["post_title"] + post_data["content"] == autosaveLast) {
		return;
	}

	autosave_disable_buttons();

	autosaveLast = jQuery("#title").val()+jQuery("#content").val();
	goodcats = ([]);
	jQuery("[@name='post_category[]']:checked").each( function(i) {
		goodcats.push(this.value);
	} );
	post_data["catslist"] = goodcats.join(",");

	if ( jQuery("#comment_status").attr("checked") )
		post_data["comment_status"] = 'open';
	if ( jQuery("#ping_status").attr("checked") )
		post_data["ping_status"] = 'open';
	if( jQuery("#excerpt"))
		post_data["excerpt"] = jQuery("#excerpt").val();

	if ( rich ) 
        tinyMCE.triggerSave();
    
	post_data["content"] = jQuery("#content").val();

	if(parseInt(post_data["post_ID"]) < 1) {
		post_data["temp_ID"] = post_data["post_ID"];
		jQuery.ajaxSetup({
			success: function(html) { autosave_update_post_ID(html); }
		});
	} else {
		jQuery.ajaxSetup({
			success: function(html) { autosave_saved(html); }
		});
	}
	jQuery.ajax({
		data: post_data,
		beforeSend: function() { autosave_loading() },
		type: "POST",
		url: autosaveL10n.requestFile
	});
}

