jQuery.myvars = {
	spinner_active: false
};

function contactUs () {
	$.ajax({
		type: 'post',
		url: 'ajax/contact.php',
		data: $('#contact').serialize(),
		success: function (data) {
			ourAlert(data);
			$('#contact').clearForm();
			window.location = 'thankyou.php';
		},
		error: function (data) {
			ourAlert(data.responseText);
		}
	});
}

function ourAlert (text,success) {
	$.ajax({
		type: 'post',
		url: 'js/components/our_alert.php',
		success: function (data) {
			$('body').append(data);
			$('#output_message_text').html(text);

			$('#our_alert').dialog({
				modal: true,
				closeOnEscape: true,
				buttons: {
					Ok: function() {
						$(this).dialog("close");
						$('#our_alert').remove();
					}
				}
			});

			if (success) {
				$('#our_alert').bind( "dialogclose", function(event, ui) {
					if ($.isFunction(success)) {
						success.call();
					} else {
						eval(success);
					}
					$('#our_alert').remove();
				});
			}
		},
		error: function () {
		}
	});
}


jQuery.fn.showSpinner = function(options) {
	spinnerSettings = jQuery.extend( {
		delay : 1000,
		text : 'Please wait<br /><br /><img src="images/animation_wait.gif" alt="Please wait..." />',
		message: null
	}, options);

	if (spinnerSettings.message != null) {
		spinnerSettings.text = spinnerSettings.message + '<br /><br /><img src="images/animation_wait.gif" alt="Please wait..." />';
	}

	var doExec = true;
	jQuery.myvars.spinner_active = true;

	var div_txt = ''
				+ '<div class="waiting_spinner" title="Please Wait...">'
				+ spinnerSettings.text
				+ '</div>';

	setTimeout(function() {
		if (jQuery.myvars.spinner_active) {
			jQuery('body').append(div_txt);
			jQuery('.waiting_spinner').dialog({
				modal: true
			});
		}
	},spinnerSettings.delay);

	return this;
}

jQuery.fn.hideSpinner = function(options) {
	hideSettings = jQuery.extend( {
		success : null
	}, options);

	jQuery.myvars.spinner_active = false;
	jQuery('.waiting_spinner').remove();

	return this;
}

jQuery.fn.clearForm = function() {
	return this.each(function() {
		var type = this.type, tag = this.tagName.toLowerCase();
		if (tag == 'form')
			return jQuery(':input',this).clearForm();
		if (type == 'text' || type == 'password' || tag == 'textarea')
			this.value = '';
		else if (type == 'checkbox' || type == 'radio')
			this.checked = false;
		else if (tag == 'select')
			this.selectedIndex = -1;
	});
};
