/* ---------- 
	Light in the Darkness: JQUERY
	Written by: Dave Robb | www.daverobb.net | dave@daverobb.net
	Version: 1.1
	Update: May 2010
---------- */

// 	----------------------------------------------------------------------
// 	UI Interaction
// 	----------------------------------------------------------------------

function uiActions() {
	$('.ui-state-default').hover(
		function() { $(this).addClass("ui-state-hover").removeClass("ui-state-default"); }, 
		function() { $(this).addClass("ui-state-default").removeClass("ui-state-hover"); }
	)
}


// 	----------------------------------------------------------------------
// 	Articles Functionality
// 	----------------------------------------------------------------------

function filtersArticles(){
	$("#filter-article-category").change(function(){
		var category = $('#filter-article-category').val();
		window.location = '#'+category;
	});	
}


// 	----------------------------------------------------------------------
// 	Blog Functionality
// 	----------------------------------------------------------------------

function filtersBlog(){
	$("#filter-blog-month").click(function(){
		var date = $('#blog-month').val();
		var explode = new Array();
		explode = date.split("-",2);
		var month = explode[0];
		var year = explode[1];
		if(date != 'Select a month...') {
			window.location = '/blog/blog.php?month='+month+'&year='+year;
		}
	return false
	});
	$("#search").click(function(){
		$("input#search").val("")
	});
	$("#search").blur(function(){
		var value = $("#search").val();
		if(value == ''){
			$("input#search").val("search the blog...");
		};
	return false
	});
}
	

// 	----------------------------------------------------------------------
// 	Calendar Functionality
// 	----------------------------------------------------------------------

function filtersCalendar(){
	$("#filter-calendar-month").click(function(){
		var date = $('#calendar-month').val();
		var explode = new Array();
		explode = date.split("-",2);
		var month = explode[0];
		var year = explode[1];
		if(date != 'Select a month...') {
			window.location = '/calendar/calendar.php?month='+month+'&year='+year;
		}
	return false
	});
	
	$('#jump-to-date').datepicker({
		onSelect: function(dateText, inst) {
			var show = $('#show').val();
			$('#calendar-content').html("");
			$('#datepicker-date').val(dateText);
			window.location = '/calendar/calendar.php?start='+dateText+'&show='+show;
		}
	});
}

function widgetsCalendar(){
	$("p.notes").hide();
	$('.a-details, .a-details-active').click(function(){
		var event_id = $(this).parent().attr("class");
		$("p.notes."+event_id).slideToggle(500);
		var active_class = $(this).attr("class");
		if($(this).attr("class") == 'a-details'){
			$(this).addClass("a-details-active").removeClass("a-details");
		} else {
			$(this).addClass("a-details").removeClass("a-details-active");
		}
		return false;
	});	
	
	var profiles = {
		windowPrint: {
			location:0,
			height:500,
			width:650,
			center:1,
			resizable:1,
			scrollbars:1
		}
	};
	$(function(){
		$(".popupwindow").popupwindow(profiles);
	});
}


// 	----------------------------------------------------------------------
// 	News Functionality
// 	----------------------------------------------------------------------

function filtersNews(){
	$("#filter-news-year").click(function(){
		var year = $('#news-year').val();
		if(year != 'Select a year...') {
			window.location = '/news/news.php?year='+year;
		}
	return false
	});
	$("#search").click(function(){
		$("input#search").val("")
	return false
	});
	$("#search").blur(function(){
		var value = $("#search").val();
		if(value == ''){
			$("input#search").val("search news articles...");
		};
	});
}


// 	----------------------------------------------------------------------
// 	Prayer Request Functionality
// 	----------------------------------------------------------------------

