	$(document).ready(function(){
		addCSS();
		hseAssessment();
	});

	var addCSS = function(){
		$('head').append('<link type="text/css" rel="stylesheet" href="/css/widget.css" />');
		if($.browser.msie && ($.browser.version < 7)){
			$('head').append('<link type="text/css" rel="stylesheet" href="/css/widget-ie6.css" />');
		};
	};/* addCSS */


	function hseAssessment(){
		var fadeSpeed = 400;
		var question = 1;
		setup();
		var	path = [],
			maxOuterHeight = 0,
			maxInnerHeight = 0,
			buttonHeight = false,
			$target = $('#wrapper'),
			$questions = $('#questions');
			
		$('.qa .content').each(function(){
			if ($(this).outerHeight() > maxOuterHeight){
				maxOuterHeight = $(this).outerHeight();
				maxInnerHeight = $(this).height();
			};
		});
		$('.qa').each(function(){
			$(this).find('.content').height(maxInnerHeight + 5);
			if(!buttonHeight){
				buttonHeight = $(this).find('.yes').height();
			};
			$(this).hide();
		});
		
		//	Start button
		$questions.find('.startButton').click(function(){
			path.push(moveNext($(this),'startButton'));
			_gaq.push(['_trackEvent','health & safety widget','interaction']);
		});
		$questions.find('.yes').click(function(){
			path.push(moveNext($(this),'yes'));
		});
		//	No Button
		$questions.find('.no').click(function(){
			path.push(moveNext($(this),'no'));
		});
		//	Prev Button
		$questions.find('.prev').click(function(){
			path = movePrev($(this),path);
		});
		
		$('.start').appendTo($target).fadeIn(fadeSpeed);

	
		function movePrev($elem,path){
			$prev = $('#' + path[path.length -1 ]);
			if($.browser.msie){
				$elem.parent().appendTo($('#questions')).hide();
				$prev.appendTo($('#wrapper')).show();
			} else {
				$elem.parent().fadeOut(fadeSpeed,function(){
					$(this).appendTo($('#questions'));
					$prev.appendTo($('#wrapper')).fadeIn(fadeSpeed);
				});
			};
			path.pop();
			question--;
			$('#id h3').text(question + '.');
			return path
		};/**/
	
		function moveNext($elem,selector){
			var $next = $('#' + $elem.parent().find('.' + selector + ' span').text());
			if($.browser.msie){
				if($elem.hasClass('startButton')){
					$elem.parent().find('.content').each(function(){
						$(this).animate({height:(maxInnerHeight + 5 + buttonHeight) + 'px'},fadeSpeed,function(){
							$elem.parent().appendTo($('#questions')).hide();
							$next.appendTo($('#wrapper')).show();
						});
					});
				} else {
					$elem.parent().appendTo($('#questions')).hide();
					$next.appendTo($('#wrapper')).show();
				};
			}else{
				if($elem.hasClass('startButton')){
					$elem.parent().find('.content').each(function(){
						$(this).animate({height:(maxInnerHeight + 5 + buttonHeight) + 'px'},fadeSpeed);
					});
				};
				$elem.parent().fadeOut(fadeSpeed,function(){
					$(this).appendTo($('#questions'));
					$next.appendTo($('#wrapper')).fadeIn(fadeSpeed);
				});
			};
			question++;
			$('#id h3').text(question + '.');
			return $elem.parent().attr('id');
		};/**/
		
		function changeColor($ele){
			if($ele.hasClass('answer')){
				$('#wrapper').animate({ backgroundColor:'#ff9' }, fadeSpeed)
			}else{
				$('#wrapper').animate({ backgroundColor:'#9cf' }, fadeSpeed)
			};
		};/**/
				
		function setup(){
			var html = [
				'<div id="hse">',
				'<div id="top" class="clrNM">',
				'<div id="wrapper" class="clrNM"></div>',
				'</div>',
				'<div id="bottom"></div>',
				'<div id="questions">',
				'<div id="start" class="wizard clrNM start"><div class="content"><h3>Start your online guide</h3><p>Complete our LPG pipework guide for advice on what to do to ensure your pipework is safe.</p></div><p class="startButton">&nbsp;<span>Q1</span></p></div>',
				'<div id="Q1" class="question qa clrNM"><div class="content"><h3>Do you have an LPG Vapour offtake installation?</h3></div><p class="yes"><strong>Yes</strong><span>Q2</span></p><p class="no"><strong>No</strong><span>A1</span></p></div>',
				'<div id="Q2" class="question qa clrNM"><div class="content"><h3>Have you completed the HSE/UKLPG questionnaire sent to you by Calor during 2009?</h3></div><p class="prev"><strong>Back</strong></p><p class="yes"><strong>Yes</strong><span>Q3</span></p><p class="no"><strong>No</strong><span>A2</span></p></div>',
				'<div id="Q3" class="question qa clrNM"><div class="content"><h3>Is part or the whole of your underground service pipework metallic?</h3></div><p class="prev"><strong>Back</strong></p><p class="yes"><strong>Yes</strong><span>Q5</span></p><p class="no"><strong>No</strong><span>Q4</span></p></div>',
				'<div id="Q4" class="question qa clrNM"><div class="content"><h3>Recommended action...</h3><p>Polyethylene (PE) pipework does not corrode, but can be damaged by impact or sharp objects etc. If you smell LPG contact our emergency line 0845 7 444 999.</p><p>&nbsp;</p><h3>Are any parts exposed to sunlight?</h3></div><p class="prev"><strong>Back</strong></p><p class="yes"><strong>Yes</strong><span>A3</span></p><p class="no"><strong>No</strong><span>A4</span></p></div>',
				'<div id="Q5" class="question qa clrNM"><div class="content"><h3>Have you been informed that your risk rating is in the top 10% of risk?</h3></div><p class="prev"><strong>Back</strong></p><p class="yes"><strong>Yes</strong><span>A6</span></p><p class="no"><strong>No</strong><span>A5</span></p></div>',
				'<div id="answers">',
				'<div id="A1" class="answer qa clrNM"><div class="content"><h3>Recommended action...</h3><p>Liquid installations such as Autogas and FLT installations are not part of the current HSE/UKLPG campaign but the pipework both above and underground should be subject to regular <a href="http://www.calor.co.uk/customer-services/lpg-safety/lpg-pipework/inspection-strategy/">inspections</a> and maintenance.</p></div><p class="prev"><strong>Back</strong></p></div>',
				'<div id="A2" class="answer qa clrNM"><div class="content"><h3>Recommended action...</h3><p>Go to <a href="http://www.hse.gov.uk/lpg" target="_blank">http://www.hse.gov.uk/lpg</a> and complete the questionnaire online.</p><p>You will need your unique Calor delivery point (dp) number to complete the survey. This can be found on your statement.</p></div><p class="prev"><strong>Back</strong></p></div>',
				'<div id="A3" class="answer qa clrNM"><div class="content"><h3>Recommended action...</h3><p>There is a potential for the degradation of pipework. You will need to</p><ol><li>Protect the pipework from UV (sunlight) immediately.</li><li>If the pipework has been exposed to UV (sunlight) consider replacing the section that has been exposed, if you are at all unsure that no damage has been caused to the pipework.</li><li>Develop / modify <a href="http://www.calor.co.uk/customer-services/lpg-safety/lpg-pipework/inspection-strategy/">inspection strategy</a> accordingly with the person who is competent to do the work.</li></ol></div><p class="prev"><strong>Back</strong></p></div>',
				'<div id="A4" class="answer qa clrNM"><div class="content"><h3>Recommended action...</h3><p>There is no immediate issue but you will need to develop / modify <a href="http://www.calor.co.uk/customer-services/lpg-safety/lpg-pipework/inspection-strategy/">inspection strategy</a> accordingly with the person who is competent to do the work.</p></div><p class="prev"><strong>Back</strong></p></div>',
				'<div id="A5" class="answer qa clrNM"><div class="content"><h3>Recommended action...</h3><p>Whilst your underground pipework does not need immediate replacement replacement is should be subject to an <a href="http://www.calor.co.uk/customer-services/lpg-safety/lpg-pipework/inspection-strategy/">inspection</a> and maintenance strategy developed by a competent person. Please be aware that your underground pipework if metallic will need replacing by 2015. Please contact CalorForce on 0845 605 4500 or <a href="mailto:enquiries@calorforce.com">enquiries@calorforce.com</a>.</p></div><p class="prev"><strong>Back</strong></p></div>',
				'<div id="A6" class="answer qa clrNM"><div class="content"><h3>Recommended action...</h3><p>Your metallic underground service pipework should be replaced as soon as possible with polyethylene (PE) by a suitably qualified LPG Gas Safe Engineer. Please contact CalorForce on 0845 605 4500 or <a href="mailto:enquiries@calorforce.com">enquiries@calorforce.com</a>.</p></div><p class="prev"><strong>Back</strong></p></div>',				'</div>',
				'</div>',
				'</div>'
			];
			$('#widget').css('display','block').append(html.join(''));
		};/**/

	};/**/
