$(document).ready(function(){
	
	
positionFooter();

function positionFooter() {
	var page_height = $("#wrapper").height();
	var window_height = $(window).height();
	var myfooterm = window_height - page_height - 70;
	if (myfooterm < 20) 
			myfooterm = 20;

	$("#footer").css({
	marginTop: myfooterm + "px"
	});

}
	
	
	$('.accordion').accordion({
		collapsible: true,
		autoHeight: false,
		active: 0
		});

$("#search-btn").click(function(){


		searchdat = {
			gender : $("input:radio[name=gender]:checked").val(),
			age : $("#input_age").val(),
			unit : $("#input_unit").val()
		}
				$("#downloadbutton").attr('href','pdf/index.php?age=' + searchdat.age + '&unit=' + searchdat.unit + '&gender=' + searchdat.gender);
		$.getJSON('data.php',searchdat,function(data){
			if (data.error)
			{
				alert(data.error);
			} else {
				$("#results_area_screening").html(data.screening);
				$("#results_area_immunization").html(data.immunization);
				if (data.show_other)
				{
					$("#results_area_other").html(data.other);
				} else {
					$("#results_area_other").hide();
					$("#results_area_other_title").hide();
				}
				$("#results-area-intro").html(data.blurb);
				$("#results_area_header").html(data.header);



				$(".tipX").bt({
					trigger:"hover",
					padding: 10,
					positions: ['left'],
					width: 350,
					spikeGirth: 30,
					spikeLength: 15,
					strokeStyle: '#222c80',
					strokeWidth: 2,
					shadow:           true,                 // use drop shadow? (only displays in Safari and FF 3.1) - experimental
					shadowOffsetX:    5,                     // shadow offset x (px)
					shadowOffsetY:    5,                     // shadow offset y (px)
					shadowBlur:       5,                     // shadow blur (px)
					shadowColor:      "#999",                // shadow color/alpha
					shadowOverlap:   false,
					fill: 'rgba(255,255,255, 1)',
					hoverIntentOpts:{interval:100,timeout:50},
					cssStyles: {
						lineHeight: '14px',
						fontSize: '11px',
						fontFamily: 'verdana, arial,helvetica,sans-serif',
						paddingTop: '9px',
						paddingBottom: '15px',
						background: 'url(assets/images/) repeat-x left top'
					}
				});
				$('#results-area').fadeIn(700);
				$("#search-btn").text("Refresh Results");

			}

		});

	return false;
});

$("#how-to-use").dialog({
	autoOpen: false,
	width:580,
	closeText: 'x',
	title: 'How To Use This Information',
	modal: true
});

$('.how-to-use').click(function() {
	$("#how-to-use").dialog('open');
	return false;
});

$("#about-uhc").dialog({
	autoOpen: false,
	width:580,
	closeText: 'x',
	title: 'About UnitedHealthcare',
	modal: true
});

$('.about-uhc').click(function() {
	$("#about-uhc").dialog('open');
	return false;
});

$("#privacy").dialog({
	autoOpen: false,
	width:580,
	closeText: 'x',
	title: 'Privacy &amp; Security',
	modal: true
});

$('.privacy').click(function() {
	$("#privacy").dialog('open');
	return false;
});


$("#legal").dialog({
	autoOpen: false,
	width:580,
	height: 450,
	closeText: 'x',
	title: 'Legal Information',
	modal: true
});

$('.legal').click(function() {
	$("#legal").dialog('open');
	return false;
});



$("#dclaimers").dialog({
	autoOpen: false,
	width:580,
	height: 450,
	closeText: 'x',
	title: 'Disclaimers',
	modal: true
});

$('.dclaimers').click(function() {
	$("#dclaimers").dialog('open');
	return false;
});



$("#contact").dialog({
	autoOpen: false,
	width:580,
	height: 450,
	closeText: 'x',
	title: 'Contact Us',
	modal: true
});

$('.contact').click(function() {
	$("#contact").dialog('open');
	return false;
});



$("#addtl-resources").dialog({
	autoOpen: false,
	width:580,
	closeText: 'x',
	title: 'Additional Resources',
	modal: true
});

$('.addtl-resources').click(function() {
	$("#addtl-resources").dialog('open');
	return false;
});


$("#sharethis").dialog({
	autoOpen: false,
	height: 640,
	width:500,
	closeText: 'x',
	title: 'Share With a Friend',
	modal: true
});

$('.sharethis').click(function() {
	$("#sharethis").dialog('open');
	$("#shareForm").attr('action','share/send.php');
	return false;
});

$('.emailthis').click(function(){
	$("#sharethis").dialog('open');
	$("#shareForm").attr('action','share/send.php?cleanresults=1');
	return false;
});


$("#shareForm").validate({
	errorPlacement : function(error, element) {},
	rules : {
		recaptcha_response_field : { required:true },
		fromname  : { required:true },
		toname    : { required:true },
		fromemail : { required:true, email:true },
		toemail   : { required:true, email:true }
	},
	submitHandler : function(form) {
		$.ajax({
			type : "GET",
			url : "share/recaptcha.php?check=1",
			data : ({
				recaptcha_challenge_field : form.recaptcha_challenge_field.value,
				recaptcha_response_field  : form.recaptcha_response_field.value,
				gender : $("input:radio[name=gender]:checked").val(),
				age : $("#input_age").val(),
				unit : $("#input_unit").val()
			}),
			success : function(result) {
				if (result == "Success")
					$(form).ajaxSubmit(function() {
						//alert("Link sent - thanks for sharing!");
						$("#sharethis").dialog('close');
						$("#sendconfirm").css('top',$("html").scrollTop() + 200);
						$("#sendconfirm").fadeIn(400).delay(1000).fadeOut(400);
					});
				else {
					alert("Recaptcha failed.");
					Recaptcha.reload();
				}
			},
			error : function() {
				alert("Network error; please try again later.");
			}
		});
	}
});


$(".sharecancel").click(function(){
	$("#sharethis").dialog('close');
});

$(".didyouknow").cycle({random:true,pause:true});


showRecaptcha('recaptcha', 'sendbutton', 'clean');




// end document.ready
});

function printme(){
	$('.accordion').accordion('destroy');
	window.print();
	$('.accordion').accordion({
		collapsible: true,
		autoHeight: false,
		active: 0
		});
}

function showRecaptcha(element, submitButton, themeName) {
	Recaptcha.create("6Le5jggAAAAAADEulvfkDE0WUsb8avsZ3qjfXOdV", element, {
		theme    : themeName,
		tabindex : 0
	});
	hideSubmitButtons();
	$("#"+submitButton).show();
}

function hideSubmitButtons() {
	$("#sendbutton").hide();
}

function destroyRecaptchaWidget() {
	hideSubmitButtons();
	Recaptcha.destroy();
}

function hideSubmitButtons() {
	$("#sendbutton").hide();
}

function destroyRecaptchaWidget() {
	hideSubmitButtons();
	Recaptcha.destroy();
}


//COOKIE FUNCTIONS


function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

