/* Poke London */

// overall maximum value
maxValue = 350;

// offset for space to left of value 1
sliderOffset = 26;

/*	variable needed to store if buildTariffRecommendations has been called in the current instance of slider moving.
	Needed to overcome a bug in slider that rounds down uneven handle position when there are more pixels than steps which confuses the show/hide of right panels */
newSliding = 1;

// stopped flag
sliderStopped = 0;

// which slider is active
sliderActive = 0;

// array of maximum slider values
sliderMax = new Array();
sliderMax[0] = parseInt(maxValue) + parseInt(sliderOffset);
sliderMax[1] = 0;
sliderMax[2] = 0;

// array of slider start values
sliderStart = new Array();
sliderStart[0] = parseInt(sliderOffset) + parseInt(1);
sliderStart[1] = 0;
sliderStart[2] = 0;

// array of current slider values
sliderValue = new Array();
sliderValue[0] = 0;
sliderValue[1] = 0;
sliderValue[2] = 0;

// array of slider help text
sliderHelp = [
	{
		"disabled" : "",
		"help"	: "Drag the slider or use the text box on the right to show the number of phones",
		"max"	: "You have reached the maximum value of NUMBER for this slider"
	},
	{
		"disabled" : "",
		"help"	: "Drag the slider or use the text box on the right to show the number of employees",
		"max"	: "You have reached the maximum value of NUMBER for this slider"
	},
	{
		"disabled" : "",
		"help"	: "Drag the slider or use the text box on the right to show the number of employees",
		"max"	: "You have reached the maximum value of NUMBER for this slider"
	}
];

// variables to store mouse position
mouseX = 0;
mouseY = 0;

// array of slider opacity states
opacitySlider = new Array();
opacitySlider[0] = "showing";
opacitySlider[1] = "showing";
opacitySlider[2] = "showing";

// array of box opacity states
opacityBox = new Array();
opacityBox[0] = "showing";
opacityBox[1] = "showing";
opacityBox[2] = "showing";
opacityBox[3] = "showing";

// variable to store if radios are showing
radiosShowing = 0;

// array of tariffs names
tariffs = [
	{
		"name" : "Business Solo",
		"link"	: "http://www.business.orange.co.uk/plans/solo/"
	},
	{
		"name" : "Business Sense",
		"link"	: "http://www.business.orange.co.uk/servlet/Satellite?c=OUKPage&pagename=Business&cid=1232365398212"
	},
	{
		"name" : "Business Infinity<br />(unlimited calls)",
		"link"	: "http://www.business.orange.co.uk/servlet/Satellite?c=OUKPage&pagename=Business&cid=1218449603476"
	},
	{
		"name" : "Business SIM Only",
		"link"	: "http://www.business.orange.co.uk/servlet/Satellite?c=OUKPage&pagename=Business&cid=1216053340630"
	}
];

// array of slider questions
questions = [
	{
		"question":	{
			"0":	"How many phones does your business need?",
			"1":	"",
			"2":	"",
			"3":	"",
			"4":	""
		}
	},
	{
		"question":	{
			"0":	"How many (if any) of your employees use over 1,500 minutes a month?",
			"1":	"How many (if any) of your NUMBER employees use over 1500 minutes a month?",
			"2":	"",
			"4":	"",
			"4":	"Do you use over 1500 minutes a month?"
		}
	},
	{
		"question":	{
			"0":	"How many (if any) of your employees do you want to put on an 18-month plus contract?",
			"1":	"How many (if any) of your NUMBER employees would you want to put on an 18-month plus contract?",
			"2":	"How many (if any) of your remaining NUMBER employees would you want to put on an 18-month plus contract?",
			"3":	"How many (if any) of your remaining NUMBER employee would you want to put on an 18-month plus contract?",
			"4":	"Do you want to be on an 18-month plus contract?"
		}
	}
];

// cookie options
 cookieOptions = {
	hoursToLive: 720
}

// cookie name
cookieName = 'OBTWState';

//cookie set already overide
cookieOveride = 0;

/* Define the base ORANGE namespace */
ORANGE = {
};

