$(document).ready(function() {
	
	// We have JS, so show JS items
	$('#header #accessability').css('display', 'inline');
	
	// Accessability - change bosy text size
	$('#header #accessability a').click(function(e) {
		e.preventDefault();
		$('#header #accessability a.on').removeClass('on');
		if($(this).attr('id') == 'small') {
			$("body").css('fontSize', '12px');
			$(this).addClass('on');	
		} else  if($(this).attr('id') == 'medium') {
			$("body").css('fontSize', '16px');
			$(this).addClass('on');		
		} else  if($(this).attr('id') == 'large') {
			$("body").css('fontSize', '20px');
			$(this).addClass('on');		
		}
	});	
	
	// Spamblock email
	var user = 'info';
	var domain = 'musicale';
	var extension = 'co.uk';
	$('span.email').html('<a href="mailto:' + user + '@' + domain + '.' + extension + '">' + user + '@' + domain + '.' + extension + '</a>');

	// Fading top images
	var imgs = $("#header #images > div").size();
	var current = 0;
	if(imgs != 1) {
		window.setInterval (function () { 
			var next = (current+1)%imgs;
			var children = $('#header #images').children('div');
			$(children.eq(current)).fadeOut(function(e) {
				$(children.eq(next)).fadeIn('slow');
			});
			current = next;
		}, 10000); 
	}
	
	// Add drop  down icon to menu items with subnav
	$('#header ul li').children('ul').each(function(e) {
		$(this).parent().children('a').addClass('sub');
	});
	
	// Image overlay
	$('#main #right img').not('.noprocess', this).each(function(e) {
		var img = $(this).attr('src');
		var width = $(this).attr('width');
		var height = $(this).attr('height');
		var alt = $(this).attr('alt');
		var altcaption = '';
		var subdir = img.indexOf('/img/'); // We are in a subdirectory if this is found other than at the start of the string
		var base = '';
		if(subdir != -1) {
			base = img.substr(0, subdir+1);
		}
		if(alt != undefined) {
				altcaption = '<p>'+alt+'</p>';
		}
		if(img != undefined && width != undefined && height != undefined) {
			$(this).replaceWith('<div class="rightpic" style="width:' + width + 'px;"><div style="background:url(' + img + '); width:' + width + 'px; height:' + height + 'px;"><img src="' + base + 'img/img_overlay.png" width="1000" height="1000" alt="' + alt + '" title="' + alt + '" /></div>' + altcaption + '</div>');
		} 
	});

	// News ticker
	if(jQuery().ULTicker) {
		$('#left #notice ul').ULTicker({speed:4000});
	}
	
	// Form validation
	if(jQuery().Validate) {
		$('form#order').Validate({emailFields:Array('ff_email')});
		$('form#add_instrument').Validate({invalidValues:Array('', 'Please select...')});
		$('form#frm_add_course').Validate();
		$('form#frm_parent_details').Validate({emailFields:Array('frm_parent_email')});
		$('form#frm_centre').Validate();
		$('form#frm_participant_details').Validate();
		$('form#frm_taster').Validate({emailFields:Array('ff_email')});
		$('form#frm_library').Validate({emailFields:Array('f_email')});
		$('form#frm_bands').Validate({emailFields:Array('f_email')});
		$('form#frm_deptacct').Validate({emailFields:Array('f_email')});
		$('form#frm_musicfest').Validate();
		$('form#frm_training').Validate({emailFields:Array('ff_email')});
		$('form#frm_prep').Validate({emailFields:Array('ff_email')});
	}
	
	// Highlight odd rows of tables with class highlight
	$("table.highlight tr:odd").addClass("highlight");
	
	// Submit the payment form automatically to transfer to payment system
	$('form#payform').submit();
	
	// Musicale Holdiays Form
	$('form#frm_musicfest').hide(0);
	$('p#musicfest').click(function(e){
		e.preventDefault();
		$('form#frm_musicfest').slideDown(1000);
		});
		
	
	
	// Musicale Shop Forms
	$('form#frm_deptacct').hide(0);
	$('form#frm_taster').hide(0);
	$('h3#taster').click(function(e){
		e.preventDefault();		
		$('form#frm_deptacct').slideUp(1000,function(e){
			$('form#frm_taster').slideDown(1000);
		});

	});
	
	$('h3#deptacct').click(function(e){
		e.preventDefault();	
		$('form#frm_taster').slideUp(1000,function(e){
			$('form#frm_deptacct').slideDown(1000);
		});

	});
	
	
	
});

