﻿var Mint = {};

Mint.CopyImage = function (div) {
	//	copy image rollover/click handling
	this.div = $(div);
	
	this.dim = this.div.getDimensions();
	
	this.div.setStyle({
		width: this.dim.width +"px",
		height: this.dim.height +"px"
	});
	
	this.cancelEffects = function () {
		if (this.growEffect) { this.growEffect.cancel(); }
		if (this.shrinkEffect) { this.shrinkEffect.cancel(); }
	};
	
	this.grow = function (evt) {
		this.cancelEffects();
		
		var width = this.dim.width * 4;
		var height = this.dim.height * 4;
		
		this.growEffect = new Effect.Morph(this.div, {
			style: "width:"+ width +"px;height:"+ height +"px;",
			duration: .4
		});
	};
	
	this.shrink = function (evt) {
		this.cancelEffects();
		
		var width = this.dim.width;
		var height = this.dim.height;
		
		this.growEffect = new Effect.Morph(this.div, {
			style: "width:"+ width +"px;height:"+ height +"px;top:0;left:0;",
			duration: .4
		});
		
		var td = this.div.up('td');
		if (td)
		{
			td.setStyle({
				height: (height * 4) +"px"
			});
			
			this.shrinkEffect = new Effect.Morph(td, {
				style: "height:"+ height +"px;",
				delay: .5,
				duration: .8
			});
		}
	};
	
	this.div.observe('mouseover', this.grow.bind(this));
	this.div.observe('mouseout', this.shrink.bind(this));
};


Mint.ContactFormSuccess = function () {
	alert("Thank you. Your enquiry has been received and one of our staff will be in contact with you shortly.");
	$('contactForm').reset();
};

Protoform = function (form, success) {
	this.form = $(form);
	this.onSuccess = success;
	
	this.highlightClassName = 'protoformHighlight';
	
	this.onSubmit = function (e) {
		Event.stop(e);
		this.clearHighlights();
		this.clearMessages();
		this.disableForm();
		this.lastRequest = this.form.request({onComplete:this.onComplete.bind(this)});
	};
	
	this.disableFormActual = function () {
		var element;
		for (var i = this.form.elements.length; i--;)
		{
			element = this.form.elements[i];
			if (!element.disabled)
			{
				element.disabled = true;
				element._protoform_disabled = true;
			}
		}
	};
	
	this.disableForm = function () {
		window.setTimeout(this.disableFormActual.bind(this), 1);
	};
	
	this.clearHighlights = function () {
		for (var i = this.form.elements.length; i--;)
			$(this.form.elements[i]).removeClassName(this.highlightClassName);
	};
	
	this.clearMessages = function () {
		var node;
		if (node = $(this.form.id +'_message'))
			node.update();
		for (var i = this.form.elements.length; i--;)
			if (node = $(this.form.id +'_'+ this.form.elements[i].name +'_message'))
				node.update();
	};
	
	this.onComplete = function (request) {
		this.enableForm();
		
		var response = request.responseText.evalJSON(true);
		if (response.success)
			this.onSuccess.call(this, request, response);
		else
			this.onFailure.call(this, request, response);
	};
	
	this.onFailure = function (request, response) {
		var node;
		
		if (response.message)
			if (node = $(this.form.id +'_message'))
				node.update(response.message)
			else
				alert(response.message);

		for (var i = this.form.elements.length; i--;)
		{
			var element = this.form.elements[i];
			var reaction = response.reactions[element.name];
			
			if (reaction && reaction.highlight)
				$(element).addClassName(this.highlightClassName);
			
			node = $(this.form.id +'_'+ element.name +'_message');
			if (reaction && node)
				node.update(reaction.message ? reaction.message : "");
		}
	};
	
	this.enableForm = function () {
		var element;
		for (var i = this.form.elements.length; i--;)
		{
			element = this.form.elements[i];
			if (element._protoform_disabled)
			{
				element.disabled = false;
				element._protoform_disabled = undefined;
			}
		}
	};
	
	this.form.observe('submit', this.onSubmit.bind(this));
};

Event.observe(window, "load", function(){
	var node;
	
	var copyImageList = document.getElementsByClassName("copyImage");
	for (var i = copyImageList.length; i--;)
	{
		new Mint.CopyImage(copyImageList[i]);
	}
	
	if (node = $('contactForm'))
	{
		node.action = "submit.asp";
		new Protoform(node, Mint.ContactFormSuccess);
	}
});

