jQuery(document).ready(function($){

	//Email Obfuscation
	$(".replaceAt").replaceWith("@");
  	$(".obfuscate").each(function () {
  		$(this).attr("href", "mailto:"+$(this).text());
  	});

	//Form Default Values
	$(".replacedefault input[type=text], .replacedefault textarea").bind('focus', function(){
		var currentValue = $(this).attr("value");
		var defaultValue = $(this).attr("defaultValue");
		if (currentValue == defaultValue) $(this).attr({ value: ""});
	});
	$(".replacedefault input[type=text], .replacedefault textarea").bind('blur', function(){
		var currentValue = $(this).attr("value");
		var defaultValue = $(this).attr("defaultValue");
		if (currentValue == '') $(this).attr({ value: defaultValue});
	});

	
	$("a#closewindow").click(function () {
		$(".footerpopup-pane").hide();
		$("#footerwrap ul li a").removeClass("selected");
		$("#footerpopup").fadeOut();
		return false;
	});
	
	$("#vidThumbs li").click(function() {
		videoID = $(this).attr("rel");
		videoCode = '<object width="420" height="278"><param name="wmode" value="true" />';
		videoCode = videoCode + '<param name="bgcolor" value="#161613" />';
		videoCode = videoCode + '<param name="allowfullscreen" value="true" />';
		videoCode = videoCode + '<param name="allowscriptaccess" value="always" />';
		videoCode = videoCode + '<param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=' + videoID + '&amp;server=vimeo.com&amp;show_title=0&amp;show_byline=0&amp;show_portrait=0&amp;color=bb8500&amp;fullscreen=1&amp;autoplay=1" />';
		videoCode = videoCode + '<embed src="http://vimeo.com/moogaloop.swf?clip_id=' + videoID + '&amp;server=vimeo.com&amp;show_title=0&amp;show_byline=0&amp;show_portrait=0&amp;color=bb8500&amp;fullscreen=1&amp;autoplay=1" bgcolor="#161613" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="420" height="278" wmode="transparent"></embed>';
		videoCode = videoCode + '</object>';
		$("#mainVideo").html(videoCode);
	});

	

	// POPUP WINDOWS
	 $('#overlayDiagnosed').jqm({trigger: 'a#bottomBox1', overlay: '75'});
	 $('#overlaySaveFriend').jqm({trigger: 'a#bottomBox2', overlay: '75'});
	 $('#overlayFindCure').jqm({trigger: 'a#bottomBox3', overlay: '75'});
	 $('#overlayUploadVideo').jqm({trigger: 'a#videoSubmissions', overlay: '75'});
	 
	 $("a#bottomBox1").click(function() {
	 	document.diagnosedForm.reset();
	 	$("#getDiagnosedMain").show();
	 	$("#overlayDiagnosed .findCureMain").hide();
	 });
	 
	 $("#clickToGet").click(function() {
	 	
	 	var theForm = document.diagnosedForm;
	 	for (i = 0; i < theForm.symptom.length; i++) {
			if (theForm.symptom[i].checked == true) var selectionNumber = theForm.symptom[i].value;
		}
		
		arrSelectionLinks = new Array();
		arrSelectionLinks[1] = "http://www.winshaperetreat.org/index.php/come/enjoy_our_activities.php";
		arrSelectionLinks[2] = "http://www.winshaperetreat.org/index.php/come_connect";
		arrSelectionLinks[3] = "http://www.winshaperetreat.org/index.php/about/our_location.php";
		arrSelectionLinks[4] = "http://www.winshaperetreat.org/index.php/come/programming/marriage_programming/";
		arrSelectionLinks[5] = "http://www.winshaperetreat.org/index.php/come/enjoy_our_activities.php";
		arrSelectionLinks[6] = "http://www.winshaperetreat.org/index.php/come/enjoy_our_activities.php";
		arrSelectionLinks[7] = "http://www.winshaperetreat.org/index.php/come/enjoy_our_activities.php";
		arrSelectionLinks[8] = "http://www.winshaperetreat.org/index.php/come/programming/ministry_programming/personal_development.php";
		arrSelectionLinks[9] = "http://www.winshaperetreat.org/index.php/come/enjoy_our_activities.php";
		arrSelectionLinks[10] = "http://www.winshaperetreat.org/index.php/come/enjoy_our_activities.php";
		
		var selectionLink = arrSelectionLinks[selectionNumber];
		if (selectionNumber == 10 || selectionNumber == 7) { selectionNumber = Math.floor(Math.random()*10) + 1; }
		//if (selectionNumber == 3 || selectionNumber == 4) { selectionNumber = selectionNumber + '-' + (Math.floor(Math.random()*3) + 1); }
		
		$("#overlayDiagnosed #cureImage").css("background-image","url(/wp-content/themes/tbd_1.0/images/cureImage"+selectionNumber+".jpg)");
		$("#overlayDiagnosed #cureText").css("background-image","url(/wp-content/themes/tbd_1.0/images/cureText"+selectionNumber+".jpg)");
		$("#winshapeRetreat").attr("href",selectionLink);
	 	$("#getDiagnosedMain").hide();
	 	$("#overlayDiagnosed .findCureMain").show();
	 	return false;
	 });
	 
	 $("#saveSubmit").click(function(){
		thisForm = document.saveFriendForm;
		if (checkForm(thisForm)) {
			$.post("/wp-content/themes/tbd_1.0/formsubmit.php", {
				name: thisForm.name.value,
				email: thisForm.email.value,
				fromname: thisForm.fromname.value,
				fromemail: thisForm.fromemail.value
			}, function(data){
			});
			document.saveFriendForm.reset();
			$("#sendsuccess").show();
			setTimeout("ThankYou()", 1000);
		}
		return false;
	 });

	 $(".backToDiagnose").click(function() {
	 	$("#getDiagnosedMain").show();
	 	$("#overlayDiagnosed .findCureMain").hide();
	 	return false;
	 });
	 
	 $("#saveSubmit").click(function(){
		thisForm = document.saveFriendForm;
		if (checkForm(thisForm)) {
			$.post("/wp-content/themes/tbd_1.0/formsubmit.php", {
				name: thisForm.name.value,
				email: thisForm.email.value,
				fromname: thisForm.fromname.value,
				fromemail: thisForm.fromemail.value
			}, function(data){
			});
			document.saveFriendForm.reset();
			$("#sendsuccess").show();
			setTimeout("ThankYou()", 1000);
		}
		return false;
	 });
	 

	$('#fileInput').uploadify({
		uploader  : '/wp-content/themes/tbd_1.0/uploadify.swf',
		'cancelImg': '/wp-content/themes/tbd_1.0/images/cancel.png',
		script    : '/wp-content/themes/tbd_1.0/uploadify.php',
		auto      : true,
		buttonText: 'UPLOAD VIDEO',
		sizeLimit : '20000000',
         	onComplete: function(event,queueID,fileObj,response,data) {
			$("#uploadsuccess").show();
			$.post("/wp-content/themes/tbd_1.0/fileuploaded.php", { filename: response });
		},
         	onOpen: function(event,queueID,fileObj,response,data) {
			$("#uploadsuccess").hide();
		}
		
	});

	
});

function ThankYou() {
	jQuery('#sendsuccess').fadeOut(750,function() {
	});
}

function isValidEmailAddress(emailAddress) {
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(emailAddress);
}

function stopDefAction(evt) {
	evt.preventDefault();
}

function checkForm(TheForm){
	if(!TheForm.fromname.value) {
		alert ("Please enter your name.");
		TheForm.fromname.focus();
		return false;
	}
	if(!TheForm.fromemail.value || !isValidEmailAddress(TheForm.fromemail.value)) {
		alert ("Please enter a valid email address.");
		TheForm.fromemail.focus();
		return false;
	}
	if(!TheForm.name.value) {
		alert ("Please enter your friend's name.");
		TheForm.name.focus();
		return false;
	}
	if(!TheForm.fromemail.value || !isValidEmailAddress(TheForm.fromemail.value)) {
		alert ("Please enter your friend's email address.");
		TheForm.fromemail.focus();
		return false;
	}
	return true;
}
