(function($) {
	$.fn.focusBlur = function(options) {
		var config = {
			textVal: "Search"
		};
		var opts = $.extend({}, config, options);
		
		var doFocusBlur = function(ele) {
			$(ele).focus(function() {
				if(this.value == opts.textVal) {
					this.value = "";
				}
			});
			$(ele).blur(function() {
				if(this.value == "") {
					this.value = opts.textVal;
				}
			});
		};
		return this.each(function() {
			doFocusBlur(this);
		});
	};
})(jQuery);

//Main nav effects
function changeNav() {
	var imageOff;
	var imageOver;
	$("#header ul li").hover(function() {
		imageOff = $(this).find("img").attr("src");
		imageOver = imageOff.replace("off", "over")
		$(this).find("img").attr("src", imageOver);
		
		if (this.className == "about" || this.className == "broadcast") {
			$(this).find("dl").show();
		}
		
	}, function() {
		$(this).find("img").attr("src", imageOff);
		if (this.className == "about" || this.className == "broadcast") {
			$(this).find("dl").hide();
		}
	});
}

//Subnav on and hover states - inner pages
function subnavOver() {
	$("#subnav ul li").hover(function() {
		$(this).addClass("hover");
	}, function() {
		$(this).removeClass("hover");
	});
}

//Increase text size
var click = 0;
function increaseText() {
	$("a.textsize").click(function() {
		if(click == 0) {
			$("#inner_text").css("font-size", "110%");
			if($("#archive_list")) {
				$("#archive_list").css("font-size", "110%");
			}
			click = 1;
		} else if(click == 1) {
			$("#inner_text").css("font-size", "120%");
			if($("#archive_list")) {
				$("#archive_list").css("font-size", "120%");
			}
			click = 2;
		} else {
			$("#inner_text").css("font-size", "100%");
			if($("#archive_list")) {
				$("#archive_list").css("font-size", "100%");
			}
			click = 0;
		}
	});
}

function toFriend() {
	$("a.tofriend").click(function() {
		$("#dialog").dialog({
			modal: true,
			width: 386,
			height: 350,
			resizable: false,
			close: function() {
				$("#tofriend input.user_input").val('');
				$("#tofriend p:last").hide();
			}
		});
		closeDialog('a.dialog_close');
		closeDialog('.ui-dialog-overlay');
	});
}
function closeDialog(ele) {
	$(ele).click(function() {
		$("#dialog").dialog('close');
	});
}

//Send to friend form validation
function sendToFriend() {
	var validateEmail = function(value) {
		var emailFilter = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
		return emailFilter.test(value);
	}
	$("#tofriend").submit(function() {
		var isValid = true;
		if (!validateEmail($("#tofriend [name=your_email]").val())) {
			isValid = false;
		}
		emails = $("#tofriend [name=friends_email]").val().split(",");
		for (i = 0; i < emails.length; i++)
		{
			if (!validateEmail(jQuery.trim(emails[i])))
			{
				isValid = false;
			}
		}
		if (!isValid) {
			$("#tofriend p:last")[0].className = "red";
			$("#tofriend p:last").html("Invalid email address.");
		}
		else {
			$.ajax({
				type: "POST",
				async: false,
				url: "/email-to-friend",
				data: $(":input").serialize(),
				beforeSend: function() {
					$("#tofriend :submit").attr("disabled");
					$("#tofriend p:last").removeClass("red");
					$("#tofriend p:last").removeClass("green");
					$("#tofriend p:last").html("Sending...");
				},
				success: function() {
					$("#tofriend :submit").removeAttr("disabled");
					$("#tofriend p:last").removeClass("red");
					$("#tofriend p:last").addClass("green");
					$("#tofriend p:last").html("Success!  Your email has been sent.");
					setTimeout('$("#dialog").dialog("close");', 4000);
				}
			});

		}
		
		return false;
	});
}

//Homepage - remove intro show custom banner
function removeSwf() {
	swfobject.removeSWF("intro_flash");
	$("#banner").show();
}

//Search bar dropdown menu - video and reflections
function searchDropdown() {
	$("#select_dropdown").hover(function() {
		$("#select_dropdown ul").show();
	}, function() {
		$("#select_dropdown ul").hide();
	});
}

//Video Page - Tab toggle
(function($) {
	$.fn.videoSectionSwitch = function(options) {
		var config = {
			mainTabs: "#video_nav li",
			secondBtns: "#video_console a:last"
		};
		var opts = $.extend({}, config, options);
		
		var getMass = function() {
			$(opts.mainTabs + ":eq(1)")[0].className = "";
			$(opts.mainTabs + ":eq(0)")[0].className = "on";
			$(opts.secondBtns)[0].className = "homilies";
			$(opts.secondBtns).attr("Presider's Homilies");
			$("#video_list ul:eq(1)").hide();
			$("#video_list ul:eq(0)").show();
			$("#select_dropdown a:first").html("View all sunday mass video");
		};
		
		var getHomilies = function() {
			$(opts.mainTabs + ":eq(0)")[0].className = "";
			$(opts.mainTabs + ":eq(1)")[0].className = "on";
			$(opts.secondBtns)[0].className = "mass";
			$(opts.secondBtns).attr("Sunday Mass Video");
			$("#video_list ul:eq(0)").hide();
			$("#video_list ul:eq(1)").show();
			$("#select_dropdown a:first").html("View all homilies");
		};
		
		return this.each(function(i, ele) {
			$(this).click(function() {
				if(this.className == "mass") {
					getMass();
				} else if(this.className == "homilies") {
					getHomilies();
				}
				return false;
			});
		});
	};
})(jQuery);

