/* ## GLOBAL config */
var MessageBoxImageDir = 'images/layout/MessageBox/';		// where does MessageBox have it's images?

/* ## AJAX mailer */
function sendEmail() {

	// Get the DOM values as predefined variables
	var site_addr	= $('site_addr').value;

	var name_value	= $('name').value;
	var email_value	= $('email').value;
	var topic_value	= $('topic').value;
	var message_value	= $('message').value;

	// Check if something's missing and display an error if it does
	if(!name_value) { var error = true; }
	if(!email_value) { var error = true; }
	if(!message_value) { var error = true; }

	// Die if any error occured
	if(error) { updateMessageBox('error', 'Error!', 'wypełnij wszystkie pola', 1450); }

	// Proceed with AJAX commands if not
	if(!error) {

		// Escape the variables
		var site_data	= encodeURIComponent(site_addr);

		var name_data	= encodeURIComponent(name_value);
		var email_data	= encodeURIComponent(email_value);
		var topic_data	= encodeURIComponent(topic_value);
		var message_data	= encodeURIComponent(message_value);

		// Set the parser URL
		var url = site_addr + 'components/ContactForm_AjaxSendMail.php';

		// Set the parameters with escaped data
		var pars = 'site=' + site_data + '&name=' + name_data + '&email=' + email_data + '&topic=' + topic_data + '&message=' + message_data + '&site_id=' + $F('site_id');

		// Disable the button and stuff
		$('ContactTable').className = 'wait';

		//$('sendbutton').disabled = true;
		Effect.DropOut('sendbutton');

		// Update the message box
		updateMessageBox('loading', 'Poczekaj...', 'wysyłanie wiadomości');

		// ! BUILD AJAX REQUEST
		var myEmail = new Ajax.Request(
			url,
			{
				method: 'post',
				parameters: pars,
				onFailure: parseEmailFailure,
				onComplete: parseEmailResponse
			});
	}
}

/* ## AJAX responses and responce parsers */
function parseEmailFailure(request) {

	// throw an error
	$('ContactTable').className = '';
	updateMessageBox('error', 'Error!', 'nie udało się wysłać wiadomości<br />spróbuj ponownie', 2150);
	setTimeout("Effect.Appear('sendbutton');", 1100);
}

function parseEmailResponse(request) { 

	// Check response for errors
	if(request.responseText == 'ok') {

		// Make the contact form fully visible again
		$('ContactTable').className = '';

		// Update the message box
		updateMessageBox('ok', 'Ok!', 'wiadomość wysłana!', 1450);

		// Clear the topic and message body
		$('topic').value = '';
		$('message').value = '';

		// Appear the send button
		Effect.Appear('sendbutton');

	} else { parseEmailFailure(request); }
}

/* ## MESSAGE BOX: Update the text and the icon */
function updateMessageBox(image,alt,text,delay) { 

	// Put correct HTML in the message box
	$('MessageBox').innerHTML = '<p><img src="' + MessageBoxImageDir + image + '.gif" alt="' + alt + '" /><br />' + text + '</p>';

	// Check if we need to show the message box or is it already visible
	if($('MessageBox').style.display == 'none') { new Effect.Appear('MessageBox'); }

	// Hide the messagebox if any delay is set
	if(delay) { setTimeout("Effect.DropOut('MessageBox');", delay); }
}

/* ## MESSAGE BOX: Preload message images */
function preloadMessageBox() {

	var images = new Array('loading', 'error', 'ok');
	var preloaded = new Array();

	images.each( function(image) {

		preloaded[image] = new Image;
		preloaded[image].src = MessageBoxImageDir + image + '.gif';
	});

}

/* ## ENGINE: Apply matching properties for every class */
function setCFProperties() {

	// define the 'eveything' var
	var allTags = $('ContactForm').getElementsByTagName('*');
	var tags = $A(allTags);

	// run the 'foreach' loop and apply attributes for each tag
	tags.each( function(tag) {

		// ## TEXTAREAS highlighting
		if((tag.nodeName == 'TEXTAREA') || (tag.nodeName == 'INPUT') && (tag.className != 'button')) {

			tag.onmouseover = new Function('this.className = "highlight";');
			tag.onmouseout = new Function('this.className = "";');
		}

		// ## SEND button
		if(tag.id == 'sendbutton') { 

			tag.onclick = new Function('sendEmail();');
		}

	});

}

/* ## ENGINE: Preload images */
function preloadImages() {

}
 
/* ## ENGINE: Plugin local runtime  */
function localRuntime() {
	setCFProperties();
	preloadMessageBox();
}

/* *** IN THE FUTURE:
        CHANGE THE ENGINES SO THE LocalRuntime() WOULD BE EXECUTED AUTOMATICALLY FROM SOME global.js FOR EVERY PLUGIN   */
window.onload = function() { localRuntime(); }