function widgetsPrayerRequest(){
	
	$('#loading').hide();
	
	$(function() {
		$('input[type=text]').focus(function() {
			$(this).val('');
			$(this).removeClass('field-error');
		});
	});
	
	$(".send-prayer-request").click(function(){
											 
		var hasError = false;
		
		var name = $("#emailName").val();
		if(name == '') {
			$("#emailName").val('You forgot to enter your name');
			$("#emailName").addClass('field-error');
			hasError = true;
		}

		var message = $("#message").val();
		var password = $("#password").val();

		if(hasError == false) {
			$('#submit').hide();
			$('#loading').show();
			$("#request-result").hide();
			$("#request-result").load("/prayer-chain/send-request.php", {name: name, message: message, password: password}, function(){
				var result = $("#request-result-value").text();
				if(result == 'fail') {
					$(".password-error").remove();
					$("#password").after('<span class="password-error">Sorry - incorrect password entered, please try again</span>');
					$('#submit').show();
					$('#loading').hide();
					$('#send-prayer-request').show();
				} else if(result == 'success') {
					$("#formContainer").slideUp("fast");
					$("#formContainer").after("<br /><br /><p class='success'>Thank you, your prayer request has been sent to everyone in the prayer chain.</p>");
				}
			});
		}
		return false;
	});		
	
	$("#widget-request-password").click(function(){
		var email = $("#email").val();
		$('#password-result').hide();
		$('#widget-request-password').hide();
		$('#widget-request-password').after('<img src="/css/ottawarpc-theme/images/spinner.gif" width="16" height="16" alt="loading" class="loading" />');
		$("#password-result").load("/prayer-chain/password-retrieval.php", {email: email}, function(){
			$(".loading").remove();
			$('#widget-request-password').show();
			$('#password-result').fadeIn('normal');
		});										 
		return false;
	});		
}


// 	----------------------------------------------------------------------
// 	Sermon Archive Functionality
// 	----------------------------------------------------------------------

function filtersSermons(){
	$("#filter-sermon-series").click(function(){
		var id = $('#sermon-series').val();
		var series_title = $('#sermon-series option:selected').text().replace(/ /g, "-");
		if(id != 'Choose a series...') {
			window.location = '/sermons/sermon-series.php/'+id+'/'+series_title;
		}
	return false
	});
	
	$("#filter-sermon-preacher").click(function(){
		var id = $('#sermon-preacher').val();
		var sermon_preacher = $('#sermon-preacher option:selected').text().replace(/ /g, "-");
		if(id != 'Choose a preacher...') {
			window.location = '/sermons/sermon-archive-search.php/preacher/'+sermon_preacher;
		}
	return false
	});
	
	$("#text-chapter").click(function(){
		$("input#text-chapter").val("")
	});
	$("#text-chapter").blur(function(){
		var value = $("#text-chapter").val();
		if(value == ''){
			$("input#text-chapter").val("ch");
		};
	});
	$("#text-verse").click(function(){
		$("input#text-verse").val("")
	});
	$("#text-verse").blur(function(){
		var value = $("#text-verse").val();
		if(value == ''){
			$("input#text-verse").val("v");
		};
	});

	$("#submit-text").click(function(){
		var text_book = $('#text-book').val();
		var text_book = (text_book).replace(/ /g, "-");
		var text_chapter = $('#text-chapter').val();
		var text_verse = $('#text-verse').val();
		if(text_chapter == 'ch') {
			text_chapter = "";
		}
		if(text_verse == 'v') {
			text_verse = "";
		}
		if($(text_verse).val() == "" && $(text_chapter).val() == ""){
			window.location = '/sermons/sermon-archive-search.php/passage/'+text_book;
		} else if($(text_verse).val() == ""){
			window.location = '/sermons/sermon-archive-search.php/passage/'+text_book+'/'+text_chapter;
		} else {
			window.location = '/sermons/sermon-archive-search.php/passage/'+text_book+'/'+text_chapter+'/'+text_verse;
		}
		return false;
	});	

	$('#jump-to-date').datepicker({
		dateFormat: 'mm-dd-yy',
		maxDate: '+0D',
		onSelect: function(dateText, inst) {
		window.location = '/sermons/sermon-archive-search.php/date/'+dateText;
		}
	});
	
	$('#date-range-start, #date-range-end').datepicker({
		dateFormat: 'mm-dd-yy',
		maxDate: '+0D'
	});

	$("#submit-date-range").click(function(){
		var date_start = $('#date-range-start').val();
		var date_end = $('#date-range-end').val();
		if(date_start != 'start date...' && date_end != "end date...") {
			window.location = '/sermons/sermon-archive-search.php/date/'+date_start+'/'+date_end;
		}
	});	
}

