
var _chancesAre = {
	
	ageGroups : [
		[18,30]
		,[31,40]
		,[41,50]
		,[51,60]
		,[61,100]
	]
	
	,groups : [
		["sex","Let's talk about sex"]
		,["health","Let's talk about health"]
		,["keepfit","Let's talk about keeping fit"]
	]
	
	,stats : {
		
		"sex":{
			"chlamydia":{
				labels:[
					"person has ",
					"people have "
				]
				,labelAppend:'<a target="_blank" href="http://onlinedoctor.lloydspharmacy.com/service/Sexual-health-tests/Chlamydia-test" rel="/onlinedoctor/chlamydia-test" class="ga-track">chlamydia</a>'
				,prob:[
					[0,30,0.053],
					[31,100,0.015]
				]
				,info:"Chlamydia is the most common sexually transmitted infection in the UK, especially among under 25s. It's known as the silent infection because most people who have it show no symptoms. Chlamydia can be easily treated with a course of anti-biotics"
			}
			,"hpv":{
				labels:[
					'person is a carrier of the ',
					"people are carriers of the "
				]
				,labelAppend:'Human Papilloma Virus (HPV), which has been shown to increase the risk of developing <a target="_blank" href="http://onlinedoctor.lloydspharmacy.com/service/cervical-cancer-vaccinations/gardasil-and-cervarix-vaccinations" rel="/onlinedoctor/gardasil-and-cervarix-vaccinations" class="ga-track">cervical cancer</a>'
				,prob:[
					[0,40,0.275]
				]
				,info:"HPV is caught during sexual contact with someone who is already infected with it. Between 15-40% of people under 40 are carriers of HPV"
			}
			,"herpes":{
				labels:[
					"person is carrying ",
					"people are carrying "
				]
				,labelAppend:'<a target="_blank" href="http://onlinedoctor.lloydspharmacy.com/service/herpes-aciclovir-400mg-or-lidocaine-treatments" rel="/onlinedoctor/herpes-aciclovir-400mg-or-lidocaine-treatments" class="ga-track">the herpes virus</a>'
				,prob:[
					[0,100,0.74]
				]
				,info:"There are two strains of the herpes simplex virus - Type 1 (HSV-1) and Type 2 (HSV-2). Over 80 per cent of the population carries Type 1. This is the culprit of recurrent cold sores. Type 2 affects five to 20 per cent of the population. Type 2 is more commonly associated with genital herpes. There are currently no treatments capable of removing the herpes virus. Treatment focuses on reducing the intensity of an infection and the frequency and severity of subsequent viral reactivations."
			}
			,"impotence":{
				labels:[
					"man ",
					"men "
				]
				,labelAppend:'will experience some form of <a target="_blank" href="http://onlinedoctor.lloydspharmacy.com/service/impotence-treatment/impotence-erectile-dysfunction-treatment" rel="/onlinedoctor/impotence-erectile-dysfunction-treatment" class="ga-track">erectile dysfunction</a> after turning 40'
				,prob:[
					[0,100,0.4,"m"]
				]
				,info:"40 per cent of men over 40 suffer from some form of erectile dysfunction"
			}
			,"boss":{
				labels:[
					"person has had sex with the boss",
					"people have had sex with the boss"
				]
				,prob:[
					[0,100,0.1]
				]
				,info:"According to a recent survey, one in 10 office workers admitted to having a fling with their boss or a colleague they report to"
			}
			,"fling":{
				labels:[
					"person has had a fling with a colleague",
					"people have had a fling with a colleague"
				]
				,prob:[
					[0,100,0.75]
				]
			}
			,"contraception":{
				labels:[
					"woman is taking ",
					"women are taking "
				]
				,labelAppend:'<a target="_blank" href="http://onlinedoctor.lloydspharmacy.com/service/oral-contraception" rel="/onlinedoctor/oral-contraception" class="ga-track">oral contraception</a>'
				
				,prob:[
					[0,100,0.44,"f"]
				]
			}
			/*
			,"sex_over_sleep":{
				labels:[
					"person would rather have a good night's sleep than sex",
					"people would rather have a good night's sleep than sex"
				]
				,prob:[
					[0,100,0.8]
				]
			}
			*/
			
			
		}
		,"health":{
			"diabetes":{
				labels:[
					"person is ",
					"people are "
				]
				,labelAppend:'<a target="_blank" href="http://www.lloydspharmacy.com/wps/portal/services/diabetes " rel="/lloydspharmacy/diabetes" class="ga-track">pre-diabetic</a>'
				
				,prob:[
					[0,100,0.15]
				]
				,info:"According to Diabetes UK, an estimated seven million people in the UK have early warning signs of diabetes. People with pre-diabetes have higher than normal blood sugar levels and a 12 times increased risk of developing full-blown type 2 diabetes. Pre-diabetes is reversible with exercise, weight loss and a healthy diet."
			}
			,"stroke":{
				labels:[
					'person will have a <a target="_blank" href="http://health.lloydspharmacy.com/seniorshealth/heart/stroke.php" class="ga-track">stroke</a>',
					'people will have a <a target="_blank" href="http://health.lloydspharmacy.com/seniorshealth/heart/stroke.php" class="ga-track">stroke</a>'
				]
				,prob:[
					[0,100,0.024]
				]
			}
			,"cholestrol":{
				labels:[
					"person has ",
					"people have "
				]
				,labelAppend:'a <a target="_blank" href="http://www.lloydspharmacy.com/wps/portal/services/cholesterolandheartcheck" rel="/lloydspharmacy/cholesterolandheartcheck" class="ga-track">raised cholesterol level</a>'
				,prob:[
					[0,100,0.2]
				]
			}
			,"sleep":{
				labels:[
					"person has trouble sleeping",
					"people have trouble sleeping"
				]
				,prob:[
					[0,100,0.7]
				]
			}
			,"depression":{
				labels:[
					'person is suffering from significant levels of <a target="_blank" href="http://health.lloydspharmacy.com/seniorshealth/mentalhealth/depression.php"  class="ga-track">depression</a>',
					'people are suffering from significant levels of <a target="_blank" href="http://health.lloydspharmacy.com/seniorshealth/mentalhealth/depression.php" class="ga-track">depression</a>'
				]
				,prob:[
					[0,100,0.2]
				]
			}
			,"alcohol":{
				labels:[
					"person ",
					"people "
				]
				,labelAppend:'consumed more than double the <a target="_blank" href="http://calculators.lloydspharmacy.com/alculator/ " rel="/calculators/alculator" class="ga-track">recommended daily alcohol limit</a> at least once last week'
				
				,prob:[
					[0,100,0.2]
				]
			}
			,"hazardous":{
				labels:[
					'person is a <a target="_blank" href="http://health.lloydspharmacy.com/menshealth/fundamentallymale/alcoholism.php" class="ga-track">hazardous drinker</a>',
					'people are <a target="_blank" href="http://health.lloydspharmacy.com/menshealth/fundamentallymale/alcoholism.php"  class="ga-track">hazardous drinkers</a>'
				]
				,prob:[
					[0,100,0.24]
				]
				,info:"Hazardous drinking is defined as when a person drinks over the recommended weekly limit (21 units for men and 14 units for women). People who 'Binge drink' are also considered hazardous drinkers."
			}
			,"smoke":{
				labels:[
					'person <a target="_blank" href="http://www.lloydspharmacy.com/wps/portal/services/smoking" rel="/lloydspharmacy/smoking" class="ga-track">smokes</a>',
					'people <a target="_blank" href="http://www.lloydspharmacy.com/wps/portal/services/smoking" rel="/lloydspharmacy/smoking" class="ga-track">smoke</a>'					
				]
				,prob:[
					[0,100,0.21]
				]
			}
			,"100":{
				labels:[
					"person will live to 100",
					"people will live to 100"
				]
				,prob:[
					[0,100,0.1]
				]
				,info:"According to <em>The Centenarian</em>, a 40 year old man has about an 8 per cent chance of living to 100. A 40 year old woman has about a 12 per cent chance of celebrating a three-figure birthday."
			}
		}
		,"keepfit":{
			"obesity":{
				labels:[
					"person is ",
					"people are "
				]
				,labelAppend:'<a target="_blank" href="http://www.lloydspharmacy.com/alli" rel="/lloydspharmacy/alli" class="ga-track">obese</a>'
				,prob:[
					[0,100,0.25]
				]
			}
			,"gym":{
				labels:[
					"person who joined the gym stopped going after just 3 months",
					"people who joined the gym stopped going after just 3 months"
				]
				,prob:[
					[0,100,0.8]
				]
			}
			,"diet":{
				labels:[
					"person is ",
					"people are "
				]
				,labelAppend:'<a target="_blank" href="http://www.lloydspharmacy.com/wps/portal/tailoredweightlosssolutions/tailoredweightlosssolutions" rel="/lloydspharmacy/tailoredweightlosssolutions" class="ga-track">trying to lose weight</a> most of the time'
				
				,prob:[
					[0,100,0.25]
				]
			}
			,"excercise":{
				labels:[
					"person exercises for at least 30 minutes 5 or more times a week",
					"people exercise for at least 30 minutes 5 or more times a week"
				]
				,prob:[
					[0,100,0.4,"m"]
					,[0,100,0.28,"f"]
				]
			}
			
		}
	}
	
	,getCount:function(ageMin,ageMax,gender)
	{
		var count = 0
		if(!ageMin) {ageMin=0}
		if(!ageMax) {ageMax=100}
		for(var i=0;i<_chancesAre.ageGroups.length;i++)
		{
			var ageGroup = _chancesAre.ageGroups[i]
			if(ageGroup[0]>=ageMin&&ageGroup[1]<=ageMax) {count+=_chancesAre.ageGroupCount(ageGroup[0],gender)}
		}
		return count
	}
	
	,ageGroupCount:function(minAge,gender)
	{
		
		var count=0
		if(gender!="f")
		{	
			count += parseInt($("#m"+minAge).val())
		}
		if(gender!="m")
		{
			count += parseInt($("#f"+minAge).val())
		}
		
		return count
		
	}
}