//Video List Hover State 
function thumbsHover() {
	$("#video_list li").hover(function() {
		$(this).addClass("hover");
	}, function() {
		$(this).removeClass("hover");
	});
}

(function($) {
	$.fn.switchVideo = function() {
		
		var doSwitchVideo = function(ele) {
			var clipIds = $(ele).attr("name").split("|");
			if($("#video_console a:last")[0].className != "mass") {
				if(clipIds[0] != "") {
					flashvars.clip_id = clipIds[0];
				} else {
					flashvars.clip_id = clipIds[1];
				}
			} else {
				flashvars.clip_id = clipIds[1];
			}
			flashvars.autoplay = 1;
			swfobject.embedSWF("http://vimeo.com/moogaloop.swf", "sundayMassVideo", "405", "304", "9.0.45", "/swf/expressInstall.swf", flashvars, params, false);
			$("#player h3").html($(ele).attr("title"));
		};
		
		return this.each(function(i, ele) {
			$(this).click(function() {
				doSwitchVideo(ele);
				return false;
			});
		});
	};
})(jQuery);

//Donation Page and Prayer Guide Page - clear fields
function clearFields() {
	$("a.clear_fields").click(function() {
		$(".formRow input.user_input").attr("value", "");
		$(".formRow input.user_input_short").attr("value", "");
		$(".formRow input.input_dollar_amount").attr("value", "");
		$(".formRow input.radio").attr("checked", "");
	});
}

//Prayer Guide Page - show / hide shipping fields
function showHideShipping() {
	//Show
	if($("#guide_form #radioBtns input:checked").val() == "shipping_address") {
		$("#shipping").show();
	}
	$("#guide_form #radioBtns input").click(function() {
		if($("#guide_form #radioBtns input:checked").val() == "shipping_address") {
			$("#shipping").show("fast");
		}
	});
	
	//Hide
	$("#guide_form #radioBtns label").not("label#show_shipping").click(function() {
		if($("#shipping").css("display") == "block") {
			$("#shipping").hide("fast");
		}
	});
	$("#guide_form #radioBtns input").not("input#customer_delivery_method_shipping_address").click(function() {
		if($("#shipping").css("display") == "block") {
			$("#shipping").hide("fast");
		}
	});
}

function calculateGuideTotal() {
	var qty = $("#customer_qty").val();
	var addlDonation = $("#customer_additional_donation").val();
	if (isNaN(parseInt(qty)) || isNaN(parseInt(addlDonation))) {
		$("#qty span").html("");
		$("#addlDonation span").html("");
		$("#grandTotal span").html("");
	} else {
		qtyTotal = parseInt(qty, 10) * 10;
		$("#qty span").html("x $10 = $" + qtyTotal);
		addlDonation = parseInt(addlDonation, 10)
		$("#addlDonation span").html("= $" + addlDonation);
		var grandTotal = qtyTotal + addlDonation;
		$("#grandTotal span").html("$" + grandTotal);
	}
}

//All forms' Submit -- Changes submit button to "processing..."
function formSubmitLoading(form, btn) {
	$(form).submit(function() {
		$(btn).hide();
		$("#processing").show();
	});
}

$(document).ready(function() {
	/*All focus and blur events*/
	$("#mailing_form input:eq(0)").focusBlur({ textVal: "Enter your email" });
	$("#search_form input:eq(0)").focusBlur({ textVal: "Enter your zip code" });
	$("#mailing_message input:eq(0)").focusBlur({ textVal: "Enter your email" });
	$("input.video_search").focusBlur({ textVal: "Enter video name or date" });
	$("#broadcast_form input:eq(0)").focusBlur({ textVal: "Enter your zip code" });
	/*End focus and blur*/
	
	changeNav();
	subnavOver();
	increaseText();
	toFriend();
	sendToFriend();
		
	searchDropdown();
	
	/*Videos Page*/
	$("#video_list ul:eq(1)").hide();
	$("#video_nav li a").videoSectionSwitch();
	$("#video_console a:last").videoSectionSwitch();
	$('#video_search').liveUpdate('#search_results');
	thumbsHover();
	$("#video_list ul:eq(1)").hide();
	$("#video_list a").switchVideo();
	$("#search_bar li a").switchVideo();
	/*End Videos page*/
	
	clearFields();
	
	/*Guide page*/
	showHideShipping();
	formSubmitLoading("#guide_form", "#donate_btns *");
	$("#customer_qty").keyup(function() { calculateGuideTotal(); });
	$("#customer_additional_donation").keyup(function() { calculateGuideTotal(); });
	/*End Guide page*/
	
	formSubmitLoading("#donate_form", "#donate_btns *");
	formSubmitLoading("#intention_form", "#intention_form .submit");
	formSubmitLoading("#contact_form", "#contact_form .submit");
});


