/****************************************************************************

  This file is part of the GamesCast.tv Website

	GamesCast.tv coded by Steve "GreasedScotsman" Huff, 
	Copyright 2010 Steve Huff
	
	It is distributed under the terms of the 
	GNU General Public License
		
***************************************************************************/

function contentUpdate(sID, nowlocal) {

	// this is an attempt to fix the syncronization problem that sometimes crops up during a 
	// server hiccup to ensure player, nowplaying and chat button info stays consistent.

	// reset the show chat button and embed to 
	// original state.
	$(".showchat").animate({"height": "0px" }, "slow"); 
	$(".showclose").fadeOut(500).hide(); 
	$(".showopen").fadeIn(500);
	$("#showchatframe").html("");
		
	if ($("#comingsoonplaceholder").is(":visible")) { $("#comingsoonplaceholder").slideUp(500); $("#comingsoonopen").show(); $("#comingsoonclose").hide(); }

	if (sID != '') {

		$.get("contentupdate.php", { sID: sID, tz: nowlocal }, function(data) {
			
			var cData = data.split("|GCTVCONTENTSPLIT|");
			$("#playerplaceholder").html(cData[0]);
			if ($("#gctvlayout").val() == "orig4x3") { shortenPlayerSchedTitles(); }
			$("#nowplayingplaceholder").html(cData[1]);
			$("#joinplaceholder").html(cData[2]);
			$("#leaveplaceholder").html(cData[3]);
	
		});

		$.get("alsoplaying.php", { sID: sID, tz: nowlocal }, function(data) { $("#alsoplayingplaceholder").html(data); } );

		$.get("comingsoon.php", { tz: nowlocal }, function(data) { $("#comingsoonplaceholder").html(data); } );
		

	} else {

		$.get("contentupdate.php", { tz: nowlocal }, function(data) {
			
			var cData = data.split("|GCTVCONTENTSPLIT|");
			$("#playerplaceholder").html(cData[0]);
			if ($("#gctvlayout").val() == "orig4x3") { shortenPlayerSchedTitles(); }
			$("#nowplayingplaceholder").html(cData[1]);
			$("#joinplaceholder").html(cData[2]);
			$("#leaveplaceholder").html(cData[3]);
	
		});

		$.get("alsoplaying.php", { tz: nowlocal }, function(data) { $("#alsoplayingplaceholder").html(data); } );

		$.get("comingsoon.php", { tz: nowlocal }, function(data) { $("#comingsoonplaceholder").html(data); } );

	}
}


/****************************************************************************

	alsoPlayingUpdate(nowlocal)

	alsoPlaying updates the alsoplayingplaceholder div, which includes
	the current show information, the upcoming schedule (TODO) and the
	Also Playing list.  The content of alsoplayingplaceholder will 
	update on an interval defined in the document.ready() function 
	defined below.
	
	nowlocal is passed but currently does nothing for now. Will be 
	used to display upcoming events on the schedule in the user's 
	local time.
		
***************************************************************************/
function alsoPlayingUpdate(sID, nowlocal) {

	sID = $("#currentshow").val();
	//alert("CS ID: "+$("#currentshow").val());

	if (sID != '') {

	$.get("alsoplaying.php", { sID: sID, tz: nowlocal }, 
		function(data) {
			$("#alsoplayingplaceholder").html(data);
		}
	);

	} else {

	$.get("alsoplaying.php", { tz: nowlocal }, 
		function(data) {
			$("#alsoplayingplaceholder").html(data);
		}
	);
	
	}
}

/****************************************************************************

	notoficationsUpdate(nowlocal)

	The notifications area updates on an interval defined in the
	document.ready() function below.  The content it grabs and 
	displays is the most recent 6 notifications pulled from the 
	notifications table in the GCTV CMS.  
	
	nowlocal is passed but currently does nothing for now. Will be 
	used to display upcoming events on the schedule in the user's 
	local time.
		
***************************************************************************/
function notoficationsUpdate(nowlocal) {
	
	var nListOrig = $("#notifylist").val();
	
	if (nListOrig == undefined) { nListOrig = ""; }

	$.get("notifications.php", { tz: nowlocal, nListOrig: nListOrig }, 
		function(data) {
			var nData = data.split("|NOTIFY|");

			$("#notificationsplaceholder").html(nData[1]);


			if (nData[0] == "") {
				// do nothing
			} 
			else if(nData[0].match(",")) {
				var newIDs = nData[0].split(",");
				var liCount = 0;
				var s = 6;
				$("#notifyholder li").each(function() { liCount++; });

	
				for (var i=0; i<newIDs.length; i++) {

					$("#notify"+newIDs[i]).hide();

					if (liCount > 6) {
						
						$("#notifyholder li:eq("+s+")").slideUp(500);
						s++;
					}

					//setTimeout(function() { 
					
						$("#notifyholder li:eq("+i+")").slideDown(1000);
						//$("#notificationsplaceholder").append($("#notify"+newIDs[i]).attr("id")+",");
						//$("#notify"+newIDs[i]).slideDown(1000);
					
					//}, 1000);
				}
				
			}
			else {
				var newIDs = nData[0];
				var liCount = 0;
				
				if (newIDs != "") {

					$("#notify"+newIDs).hide();

					$("#notifyholder li").each(function() { liCount++; });

					if (liCount > 6) {
						$("#notifyholder li:last").slideUp(500);
					}

					//setTimeout(function() { 

						$("#notify"+newIDs).slideDown(1000);

					//}, 1000);					
					//$("#notificationsplaceholder").append("single");	

				}
			}
		
		}
	);

}

