
					/****************************************
					 *               player.js              *
					 *                                      *
					 *    Contains all necessary functions  *
					 *    for utilizing the Livestream API  *
					 *                                      *
					 *             REQUIRES jQuery          *
					 *                                      *
					 *           www.deepgamers.com         *
					 *                                      *
					 ****************************************/




var muted = false;                         // Current mute status of player
var paused = false;                        // Current pause status of player
var lights = false;                        // Current status of the lights
var currentChannel = "deepgamers";         // Current channel
var otherChannel = false;                  // True if the user specified a different channel via the URL
var updateInterval;                        // The interval number for the updateviewercount()
var loadedPlayer = "ls";                   // The currently loaded player.  Either LS or JTV
var autoswitch = readCookie("autoswitch"); // If true the player will autoswitch between JTV and LS
var lsLive = false;                        // True if the Livestream channel is true
var jtvLive = false;                       // True if the JTV channel is true.  

//Parameters to pass with swfobject
var params = { AllowScriptAccess: 'always', allowfullscreen: 'true', wmode: 'window' };

		
//Pops out chat for currentChannel
function popoutChat() {
	if (currentChannel == "animeworldorder")
		chatWindow = window.open("http://cdn.livestream.com/chat/LivestreamChat.swf?channel=" + currentChannel, "Chat", "status=0, height=500, width=350. scrollbars=0, resizable=1" );
	else
		chatWindow = window.open("http://www.livestream.com/procaster/swfs/Procaster.swf?channel=" + currentChannel, "Chat", "status=0, height=500, width=350. scrollbars=0, resizable=1" );
}

//Pops out stream for currentChannel
function popoutStream() {
	player.stopPlayback();
	streamWindow = window.open("http://www.deepgamers.com/popout.php?" + currentChannel, "Steam", "status=0, height=480, width=505" );
}

//Initializes the livestream player
function livestreamPlayerCallback(event) {
	if (event == 'ready') {
		player = document.getElementById("ls-player");
		player.load(currentChannel);
		updateInterval = setInterval("updateViewerCount()", 2000);
		player.showFullscreenButton(true);
		player.showPlayButton(false);
		player.showPauseButton(false);
		player.showMuteButton(false);
		if (otherChannel == true)
			switchChannel(currentChannel);
		player.startPlayback();
	}
}

