/* javascript used on the resource index template*/

window.Advanced = {
    events: {
		basicToAdvanced: function(event) {
			//load the advanced search via ajax
			//force showAdvanced to true
			jQuery("#searchcontainer form input[name=showAdvanced]").val("true");
			$.ajax({
				url: '../../display_includes/resource_search/advanced_search.rm?is_ajax=true',
				data: jQuery("#searchcontainer form").serialize(),
				type: 'post', complete: function(xhr) {
					$("#searchcontainer").html(xhr.responseText);
					Advanced.addFormToggle();
				}
			});
			event.preventDefault();
		},
		advancedToBasic: function(event) {
			//load the basic search via ajax
			//force showAdvanced to false
			jQuery("#searchcontainer form input[name=showAdvanced]").val("false");
			$.ajax({
				url: '../../display_includes/resource_search/basic_search.rm?is_ajax=true',
				data: jQuery("#searchcontainer form").serialize(),
				type: 'post', complete: function(xhr) {
					$("#searchcontainer").html(xhr.responseText);
					Advanced.addFormToggle();
				}
			});
			event.preventDefault();
		},
		ageRangeToKeystage: function(event) {
			//load the keystage search include via ajax
			$.ajax({
				url: '../../display_includes/resource_search/advanced_search_keystage.rm?is_ajax=true',
				data: jQuery("#searchcontainer form").serialize(),
				type: 'post', complete: function(xhr) {
					$("#includeContainer").html(xhr.responseText);
					Advanced.ageRangeToKeystageCallback();
				}
			});	
			event.preventDefault();
		},
		keystageToAgeRange: function(event) {
			//load the age range search include via ajax
			$.ajax({
				url: '../../display_includes/resource_search/advanced_search_agerange.rm?is_ajax=true',
				data: jQuery("#searchcontainer form").serialize(),
				type: 'post', complete: function(xhr) {
					$("#includeContainer").html(xhr.responseText);
					Advanced.keystageToAgeRangeCallBack();
				}
			});
			event.preventDefault();
		},
		keystageToYeargroup: function(event) {
			if ($('#country option:selected').text() == 'Scotland'){
				//load the yeargroup dropdown via ajax
				$.ajax({
					url: '../../display_includes/resource_search/yeargroup.rm?is_ajax=true',
					data: jQuery("#searchcontainer form").serialize(),
					type: 'post', complete: function(xhr) {
						$("#yeargroupkeystage").html(xhr.responseText);
						Advanced.addFormToggle();
					}
				});
			} else {
				//load the keystage dropdown via ajax
				$.ajax({
					url: '../../display_includes/resource_search/keystage.rm?is_ajax=true',
					data: jQuery("#searchcontainer form").serialize(),
					type: 'post', complete: function(xhr) {
						$("#yeargroupkeystage").html(xhr.responseText);
						Advanced.addFormToggle();
					}
				});
			}
		}
	},
	addFormToggle: function() {
		//bind the click event on the + advanced search link
		$('li#chooseadvanced a').bind('click keypress', Advanced.events.basicToAdvanced);
		//bind the click event on the - advanced search link
		$('li#choosebasic a').bind('click keypress', Advanced.events.advancedToBasic);
		//bind the click event on the search by country / keystage link
		$('#kstab span a').bind('click keypress', Advanced.events.ageRangeToKeystage);
		//bind the click event on the search by age range link
		$('#agerangetab span a').bind('click keypress', Advanced.events.keystageToAgeRange);
		//bind the change evnet on the country drop down
		$('#country').change(Advanced.events.keystageToYeargroup);
		// deal with search box
		var defaultText = "Enter search term";
		$('#keyword').focus(function() {
			if (this.value == defaultText) {
				$(this).val('');
			}
		}).blur(function() {
			if (this.value == '') {
				$('#keyword').val(defaultText);
			}
		});
	},
	addAgeRangeLink: function() {
		//add the link onto the age range tab
		var agerangetext = $('#agerangetab span').html();
		var newtext = $('<a href="/applications/dynamic/resource_index.rm"></a>').append(agerangetext);
		$('#agerangetab span').html(newtext);
		//remove the link from the keystage tab
		var keystagetext = $('#kstab span a').html();
		$('#kstab span').html(keystagetext);
		//make the age range tab grey
		$('#agerangetab').addClass('grey');
		//make the key stage tab purple
		$('#kstab').removeClass('grey');
		//use the taller background image
		//$('#search-resources-advanced').addClass('agerange');
	},
	addKeystageLink: function() {
		//add the link onto the keystage tab
		var keystagetext = $('#kstab span').html();
		var newtext = $('<a href="/applications/dynamic/resource_index.rm"></a>').append(keystagetext);
		$('#kstab span').html(newtext);
		//remove the link from the age range tab
		var agerangetext = $('#agerangetab span a').html();
		$('#agerangetab span').html(agerangetext);
		//make the key stage tab grey
		$('#kstab').addClass('grey');
		//make the age range tab purple
		$('#agerangetab').removeClass('grey');
		//use the shorter background image
		//$('#search-resources-advanced').removeClass('agerange');
	},
	ageRangeToKeystageCallback: function(){
		Advanced.addAgeRangeLink();
		Advanced.addFormToggle();
	},
	keystageToAgeRangeCallBack: function(){
		Advanced.addKeystageLink();
		Advanced.addFormToggle();
	}
};

$(document).ready(function() {
	Advanced.addFormToggle();
	Advanced.events.keystageToYeargroup();
});