function updateNoStreams(sID, nowlocal) {
	sID = sID;
	clearTimeout(lastOnlineTimerId);
	clearTimeout(rateShowTimerId);
	clearTimeout(rateGameTimerId);
	clearTimeout(rateEventTimerId);
	clearTimeout(noShowsOnlineTimerId);
	
	
	contentUpdate(sID, nowlocal);
	
	$("#comingsoonplaceholder").hide();
	$("#comingsoonopen").hide();
	$("#comingsoonclose").hide();

}


function updateAll(sID, nowlocal) {
	sID = sID;
	clearTimeout(lastOnlineTimerId);
	clearTimeout(rateShowTimerId);
	clearTimeout(rateGameTimerId);
	clearTimeout(rateEventTimerId);
	clearTimeout(noShowsOnlineTimerId);

	// clear the intervals by removing nowplaying from the dom
	$("#nowplayingplaceholder").empty();
	$("#nowplayingplaceholder").append('<div class="loading"></div>');
	$("#playerplaceholder").append('<div class="loading"></div>');
	//$("#joinplaceholder").empty().append('<div class="loading"></div>');
	//$("#leaveplaceholder").append('<div class="loading"></div>');
	//$("#alsoplayingplaceholder").append('<div class="loading"></div>');
	//$("#notificationsplaceholder").append('<div class="loading"></div>');

	contentUpdate(sID, nowlocal);
	
	$("#comingsoonopen").show();

}

var shareFB = null;
var shareTW = null;

function openShareSite(siteType, tID) {

	if (siteType == "fb") {
		
		if (shareFB == null || shareFB.closed) {
			shareFB = window.open("/gctv_facebook.php", "gctvFBwin", "location=0,status=0,scrollbars=0,width=730,height=360,toolbar=0,copyhistory=1,menubar=0,toolbar=0,resizable=0"); 
		} else { shareFB.focus(); }
	}
	else {
		if (tID == '') {

			if (shareTW == null || shareTW.closed) {
		shareTW = window.open("/twitter_update_form.php", "gctvTWwin", "location=0,status=0,scrollbars=0,width=800,height=460,toolbar=0,copyhistory=1,menubar=0,toolbar=0,resizable=0"); 
			} else { shareTW.focus(); }
		} else {

			if (shareTW == null || shareTW.closed) {
		shareTW = window.open("/twitter_update_form.php?tID="+tID, "gctvTWwin", "location=0,status=0,scrollbars=0,width=800,height=460,toolbar=0,copyhistory=1,menubar=0,toolbar=0,resizable=0"); 
			} else { shareTW.focus(); }

		}
	}
	return false;
}


function popoutVideo(sID) {

	var vidPopout = window.open("http://www.gamescast.tv/videopopout.php?sID="+sID, "gctvVidPopWin", "location=0,width=854,height=480,status=0,scrollbars=0,toolbar=0,menubar=0,resizable=1"); 
	return false;
}

function popoutShowChat(sID, ircName) {

	$(".showchat").animate({"height": "0px" }, "slow"); 
	$(".showclose").fadeOut(500).hide(); 
	$(".showopen").fadeIn(500);
	$("#showchatframe").html("");

	var chatPopout = window.open("http://www.gamescast.tv/chatpopout.php?sID="+sID+"&ircname="+ircName, "showChatPopWin", "location=0,width=500,height=480,status=0,scrollbars=0,toolbar=0,menubar=0,resizable=1"); 
	return false;
}

function popoutGCTVChat(ircName) {

	$(".gctvchat").animate({"height": "0px" }, "slow"); 
	$(".gctvclose").fadeOut(500).hide(); 
	$(".gctvopen").fadeIn(500);
	$("#gctvchatframe").html("");

	var chatPopout = window.open("http://www.gamescast.tv/gctvchatpopout.php?ircname="+ircName, "gctvChatPopWin", "location=0,width=500,height=480,status=0,scrollbars=0,toolbar=0,menubar=0,resizable=1"); 
	return false;
}


function openLiveStreamChat(url) {
	var chatWin = window.open(url, "lsChatWin", "location=1,status=1,scrollbars=0,width=310,height=420,toolbar=0,copyhistory=1,menubar=0,toolbar=0,resizable=1"); 
}

function openJustintvChat(url) {
	var chatWin = window.open(url, "jtvChatWin", "location=1,status=1,scrollbars=0,width=310,height=430,copyhistory=1,menubar=0,toolbar=0,resizable=1"); 
}


/////////////////////////////////////////////////////////////////////
//	INTERVAL/TIMEOUT GLOBALS
/////////////////////////////////////////////////////////////////////
var lastOnlineTimerId;
var rateShowTimerId;
var rateGameTimerId;
var rateEventTimerId;

/////////////////////////////////////////////////////////////////////

$(document).ready(function(){

	contentUpdate(sID, nowlocal);

	notoficationsUpdate(nowlocal);
	setInterval( "notoficationsUpdate(nowlocal)", 60000 );

	// The first call of alsoplaying happens in contentUpdate() so we just need to set the polling here...
	setInterval( "alsoPlayingUpdate(sID, nowlocal)", 60000 );

	onlineLogoUpdate();
	setInterval( "onlineLogoUpdate()", 120000 );
	
	$("#comingsoonplaceholder").hide();
	$("#comingsoonclose").hide();

	$("#comingsoonopen").click( function() { $("#comingsoonplaceholder").slideDown(500); $("#comingsoonopen").hide(); $("#comingsoonclose").show(); } );
	$("#comingsoonclose").click( function() { $("#comingsoonplaceholder").slideUp(500); $("#comingsoonopen").show(); $("#comingsoonclose").hide(); } );

	
});

// EOF
