function showStatus (newstatus)
{
	if (newstatus == "STREAMING") $("#push_status").html("<img src='images/circle_green.png' height='8'/> "+newstatus);
	else if (newstatus == "POLLING") $("#push_status").html("<img src='images/circle_orange.png' height='8'/> "+newstatus);
	else $("#push_status").html("<img src='images/circle_red.png' height='8'/> "+newstatus);
}	
function update(itemPos, updateInfo, itemName)
{			
	var d = new Date();			
	time = d.getFullYear() + "-" + (d.getUTCMonth() + 1) + "-" + d.getUTCDate() + "T" + d.getUTCHours() + ":" + d.getUTCMinutes() + ":" + d.getUTCSeconds() + "Z";
	
	var data = updateInfo.getNewValue(itemPos);
	json = $.evalJSON(data);
				
	var id = json.id;
	var type = json.type;
	var text = json.text;
	var nickname = json.nickname;
	var nickname_id = json.nickname_id;
	
	if (type == "command")
	{
		eval(text);
	}
	else if (type == "chat")
	{
		if (!in_array(nickname_id,muted_users))
		{
			var formatted = "<li id='blog_"+id+"' class='nick_"+nickname_id+"'><div class='blog_"+type+"'></div><span class='nick'>"+nickname+"</span>"+text+" <span class='timestamp'>(<abbr id='push' class='timeago' title='" + time + "'></abbr>)</span><span class='chat_tools'><a href='#' onclick='MuteUser("+nickname_id+");return false'>Mute</a></span></li>";
		}
	}
	else
	{			
		var formatted = "<li id='blog_"+id+"'><div class='blog_"+type+"'></div>"+text+" <span class='timestamp'>(<abbr id='push' class='timeago' title='" + time + "'></abbr>)</span></li>";
	}
	
	$("#liveblog").prepend(formatted);
	$('#push').timeago();
}

$(document).ready(function(){
	$("#content_tabs > ul").tabs();
	$('.ui-tabs-nav').bind('tabsselect', function(event,ui) {
		var tab = ui.index;
		lastselectedtab = tab;
		ChangeTabs(tab);
		return true;
	});
	
	$('abbr[class*=timeago]').timeago();
	
	if ($.cookie("nickname_id"))
	{
		$("#tool_nick").html("Welcome back, "+$.cookie("nickname"));
		$("#tool_nick").show();
		$("#set_chat_name").hide();	
	}
});

function ChangeTabs(tab)
{
	if (tab == 1)
	{
		if ($.cookie("nickname")) LoadChat();
	}
		
	return false;
}

function nav_button(buttonid)
{
	//ToDo: need to pull this from the DB
	switch (buttonid)
	{
		case 1:
			window.open("http://talk.brandnewchurch.com/request.php?l=brandnewchurch&x=1&deptid=1&email=&question=Chat&firstname=","phpLive", "status = 1, height = 360, width = 460, resizable = 0");
			break;
		case 2:
			window.open("http://talk.brandnewchurch.com/request.php?l=brandnewchurch&x=1&deptid=2&email=&question=Prayer&firstname=", "phpLive", "status = 1, height = 360, width = 460, resizable = 0");
			break;
		case 3:
			window.open('giving.php','giveicampus','menubar=0,resizable=1,width=800,height=600,status=no,scrollbars=1');
			break;
		case 4:
			window.open('http://forms.brandnewchurch.com/view.php?id=12','icampuscommcard','menubar=0,resizable=0,width=700,height=500,status=no,scrollbars=1');
			break;
		case 5:
			window.open("http://forms.brandnewchurch.com/view.php?id=10","icampus_feedback","height = 500, width = 700, scrollbars=1");
			break;
		case 7:
			window.open("http://forms.brandnewchurch.com/view.php?id=5","icampus_feedback","height = 500, width = 700, scrollbars=1");
			break;	
		case 8:
			window.open("http://talk.brandnewchurch.com/request.php?l=brandnewchurch&x=1&deptid=3&email=&question=TechHelp&firstname=", "phpLive", "status = 1, height = 360, width = 460, resizable = 0");
			break;
	}	
}

function lcm_started() {
	
	$("#top_countdown").html("BNC Live NOW!");
		
	//get rid of countdown and show notes
	$("#countdown_container").css("display","none");
	$("#notes").css("display","block");
	RecordAttendance();
}

function RecordAttendance() {
	$.get("remote.php?action=record_attendance+nickname="+$.cookie("nickname_id"));
}
function sendLS(text) {
    engineRef.sendMessage("CHAT|" + text);
}
function PostComment() {
	var text = $("#post_comment_text").val();
	if (text == "")
	{
		alert("Comment empty.");
	}
	else
	{
		//see if a nickname has been set
		var nickname_id = $.cookie("nickname_id");

		if (nickname_id)	
		{
			$("#post_comment_text").val("");
			
			$.post("remote.php?action=post_comment",{ text : text, nickname_id : nickname_id},
				function(json) {
					var nickname = $.cookie("nickname");
					data = {id: json.aid, text: text,type:"chat",nickname:nickname,nickname_id:nickname_id}
					sendLS($.toJSON(data));
				},"json"
			);	
		}
		else
		{
			$("#comment_form").hide();
			$("#set_nickname").show();
		}
	}
}
function LoadChat() {		
	var nickname_id = $.cookie("nickname_id");
	if (nickname_id)
	{
		$.post("chat.php",{nickname_id:nickname_id},function(text){
			$("#chat").html(text);
		});
	}
}
function SetChatName() {
	var nick = $("#nick").val();
	if (nick)
	{
		SetNickname(nick);
		$("#set_chat_name").hide();
		//hack to make it work...TODO: come back and fix this for real
		setTimeout("LoadChat();",500);
	}
}
function SetPostName() {
	var nick = $("#set_nickname_text").val();
	if (nick)
	{	
		SetNickname(nick);
		//hack to make it work...TODO: come back and fix this for real
		setTimeout("PostComment();",500);
		$("#comment_form").show();
		$("#set_nickname").hide();
	}
}
function SetNickname(nickname) {
	$.getJSON("remote.php?action=save_nick",{nickname:nickname},function(json){
		var nickname_id = json.id;
		$.cookie("nickname_id",nickname_id);
		$.cookie("nickname",nickname);
	
		$("#tool_nick").html("Welcome, "+$.cookie("nickname"));
		$("#tool_nick").show();
	});	
}
function Logout()
{
	$.cookie("nickname",null)
	$.cookie("nickname_id",null)
	$("#tool_nick").hide();
	$("#chat").empty();
	$("#set_chat_name").show();
}
function in_array (needle, haystack, argStrict) {
    // http://kevin.vanzonneveld.net
    var key = '', strict = !!argStrict; 
    if (strict) {
        for (key in haystack) {
            if (haystack[key] === needle) {
                return true;
            }
        }
    } else {
        for (key in haystack) {
            if (haystack[key] == needle) {
                return true;
            }
        }
    }
    return false;
}
function MuteUser(nickname_id)
{
	muted_users.push(nickname_id);
	$(".nick_"+nickname_id).remove();
}