
(function($) {
	
	$(document).ready(function() {
		
		allpages.setupSearchInput();
		
		if($("div.spotlight").length > 0) {
			home.rotateSpotlights(0);
		}
		
		if($("div.page").hasClass("sermons")) {
			sermons.pageInit();
		}
		
		if($("div.page").hasClass("login")) {
			login.pageInit();
		}
	});
	
	
	var allpages = function() {
		
		return {
			setupSearchInput: function() {
				$("form#searchform input#s").val("Search RPC")
				.focusin(function(evtObj) {
					var input = $(evtObj.currentTarget);
					if(input.val() == "Search RPC") {
						input.val("");
					}
				})
				.focusout(function(evtObj) {
					var input = $(evtObj.currentTarget);
					if(input.val() == "") {
						input.val("Search RPC");
					}
				});
			}
		};
	}();
	
	var home = function() {
		var spotlightsCount = 0;
		
		return {
			rotateSpotlights: function(index) {
				//if(interrupted()) return;
				
				if(spotlightsCount == 0) {
					spotlightsCount = $("div.spotlight").length;
				}
				
				$("div.spotlight.index"+index).delay(5000).fadeOut(250, function() {
					//if(interrupted()) return;
					
					var nextIndex = (index == spotlightsCount-1)? 0 : index+1;
					$("div.spotlight.index"+nextIndex).fadeIn(250, function() {
						home.rotateSpotlights(nextIndex);
					});
				});
			},
			
			jumpToSpotlight: function(index) {
				//placeholder: function will jump to index provided and restart rotation
			}
		};
	}();
	
	var sermons = function() {
		return {
			pageInit: function() {
				//init the player with the first sermon in the list
				$("#audio").attr("href", $("div.sermon.first").attr("href"));
				
				//player config
				$f("audio", templateUrl+"/mediaplayer/flowplayer-3.2.7.swf", {
					plugins: {
						audio: {
							url: templateUrl+"/mediaplayer/flowplayer.audio-3.2.2.swf"
						},

						controls: {
							fullscreen: false,
							height: 30,
							autoHide: false
						},

						clip: {
							autoPlay: false,
							autoBuffering: true
						}
					},
					
					//if autoPlay: false fails, will stop the clip after load event
					onLoad: function() {
						setTimeout(function() {
							$f("audio").stop();
						}, 100);
					}
				});

				//set click handlers for playlist
				$(".sermon").click(function() {
					sermons.displaySermon($(this));
					$f("audio").play($(this).attr("href"));
				});
			},
			
			displaySermon: function(sermon) {
				$("#sermonDisplay").children("h3").text(sermon.children(".title").text());
				$("#sermonDisplay").children("h2").text("Speaker: " + sermon.children(".speaker").children(".name").text());
			}
		};
	}();
	
	var login = function() {
		
		return {
			pageInit: function() {
				
				$( "#registerDialog" ).dialog({
					autoOpen: false,
					height: 250,
					width: 450,
					modal: true,
					buttons: {
						"Continue": function() {
							$( this ).dialog( "close" );
							window.location = $("#goToRegistration").children("a").attr("href");
						},
						Cancel: function() {
							$( this ).dialog( "close" );
						}
					}
				});
				
				$("#registrationLink").click(function () {
					$("#registerDialog").dialog("open");
				});
			}
		};
	}();
	
})(jQuery);