function widgetsSermons(){
	$("p.summary").hide();
	$('.a-summary, .a-summary-active').click(function(){
		var sermon_id = $(this).parent().attr("class");
		$("p.summary."+sermon_id).slideToggle("slow");
		var active_class = $(this).attr("class");
		if($(this).attr("class") == 'a-summary'){
			$(this).addClass("a-summary-active").removeClass("a-summary");
		} else {
			$(this).addClass("a-summary").removeClass("a-summary-active");
		}
		return false;
	});	

	var profiles = {
		windowCenter: {
			location:0,
			height:180,
			width:417,
			center:1,
			resizable:0
		},
		windowPrint: {
			location:0,
			height:500,
			width:650,
			center:1,
			resizable:1,
			scrollbars:1
		},
		videoCenter: {
			location:0,
			height:450,
			width:650,
			center:1,
			resizable:0
		}
	};
	$(function(){
		$(".popupwindow").popupwindow(profiles);
	});
	$(".a-email").click(function(){
		var id = $(this).parent().attr("class");
		$("#dialog-email").html("");
		$("#dialog-email").load('/sermons/sermon-send.php?id='+id);
		$('#dialog-email').dialog('open');
		return false;
	});
	$(function() {
		$("#dialog-email").dialog({
			bgiframe: true,
			autoOpen: false,
			height: 350,
			width: 500,
			modal: true,
			close: function(){
				$("#dialog-email").html("");
			}
		});
	});	
	$("#email-sermon").parent().click(function(){
		var id = $(this).children().attr("class");
		$("#dialog-email").html("");
		$("#dialog-email").load('/sermons/sermon-send.php?id='+id);
		$('#dialog-email').dialog('open');
		return false;
	});
	$(function() {
		$("#dialog-email").dialog({
			bgiframe: true,
			autoOpen: false,
			height: 350,
			width: 500,
			modal: true,
			close: function(){
				$("#dialog-email").html("");
			}
		});
	});	
	$(".a-notes").click(function(){
		var id = $(this).parent().attr("class");
		$("dialog-view-notes").html("");
		$("#dialog-view-notes").load('/sermons/sermon-notes.php?id='+id);
		$('#dialog-view-notes').dialog('open');
		return false;
	});
	$(function() {
		$("#dialog-view-notes").dialog({
			bgiframe: true,
			autoOpen: false,
			height: 510,
			width: 666,
			modal: true,
			close: function(){
				$("#dialog-view-notes").html("");
			}
		});
	});			
	$(".li-video").click(function(){
		var id = $(this).children().attr("class");
		$("dialog-video").html("");
		$("#dialog-video").load('sermon-video.php?id='+id);
		$('#dialog-video').dialog('open');
		return false;
	});
	$(function() {
		$("#dialog-video").dialog({
			bgiframe: true,
			autoOpen: false,
			height: 510,
			width: 666,
			modal: true,
			close: function(){
				$("#dialog-video").html("");
			}
		});
	});		
}


// 	----------------------------------------------------------------------
// 	Video Functionality
// 	----------------------------------------------------------------------

function filtersVideo(){
	$("#filter-video-category").click(function(){
		var id = $('#video-category').val();
		var video_category = $('#video-category option:selected').text().replace(/ /g, "-");
		if(id != 'Please select...') {
			window.location = '/video/video-category.php/'+id+'/'+video_category;
		}
	return false
	});
}

function widgetsVideo(){
	var profiles = {
		windowCenter: {
			location:0,
			height:450,
			width:650,
			center:1,
			resizable:0
		}
	};
	$(function(){
		$(".popupwindow").popupwindow(profiles);
	});
	$(".a-email").click(function(){
		var id = $(this).parent().attr("class");
		$("#dialog-email").html("");
		$("#dialog-email").load('/video/video-send.php?id='+id);
		$('#dialog-email').dialog('open');
		return false;
	});
	$(function() {
		$("#dialog-email").dialog({
			bgiframe: true,
			autoOpen: false,
			height: 350,
			width: 500,
			modal: true,
			close: function(){
				$("#dialog-email").html("");
			}
		});
	});
}

