/*
@description loads top splash randomly and initialises resource search form
@author DC
@created 2008-09-29
*/
$(function(){
	$('.main-teaser').hide();
	randTeaser=$('.main-teaser')[Math.floor(Math.random() * $('.main-teaser').length)];
	$(randTeaser).show(); //all others are hidden with css
	
	Advanced.addFormToggle();
	Advanced.events.keystageToYeargroup();
	
	//twitter feed
	$('.twitter .content').tweet({
		username: "pfeg_org",
		count: 2,
		loading_text: "Loading tweets...",
		filter: function(t){ return ! /^@\w+/.test(t["tweet_raw_text"]); }
	});
	$('.twitter .profileLink').click(function(){
		try{pageTracker._trackEvent('Twitter profile link', 'click', document.location.href + ' - ' + $('title').html().replace(/\'/g,""));}catch(e){}
	});
});

/* simplified version of javascript from resource index template */

window.Advanced = {
    events: {
		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 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);
			}
		});
	}
};

