var today = new Date();
var expiry = new Date(today.getTime() + 365 * 24 * 60 * 60 * 1000);
var quoteInterval = '';
var quoteTimer = '';
var quoteData = new Array();
var quoteIntervalIndex = 0;
var quoteIntervalLength = 0;

function printPage(id)
{
	MM_openBrWindow('news_item_print.php?id=' + id,'printer','scrollbars=yes,resizable=yes,width=700,height=600');
}

function calcHeight()
{
	document.getElementById('results').height=50;
	var the_height=document.getElementById('results').contentWindow.document.body.scrollHeight;
	document.getElementById('results').height=the_height;
}

function toggleMenu(id)
{
	if($('#navSub_' + id).is(":visible"))
	{
		$('#navSub_' + id).slideUp('fast', function(){
			$('#nav_' + id).removeClass('navigation_selected navigation_selected_arrow');
		});
	}
	else
	{
		$('.navigation_sub').slideUp('fast', function(){
			$('.navigation_arrow').removeClass('navigation_selected navigation_selected_arrow');
			setTimeout(function(){
				$('#navSub_' + id).slideDown('fast');
				$('#nav_' + id).addClass('navigation_selected navigation_selected_arrow');
			}, 200);
		});
	}
}

function searchSite()
{
	if($('#search_query').val() == '')
	{
		alert('Please enter a keyword to search for');
		return false;
	}
	
	url = "search.php?query=" + $('#search_query').val();
	getContent(url);
	return false;
}

function getContent(url, menu, sub)
{
	parent.window.location = '#' + url;
	/*

	$("#content").load(url + urlAjax, function() {
		getQuote();
		$('.navigation_selected_active').removeClass('navigation_selected_active');
		$('#nav_' + menu).addClass('navigation_selected_active');
		if(sub)
		{
			$('#nav_' + menu + '_' + sub).addClass('navigation_selected_active');
		}
	});
	*/
	
	return false;
}

// the following handles the back button states

$(function(){
	$(window).bind('hashchange', function(e) {
		var url = $.param.fragment();
		if(url != ''){
			urlAjax = '?ajax=1';
			if(url.indexOf("?") > 0)
			{
				urlAjax = '&ajax=1';
			}
			lastSection = currentSection;
			currentSection = 0;
			$.get(url + urlAjax + '&rnd=' + Math.floor(Math.random()*9999), function(data){
				$("#content").html(data);
				getQuote();
				if(currentSection == 0){
					$('.navigation_sub').slideUp('fast', function(){
						$('.navigation_arrow').removeClass('navigation_selected navigation_selected_arrow');
					});
				}else{
					currentSection = currentSection-1;
					$('#navSub_' + currentSection).slideDown('fast');
					$('#nav_' + currentSection).addClass('navigation_selected navigation_selected_arrow');
				}
				loadSearch();
			});
		}
	})
	$(window).trigger('hashchange');
});

var currentPage = 1;

function shuffle(o){ 
	for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
	return o;
};

function getQuote()
{
	clearTimeout(quoteTimer);
	
	if(quotesJson[currentPage])
	{
		quoteData = shuffle(quotesJson[currentPage]);
		quoteIntervalLength = quoteData.length;
		quoteIntervalIndex = 0;
		replaceQuote();
	}
	else
	{
		$('#quotesTextQuote').html('When you play music, you discover a part of yourself that you never knew existed.').fadeIn();
		$('#quotesTextAuthor').html('Bill Evans').fadeIn();
	}
}

function replaceQuote() {
	 $('#quotesTextQuote').html(quoteData[quoteIntervalIndex]['quote']).fadeIn();
	 $('#quotesTextAuthor').html(quoteData[quoteIntervalIndex]['author']).fadeIn();
	 
     quoteIntervalIndex++;

     if(quoteIntervalLength > 1)
	 {
		if(quoteIntervalIndex >= quoteIntervalLength) {
			quoteTimer = setTimeout(getQuote, 5000); 
     	}else{
			quoteTimer = setTimeout(replaceQuote, 5000); 
	 	}
	 }
}

function timerQuotes(){
	//quoteInterval = setInterval(getQuote,20000);	
}

function timerQuotesClear(){
	 //clearInterval(quoteInterval);	
}

function submitContact()
{
	$.post("contact.php", { 
		name: $('#name').val(), 
		telephone: $('#name').val(), 
		email: $('#email').val(),
		contactby: $("#contactForm input[type='radio']:checked").val(),
		enquiry: $('#enquiry').val(),
		surname: $('#surname').val()
	}, function(data){
		$("#content").html("<h1>Thank you</h1>Thank you for your e-mail, I will respond to your message as soon as possible.");
	});
	
	return false;
}

function submitRegister(id)
{
	if(!$('#firstname').val()){
		alert('Please enter your first name');
		return false;
	}else if(!$('#lastname').val()){
		alert('Please enter your last name');
		return false;
	}else if(!$('#email').val()){
		alert('Please enter your email address');
		return false;
	}
	
	if($('#confirm').is(':checked'))
	{
		$.post("news_item.php?id=" + id, { 
			firstname: $('#firstname').val(), 
			lastname: $('#lastname').val(), 
			email: $('#email').val(),
			confirm: 1
		}, function(data){
			$("#subscribeMe").html("Thank you - we have sent an automated email to confirm your subscription.");
		});
	}else{
		alert('You must agree to our terms and conditions');
	}
	return false;
}

