// JavaScript Document
$(document).ready(function (){

	var aClearOnClicks = new Array();

	$('.clearOnClick').each(function(i) {

		aClearOnClicks[this.id] = this.value;
	
	});

	$('.clearOnClick').bind("focus", function(e) {

		if ($(this).val() == aClearOnClicks[$(this).attr("id")]) {
			
			$(this).val('');
			
		}

	});

	$('.clearOnClick').bind("blur", function(e) {

		if ($(this).val() == '') {

			$(this).val(aClearOnClicks[$(this).attr("id")]);
			
		}

	});

	$('.topMenu th, .CMSMenu th').click(function(e) {

		if ($(e.target).attr("tagName") != 'A') {
			
			window.location = $(e.target).find('a')[0].href;
			
		}

	});

	$('.topMenu th, .CMSMenu th').mouseover(function(e) { if ((e.target).tagName != 'A') { swapColours(); } });
	$('.topMenu th, .CMSMenu th').mouseout (function(e) { if ((e.target).tagName != 'A') { swapColours(); } });

	if (window.location.toString().indexOf('/manage/') == -1) {
		
		startScrolling();
		
	}

});

function swapColours() {

	//Flip colours!
	boxColour  = $(this).css("background-color");
	linkColour = $(this).find('a').css("color");
	
	$(this).find('a').css("color", boxColour);
	$(this).css("background-color", linkColour);

}
	
var cyclesPerSecond = 25;
var stepSize = 2;
var pauseTime = 2000;


function startScrolling() {

	//Initialise scroller
	$('.scrollingMessageInner').css("left", $('.scrollingMessageOuter').width());

	scrollingMessageIntervalID = setInterval("moveTarget('.scrollingMessageOuter', '.scrollingMessageInner')", 1000 / cyclesPerSecond);

}

function moveTarget(parent, target) {

	parentRight = parseInt($(parent).width());
	targetLeft  = parseInt($(target).css("left").replace(/px/, ''));
	targetWidth = parseInt($(target).width());

	if ((targetLeft + targetWidth) < 0) {
		
		clearInterval(scrollingMessageIntervalID);
		setTimeout(startScrolling, pauseTime);
	
	} else {
	
		$(target).css("left", targetLeft - stepSize);
		
	}

	return true;
	
}


function checkField(obj, defValue, msg) {

	if (obj.value == '' || obj.value == defValue) {
	
		obj.focus();

		if (msg != false)
			alert(msg);

		return false;
	
	}
	
	return true;
	
}

function checkTicks(aFormIDs, msg) {

	var formID;
	var oneChecked = false;

	for (formID in aFormIDs) {

		if (document.getElementById(aFormIDs[formID]).checked) {
			
			oneChecked = true;
		
		}
	
	}

	if (oneChecked) {
		
		return true;
	
	} else {
	
		if (msg != false) {
		
			alert(msg);
			
		}
		
		return false;
	
	}

}

function checkEmail(obj, defValue, msg) {

	if (obj.value == '' || obj.value == defValue) {

		obj.focus();

		if (msg != false)
			alert(msg);

		return false;
	
	}
	
	if (obj.value.indexOf('@') == -1 || obj.value.indexOf('@') > obj.value.lastIndexOf('.')) {

		obj.focus();

		if (msg != false)
			alert(msg + ' The value entered is not a valid email address.');

		return false;
	
	}
	
	return true;
	
}

function checkMailingListForm(form) {

	if (checkField(form.txtName, 'enter your name...', 'Please enter your name.') == false)
		return false;

	if (checkEmail(form.txtEmail, 'enter your email address...', 'Please enter your email address.') == false)
		return false;

	var chkElNames = new Array('chkFood', 'chkOffers', 'chkRacing', 'chkAccomodation', 'chkWine', 'chkEvents', 'chkBus', 'chkTours');

	if (checkTicks(chkElNames, false) == false && checkField(form.txtOther, '', false) == false) {

		alert('Please indicate your interests');
		return false;

	}

	return true;

}

function checkCMSLogin(form) {
	
	if (checkField(form.txtUsername, '', 'Please enter your username.') == false)
		return false;

	if (checkField(form.txtPassword, '', 'Please enter your password.') == false)
		return false;

	return true;

}

function checkScroller(form) {
	
	if (checkField(form.txtText, '', 'Please enter the new text to use on the text scroller.') == false)
		return false;

}