// 
//  main.js
//  site
//  
//  Created by Chris Mytton on 2009-07-01.
//  Copyright 2009 Cutpastecreate. All rights reserved.
// 

// initialize a variable to hold the player instance in.
var player = null;

// handler fired when the player
function changeState (obj) {
	switch (obj.newstate) {
		case "PLAYING":
		case "BUFFERING":
			jQuery("#audio_player p a.control").text('Pause clip');
		break;
		case "PAUSED":
			jQuery("#audio_player p a.control").text('Play clip');
		break;
		case "COMPLETED":
			jQuery("#audio_player p a.control").text('Replay clip');
		break;
	}
}

// triggers the play pause mechanism
function playPause (e) {
	if (!jQuery(this).hasClass('playing'))
	{
		player.sendEvent("PLAY", "true");
		jQuery(this).addClass('playing');
	}
	else
	{
		jQuery(this).removeClass('playing');
		player.sendEvent("PLAY", "false");
	}

	e.preventDefault();
}

// add listeners after the player has been initialised
function addListeners () {
	// check that player has been initialised properly
	if (player) {
		// add a click listener to the play / pause button
		jQuery("#audio_player p a.control").click(playPause);
		
		// add a model listener for when the player changes state.
		player.addModelListener("STATE", "changeState");
	} else {
		// no player, set a timeout to try this again
		setTimeout("addListeners()",100);
	}
}

// player ready function - fired by the player when it's ready to be used.
function playerReady (obj) {
	// get a reference to the audio player we initialised
	player = document.getElementById('audioPlayer');
	
	// add listeners to the player
	addListeners();
}

jQuery(function($) {
	$("li.current_page_parent ul li a, li.current_page_item ul li a").prepend("&raquo; ");
	
	
	
	$("div#map").width("279px").height("320px");
	
	if (/contact/.test(location.href))
	{
		$("div#map").googleMap({
			zoom: 15
		});
	}
	
	if (typeof SWFObject !== "undefined")
	{
		if (document.getElementById('video-preview')) {
			// home page
			var s1 = new SWFObject(base_url + '/wp-content/plugins/shadowbox-js/flvplayer.swf','player','279','210','9');
			s1.addParam('allowfullscreen','true');
			s1.addParam('allowscriptaccess','always');
			s1.addParam('flashvars','file=' + base_url + '/static/video/acorn_school.mp4&backcolor=#292F30&frontcolor=#FFFFFF&controlbar=over&image=/cpc/wp-content/themes/theacornschool/images/video-preview.png');
			s1.write('video-preview');
		}
		
		if (document.getElementById("audio_player")) {			
			// there is a player in the page
			var s2 = new SWFObject(base_url + '/wp-content/plugins/shadowbox-js/flvplayer.swf','audioPlayer','332','20','9');
			
			s2.addParam('allowfullscreen','false');
			s2.addParam('allowscriptaccess','always');
			s2.addParam('wmode', 'opaque');
			
			s2.addParam('flashvars','file=' + base_url + '/static/audio/Education_audio.mp3&backcolor=#292F30&frontcolor=#FFFFFF');
			
			// $("body").append('<div id="audio_container" />');
			
			s2.write('audio_player');
		}
		
		if (document.getElementById('activity_player')) {
			var s3 = new SWFObject(base_url + '/wp-content/plugins/shadowbox-js/flvplayer.swf','activityPlayer','332','20','9');
			
			s3.addParam('allowfullscreen','false');
			s3.addParam('allowscriptaccess','always');
			s3.addParam('wmode', 'opaque');
			
			s3.addParam('flashvars','file=' + base_url + '/static/audio/Activities_audio.mp3&backcolor=#292F30&frontcolor=#FFFFFF');
			
			// $("body").append('<div id="audio_container" />');
			
			s3.write('activity_player');
		}
		
		if ($("div#post-490 a[href$='.m4a']").is(':visible')) {
			var file = $("div#post-490 a[href$='.m4a']").attr('href');
			
			$("div#post-490 a[href$='.m4a']").parent('p').replaceWith('<div id="radio_clip"></div>');
			
			var s4 = new SWFObject(base_url + '/wp-content/plugins/shadowbox-js/flvplayer.swf','radioClip','332','20','9');
			
			s4.addParam('allowfullscreen','false');
			s4.addParam('allowscriptaccess','always');
			s4.addParam('wmode', 'opaque');
			
			s4.addParam('flashvars','file=' + file + '&backcolor=#292F30&frontcolor=#FFFFFF');
			
			s4.write('radio_clip');
		}
		
	}

	$("li.cycle ul").cycle();
	
	$("form.subscribe").append('<div class="status" />');
	
	$("form.subscribe").validate({
		submitHandler: 	function (form) {
			$("input[type='submit']", form).attr("disabled", "disabled");
			var that = form;
			$.post(form.action, $(form).serialize(), function(data) {
				$("input[type='submit']", that).removeAttr("disabled");
				$("div.status", that).html(data);
				$("form.subscribe").get()[0].reset();
			}, 'html');
			return false;
		}
	});
	
	// unload the google map api to prevent memory leaks in Internet Explorer
	$(document.body).unload(function() {
		if (GBrowserIsCompatible()) {
			GUnload();
		}
	});
});