/**
 * Publish step scripts
 *
 * @copyright (c) 2004-2007, SWsoft
 */
function SbPublishStep(formContainerId) {
	sbNavigationObject.getForm().action = baseUrl + '/Wizard/Publish/Save';
	
	this._formContainer = self.document.getElementById(formContainerId);
	this._updatePreloader = sbPreloader.isEnabled();
	this._isPublishing = false;
	this._isReverting = false;
	this._publishSettingsFormOnSubmitMethod = function() {};

	var publishStep = this;
	sbProgressBar.addOnHideEvent(function() {publishStep.showAllContent();});
	sbProgressBar.addOnShowEvent(function() {publishStep.hideAllContent();});
	sbNavigationObject.registerOnSubmitFunction(function() {return publishStep._publishSettingsFormOnSubmitMethod();});

	this._request();
}

SbPublishStep.prototype.hideAllContent = function() {
	this._formContainer.style.display = 'none';
}

SbPublishStep.prototype.showAllContent = function() {
	this._formContainer.style.display = 'block';
}

SbPublishStep.prototype.publish = function() {
	sbApplyChangesObject.registerChange();
	sbApplyChangesObject.registerForcedSaving();
	sbNavigationObject.getForm().action = baseUrl + '/Wizard/Publish';
	this._isPublishing = true;
	if ('undefined' == typeof(sbPublicationSettingsForm)) {
		this._runProgressBar();
	}
	var publishStep = this;
	this._request(
		function() {
			sbProgressBar.endProcess();
			publishStep._isPublishing = false;
			sbNavigationObject.getForm().action = baseUrl + '/Wizard/Publish/Save';
		}
	);
	sbApplyChangesObject.uregisterForcedSaving();
	sbNavigationObject.getForm().action = baseUrl + '/Wizard/Publish/Save';
}

SbPublishStep.prototype._request = function(onCompleteMethod) {
	var publishStep = this;
	sbNavigationObject.ajaxRequest(baseUrl + '/Ajax/Wizard/Publish', self, {
		onSuccess: function(transport) {
			sbApplyChangesObject.uregisterChange();
			sbApplyChangesObject.uregisterForcedSaving();
		    var html = ('undefined' == typeof transport.responseText) ? '' : transport.responseText.toString();
		    $(publishStep._formContainer).innerHTML = html.stripScripts();
		    html.evalScripts();
			if ('undefined' != typeof(sbPublicationSettingsForm)) {
				sbPublicationSettingsForm.addOnRevertButtonHideEvent(function() {publishStep.restoreRevertSettings();});
				sbPublicationSettingsForm.addOnRevertSettingsEvent(function() {return publishStep.onRevert();});
				sbPublicationSettingsForm.addOnSubmitEvent(function() {return publishStep.onSubmit();});
			}
		},
			
		onFailure: function(transport) {
			alert('Internal SiteBuilder error');
			$(publishStep._formContainer).update(transport.responseText);
		},

		onCreate: function() {
			if (publishStep._updatePreloader && !publishStep._isPublishing) {
				sbPreloader.show();
				sbPreloader.disable();
			}
			$$('body').invoke('setStyle', { cursor: 'wait' });
		},	
			
		onComplete: function() {
			if (publishStep._updatePreloader && !publishStep._isPublishing) {
				sbPreloader.enable();
				sbPreloader.hide();
			}
			$$('body').invoke('setStyle', { cursor: '' });
			if ('function' == typeof(onCompleteMethod)) {
				onCompleteMethod();
			}
		}
	});
}

SbPublishStep.prototype.registerOnSubmitFunction = function(method) {
	this._publishSettingsFormOnSubmitMethod = method;
}

SbPublishStep.prototype.getForm = function() {
	return sbNavigationObject.getForm();
}

SbPublishStep.prototype.go = function() {
	this._request();
}

SbPublishStep.prototype.onRevert = function() {
	sbNavigationObject.getForm().action = baseUrl + '/Wizard/Publish/RevertSettings';
	this._isReverting = true;
}

SbPublishStep.prototype.restoreRevertSettings = function() {
	sbNavigationObject.getForm().action = baseUrl + '/Wizard/Publish/Save';
	this._isReverting = false;
}

SbPublishStep.prototype.onSubmit = function() {
	var result = false;
	if (this._isReverting) {
		result = true;
	} else {
		if (this._isPublishing) {
			result = sbPublicationSettingsForm.validate();
			if (result) {
				this._runProgressBar();
			} else {
				this._isPublishing = false;
			}
		} else {
			result = sbPublicationSettingsForm.validateIfNotEmpty();
		}
	}
	if (!result) {
		sbApplyChangesObject.uregisterForcedSaving();
		sbNavigationObject.getForm().action = baseUrl + '/Wizard/Publish/Save';
	}
	return result;
}

SbPublishStep.prototype._runProgressBar = function() {
	var republicationControl = self.document.getElementById('fullRepublication');
	if (republicationControl) {
		if (republicationControl.checked) {
			sbProgressBar.setFullPublicationExpectedTime();
		} else {
			sbProgressBar.setShortPublicationExpectedTime();
		}
	} else {
		sbProgressBar.setFullPublicationExpectedTime();
	}
	sbProgressBar.process();
}