//Replaces special characters in a string and returns it
function htmlspecialchars(str) {
	if (typeof(str) == "string") {
		str = str.replace(/&/g, "&amp;"); /* must do &amp; first */
		str = str.replace(/"/g, "&quot;");
		str = str.replace(/'/g, "&#039;");
		str = str.replace(/</g, "&lt;");
		str = str.replace(/>/g, "&gt;");
	}
	return str;
}

//Updates "below-player" with the current streamer
function updateViewerCount() {
	var streamer;
	streamer = player.getCurrentContentTitle();
	if (streamer == "deepgamers 696Kbps")
		streamer = "Mareeo's Camera";
	else if (streamer == "deepgamers 529Kbps")
		streamer = "Smew's Camera";

	div = document.getElementById('below-player');
	streamer = htmlspecialchars(streamer);
	if (player.isLive())
		div.innerHTML ='<b>Now <span style="color: #FF0000">LIVE</span>: </b>' +  streamer;
	else
		div.innerHTML = '<b>Now Playing: </b>' + streamer;
}


//Toggles pausing of the player and changed the button icon appropriately
function pause() {
	if (player.isOnAir()) {
		player.stopPlayback();
		document.getElementById("pause").style.background = "url(images/player/play.png)";
		paused = true;
	} else {
		player.startPlayback();
		document.getElementById("pause").style.background = "url(images/player/pause.png)";
		paused = false;
	}
}

//Switches player and chat to channel passed
function switchChannel(channel) {

		player.load(channel);
		currentChannel = channel;
		
		if (channel == "deepgamers") {				
			document.getElementById('ls-chat').innerHTML = '<embed type="application/x-shockwave-flash" src="http://www.livestream.com/procaster/swfs/Procaster.swf?channel=' + channel + '" width="308" height="489" style="undefined" id="Procaster" name="Procaster" quality="high" wmode="window" allowscriptaccess="always" bgcolor="#000000"></embed>';
		} else if (channel == "animeworldorder") {
			document.getElementById('ls-chat').innerHTML = '<embed type="application/x-shockwave-flash" src="http://cdn.livestream.com/chat/LivestreamChat.swf?channel=' + channel + '" width="308" height="489" style="undefined" id="Procaster" name="Procaster" quality="high" wmode="window" allowscriptaccess="always" bgcolor="#000000"></embed>';
		} else {
			document.getElementById('ls-chat').innerHTML = '<embed type="application/x-shockwave-flash" src="http://www.livestream.com/procaster/swfs/Procaster.swf?channel=' + channel + '" width="308" height="489" style="undefined" id="Procaster" name="Procaster" quality="high" wmode="window" allowscriptaccess="always" bgcolor="#000000"></embed>';
		}
}

//Toggles the 'lights' on and off
function toggleLights() {
	div = document.getElementById("dark-background");
	if (div.style.display == "block") {
		div.style.display = "none"
		lights = false;
		document.getElementById("lights").style.background = "url(images/player/lights.png) no-repeat";
	} else {
		div.style.display = "block";
		lights = true;
		document.getElementById("lights").style.background = "url(images/player/lights-on.png) no-repeat";	
	}
}

//Toggles muting of the player and changed the button icon appropriately
function mute() {
	player.toggleMute();
	if (player.isMute()) {
		document.getElementById("mute").style.background = "url(images/player/muted.png) no-repeat";
		muted = true;
	} else {
		document.getElementById("mute").style.background = "url(images/player/mute.png) no-repeat";
		muted = false;
	}
}

//Pass element id and it will show the mouse over image
function hover(id) {
	if (id == "pause") 
		if (paused) 
			document.getElementById(id).style.background = "url(images/player/play-over.png) no-repeat";
		else 
			document.getElementById(id).style.background = "url(images/player/pause-over.png) no-repeat";	
	else if (id == "mute") 
		if (muted) 
			document.getElementById(id).style.background = "url(images/player/muted-over.png) no-repeat";
		else 
			document.getElementById(id).style.background = "url(images/player/mute-over.png) no-repeat";
	else if (id == "lights") 
		if (lights) 
			document.getElementById(id).style.background = "url(images/player/lights-on-over.png) no-repeat";
		else 
			document.getElementById(id).style.background = "url(images/player/lights-over.png) no-repeat";
	else if (id == "ls")
		if (lsLive)
			document.getElementById("ls").style.background = "url(images/player/ls-live-over.png) no-repeat";
		else
			document.getElementById("ls").style.background = "url(images/player/ls-over.png) no-repeat";
	else if (id == "jtv")
		if (jtvLive)
			document.getElementById("jtv").style.background = "url(images/player/jtv-live-over.png) no-repeat";
		else
			document.getElementById("jtv").style.background = "url(images/player/jtv-over.png) no-repeat";
	else if (id == "autoswitch")
		if (autoswitch == 1)
			document.getElementById("autoswitch").style.background = "url(images/player/autoswitch-on-over.png) no-repeat";
		else
			document.getElementById("autoswitch").style.background = "url(images/player/autoswitch-off-over.png) no-repeat";
	else 
		document.getElementById(id).style.background = "url(images/player/" + id + "-over.png) no-repeat";
}

//Pass element id and it will show the normal image on mouse exit
function unhover(id) {
	if (id == "pause")
		if (paused) 
			document.getElementById(id).style.background = "url(images/player/play.png) no-repeat";
		else 
			document.getElementById(id).style.background = "url(images/player/pause.png) no-repeat";	
	else if (id == "mute")
		if (muted) 
			document.getElementById(id).style.background = "url(images/player/muted.png) no-repeat";
		else 
			document.getElementById(id).style.background = "url(images/player/mute.png) no-repeat";
	else if (id == "lights")
		if (lights) 
			document.getElementById(id).style.background = "url(images/player/lights-on.png) no-repeat";
		else 
			document.getElementById(id).style.background = "url(images/player/lights.png) no-repeat";
	   else if (id == "ls")
		if (lsLive)
			document.getElementById("ls").style.background = "url(images/player/ls-live.png) no-repeat";
		else
			document.getElementById("ls").style.background = "url(images/player/ls.png) no-repeat";
	else if (id == "jtv")
		if (jtvLive)
			document.getElementById("jtv").style.background = "url(images/player/jtv-live.png) no-repeat";
		else
			document.getElementById("jtv").style.background = "url(images/player/jtv.png) no-repeat";
	else if (id == "autoswitch")
		if (autoswitch == 1)
			document.getElementById("autoswitch").style.background = "url(images/player/autoswitch-on.png) no-repeat";
		else
			document.getElementById("autoswitch").style.background = "url(images/player/autoswitch-off.png) no-repeat";
	else 
		document.getElementById(id).style.background = "url(images/player/" + id + ".png) no-repeat";

}




//Loads the JTV player and chat in the screen
function loadJTV() {
	
	var code = 
	'<object type="application/x-shockwave-flash" height="510" width="640" data="http://www.justin.tv/widgets/live_embed_player.swf?start_volume=1.0&channel=deepgames&auto_play=true" style="float:left;">\n' +
	'<param name="AllowScriptAccess" value="always"/>\n' +
	'<param name="allowfullscreen" value="true"/>\n' +
	'<param name="wmode" value="window"/>\n' +
	'</object>\n' +
	'<iframe frameborder="0" scrolling="no" id="chat_embed" src="http://www.justin.tv/chat/embed?channel=deepgames&default_chat=jtv&popout_chat=true#r=-rid-&s=em" height="510" width="320"></iframe>\n';
	
	if (loadedPlayer != "jtv") {
		document.getElementById('screen').innerHTML = code;
		document.getElementById('playback-controls').style.display = "none";
		document.getElementById('below-player').style.display = "none";
		clearInterval(updateInterval);
		loadedPlayer = "jtv";
		}
}


//Loads the Livestream player and chat in the screen
function loadLS() {
	
	var code = 
	'<div id="ls-player"></div>\n' +
	'<div id="ls-chat"><embed type="application/x-shockwave-flash" src="http://www.livestream.com/procaster/swfs/Procaster.swf?channel=deepgamers" width="308" height="489" style="undefined" id="Procaster" name="Procaster" quality="high" wmode="window" allowscriptaccess="always" bgcolor="#000000"></embed></div>\n';
	
	if (loadedPlayer != "ls") {
		document.getElementById('screen').innerHTML = code;
		document.getElementById('playback-controls').style.display = "block";
		document.getElementById('below-player').style.display = "block";
		swfobject.embedSWF("http://cdn.livestream.com/chromelessPlayer/v20/playerapi.swf", "ls-player", "100%", "100%", "9.0.0", "expressInstall.swf", null, params);

		loadedPlayer = "ls";
		}
}

//Creates a cookie with name, value, and expiration length
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

//Reads the value of the cookie
function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

//Erases the cookie
function eraseCookie(name) {
	createCookie(name,"",-1);
}
//Updates lsLive and jtvLive boolean variables, sets channel buttons
//appropriately, and updates the "on main channel" div 
function getChannelInfo() {
	
	//Get the channel info for Livestream and update the button and div
	$.get("scripts/getchannelinfo.php?channel=deepgamers", function(data){
		
		//Update the div.
		$("#deepgamers-status").html(function() {
			live = "";
			if (data.isLive) {
				live = '<span style="color: red; font-weight: bold;">Live</span> ';
				$("#ls").css('background-image', 'url(images/player/ls-live.png)');
				lsLive = true;
			} else {
				lsLive = false;
				$("#ls").css('background-image', 'url(images/player/ls.png)');
			}
			
			return '<b>Right now on the main channel</b><br /><img src="http://thumbnail.api.livestream.com/thumbnail?name=deepgamers&t=' + new Date().valueOf() + '" width="320"  onclick="javascript:loadLS();" style="cursor: pointer" /><br />' + live + data.currentViewerCount + ' viewers';;
		});
	}, "json");
		
	//Get the channel info for JTV and update the button
	$.get("scripts/getjtvinfo.php", function(jtvstatus){
		
		if (jtvstatus == 1) {
			jtvLive = true;
			$("#jtv").css('background-image', 'url(images/player/jtv-live.png)');
		} else {
			jtvLive = false;
			$("#jtv").css('background-image', 'url(images/player/jtv.png)');
		}
		
		//Both channel infos have been gotten.  Autoswitch.
		autoSwitch()
	});
}

//Automatically switches the player to the channel that is live
//Reads from the "autoswitch" cookie
function autoSwitch() {
	
	//If they have autoswitch enabled
	if (autoswitch  == 1) {
		//Change the image on the button to on
		$("#autoswitch").css("background-image", "url(images/player/autoswitch-on.png)");
		
		//If they have the Livestream player loaded
		if (loadedPlayer == "ls") {
			
			//If Livestream isn't live and JTV is
			if (player.isLive() == false && jtvLive == true) {
				loadJTV();
			}
		}
		
		//If they have the JTV player loaded
		if (loadedPlayer == "jtv") {
			
			//If JTV isn't live
			if (jtvLive == false) {
				loadLS();
				
			}
		}	
	} else {
		//Change the image on the button to off
		$("#autoswitch").css("background-image", "url(images/player/autoswitch-off.png)");
	}
}