/* Define the Orange Business Tariff Widget */
ORANGE.OBTW = {

	// reference overall maximum value
	maxValue: maxValue,

	// reference offset for space to left of value 1
	sliderOffset: sliderOffset,

	// reference bug fixing flag (see explanation above)
	newSliding: newSliding,

	// reference stopped flag
	sliderStopped: sliderStopped,

	// reference active slider
	sliderActive: sliderActive,

	// reference array of maximum slider values
	sliderMax: sliderMax,

	// reference array of slider start values
	sliderStart: sliderStart,

	// reference array of current slider values
	sliderValue: sliderValue,

	// reference array of slider help text
	sliderHelp: sliderHelp,

	// reference to mouse position variables
	mouseX: mouseX,
	mouseY: mouseY,

	// reference array of slider opacity states
	opacitySlider: opacitySlider,

	// reference array of box opacity states
	opacityBox: opacityBox,

	// reference variable to store if radios are showing
	radiosShowing: radiosShowing,

	// reference array of tariffs names
	tariffs: tariffs,

	// reference array of slider questions
	questions: questions,

	// reference cookie name
	cookieName: cookieName,
	
	// reference cookie options
	cookieOptions: cookieOptions,

	// reference cookie options
	cookieOveride: cookieOveride,
	
	//initialise the active elements on the page
	initialisePage: function() {
		ORANGE.OBTW.getSliderCookie();
		ORANGE.OBTW.trackMouse();
		ORANGE.OBTW.trackKeyPress();

		for(var v = 0; v < sliderValue.length; v++) {
			ORANGE.OBTW.sliderTooltipOn(v);
			$('#slider_' + v + ' input').val(ORANGE.OBTW.sliderToInput(sliderValue[v]));
			ORANGE.OBTW.buildSlider(v);
			ORANGE.OBTW.applyInputBinding(v);
		}
		ORANGE.OBTW.applyRadioBinding(1);
		ORANGE.OBTW.applyRadioBinding(2);
		for(var v = 0; v < tariffs.length; v++) {
			ORANGE.OBTW.boxFadeOut(v);
			ORANGE.OBTW.applyBoxBinding(v);
		}
		ORANGE.OBTW.buildTariffRecommendations();
		ORANGE.OBTW.showSliderQuestion();
		$('.obtWrapper .ui-slider-value input').css('background-color','transparent');
	},

	//build a slider
	buildSlider: function(sliderID) {
		if (sliderValue[0] == 0) {
			ORANGE.OBTW.activateSlider(1);
			ORANGE.OBTW.activateSlider(2);
		} else if (sliderValue[0] == parseInt(sliderOffset) + parseInt(1)) {
			ORANGE.OBTW.activateRadio(1);
			ORANGE.OBTW.activateRadio(2);
		} else if (sliderValue[0] > parseInt(sliderOffset) + parseInt(1)) {
			if (sliderMax[1] >= parseInt(sliderOffset) + parseInt(1)) {
				ORANGE.OBTW.activateSlider(1);
			}
			//if (sliderMax[2] >= parseInt(sliderOffset) + parseInt(1)) {
				ORANGE.OBTW.activateSlider(2);
			//}
		}
		
		$('#slider_' + sliderID + ' .ui-slider').slider({
			animate:	false,
			range:		true,
			min:		0,
			max:		parseInt(maxValue) + parseInt(sliderOffset),
			steps: 		parseInt(maxValue) + parseInt(sliderOffset),
			handle:		'.ui-slider-handle',
			handles:	[{start:0, min:0, max:0},{start:sliderValue[sliderID], min:sliderStart[sliderID], max:sliderMax[sliderID]}],
			change: function(e, ui) { ORANGE.OBTW.sliderChange(ui, sliderID) },
			start: function(e, ui) { ORANGE.OBTW.sliderStart(ui, sliderID) },
			slide: function(e, ui) { ORANGE.OBTW.sliderSlide(ui, sliderID) },
			stop: function(e, ui) { ORANGE.OBTW.sliderStop(ui, sliderID) }
		});
		if (sliderValue[0] == 0) {
			ORANGE.OBTW.sliderFadeOut(1);
			ORANGE.OBTW.sliderFadeOut(2);
		} else if (sliderValue[0] > 0) {
			if (sliderMax[sliderID] == 0) {
				//ORANGE.OBTW.sliderFadeComplete(sliderID);
				ORANGE.OBTW.sliderFadeOut(sliderID);
			}
		} else {
			ORANGE.OBTW.sliderFadeIn(1);
			ORANGE.OBTW.sliderFadeIn(2);
		}
		if (sliderID == sliderActive) {
			ORANGE.OBTW.sliderFocus(sliderID);
		}
	},

	//rebuild a slider with a new maximum value
	rebuildSlider: function(sliderID) {
		if ((sliderID == 2) && (sliderValue[2] == parseInt(sliderOffset) + parseInt(1)) && (sliderValue[0] == parseInt(sliderOffset) + parseInt(1)) && (sliderValue[1] == 0)) {
			sliderValue[2] = sliderValue[0];
		} else {
			sliderValue[sliderID] = sliderStart[sliderID];
		}
		$('#slider_' + sliderID + ' input').val(ORANGE.OBTW.sliderToInput(sliderValue[sliderID]));
		$('#slider_' + sliderID + ' .ui-slider').slider('destroy');
		$('#slider_' + sliderID + ' .ui-slider-container div:first').attr('class','ui-slider');
		$('#slider_' + sliderID + ' .ui-slider-range').remove();
		ORANGE.OBTW.buildSlider(sliderID);
	},

	// what happens when a slider starts
	sliderStart: function(ui, sliderID) {
		sliderStopped = 0;
		ORANGE.OBTW.sliderTooltipOff(sliderID);
	
		// what happens when slider 0 starts
		if (sliderID == 0) {
			if (ui.value > 0) {
				//ORANGE.OBTW.sliderFadeOut(1);
				//ORANGE.OBTW.sliderFadeOut(2);
			}
		}

		// what happens when slider 1 starts
		if (sliderID == 1) {
			//ORANGE.OBTW.sliderFadeOut(2);
		}
		newSliding = 1;
	},
	
	// what happens when a slider stops
	sliderStop: function(ui, sliderID) {
		sliderStopped = 1;
		sliderActive = sliderID;
		ORANGE.OBTW.hideMaxMessage(sliderID);
		ORANGE.OBTW.sliderFocus(sliderID);
		ORANGE.OBTW.storeValueChanges(ui, sliderID);

		// what happens when slider 0 slides
		if (sliderID == 0) {
			if (sliderValue[0] > sliderOffset) {
				ORANGE.OBTW.sliderFadeIn(1);
				ORANGE.OBTW.sliderFadeIn(2);
			} else {
				//ORANGE.OBTW.sliderFadeOut(1);
				//ORANGE.OBTW.sliderFadeOut(2);
			}
			sliderMax[1] = ORANGE.OBTW.getSliderMax(sliderValue[0]);
			sliderMax[2] = ORANGE.OBTW.getSliderMax(sliderValue[0]);
			ORANGE.OBTW.rebuildSlider(1);
			ORANGE.OBTW.rebuildSlider(2);
		}
		// what happens when slider 1 stops
		if (sliderID == 1) {
			sliderMax[2] = ORANGE.OBTW.getSliderMax(ORANGE.OBTW.sliderToInput(sliderValue[0]) - ORANGE.OBTW.sliderToInput(sliderValue[1]) + parseInt(sliderOffset));
			ORANGE.OBTW.rebuildSlider(2);
			if (sliderMax[2] > sliderOffset) {
				ORANGE.OBTW.sliderFadeIn(2);
			}
		}
		
		if (sliderValue[sliderID] <= sliderOffset) {
			ORANGE.OBTW.rebuildSlider(sliderID);
		}

		if (newSliding == 0) {
			ORANGE.OBTW.buildTariffRecommendations();
			ORANGE.OBTW.showSliderQuestion();
		} else {
			newSliding = 0;
		}
		ORANGE.OBTW.setSliderCookie();
	},
	
	// what happens when a slider slides
	sliderSlide: function(ui, sliderID) {
		ORANGE.OBTW.storeValueChanges(ui, sliderID);
		// what happens when slider 0 slides
		if (sliderID == 0) {
			sliderMax[1] = ORANGE.OBTW.getSliderMax(sliderValue[0]);
			sliderMax[2] = ORANGE.OBTW.getSliderMax(sliderValue[0]);
			sliderValue[1] = 0;
			sliderValue[2] = 0;
		}

		// what happens when slider 1 stops
		if (sliderID == 1) {
			sliderMax[2] = ORANGE.OBTW.getSliderMax(ORANGE.OBTW.sliderToInput(sliderValue[0]) - ORANGE.OBTW.sliderToInput(sliderValue[1]) + parseInt(sliderOffset));
		}

		if ((sliderValue[sliderID] == 0) || (sliderValue[sliderID] < sliderMax[sliderID]) || (sliderStopped == 1)) {
			ORANGE.OBTW.hideMaxMessage(sliderID);
		} else {
			ORANGE.OBTW.showMaxMessage(sliderID);
		}

		if (sliderStopped == 1) {
			ORANGE.OBTW.setSliderCookie();
		}
		if (newSliding == 0) {
			ORANGE.OBTW.buildTariffRecommendations();
			ORANGE.OBTW.showSliderQuestion();
		} else {
			newSliding = 0;
		}
	},

	// what happens when a slider changes
	sliderChange: function(ui, sliderID) {
		if (sliderID != 2) {
			if ((sliderValue[0] == parseInt(sliderOffset) + parseInt(1)) && (sliderValue[1] == 0) && (cookieOveride == 0)) {
				$('#slider_2_Value_Yes').trigger('click');
			}
		}
	},
	
	// stores slider and field value changes
	storeValueChanges: function(ui, sliderID) {
		sliderValue[sliderID] = ui.value;
		if ((sliderOffset > 0) && (sliderValue[sliderID] == 1) && (sliderStopped == 1)) {
			sliderValue[sliderID] = parseInt(sliderOffset) + parseInt(1);
			$('#slider_' + sliderID  + ' .ui-slider').slider('moveTo', sliderValue[sliderID], 1);
		}
		$('#slider_' + sliderID + ' input').val(ORANGE.OBTW.sliderToInput(sliderValue[sliderID]));
	},

	// bind input field changes
	applyInputBinding: function(sliderID) {
		$('#slider_' + sliderID  + ' input').bind('change', function() {
			ORANGE.OBTW.updateSlider(sliderID);
		});
		$('#slider_' + sliderID  + ' input').bind('focus', function() {
			sliderActive = sliderID;
		});
	},

	// update the slider when input changes
	updateSlider: function(sliderID) {
		validatedInput = ORANGE.OBTW.validateFieldInput(sliderID);
		$('#slider_' + sliderID  + ' input').val(validatedInput);
		sliderValue[sliderID] = ORANGE.OBTW.inputToSlider(validatedInput);
		$('#slider_' + sliderID  + ' .ui-slider').slider('moveTo', sliderValue[sliderID], 1);
	},

	// bind box mouseover and mouseout
	applyBoxBinding: function(sliderID) {
		$('#box_' + sliderID).bind('mouseover', function() {
			$(this).addClass('rightBoxOpen');
		});
		$('#box_' + sliderID).bind('mouseleave', function() {
			$(this).removeClass('rightBoxOpen');
		});
	},
	
	// validate field inputs
	validateFieldInput: function(sliderID) {
		inputToValidate = $('#slider_' + sliderID  + ' input').val();
		if (isNaN(inputToValidate)) {
			inputToValidate = 0;
		}
		if (inputToValidate > ORANGE.OBTW.sliderToInput(sliderMax[sliderID])) {
			inputToValidate = ORANGE.OBTW.sliderToInput(sliderMax[sliderID]);
		}
		return inputToValidate;
	},
	
	// get slider maximum
	getSliderMax: function(sliderValueTemp) {
		if (sliderValueTemp <= sliderOffset) {
			sliderValueTemp = 0;
		}
		return sliderValueTemp;
	},
	
	// converts slider value to input value
	sliderToInput: function(sliderValueTemp) {
		inputValueTemp = sliderValueTemp - sliderOffset;
		if (inputValueTemp < 0) {
			inputValueTemp = 0;
		}
		return inputValueTemp;
		/*return sliderValueTemp;*/
	},

	// converts input value to slider value
	inputToSlider: function(inputValueTemp) {
		sliderValueTemp = inputValueTemp;
		if (inputValueTemp > 0) {
			sliderValueTemp = parseInt(inputValueTemp) + parseInt(sliderOffset);
		}
		return sliderValueTemp;
		/*return inputValueTemp;*/
	},

	// handles slider fading in
	sliderFadeIn: function(sliderID) {
		if (opacitySlider[sliderID] != 'showing') {
			if (opacitySlider[sliderID] == 'complete') {
				ORANGE.OBTW.fadeOpacity('slider_' + sliderID, 0, 100, 200, 10);
			}
			if (opacitySlider[sliderID] == 'hiding') {
				ORANGE.OBTW.fadeOpacity('slider_' + sliderID, 40, 100, 200, 10);
			}			
			$('#slider_' + sliderID + ' .ui-slider-value input').removeAttr('readonly');
			$('#slider_' + sliderID + ' .ui-radio input').removeAttr('disabled');
			ORANGE.OBTW.sliderTooltipOn(sliderID);
			opacitySlider[sliderID] = 'showing';
		}
	},

	// handles slider fading out
	sliderFadeOut: function(sliderID) {		
		if (opacitySlider[sliderID] != 'hiding') {
			if (opacitySlider[sliderID] == 'showing') {
				ORANGE.OBTW.fadeOpacity('slider_' + sliderID, 100, 40, 200, 10);
			}			
			if (opacitySlider[sliderID] == 'complete') {
				ORANGE.OBTW.fadeOpacity('slider_' + sliderID, 0, 40, 200, 10);
			}						
			$('#slider_' + sliderID + ' .ui-slider-value input').attr('readonly',true);
			$('#slider_' + sliderID + ' .ui-radio input').attr('disabled',true);
			ORANGE.OBTW.sliderTooltipOff(sliderID);
			opacitySlider[sliderID] = 'hiding';
		}
	},

	// handles slider complete fading out
	sliderFadeComplete: function(sliderID) {		
		if (opacitySlider[sliderID] != 'complete') {
			if (opacitySlider[sliderID] == 'showing') {
				ORANGE.OBTW.fadeOpacity('slider_' + sliderID, 100, 0, 200, 10);
			}			
			if (opacitySlider[sliderID] == 'hiding') {
				ORANGE.OBTW.fadeOpacity('slider_' + sliderID, 40, 0, 200, 10);
			}						
			$('#slider_' + sliderID + ' .ui-slider-value input').attr('readonly',true);
			$('#slider_' + sliderID + ' .ui-radio input').attr('disabled',true);
			ORANGE.OBTW.sliderTooltipOff(sliderID);
			opacitySlider[sliderID] = 'complete';
		}
	},

	// switches tooltip on
	sliderTooltipOn: function(sliderID) {
		ORANGE.OBTW.actionTooltip(sliderID, 750, sliderHelp[sliderID]['help']);
	},

	// switches tooltip off
	sliderTooltipOff: function(sliderID) {		
		ORANGE.OBTW.actionTooltip(sliderID, 0, sliderHelp[sliderID]['disabled']);
	},

	// shows max tooltip
	showMaxMessage: function(sliderID) {
		$('#tooltip').css("display","none");
		ORANGE.OBTW.sliderTooltipOff(sliderID);
		$('#maxMessage').html("<h3>" + sliderHelp[sliderID]['max'].replace('NUMBER',ORANGE.OBTW.sliderToInput(sliderMax[sliderID])) + "</h3>");
		maxMessageX = parseInt(mouseX) + parseInt(15);
		maxMessageY = parseInt(mouseY) + parseInt(15);
		if ((parseInt(maxMessageY) + parseInt(20)) > $(window).height()) {
			maxMessageY = parseInt(mouseY) - 42;
		}
		$('#maxMessage').css({"display":"block","top":maxMessageY,"left":maxMessageX});
	},

	// hides max tooltip
	hideMaxMessage: function(sliderID) {
		$('#maxMessage').html("");
		$('#maxMessage').css("display","none");
		ORANGE.OBTW.actionTooltip(sliderID, 750, sliderHelp[sliderID]['help']);
	},

	// actions tooltip change
	actionTooltip: function(sliderID, tooltipDelay, tooltipText) {
		$('#slider_' + sliderID + ' .ui-slider').attr('title',tooltipText);
		$('#slider_' + sliderID + ' .ui-slider').tooltip({delay:tooltipDelay,track:true,extraClass:"tooltip"});
	},
	
	// bring focus to slider
	sliderFocus: function(sliderID) {
		if (sliderActive == sliderID) {
			if (navigator.appName.indexOf("Microsoft") == 0) {
				$('#slider_' + sliderID + ' input').focus();
			}
			$('#slider_' + sliderID + ' .ui-slider-handle:nth(1)').focus();
		}
	},

	// handles box fading in
	boxFadeIn: function(boxID) {
		if (opacityBox[boxID] == 'hiding') {
			ORANGE.OBTW.fadeOpacity('box_' + boxID, 40, 100, 200, 10);
			/*$('#box_' + boxID).addClass('rightBoxOpen');*/
			/*$('#box_' + boxID).removeClass('rightBoxFaded');*/
			opacityBox[boxID] = 'showing';
			//$('#box_' + boxID + ' .rightBoxIntro a').attr('href',tariffs[boxID]['link']);
		}
	},

	// handles slider fading out
	boxFadeOut: function(boxID) {		
		if (opacityBox[boxID] == 'showing') {
			ORANGE.OBTW.fadeOpacity('box_' + boxID, 100, 40, 200, 10);
			/*$('#box_' + boxID).removeClass('rightBoxOpen');*/
			/*$('#box_' + boxID).addClass('rightBoxFaded');*/
			opacityBox[boxID] = 'hiding';
			$('#box_' + boxID + ' a').attr('href',tariffs[boxID]['link']);
			//$('#box_' + boxID + ' .rightBoxIntro a').attr('href','javascript: void(0);');
		}
	},

	// track the mouse
	trackMouse: function() {
		$().mousemove(function(e){
			mouseX = e.pageX;
			mouseY = e.pageY;
		});
	},

	// catch enter keypress
	trackKeyPress:function() {
		$().keypress(function(e){
			if (e.keyCode=='13') {
				$('#slider_' + sliderActive + ' input').trigger('change');
				return false;
			}
		});
	},

	// builds recommended tariff text and shows/hides tariff boxes
	buildTariffRecommendations: function() {
		value0 = ORANGE.OBTW.sliderToInput(sliderValue[0])
		value1 = ORANGE.OBTW.sliderToInput(sliderValue[1])
		value2 = ORANGE.OBTW.sliderToInput(sliderValue[2])
		if (value0 > 20) {
			$('#telephone1To20').removeClass('telephone-inactive');
			$('#telephoneOver20').addClass('telephone-inactive');
		} else if (value0 > 0) {
			$('#telephoneOver20').removeClass('telephone-inactive');
			$('#telephone1To20').addClass('telephone-inactive');
		} else {
			$('#telephoneOver20').addClass('telephone-inactive');
			$('#telephone1To20').addClass('telephone-inactive');
		}
		
		if ((value0 == 0) && (value1 == 0) && (value2 == 0)) {
			tariffRecommendations = "<p>Just use the sliders to answer the questions, and we'll find the right plan for your business.</p>";
		} else {
			tariffRecommendations = "<ul>";
			if ((value1 == 0) && (value2 == 0)) {
				tariffRecommendations = tariffRecommendations + "<li><a href='" + tariffs[3]['link'] + "'>" + value0 + " x " + tariffs[3]['name'] + "</a></li>";
			} else if ((value0 == 1) && (value2 == 1)) {
				tariffRecommendations = tariffRecommendations + "<li><a href='" + tariffs[0]['link'] + "'>" + value0 + " x " + tariffs[0]['name'] + "</a></li>";
			} else {
				if (value1 >= 1) {
					tariffRecommendations = tariffRecommendations + "<li><a href='" + tariffs[2]['link'] + "'>" + value1 + " x " + tariffs[2]['name'] + "</a></li>";
				}
				if (value2 >= 1) {
					tariffRecommendations = tariffRecommendations + "<li><a href='" + tariffs[1]['link'] + "'>" + value2 + " x " + tariffs[1]['name'] + "</a></li>";
				}
				if ((parseInt(value1) + parseInt(value2)) < value0) {
					tariffRecommendations = tariffRecommendations + "<li><a href='" + tariffs[3]['link'] + "'>" + (value0 - value1 - value2) + " x " + tariffs[3]['name'] + "</a></li>";
				}
			}
			tariffRecommendations = tariffRecommendations + "</ul>";
		}

		if (value0 == 0) {
			//ORANGE.OBTW.boxFadeOut(0);
			//ORANGE.OBTW.boxFadeOut(1);
			//ORANGE.OBTW.boxFadeOut(2);
			//ORANGE.OBTW.boxFadeOut(3);
			ORANGE.OBTW.boxFadeIn(0);
			ORANGE.OBTW.boxFadeIn(1);
			ORANGE.OBTW.boxFadeIn(2);
			ORANGE.OBTW.boxFadeIn(3);
		} else if (value0 == 1) {
			ORANGE.OBTW.boxFadeOut(1);
			if ((value1 == 0) && (value2 == 0)) {
				ORANGE.OBTW.boxFadeOut(0);
				ORANGE.OBTW.boxFadeOut(2);
				ORANGE.OBTW.boxFadeIn(3);
			}
			if (value1 == 1) {
				ORANGE.OBTW.boxFadeOut(0);
				ORANGE.OBTW.boxFadeIn(2);
				ORANGE.OBTW.boxFadeOut(3);
			}
			if (value2 == 1) {
				ORANGE.OBTW.boxFadeIn(0);
				ORANGE.OBTW.boxFadeOut(2);
				ORANGE.OBTW.boxFadeOut(3);
			}
		} else {
			ORANGE.OBTW.boxFadeOut(0);
			if (value1 > 0) {
				ORANGE.OBTW.boxFadeIn(2);
			} else {
				ORANGE.OBTW.boxFadeOut(2);
			}
			if (value2 > 0) {
				ORANGE.OBTW.boxFadeIn(1);
			} else {
				ORANGE.OBTW.boxFadeOut(1);
			}
			if (parseInt(value1) + parseInt(value2) < value0) {
				ORANGE.OBTW.boxFadeIn(3);
			} else {
				ORANGE.OBTW.boxFadeOut(3);
			}
		}	
		$('#slider_results .leftPanel').html(tariffRecommendations);
	  $("#slider_results .leftPanel a").click(register_slider_result_tracking);
	},

	// show slider question
	showSliderQuestion: function() {
		$('#slider_0 p').html(questions[0]['question'][0]);
		if (sliderValue[0] <= sliderOffset) {
			$('#slider_1 p').html(questions[1]['question'][0]);
			$('#slider_2 p').html(questions[2]['question'][0]);
			ORANGE.OBTW.hideRadios();
		} else {
			if(sliderValue[0] == parseInt(sliderOffset) + parseInt(1)) {
				$('#slider_1 p').html(questions[1]['question'][4]);
				$('#slider_2 p').html(questions[2]['question'][4]);
				ORANGE.OBTW.showRadios();
			} else {
				ORANGE.OBTW.hideRadios();
				$('#slider_1 p').html(questions[1]['question'][1].replace('NUMBER',ORANGE.OBTW.sliderToInput(sliderMax[1])));
				if (sliderValue[1] <= sliderOffset) {
					$('#slider_2 p').html(questions[2]['question'][1].replace('NUMBER',ORANGE.OBTW.sliderToInput(sliderMax[2])));
				} else if ((sliderMax[2] > sliderOffset) && (sliderValue[1] > sliderOffset)) {
					if (ORANGE.OBTW.sliderToInput(sliderMax[2]) == 1) {
						$('#slider_2 p').html(questions[2]['question'][3].replace('NUMBER',ORANGE.OBTW.sliderToInput(sliderMax[2])));
					} else {
						$('#slider_2 p').html(questions[2]['question'][2].replace('NUMBER',ORANGE.OBTW.sliderToInput(sliderMax[2])));
					}
				} else {
					$('#slider_2 p').html(questions[2]['question'][0]);
				}
			}
		}
	},

	// show radios instead of slider
	showRadios: function() {
		if (radiosShowing == 0) {
			for(var v = 1; v < sliderValue.length; v++) {
				ORANGE.OBTW.activateRadio(v);
				if(sliderValue[1] == parseInt(sliderOffset) + parseInt(1)) {
					sliderValue[2] = 0;
				}
				if (sliderValue[v] > 0) {
					$('#slider_' + v + '_Value_Yes').attr('checked','checked');
					$('#slider_' + v + '_Value_No').removeAttr('checked');
				} else {
					$('#slider_' + v + '_Value_No').attr('checked','checked');
					$('#slider_' + v + '_Value_Yes').removeAttr('checked');
				}
			}
			radiosShowing = 1;
		}
	},

	// bind box mouseover and mouseout
	applyRadioBinding: function(sliderID) {
		$('#slider_' + sliderID + '_Value_Yes').bind('click', function() {
			sliderValue[sliderID] = parseInt(sliderOffset) + parseInt(1);
			$('#slider_' + sliderID  + ' .ui-slider').slider('moveTo', sliderValue[sliderID], 1);
			if (sliderID == 1) {
				$('#slider_2_Value_Yes').removeAttr('checked');
				$('#slider_2_Value_No').attr('checked','checked');
			}
		});
		$('#slider_' + sliderID + '_Value_No').bind('click', function() {
			sliderValue[sliderID] = 0;
			$('#slider_' + sliderID  + ' .ui-slider').slider('moveTo', sliderValue[sliderID], 1);
		});
	},

	// show sliders instead of radios
	hideRadios: function() {
		if (radiosShowing == 1) {
			for(var v = 1; v < sliderValue.length; v++) {
				ORANGE.OBTW.activateSlider(v)
			}
			radiosShowing = 0;
		}
	},

	// activates radio classes
	activateRadio: function(sliderID) {
		$('#slider_' + sliderID).removeClass('ui-radio-inactive');
		$('#slider_' + sliderID).removeClass('ui-slider-active');
		$('#slider_' + sliderID).addClass('ui-radio-active');
		$('#slider_' + sliderID).addClass('ui-slider-inactive');
	},

	// activates slider classes
	activateSlider: function(sliderID) {
		$('#slider_' + sliderID).removeClass('ui-radio-active');
		$('#slider_' + sliderID).removeClass('ui-slider-inactive');
		$('#slider_' + sliderID).addClass('ui-slider-active');
		$('#slider_' + sliderID).addClass('ui-radio-inactive');
	},
	
	// get slider cookie
	getSliderCookie: function() {
		cookieValueIn = jaaulde.utils.cookies.get(cookieName)
		if(cookieValueIn) {
			cookieOveride = 1;
			cookieArray = cookieValueIn.split(':');
			for(var v = 0; v < sliderValue.length; v++) {
				sliderArray = cookieArray[v].split(',');
				if (isNaN(sliderArray[0])) {
					sliderValue[v] = 0;
				} else {
					sliderValue[v] = sliderArray[0];
					if (sliderValue[v] <= sliderOffset) {
						sliderValue[v] = 0;
					}
				}
				if (!isNaN(sliderArray[1])) {
					if (v > 0) {
						sliderMax[v] = sliderArray[1];
					}
				}
			}
		}
	},

	// set slider cookie
	setSliderCookie: function() {
		cookieOveride = 0;
		cookieValueOut = sliderValue[0] + ',' + sliderMax[0] + ':' + sliderValue[1] + ',' + sliderMax[1] + ':' + sliderValue[2] + ',' + sliderMax[2];
		jaaulde.utils.cookies.set(cookieName, cookieValueOut);
		jaaulde.utils.cookies.setOptions(cookieOptions);
	},

	// jQuery animate does not work in IE so using this from http://www.akxl.net/labs/articles/use-javascript-to-change-or-fade-opacity-or-transparency-of-html-entities/
	setOpacity: function(elem, opacityAsInt) {
		var opacityAsDecimal = opacityAsInt;
		if (opacityAsInt > 100)
			opacityAsInt = opacityAsDecimal = 100; 
		else if (opacityAsInt < 0)
			opacityAsInt = opacityAsDecimal = 0;
		opacityAsDecimal /= 100;
		if (opacityAsInt < 1)
			opacityAsInt = 1; // IE7 bug, text smoothing cuts out if 0
		elem.style.opacity = opacityAsDecimal;
		elem.style.filter  = "alpha(opacity=" + opacityAsInt + ")";
	},

	// jQuery animate does not work in IE so using this from http://www.akxl.net/labs/articles/use-javascript-to-change-or-fade-opacity-or-transparency-of-html-entities/
	fadeOpacity: function(elemId, fromOpacity, toOpacity, time, fps) {
		var steps = Math.ceil(fps * (time / 1000));
		var delta = (toOpacity - fromOpacity) / steps;
		ORANGE.OBTW.fadeOpacityStep(elemId, 0, steps, fromOpacity, delta, (time / steps));
	},
	
	// jQuery animate does not work in IE so using this from http://www.akxl.net/labs/articles/use-javascript-to-change-or-fade-opacity-or-transparency-of-html-entities/
	fadeOpacityStep: function(elemId, stepNum, steps, fromOpacity, delta, timePerStep) {
		ORANGE.OBTW.setOpacity(document.getElementById(elemId), Math.round(parseInt(fromOpacity) + (delta * stepNum)));
		if (stepNum < steps)
			setTimeout("ORANGE.OBTW.fadeOpacityStep('" + elemId + "', " + (stepNum+1) + ", " + steps + ", " + fromOpacity + ", " + delta + ", " + timePerStep + ");", timePerStep);
	}


};

// initialise the widget on document load
$(document).ready(function() {
	ORANGE.OBTW.initialisePage();
});