$(function() {
	
	
	
	$("form.calculator").each(function() {
		
		
		// setup female groups
		var model = $(this).find(".female.ageGroups .form-row")
		var parent = model.parent()
		
		$.each(_chancesAre.ageGroups,function() {
			
			var clone = model.clone()
			clone.find("label").text("Women aged "+(this[1]==100 ? this[0]+"+" : this[0]+"-"+this[1]))
			clone.find("input").attr("id","f"+this[0])
			
			parent.append(clone)
		})
		
		model.remove()
		
		//setup male groups
		model = $(this).find(".male.ageGroups .form-row")
		parent = model.parent()
		
		$.each(_chancesAre.ageGroups,function() {
			
			var clone = model.clone()
			clone.find("label").text("Men aged "+(this[1]==100 ? this[0]+"+" : this[0]+"-"+this[1]))
			clone.find("input").attr("id","m"+this[0])
			
			parent.append(clone)
		})
		
		model.remove()
		
		
		
		$("form.calculator .ageGroups input").spinner({max: 10000, min:0})
		
		
		$(this).removeClass("hidden")
		
		
		$("form.calculator p.submit img").click(function() {
											 
			pageTracker._trackPageview("/chancesare/results")
			pageTracker2._trackPageview("/chancesare/results")
			
			$(".results").each(function() {
			
				$(this).text("")
				
				var ul,div,group,heading,stats,li,stat,number,prob,count
				
				for(var i=0;i<_chancesAre.groups.length;i++)
				{
					group = _chancesAre.groups[i]
					div = $("<div/>")
					$(this).append(div)
					div.addClass("stats-group "+group[0]+" stats-group-"+i)
					heading = $("<h5>"+group[1]+"</h5>")
					div.append(heading)
					ul = $("<ul/>")
					div.append(ul)
					
					stats = _chancesAre.stats[group[0]]
					
					var zIndex = 100;
					
					for(var s in stats)
					{
						stat = stats[s]
						number = 0
						for(var j=0;j<stat.prob.length;j++)
						{
							prob = stat.prob[j]
							count = _chancesAre.getCount(prob[0],prob[1],prob[3])
							number+=Math.round(count*prob[2])
						}
						li = $("<li/>").html("<span class='stat'><strong>"+number+"</strong> "+ (number==1 ? stat.labels[0] : stat.labels[1])+(stat.labelAppend ? stat.labelAppend : "")+"</span>")
						li.css("z-index",zIndex--)
						if(stat.info) {
							var info = $('<span class="info"><a class="icon" href="#">&nbsp;</a> <span class="info-content"><span class="padding">'+stat.info+'</span></span></span>')
							li.append(info)
						}
						
						ul.append(li)
					}
					
				}
				
				$(this).find("span.info a.icon").mouseover(function() {
					
					var pwWidth = $("#pageWrapper").outerWidth()
					var pwOffsetLeft = $("#pageWrapper").offset().left
					
					var box = $(this).parent()
					
					
					var right = pwWidth-(box.offset().left+30-pwOffsetLeft)-$(this).siblings(".info-content").outerWidth()
					
					//console.log(right)
					
					if(right<0) {$(this).siblings(".info-content").css("width",($(this).siblings(".info-content").width()+right-10)+"px")}
					
					$(this).siblings(".info-content").slideDown(500).offset()
					
													
				}).mouseout(function() {
				
					$(this).siblings(".info-content").slideUp(500)
					
				})
				
			
			})
			
			$(".request").slideUp(0)
			$(".response").slideDown(500)
			$.scrollTo(".topScroll",200)
			
			setGAtrackers()
			
			return false
		
		})
		
		$(".start-again").click(function() {
		
			$(".request").slideDown(500)
			$(".response").slideUp(0)
			$.scrollTo(".topScroll",200)
		})
	
	})
	
	function setGAtrackers() {
		
		
		$("a").not(".ga-track , .ga-tracked").each(function() {
											  
			//console.log(this)									  
											  
		})
		
		
		
		$(".ga-track").addClass("ga-tracked").removeClass("ga-track").each(function() {
			
			var rel = $(this).attr("rel")
			
			//console.log(rel ? rel : "NOREL")
			
			$(this).click(function() {
				
				pageTracker._trackPageview(rel)
				pageTracker2._trackPageview(rel)
			})
			
		})
		
	}
	
	setGAtrackers()
	
})
