function myInit() {	
	
	$("a[href^='http:']").not("[href*='www.westlondonfreeschool.co.uk']").attr('target','_blank');
	
	$('#navigation div > ul > li:last').addClass('last');
	$('#navigation ul li:has(li.selected)').addClass('selected');
	
	$('#navigation ul > li:has(ul) > a').click( function() {			
		if((navigator.userAgent.match(/iPad/i)) || (navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/Android/i))) {
			return false;
			}
		});
	
	$("#navigation > div > ul > li").hover( function () {		
		$("#navigation > div > ul > li").removeClass('hover');
		$(this).addClass('hover');
		},function () {
		//$(this).removeClass('hover');
		}
		);
	$("#navigation > div > ul > li > ul").hover( function () {	
		},function () {
		$("#navigation > div > ul > li").removeClass('hover');
		}
		);
	
	
	$('#archive > li > a').click( function() {		
		$(this).parent().find("ul:first").toggleClass("closed"); 
		return false;
		});
	$('#archive > li > ul > li > a').click( function() {		
		$(this).parent().find("ul:first").toggleClass("closed"); 
		return false;
		});
	
	$('#content div.productImage ul.productThumbnails li img').click( function() {		
		src = $(this).attr('src');
		newSrc = src.replace('t4_','t1_')
		$('#popupImage').html('<img src="'+newSrc+'" /><a href="#>Close</a>');
		$('#popupImage').show(); 
		});
	$('#content div.productImage div#mainImage img').click( function() {		
		src = $(this).attr('src');
		newSrc = src.replace('t2_','t1_')
		$('#popupImage').html('<img src="'+newSrc+'" /><a href="#>Close</a>');
		$('#popupImage').show(); 
		});
	$('#popupImage').click( function() {
		$('#popupImage').hide(); 
		});
	
	
	$('#name').focus( function() {
		if ($('#name').val() == 'Name') {
			$('#name').val('');
			}		
		});
	$('#email').focus( function() {
		if ($('#email').val() == 'Email') {
			$('#email').val('');
			}		
		});
	$('#tel').focus( function() {
		if ($('#tel').val() == 'Telephone Number') {
			$('#tel').val('');
			}		
		});
	$('#comment').focus( function() {
		if ($('#comment').val() == 'Comment') {
			$('#comment').val('');
			}		
		});
	
	$('div#enquiryForm a#formSubmit').click( function() {
		errors = "";
		productId = $('#productId').val();
		if (!$('#name').val() || $('#name').val() == 'Name') {
			errors += "Name\n";
			} else {
			name = $('#name').val();
			}
		if (!$('#email').val() || $('#email').val() == 'Email') {
			errors += "Email\n";
			} else {
			email = $('#email').val();
			}
		if (!$('#tel').val() || $('#tel').val() == 'Telephone Number') {
			errors += "Telephone Number\n";
			} else {
			tel = $('#tel').val();
			}
		if (!$('#comment').val() || $('#comment').val() == 'Comment') {
			errors += "Comment\n";
			} else {
			comment = $('#comment').val();
			}
		if (errors) {
			alert("The following fields are mandatory:\n\n"+errors);
			return false;
			} else {
			
			$.ajax({
				type: "POST",
				url: "/send.php",
				data: "productId="+productId+"&name="+name+"&email="+email+"&tel="+tel+"&comment="+comment,
				success: function(returned){	
					retLink = document.location.href;
					retLink = retLink.replace('?showForm=true','');
					$('div#enquiryForm').html('<p>Your form has been submitted. Thank you for your enquiry.</p><p><a href="'+retLink+'">Back</a></p>');
					}
				});	
			}
		return false;
		});
	
	$('form.application').submit( function() {
		errors = '';
		if (  $("input[name='name']").val() == '') {
			errors += "Your name\n";
			}
		if (  $("input[name='child_name']").val() == '') {
			errors += "Child's name\n";
			}
		if (  $("input[name='dob']").val() == '') {
			errors += "Date of birth\n";
			}
		if (  $("input[name='dob']").val() == '') {
			errors += "Date of birth\n";
			}
		if (  $("input[name='email']").val() == '') {
			errors += "Email\n";
			}
		if (  $("input[name='postcode']").val() == '') {
			errors += "Postcode\n";
			}
			
		if (errors) {			
			alert("The following fields are required:\n\n"+errors);
			return false;
			} 	
		
		$('form.application').attr('method','post');
		
		});
	
	
	$("#commentForm").submit(function() {		
		$('#commentForm').attr('method','post');
		errors = '';	
		if (!$('#cf_comment').val()) {
			errors += "Comment\n";
			}
		if (!$('#cf_name').val()) {
			errors += "Name\n";
			}
		if (!$('#cf_email').val()) {
			errors += "Email\n";
			}
		if (errors) {			
			alert("The following fields are required:\n\n"+errors);
			return false;
			} 	
		});
	
	}



window.onload = function() {
	var url = 'http://twitter.com/statuses/user_timeline/wlfreeschool.json?callback=twitterCallback2&count=3';
	var script = document.createElement('script');
	script.setAttribute('src', url);
	document.body.appendChild(script);	
	}
	
function twitterCallback2(twitters) {
	var statusHTML = [];
	for (var i=0; i<twitters.length; i++){
		var username = twitters[i].user.screen_name;
		var status = twitters[i].text.replace(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g, function(url) {
			return '<a href="'+url+'">'+url+'</a>';
			}).replace(/\B@([_a-z0-9]+)/ig, function(reply) {
			return  reply.charAt(0)+'<a href="http://twitter.com/'+reply.substring(1)+'">'+reply.substring(1)+'</a>';
			});
		statusHTML.push('<li><span>'+status+'</span> <a style="font-size:85%" href="http://twitter.com/'+username+'/statuses/'+twitters[i].id+'">'+relative_time(twitters[i].created_at)+'</a></li>');
		}
	document.getElementById('twitter_update_list').innerHTML = statusHTML.join('');
	}





function relative_time(time_value) {
  var values = time_value.split(" ");
  time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
  var parsed_date = Date.parse(time_value);
  var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
  var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
  delta = delta + (relative_to.getTimezoneOffset() * 60);

  if (delta < 60) {
    return 'less than a minute ago';
  } else if(delta < 120) {
    return 'about a minute ago';
  } else if(delta < (60*60)) {
    return (parseInt(delta / 60)).toString() + ' minutes ago';
  } else if(delta < (120*60)) {
    return 'about an hour ago';
  } else if(delta < (24*60*60)) {
    return 'about ' + (parseInt(delta / 3600)).toString() + ' hours ago';
  } else if(delta < (48*60*60)) {
    return '1 day ago';
  } else {
    return (parseInt(delta / 86400)).toString() + ' days ago';
  }
}
