/******************************************
 * The Contact Form Modal Class
 *
 * Opens a contact modal on the page.
 ******************************************/

onepica.Modal.Contact = Class.create(onepica.Modal.Content, {
	/**
	 * Constructor
	 *
	 * Add the option {useOverlay: true} to disable the screen when the modal is open.
	 */
	initialize: function (customOptions) {
		this.containerId = 'contact-modal';
		var options = Object.extend(this.options, {
			className: 'content-modal',
			zIndex: 1000,
			showEffect: Effect.Appear,
			hideEffect: Effect.Fade,
			useOverlay: true,
			onClose: this.hideWindow.bindAsEventListener(this),
			recenterAuto: false
		});

		// custom options
		options = Object.extend(options, customOptions || {});
		loadOptions = Object.extend(this.loadOptions, customOptions);
		if (options['useOverlay'] && options['useOverlay'] === true) {
			this.useOverlay = true;
		}
		loadOptions['destroyOnClose'] = options['destroyOnClose'];

		this.window = new Window(this.containerId, options);
		this.loadWindow = new Window(this.containerId + '-load', loadOptions);
	},

	attachListeners: function () {
		this.container = $(this.containerId);
		this.form = new onepica.Form('contactForm', false);
		this.submit = $('contactFormSubmit');
		this.submit.observe('click', this.submitForm.bind(this));
		this.messageContainer = $('contact-message-container');
		this.loadingIcon = $('contact-form-loading');
	},

	/**
	 * Override the parent load to add a 'secure' param.
	 *
	 * @param string url
	 */
	load: function(url) {
		if (Object.isString(url)) {
			this.setUrl(url);
		}
		var params = {};
		if (document.location.protocol == 'https:') {
			params.secure = 1;
		}
		else {
			params.secure = 0;
		}
		this.eventCreate = this.loadCreate.bindAsEventListener(this);
		this.eventSuccess = this.loadSuccess.bindAsEventListener(this);
		this.eventFailure = this.loadFailure.bindAsEventListener(this);

		var options = {
			method: 'get',
			parameters: params,
			onCreate: this.eventCreate,
			onSuccess: this.eventSuccess,
			onFailure: this.eventFailure
		};
		this.ajaxRequest = new onepica.Ajax.Request(this.url, options);
	},
	
	/**
	 * Load success callback.
	 */
	loadSuccess: function(transport) {
		this.loadWindow.hide();
		this.window.getContent().innerHTML = transport.responseText;
		this.evalHtml(this.window.getContent());
		this.showWindow();

		/**
		 * For IE6, delay for a couple milliseconds to allow the elements to register.
		 */
		if (!/MSIE (5\.5|6\.)/.test(navigator.userAgent)) {  // IE6
			setTimeout(function () {
				this.attachListeners();
			}.bind(this), 100);
		}
		else {
			this.attachListeners();
		}

		// track the page view
		screenManager.trackPageview(this.url);
	},

	/**
	 * Submit the form if validation succeeds.
	 */
	submitForm: function () {
		if (this.form.validator.validate()) {
			new onepica.Ajax.Request(this.form.form.getAttribute('action'), {
				method: 'post',
				parameters: this.form.form.serialize(),
				onSuccess: this.postSuccess.bindAsEventListener(this),
				onCreate: this.postCreate.bindAsEventListener(this),
				onFailure: this.postFailure.bindAsEventListener(this)
			});
		}
	},
	
	postSuccess: function(transport) {
		this.formHeight = this.form.form.getHeight();
		this.messageContainer.innerHTML = transport.responseText;
		this.loadingIcon.hide();
		this.submit.show();
		this.adjustModalHeight();
	},
	
	postFailure: function(transport) {
		this.loadingIcon.hide();
		this.submit.show();
	},

	postCreate: function(transport) {
		this.submit.hide();
		this.loadingIcon.show();
	},

	/**
	 * Adds height to the modal to account for the response message.
	 */
	adjustModalHeight: function () {
		var heightDiff = this.form.form.getHeight() - this.formHeight;
		var newHeight = (this.container.getHeight() + heightDiff).toString() + 'px';
		this.container.setStyle({height: newHeight});
		$(this.containerId + '_content').setStyle({height: newHeight});
	}
});