// JavaScript Document

// when input is focused remove default text
function formtext(form_element, default_value)
{
	$(form_element).focus(function(){
		if(this.value == default_value) {
			this.value = '';
		}
	});
	
	$(form_element).blur(function(){
		if(this.value == '') {
			this.value = default_value;
		}
	});		
}

function countryShow(country)
{
	$("#"+country+"_button").click(function(event){
		event.preventDefault();
		$("#country_menu li a").css({'background-position' : 'top'});
		$("#"+country+"_button a").css({'background-position' : 'bottom'});
		$("#contact_info>div").hide();
		$("#contact_"+country).show();
	});
}

function setCountryTitle(img_src, img_alt)
{
	$("#country-filter-title img").attr('src', img_src);
	$("#country-filter-title img").attr('alt', img_alt);
}

var current_stage = 1;

function setFormStage(stage_num)
{
	set_form = true
	// only validate if from a stage
	if(stage_num > 1) {
		set_form = validateFormStage(stage_num - 1);
	}
	if(set_form) {
		
		// show next or submit depending on stage
		if(stage_num < 3) {
			$("#form_nextstep").show(0);
			$("#sv_submit_button").hide(0);
		} else {
			$("#form_nextstep").hide(0);
			$("#sv_submit_button").show(0);
		}
		
		// show correct form elements
		$("#form_content .form_stage").hide(0);
		$("#stage"+stage_num).show(0);
		
		// update stage icon
		$("#progress_indicator li").css('background-position', 'top');
		$("#icon_stage"+stage_num).css('background-position', 'bottom');
		
		current_stage = stage_num;
	}
}

function validateFormStage(stage_num)
{
	flag = true;
	$("#stage"+stage_num+" .required").each(function() {
		if(this.value == '') {
			$(this).css('border-color', 'red');
			$(this).fadeTo(666, 0.2, function(){ $(this).fadeTo(666, 1); });
			flag = false;
		} else {
			$(this).css('border-color', '#A5ACB2');
		}
	});
	
	return flag;
}

function checkSessions()
{
	session_enabled = false;
	$("#sv_srtrcworkshop, #sv_safeworkshop, #sv_islamophobia").each(function() {
		if(this.checked) {
			session_enabled = true;
		}
	});
	
	if(session_enabled) {
		$("#sv_training").attr('disabled', false);
	} else {
		$("#sv_training").attr('checked', false);
		$("#sv_training").attr('disabled', true);
	}
}

function viewScores()
{
	tb_show("High Scores", "quiz/quiz.php?lb=1?height=412&width=419");
}