function widgetsSingleVideo(){
	$(".a-email").click(function(){
		var id = $(this).parent().attr("class");
		$("#dialog-email").html("");
		$("#dialog-email").load('/video/video-send.php?id='+id);
		$('#dialog-email').dialog('open');
		return false;
	});
	$(function() {
		$("#dialog-email").dialog({
			bgiframe: true,
			autoOpen: false,
			height: 350,
			width: 500,
			modal: true,
			close: function(){
				$("#dialog-email").html("");
			}
		});
	});
}


// 	----------------------------------------------------------------------
// 	Email Form Functionality
// 	----------------------------------------------------------------------

function sendEmail() {
	
	$('#loading').hide();
	
	$(function() {
		$('input[type=text], textarea').focus(function() {
			$(this).val('');
			$(this).removeClass('field-error');
		});
	});
	$(".send-email").click(function(){	
		$(".error").hide();
		var hasError = false;
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;

		var email_from = $("#email_from").val();
		if(email_from == '') {
			$("#email_from").val('You forgot to enter your email');
			$("#email_from").addClass('field-error');
			hasError = true;
		} else if(!emailReg.test(email_from)) {	
			$("#email_from").val('You did not enter a valid email');
			$("#email_from").addClass('field-error');
			hasError = true;
		}
		
		var email_from_name = $("#email_from_name").val();
		if(email_from_name == '') {
			$("#email_from_name").val('You forgot to enter your name');
			$("#email_from_name").addClass('field-error');
			hasError = true;
		}
	
		var message = $("#message").val();
		if(message == '') {
			$("#message").val('You forgot to enter your message');
			$("#message").addClass('field-error');
			hasError = true;
		}
	
		var message = $("#message").val();
		var question = $("#question").val();
		var type = $("#type").val();
			
		if(hasError == false) {
			$('#submit').hide();
			$('#loading').show();
			$.post("/email/send-email.php", { type: type, email_from: email_from, email_from_name: email_from_name, message: message }, function(data){
				$("#send-email-form").slideUp("normal", function() {				   
					$("#send-email-form").before('<br /><br /><p class="success">Your email has been sent, thanks!  We will do our best to respond to you quickly.  We hope you found our website beneficial.</p><br />');											
					});
				}
			 );
		}
		return false;
	});
}

// 	----------------------------------------------------------------------
// 	Google Map Functionality
// 	----------------------------------------------------------------------

function googleMap() {
  if (GBrowserIsCompatible()) {
	var map = new GMap2(document.getElementById("google-map"));
	map.addControl(new GSmallMapControl());
	map.setCenter(new GLatLng(45.37035, -75.77545), 12);
					
	var point = new GPoint (-75.77545, 45.37035); 
	var marker = new GMarker (point); 
	map.addOverlay(marker);
  }
}


// 	----------------------------------------------------------------------
// 	Additional JS for Website
// 	----------------------------------------------------------------------

function introVideo() {
	if (AC_FL_RunContent == 0) {
		alert("This page requires AC_RunActiveContent.js.");
	} else {
		AC_FL_RunContent(
			'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0',
			'width', '652',
			'height', '378',
			'src', 'intro-video',
			'quality', 'high',
			'pluginspage', 'http://www.macromedia.com/go/getflashplayer',
			'align', 'middle',
			'play', 'true',
			'loop', 'true',
			'scale', 'showall',
			'wmode', 'transparent',
			'devicefont', 'false',
			'id', 'intro-video',
			'bgcolor', '#ffffff',
			'name', 'intro-video',
			'menu', 'true',
			'allowFullScreen', 'false',
			'allowScriptAccess','sameDomain',
			'movie', 'intro-video',
			'salign', ''
			); //end AC code
	}
}

$(document).ready(function() {
	$(document).pngFix(); 
	$('.featured').after('<img src="/img/layout/bg-featured-bottom.jpg" width="299" height="6" class="featured-bottom" />');
	$('#content').after('<img src="/img/layout/bg-content-bottom.jpg" width="951" height="39" class="content-bottom" />');
});