/// <reference path="jquery-1.3.2-min-vsdoc.js" />
var settings; //Cache of settings

var settingsRequestPending = false;

//Current time within video
var videoPreview_Mins;
var videoPreview_Secs;

$(document).ready(function (e)
    {
	    setInterval(updateSettings, 5000); //Check for updates to settings every 5 seconds
	    updateSettings(); //Initial call
    });
function displayFeaturedContent() {
	try
	{
		var fc = null;
		for(var i = 0; i<settings.FeaturedContents.length; i++) {
			if (settings.FeaturedContents[i].TCMins > videoPreview_Mins || 
				(settings.FeaturedContents[i].TCMins == videoPreview_Mins && settings.FeaturedContents[i].TCSecs > videoPreview_Secs)) {
				break;
			} else {
				fc = settings.FeaturedContents[i];
			}
		}
		
		if (fc != null) {
			$("#featuredContentWrapper").show();
			$("#featuredContentWrapper").html(fc.Content);
		} else {
			$("#featuredContentWrapper").hide();
		}
	} catch (e) {
        pageTracker._trackEvent('Chat', 'Featured Content', 'Error Updating Featured Content', e.description);
	}
}
var updateVideoTimeTimer = null; //Video timer (used for live/non-lightcast players)
var vtElapsedSecs = 0; //Counts elapsed time for the video timer
function updateVideoTime() {
	try
	{
		vtElapsedSecs++;
		if (settings.DateTimeServer != null) {
			var currentTime = new Date(settings.DateTimeServer.valueOf() + vtElapsedSecs*1000);
			var currentDay = currentTime.getDay();
			var currentHours = currentTime.getHours();
			var currentMins = currentTime.getMinutes();
			var currentSecs = currentTime.getSeconds();
	
			//Determine current service time
			for(var i = 0; i<settings.ServiceTimes.length; i++){
				var cServiceTimeDay = settings.ServiceTimes[i].Day;
				var cServiceTimeHours = settings.ServiceTimes[i].Hour;
				var cServiceTimeMins = settings.ServiceTimes[i].Min;
	
				if (currentDay == cServiceTimeDay && currentHours >= cServiceTimeHours && currentMins >= cServiceTimeMins) {
					videoPreview_Mins = (currentMins + currentHours*60) - (cServiceTimeMins + cServiceTimeHours*60);
				}
			}
	
			videoPreview_Secs = currentSecs;
			//$("#videoPlaybackTime").text($("#videoPlaybackTime").text() + " --> " + videoPreview_Mins + " -- " + videoPreview_Secs);
			
			displayFeaturedContent();
		}
	} catch (e) {
        pageTracker._trackEvent('Chat', 'Featured Content', 'Error Updating Video Timecode', e.description);
	}
}
function updateSettings(){
	try
	{
		if (!settingsRequestPending)
		{
			var last = settings != null ? settings.DateTimeLastChangedStamp : null;
			iCampus.GetSettingsForPlayer(last, getSettingsSuccess, getSettingsFailure); //Load in settings
			settingsRequestPending = true;
		}
	} catch (e) {
        pageTracker._trackEvent('Chat', 'Settings', 'Error Loading Settings', e.description);
	}
}
function getSettingsSuccess(result) {
	try
	{
		settingsRequestPending = false;
		if (result != null) {
			if (settings == null)
			{
				pageTracker._trackEvent('Settings', 'Initialized');
			}
			else
			{
				pageTracker._trackEvent('Settings', 'Refreshed');
			}
	
			settings = result; //Cache the settings on the client


			chat.enabled = !settings.ChatEmergencyShutdown;
			if ($(".dashboard .nav span.navActive").attr("href") == "#chat") {
			    showChat();
			    chatConversationResizeHandler();
			}


            //BYPASS GOOGLE TRANSLATE?
            if (!settings.GoogleTranslateEnabled) {
                //Bypass
                $(".googleTranslateWrapper").hide();

                chat.translate = false;
                $('#Checkbox_Translate').attr("checked", "");

                dashboardUpdateHeight();
                updateTranslations();
            }
		
			//SETUP VIDEO PLAYBACK
			$("#videoName").text(settings.VideoName);
			$("#videoByLine").text(settings.VideoByLine);
			
			//SET SHARE LINKS
			var shareURL = "http://live.liquidchurchonline.com";
			$("#shareFacebook").click(function(e){window.open("http://www.facebook.com/share.php?u=" + shareURL + "&t=" + escape(settings.ShareStatusMessage)); pageTracker._trackEvent('Share Clicked', 'Facebook');});
			$("#shareTwitter").click(function(e){window.open("http://twitter.com/home?status=" + escape(settings.ShareStatusMessage) + "%20-%20" + shareURL); pageTracker._trackEvent('Share Clicked', 'Twitter');});
			$("#shareEmail").click(function(e){window.location = "mailto:?subject=" + escape(settings.ShareEmailSubject) + "&body=" + escape(settings.ShareEmailBody) + "%20-%20" + shareURL; pageTracker._trackEvent('Share Clicked', 'E-mail');});
			$("#shareDigg").click(function(e){window.open("http://digg.com/submit?url=" + shareURL + "&title=" + escape(settings.ShareBookmarks)); pageTracker._trackEvent('Share Clicked', 'Digg');});
			$("#shareReddit").click(function(e){window.open("http://reddit.com/submit?url=" + shareURL + ";title=" + escape(settings.ShareBookmarks)); pageTracker._trackEvent('Share Clicked', 'Reddit');});
			$("#shareStumbleUpon").click(function(e){window.open("http://www.stumbleupon.com/submit?url=" + shareURL + "&title=" + escape(settings.ShareBookmarks)); pageTracker._trackEvent('Share Clicked', 'StumbleUpon');});
			
			if (settings.VideoType == 0) { //Handle lightcast media            
				$("#video").html("<div id='lcm_video'></div>");
				try {
					$.getScript(
						settings.VideoLightcastURL + "/console_js.php?u="  + settings.VideoLightcastAccountNumber + "&c=" + settings.VideoLightcastConsoleNumber, 
						function(e)
							{ $.getScript("js/lightcastMods.js"); }
						);
				} catch (err) {
					$.getScript("js/lightcastMods.js");
				}
			} else { //Handle other players
				$("#video").html(settings.VideoSource); //Handle other
				if (updateVideoTimeTimer != null) {
					clearInterval(updateVideoTimeTimer); //Clear the current interval
				}
				vtElapsedSecs = 0; //Reset the count of elapsed time
				updateVideoTimeTimer = setInterval(updateVideoTime, 1000); //Set the timer to update the video time every second based on the current time
			}
		}
	} catch (e) {
        pageTracker._trackEvent('Chat', 'Settings', 'Error Processing Settings', e.description);
	}
}
function getSettingsFailure(result) {
	settingsRequestPending = false;
}