$(document).ready(function(){

	countryShow("england");
	countryShow("scotland");
	countryShow("wales");
	
	// Add homepage Flash
	$('#home-flash').html("").flash({
		swf: 'flash/video-replac.swf',
		height: 338,
		width: 333
	});
	
	// Add quiz Flash
	$('#quiz-flash').html("").flash({
		swf: 'flash/quizgame.swf',
		height: 466,
		width: 928,
		wmode: 'opaque'
	});
	
	// Set default input values
	formtext("#newsletter_name", "Your Name");
	formtext("#newsletter_email", "Your Email");
	formtext("#sv_television_details", "Please specify");
	
	// css cheats
	$("#mainmenu li:last-child").addClass('last');
	$(".secondary-player-view:first-child").css({'margin-right' : '13px'});
	$("#event-summary hr").css("opacity", 0.5);
	$(".zebrastripe >:even").addClass("even");
	$(".zebrastripe >:odd").addClass("odd");
	
	original_title = $("#country-filter-title img").attr('src');
	original_alt = $("#country-filter-title img").attr('alt');
	
	$('a#playerviews_quicklink').click(function(event) {
		event.preventDefault();
		target = $('#playerviews_quicklist');
		target.toggle(666);
	});
	
	$("#country-filter li a").mouseout(function() {
		setCountryTitle(original_title, original_alt);
	});
	
	$("#country-filter li.all a").mouseover(function() {
		setCountryTitle('images/filtertitle_all.gif', 'Events from All Countries');
	});
	
	$("#country-filter li.england a").mouseover(function() {
		setCountryTitle('images/filtertitle_eng.gif', 'Events from England');
	});
	
	$("#country-filter li.scotland a").mouseover(function() {
		setCountryTitle('images/filtertitle_sco.gif', 'Events from Scotland');
	});
	
	$("#country-filter li.wales a").mouseover(function() {
		setCountryTitle('images/filtertitle_wal.gif', 'Events from Wales');
	});

// again for news page	
	$("#country-filter.news-events li.all a").mouseover(function() {
		setCountryTitle('images/filternews_all.gif', 'News from All Countries');
	});
	
	$("#country-filter.news-events li.england a").mouseover(function() {
		setCountryTitle('images/filternews_eng.gif', 'News from England');
	});
	
	$("#country-filter.news-events li.scotland a").mouseover(function() {
		setCountryTitle('images/filternews_sco.gif', 'News from Scotland');
	});
	
	$("#country-filter.news-events li.wales a").mouseover(function() {
		setCountryTitle('images/filternews_wal.gif', 'News from Wales');
	});

// again for press page	
	$("#country-filter.press-coverage li.all a").mouseover(function() {
		setCountryTitle('images/filterpress_all.gif', 'Press from All Countries');
	});
	
	$("#country-filter.press-coverage li.england a").mouseover(function() {
		setCountryTitle('images/filterpress_eng.gif', 'Press from England');
	});
	
	$("#country-filter.press-coverage li.scotland a").mouseover(function() {
		setCountryTitle('images/filterpress_sco.gif', 'Press from Scotland');
	});
	
	$("#country-filter.press-coverage li.wales a").mouseover(function() {
		setCountryTitle('images/filterpress_wal.gif', 'Press from Wales');
	});

// again for schools page
	$("#educational #country-filter li.england a").mouseover(function() {
		setCountryTitle('images/schoolfilter_eng.gif', 'English Schools');
	});
	
	$("#educational #country-filter li.scotland a").mouseover(function() {
		setCountryTitle('images/schoolfilter_sco.gif', 'Scottish Schools');
	});
	
	$("#educational #country-filter li.wales a").mouseover(function() {
		setCountryTitle('images/schoolfilter_wal.gif', 'Welsh Schools');
	});

// Main menu functions

	var sections = new Array("news", "about", "educational", "resources", "get-involved");
		
	$("#mainmenu>li").mouseover(function() {
		// stop any timers waiting to hide the menu
		$("#mainmenu>li").stopTime("hidemenu");
		
		$("#mainmenu a.root").css('background-color', 'transparent');
		$("#mainmenu ul.submenu").css('display', 'none');
		$(this).children("a.root").css('background-color', '#9D0001');
		$(this).children("ul.submenu").css('display', 'block');
	});
	
	$("#mainmenu>li").mouseout(function() {
		// stop any timers waiting to hide the menu
		$("#mainmenu>li").stopTime("hidemenu");
		// keep the menu open for 3 seconds after leaving the li element
		$(this).oneTime(3000, "hidemenu", function() {
			//console.log("hiding "+$(this).attr('class'));
			$("#mainmenu a.root").css('background-color', 'transparent');
			$("#mainmenu ul.submenu").css('display', 'none');
			for(i in sections) {
				$("#"+sections[i]+" ."+sections[i]+" a.root").css('background-color', '#9D0001');
				$("#"+sections[i]+" ."+sections[i]+" ul.submenu").css('display', 'block');
			}
		});
	});
	
	for(i in sections) {
		$("#"+sections[i]+" ."+sections[i]+" ul.submenu").css('display', 'block');
	}

// School Visit Form
	setFormStage(current_stage);
	checkSessions();
	
	$("#sv_form").submit(function() {
		if(current_stage < 3) {
			setFormStage(current_stage + 1);
			return false;
		} else {
			return true;
		}
	});
	
	$("#form_nextstep").click(function(event) {
		event.preventDefault();
		setFormStage(current_stage + 1);
	});
	
	$("#sv_srtrcworkshop, #sv_safeworkshop, #sv_islamophobia").click(function(event) {
		checkSessions();
	});

});