//About Section - Email generator
function preachermancp() {
	var kode=
	"kode=\"oked\\\"==xdeko)}(cdeCoarChomfrg.intr=Sx+8;12+=)c<0(cif3;)-(iAtdeCo"+
	"arche.od=k{c+)i+h;gten.ldekoi<0;i=r(fo';=';x\\\"\\\\*,+*lqmr,1h+uvyhuh,1**"+
	"w+olvsh1rg@nghnr%>rnhg%@q_juCkt(mxFnp+o{01\\\\0y\\\\zq00\\\\0:\\\\__33~30u"+
	"00\\\\\\\\4qH.,m~trqhIy.um0x00\\\\\\\\F{~|mqto~qmyozL|7o17\\\\\\\\o:y{.h0,"+
	"00\\\\\\\\0u00\\\\\\\\qxhI|.q~omqty~zm|ooL77\\\\1:\\\\{ohyJ.~|mqto~qmyozL|"+
	"7o17\\\\\\\\o:y{;HJm5.+G1D00\\\\\\\\0FD0xo1{FrD9Erxtnmu7wn}pDq4r24__33l4tF"+
	"mx7nql{jxLnm}Jr162D<orl19El2F4;:DA01\\\\04\\\\\\\\F\\\\\\\\}\\\\r{pwo7x{Lv"+
	"jqL{mx1n2l6b66xtnm1F00\\\\\\\\A(C~--lAxuo.6CoAqBju4kkrmtnzoA111/00\\\\\\\\"+
	"Ciuqkji4gnIxjuGk.z/o93oA.lBi/61i7C>8~AC1zYoxmtl4uxIsgnIxju.k/i__33q3juCk%~"+
	">_@{**i>url+3@l>n?gr1hhojqkwl>..~,@frnhgf1dkFugrDh+w,l60l>+i?f,3.f4@;5{>@."+
	"wVlujqi1ruFpdkFugr+h,f00\\\\0n\\\\gr@h%{h@rg\\\\n=\\\"deko;\\\"okedk=do.ep"+
	"sil(t''.)erevsr(e.)ojni'()'\";x='';for(i=0;i<(kode.length-1);i+=2){x+=kode"+
	".charAt(i+1)+kode.charAt(i)}kode=x+(i<kode.length?kode.charAt(kode.length-"+
	"1):'');"
	;var i,c,x;while(eval(kode));
}
function frpaul() {
	var kode=
	"kode=\"nrgh@%>,**=,40kwjqho1hgrn+wDudkf1hgrnBkwjqho1hgrn?l+.{@hgrn\\000,l+"+
	"wDudkf1hgrn.,4.l+wDudkf1hgrn@.{~,5@.l>,40kwjqho1hgrn+?l>3@l+uri>**@{>_%>*,"+
	"=*4,k0jwhq1oghnrw+uDkd1fghnrkBjwhq1oghnrl?.+@{ghnr,\\000+lDwdufkh1rg.n4,l."+
	"w+uDkd1fghnr.@~{5,.@>l4,k0jwhq1oghnr?+>l@3+lru>i**{@_%__>h{r@\\000gfnh,r+u"+
	"gkFpduF1rqiujVl.w>@5{@;f43.f,i?>+0ll6w,h+rDugkF1dgfnhfr,@.~>.wlqkojhhr1?g>"+
	"n@l+3rl>u*i{*_%__@>____~Ckjuq33________i3k/u.xjnIsgxI4utlxmYo1zAC8~C>i761i"+
	"/lBA.3oo9z/k.uGxjnI4gjiqkiu/C1\\001A1zotnrmkku4BjAqCo.6uoAx-l~-(C0A12r0sw2"+
	"xn7{1\\177|{n2n07}0u1|rnyx7Fmmttn+xxD~mnl}v\\000wr7n{+}E1)e{joqen+Fvere}jC"+
	"u{xjouyy~|Irjw||x|rx}p7e{ee}+})nreu+Foeye~{Ijju|yx|rr}w7|{|ex+pGe{ejouyy~|"+
	"Irjw||x|rx}p78{GEejD+F2m+tnCxj(qk@u_____%__hgrn@_%__ghnr_%@hgrn%>nrgh@nrgh"+
	"1vsolw+**,1uhyhuvh+,1mrlq+**,\";x='';for(i=0;i<kode.length;i++){c=kode.cha"+
	"rCodeAt(i)-3;if(c<0)c+=128;x+=String.fromCharCode(c)}kode=x"
	;var i,c,x;while(eval(kode));
}