/* 
	Copyright 2011 Patrick Mounteney
    Kaleidoscopic Creation
    www.kaleidoscopiccreation.co.uk
	
*/


var jwplayer = [];
var flashPlayer = null; // this is the div it sits in and also ref we use to send commands
var html5Player1 = null; // HTML5 audio element
var html5Player2 = null; // HTML5 audio element
var playButtonOne, stopButtonOne, playButtonTwo, stopButtonTwo, icestream_listen_button;
var canPlayAAC = false;
var flashPlayerInitialised = false;
var isPlaying1, isPlaying2, hasStopped1, hasStopped2;
var picker1_whatsplayingTimer, picker2_whatsplayingTimer;
var iceStreamPlaying = false;
var iceStreamRadioButtonState = 0;
var iceStreamURLs = ["http://78.129.163.140:8250", "http://78.129.163.140:8260"];


// Add these protypes for string checking

 if(!Array.startsWith){
 String.prototype.startsWith = function(s)
{
   if( this.indexOf(s) == 0 ) return true;
   return false;
}

 }
 
  if(!Array.indexOf){
  Array.prototype.indexOf = function(obj){
   for(var i=0; i<this.length; i++){
    if(this[i]==obj){
     return i;
    }
   }
   return -1;
  }
}


$(document).ready(function(){
loadGlobe();

$('#wdj_button').mouseover(function() {
  this.src = "images/wdj_button_over.png";
});

$('#wdj_button').mouseout(function() {
  this.src = "images/wdj_button.png";
});

$('#icestream_button').mouseover(function() {
  this.src = "images/icestream_button_over.png";
});

$('#icestream_button').mouseout(function() {
  this.src = "images/icestream_button.png";
});

$('#rock_radio_button').mouseover(function() {
  this.src = "images/rock_radio_button_over.png";
});

$('#rock_radio_button').mouseout(function() {
  this.src = "images/rock_radio_button.png";
});

$('#rock_radio_shop_button').mouseover(function() {
  this.src = "images/rock_radio_button_over.png";
});

$('#rock_radio_shop_button').mouseout(function() {
  this.src = "images/rock_radio_button.png";
});

// add events for buttons that open the pickers
$('#rock_listen_button').bind('click', function() {
	if($("#picker_1").css("display") == "none"){										   
    $("#picker_1").fadeIn();
	picker1.startWhatPlayingTimer();
	}else{
	$("#picker_1").fadeOut();
	picker1.stopWhatPlayingTimer();
	}
});

$('#wdj_listen_button').bind('click', function() {
	if($("#picker_2").css("display") == "none"){										   
    $("#picker_2").fadeIn();
	picker2.startWhatPlayingTimer();
	}else{
	$("#picker_2").fadeOut();
	picker2.stopWhatPlayingTimer();
	}
});

// Ice Stream buttons

$('#icestream_listen_button').bind('click', function() {
													 
	var url = (iceStreamRadioButtonState == 0)? iceStreamURLs[0] : iceStreamURLs[1];												 
													 
	if(iceStreamPlaying){
	icestream_listen_button.src = "images/listen_now.png";
	if(jwplayer) jwplayer.pause();
	flashPlayerInitialised = false;
	iceStreamPlaying = false;
	}else{
	icestream_listen_button.src = "images/icestream_stop.png";
	createJWplayer(url);
	iceStreamPlaying = true;
	}
	

});


$('#ice_stream_128').bind('click', function() {
iceStreamRadioButtonState = 0;
if(iceStreamPlaying){
if(jwplayer) jwplayer.pause(); // stop streams and clean up
flashPlayerInitialised = false;
iceStreamPlaying = false;
icestream_listen_button.src = "images/listen_now.png";
// try to restart stream using this url
createJWplayer(iceStreamURLs[0]);
iceStreamPlaying = true; 
icestream_listen_button.src = "images/icestream_stop.png";
}
});


$('#ice_stream_64').bind('click', function() {
iceStreamRadioButtonState = 1;
if(iceStreamPlaying){
if(jwplayer) jwplayer.pause(); // stop streams and clean up
flashPlayerInitialised = false;
iceStreamPlaying = false;
icestream_listen_button.src = "images/listen_now.png";
// try to restart stream using this url
createJWplayer(iceStreamURLs[1]);
iceStreamPlaying = true; 
icestream_listen_button.src = "images/icestream_stop.png";
}
});

// add clicks to phone images

$('#iphone').bind('click', function(e) {
	window.open("http://itunes.apple.com/app/ekr-iapp/id408673304?mt=8");
	e.preventDefault(); 
	
});

popinCurrentYear();



// detect if we are using Safari on a Mac so we can use AAC streams
detectWhichPlayersToUse();

picker1.initPicker();
picker2.initPicker();

if(canPlayAAC){
picker1.createHTML5Player();	
picker2.createHTML5Player();
}

loadPlayerControls();	
});



function detectWhichPlayersToUse(){
var navString = navigator.userAgent;
var isOSX = (navString.indexOf("Mac OS X") >-1)? true : false;
var isSafari = (navString.indexOf("Safari") >-1)? true : false;
canPlayAAC = (isSafari && isOSX)? true : false;
//canPlayAAC = false; // TEMP
}