function submitScholarship()
{
	if(!$('#lastname').val()){
		alert('Please enter your last name');
		return false;
	}else if(!$('#firstname').val()){
		alert('Please enter your first name(s)');
		return false;
	}else if(!$('#sex_1').is(':checked') && !$('#sex_2').is(':checked')){
		alert('Please select your sex');
		return false;
	}else if(!$('#dob_dd').val() || !$('#dob_mm').val() || !$('#dob_yyyy').val()){
		alert('Please enter your date of birth');
		return false;
	}else if(!$('#address1').val()){
		alert('Please enter your address');
		return false;
	}else if(!$('#town').val()){
		alert('Please enter your town');
		return false;
	}else if(!$('#county').val()){
		alert('Please enter your county');
		return false;
	}else if(!$('#postcode').val()){
		alert('Please enter your postcode');
		return false;
	}else if(!$('#tel_land').val()){
		alert('Please enter your telephone (landline)');
		return false;
	}else if(!$('#tel_mob').val()){
		alert('Please enter your telephone (mobile)');
		return false;
	}else if(!$('#email').val()){
		alert('Please enter your email address');
		return false;
	}else if(!$('#parent').val()){
		alert('Please enter your parent name(s)');
		return false;
	}else if(!$('#school').val()){
		alert('Please enter the school you attended');
		return false;
	}else if(!$('#experience').val()){
		alert('Please enter details of your musical experience');
		return false;
	}else if(!$('#ambitions').val()){
		alert('Please enter details of your musical ambitions');
		return false;
	}else if(!$('#ref_name').val()){
		alert('Please enter your referee name');
		return false;
	}else if(!$('#ref_address').val()){
		alert('Please enter your referee address');
		return false;
	}else if(!$('#ref_tel').val()){
		alert('Please enter your referee telephone (landline)');
		return false;
	}else if(!$('#ref_tel_mob').val()){
		alert('Please enter your referee telephone (mobile)');
		return false;
	}else if(!$('#ref_email').val()){
		alert('Please enter your referee email address');
		return false;
	}else if(!$('#ref_relationship').val()){
		alert('Please enter the relationship to your referee');
		return false;
	}else if(!$('#hear_1').is(':checked') && !$('#hear_2').is(':checked') && !$('#hear_3').is(':checked') && !$('#hear_4').is(':checked')){
		alert('Please select how your heard about this scholarship');
		return false;
	}
	
	if($('#hear_4').is(':checked')){
		if(!$('#hearOther').val()){
			alert('Please enter how your heard about this scholarship');
			return false;
		}
	}
	
	$.post("scholarships_apply.php", $("#scholarships_apply_form").serialize(), function(data){
		$("#content").html("<h1>Thank you</h1>Your application has been received.<br><br>All applicants will be contacted in March, and successful candidates will be invited to attend an audition in April.");
	});
	
	return false;
}

function loginUser()
{
	if(!$('#username').val()){
		alert('Please enter your username');
		return false;
	}else if(!$('#password').val()){
		alert('Please enter your password');
		return false;
	}
	
	$("#failedLogin").slideDown();
	return false;
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function play() {
	var dewp = document.getElementById("dewplayer");
	if(dewp!=null) dewp.dewplay();
	$('#play').hide();
	$('#pause').show();
	DeleteCookie('paused');
}
function pause() {
	var dewp = document.getElementById("dewplayer");
	if(dewp!=null) dewp.dewpause();
	$('#play').show();
	$('#pause').hide();
	SetCookie('paused',1,expiry);
}
function next() {
	currentPos = 0;
	var dewp = document.getElementById("dewplayer");
	if(dewp!=null) dewp.dewnext();
	$('#play').hide();
	$('#pause').show();
	processNext();
	DeleteCookie('paused');
}
function prev() {
	currentPos = 0;
	var dewp = document.getElementById("dewplayer");
	if(dewp!=null) dewp.dewprev();
	$('#play').hide();
	$('#pause').show();
	processPrevious();
	DeleteCookie('paused');
}

function getpos() {
	var dewp = document.getElementById("dewplayer");
	if(dewp!=null) {
		myPos = dewp.dewgetpos();
		if(myPos < currentPos){
			myPos = 0;
			processNext();
		}
		currentPos = myPos;
	}
}

function processNext() {
	mp3Current++;
	if(mp3Current > mp3Total){
		mp3Current = 1;
	}
	updateText();
}

function processPrevious() {
	mp3Current--;
	if(mp3Current < 1){
		mp3Current = mp3s.length-1;
	}
	updateText();
}

function autoPlay(){
	if(!GetCookie('paused'))
	{
		setTimeout('play()', 200);
	}
}

function updateText() {
	$('#audioTitle').html(mp3s[mp3Current]);
}

function loadSearch()
{
	if($('#query')){
		$('#query').focus();
	}
}

$(document).ready(function() {	
	getQuote();
	setInterval(getpos,500);
	loadSearch();
	autoPlay();
});

var currentPos = 0;
var currentSection = 0;

function getCookieVal (offset) {
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1) { endstr = document.cookie.length; }
  return unescape(document.cookie.substring(offset, endstr));
  }

function GetCookie (name) {
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  while (i < clen) {
    var j = i + alen;
    if (document.cookie.substring(i, j) == arg) {
      return getCookieVal (j);
      }
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break; 
    }
  return false;
  }

function DeleteCookie (name,path,domain) {
  if (GetCookie(name)) {
    document.cookie = name + "=" +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
  }

function SetCookie (name,value,expires,path,domain,secure) {
  document.cookie = name + "=" + escape (value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
  }

