// CoriDevlin JavaScript functions
// Copyright GroundVision Enterprises 2008

var textileArray = new Array();

var wordsArray = null;
var wordIndexLast = -1;

var wordIntervalInitialSeconds = 4;
var wordIntervalMinSeconds = 2;
var wordIntervalMaxSeconds = 6;

var wordFontSizeMinPixels = 24;

var wordMovementMinPixels = 48;
var wordMovementMaxPixels = 96;

var wordDurationMinSeconds = 4;
var wordDurationMaxSeconds = 10;

function deleteWord(object)
{
	word = object.element;
	$('wordsbox').removeChild(word);
}

function fadeWord(object)
{
	word = object.element;
	new Effect.Opacity(word, {duration: word.wordDurationSeconds / 2, from: 1.0, to: 0.0, afterFinish: deleteWord});
}

function createWord()
{
	var wordsBox = $('wordsbox');
	var wordsBoxWidth = wordsBox.getWidth();
	var wordsBoxHeight = wordsBox.getHeight();
	
	var word = document.createElement('div');
	Element.extend(word);
	word.addClassName('word');

	var wordIndex;
	do
		wordIndex = Math.floor(Math.random() * wordsArray.length);
	while (wordIndex == wordIndexLast);
	word.innerHTML = wordsArray[wordIndex];
	wordIndexLast = wordIndex;
	
	var fontSizePixels = Math.random() * (wordsBoxHeight - wordFontSizeMinPixels) + wordFontSizeMinPixels;
	var red = Math.floor(Math.random() * 128 + 64);
	var green = Math.floor(Math.random() * 128 + 64);
	var blue = Math.floor(Math.random() * 128 + 64);
	var colour = '#' + red.toString(16) + green.toString(16) + blue.toString(16);
	word.setStyle({fontSize: fontSizePixels + 'px', color: colour, opacity: 0});
	if (Prototype.Browser.IE)
		word.setStyle({background: colourLow});  // under IE words are shown anti-aliased to black without an explicit background (unfortunately, the explicit background means overlapping words block each other out, but if you will use Microsoft products, what do you expect?)
	
	wordsBox.appendChild(word);
	
	wordWidth = word.getWidth();
	wordHeight = word.getHeight();
	var wordLeft = Math.floor(Math.random() * (wordsBoxWidth - wordWidth));
	var wordTop = Math.floor(Math.random() * (wordsBoxHeight - wordHeight));
	var wordLeftDelta = Math.floor(Math.random() * (wordMovementMaxPixels - wordMovementMinPixels) + wordMovementMinPixels);
	if (Math.random() < 0.5)
	{
		if (wordLeft - wordLeftDelta >= 0)
			wordLeftDelta = -wordLeftDelta;
	}
	else
	{
		if (wordLeft + wordLeftDelta >= wordsBoxWidth - wordWidth)
			wordLeftDelta = -wordLeftDelta;
	}

	word.setStyle({left: wordLeft + 'px', top: wordTop + 'px'});
	
	var wordDurationSeconds = Math.random() * (wordDurationMaxSeconds - wordDurationMinSeconds) + wordDurationMinSeconds;
	word.wordDurationSeconds = wordDurationSeconds;
	
	new Effect.Opacity(word, {duration: wordDurationSeconds / 2, afterFinish: fadeWord});
	new Effect.Move(word, {duration: wordDurationSeconds, transition: Effect.Transitions.linear, x: wordLeftDelta, mode: 'relative'});

	setTimeout('createWord()', (Math.random() * (wordIntervalMaxSeconds - wordIntervalMinSeconds) + wordIntervalMinSeconds) * 1000);
}

function initialiseWords()
{
	wordsArray = wordsString.split(',');
	for (var wordIndex = 0; wordIndex < wordsArray.length; wordIndex++)
		wordsArray[wordIndex] = wordsArray[wordIndex].strip();
	
	setTimeout('createWord()', wordIntervalInitialSeconds * 1000);
}

function validateContactForm(form)
{
	var phone = form.phone.value;
	var email = form.email.value;
	
	if (phone.blank() && email.blank())
	{
		alert('Please enter a valid phone number or e-mail address so that Cori can get back to you.');
		return false;
	}
	
	if (!email.blank() && (email.indexOf('@') == -1))
	{
		alert('Please check that you\'ve entered your e-mail address correctly (or you can leave it blank and just enter your phone number).');
		return false;
	}
	
	return true;
}