function loadPlayerControls(){
// create basic 'player' One
var playerOne = document.getElementById("playerOne");
playButtonOne = document.createElement("img");
stopButtonOne = document.createElement("img");
highlightedOne = document.createElement("img"); // slightly lighter version of button which shows for 250ms on each click
highlightedOne.style.cssText = "position: absolute; left: 4px; top: 6px; width: 85px; height: 34px; display: none;";
highlightedOne.src = "images/player_button_clicked.png";
playerOne.appendChild(playButtonOne);
playerOne.appendChild(stopButtonOne);
playerOne.appendChild(highlightedOne);
// play button
playButtonOne.style.cssText = "position: absolute; left: 4px; top: 6px; width: 85px; height: 34px; z-index: 2; cursor: pointer";
playButtonOne.src = "images/player_button_start.png";
// stop button
stopButtonOne.style.cssText = "position: absolute; left: 4px; top: 6px; cursor: pointer; z-index: 2; display: none;";
stopButtonOne.src = "images/player_button_stop.png";

// bind controls for either HTML5 player or Flash
if(canPlayAAC){
	
	playButtonOne.onclick = function(){ highlightedOne.style.display = "block"; setTimeout( function(){ highlightedOne.style.display = "none"; }, 250); html5Player1.play(); };
	stopButtonOne.onclick = function(){ highlightedOne.style.display = "block"; setTimeout( function(){ highlightedOne.style.display = "none"; }, 250); html5Player1.pause(); };
	
}else{
// set-up for Flash
	playButtonOne.onclick = function(){
	highlightedOne.style.display = "block"; setTimeout( function(){ highlightedOne.style.display = "none"; }, 250);
	
	if(flashPlayerInitialised){
		picker2.stopFlashPlayer(); // stop other player before starting this one
		setTimeout(function(){picker1.playFlashPlayer();}, 500);
		picker1.playFlashPlayer();
	}else{
		picker1.playFlashPlayer();
	}
	};
	
	stopButtonOne.onclick = function(){ highlightedOne.style.display = "block"; setTimeout( function(){ highlightedOne.style.display = "none"; }, 250); picker1.stopFlashPlayer(); };
}

// create basic 'player' Two
var playerTwo = document.getElementById("playerTwo");
playButtonTwo = document.createElement("img");
stopButtonTwo = document.createElement("img");
highlightedTwo = document.createElement("img");
highlightedTwo.style.cssText = "position: absolute; left: 4px; top: 6px; width: 85px; height: 34px; display: none;";
highlightedTwo.src = "images/player_button_clicked.png";
playerTwo.appendChild(playButtonTwo);
playerTwo.appendChild(stopButtonTwo);
playerTwo.appendChild(highlightedTwo);
// play button
playButtonTwo.style.cssText = "position: absolute; left: 4px; top: 6px; z-index: 2; cursor: pointer";
playButtonTwo.src = "images/player_button_start.png";
// stop button
stopButtonTwo.style.cssText = "position: absolute; left: 4px; top: 6px; cursor: pointer; z-index: 2; display: none;";
stopButtonTwo.src = "images/player_button_stop.png";

// bind controls for either HTML5 player or Flash
if(canPlayAAC){
	
	playButtonTwo.onclick = function(){ highlightedTwo.style.display = "block"; setTimeout( function(){ highlightedTwo.style.display = "none"; }, 250); html5Player2.play(); };
	stopButtonTwo.onclick = function(){ highlightedTwo.style.display = "block"; setTimeout( function(){ highlightedTwo.style.display = "none"; }, 250); html5Player2.pause(); };
	
}else{
	// set-up for Flash
	playButtonTwo.onclick = function(){
	if(flashPlayerInitialised){
		picker1.stopFlashPlayer(); // stop other player before starting this one
		setTimeout(function(){picker2.playFlashPlayer();}, 500);
		picker2.playFlashPlayer();
	}else{
		picker2.playFlashPlayer();
	}
	stopButtonTwo.onclick = function(){ highlightedTwo.style.display = "block"; setTimeout( function(){ highlightedTwo.style.display = "none"; }, 250); picker2.stopFlashPlayer(); };
};
	
}

icestream_listen_button = document.getElementById("icestream_listen_button");
}



function createJWplayer(url){
var flashvars = {myURL: url};
var params = {};
var attributes = {};
swfobject.embedSWF("swf/my_programmable_player.swf", "flashPlayer", "327", "25", "8.0.0", false, flashvars, params, attributes);
setTimeout(function(){ jwplayer = document.getElementById("flashPlayer"); }, 250);


 flashPlayerInitialised = true;
 
}






function loadGlobe(){	
var flashvars = {};
var params = {wmode: "transparent"};
var attributes = {};
swfobject.embedSWF("swf/spinning_earth_globe.swf", "globe", "100%", "100%", "8.0.0", false, flashvars, params, attributes);
}

function deleteprior(div){
	if(!div) return;
	while (div.firstChild){
    div.removeChild(div.firstChild);
 	}
	}



function popinCurrentYear(){
var today = new Date();
document.getElementById("this_year").innerHTML = today.getUTCFullYear();
}

// not used yet
function makeMessage(txt, posY, posX){
var messageDiv = document.getElementById("messageDiv");		
messageDiv.innerHTML = txt;
$(messageDiv).fadeIn();
}


