// JavaScript Document

$('html').addClass('js');

// Function to add commas for fields
(function($){
	$.fn.commas = function(elem){
		//Get the value from the input
		var myVal = elem.value;
		//Remove any commas that might already be there
		var temp = myVal.replace(/,/g, '');
		//Put commas in
		elem.value = temp.replace(/(\d)(?=(\d{3})+($|\.))/g, "$1,");
	};
})(jQuery);

// Set and Clear
(function($){
	$.fn.clearDefault = function(){
		return this.each(function(){
			var default_value = $(this).val();
			$(this).focus(function(){
				if ($(this).val() == default_value) $(this).val("");
			});
			$(this).blur(function(){
				if ($(this).val() == "") $(this).val(default_value);
			});
		});
	};
})(jQuery);

//Drop Down
var timeout    = 500;
var closetimer = 0;
var ddmenuitem = 0;
function jsddm_open(){
	jsddm_canceltimer();
	jsddm_close();
	ddmenuitem = $(this).find('ul').css('visibility', 'visible');
}
function jsddm_close(){
	if(ddmenuitem) {
		ddmenuitem.css('visibility', 'hidden')
	};
}
function jsddm_timer(){
	closetimer = window.setTimeout(jsddm_close, timeout);
}
function jsddm_canceltimer(){
	if(closetimer){  
		window.clearTimeout(closetimer);
		 closetimer = null;
	}
}


$(document).ready(function() {
						   
						   
	//if($('#logo')[0].offsetWidth == 288) {
		$('.imgReplacement').each(function() {
			string = $(this).text();
			filename = string.toLowerCase().replace(/ /g, '-').replace(/([^0-9a-z-])/g,'');
			$(this).html('<img src="/assets/img/t/' + filename + '.png" alt="' + string + '" title="' + string + '" />');
		});
	//}
						  
	// Dropdown Navigation Function
	$('.nav > li').bind('mouseover', jsddm_open)
	$('.nav > li').bind('mouseout',  jsddm_timer)
	document.onclick = jsddm_close;
	// Dropdown rounded bottom
	//$('.subNav').corner("bottom 11px");
	// End Dropdown Navigation Function
	
							   
	// POPUPS
	$('.popup').fancybox({
		frameWidth: 760,
		frameHeight: 420
	});
	
	$("a.video").fancybox({
		frameWidth 		: 560, 
		frameHeight 		: 398, 
		'type' 			: 'swf' ,
		hideOnOverlayClick	: false,
		hideOnContentClick	: false
	});


	
	
	
	// COMMAS
	$('.commas').blur(function(){
		$(this).commas(this)
	});
	
	// SET AND CLEAR DEFAULT
	$('input.clear-default').clearDefault();
	
	
	
	//if($('#logo')[0].offsetWidth == 288) {
	//	$('.imgReplacement').each(function() {
	//		string = $(this).text();
	//		filename = string.toLowerCase().replace(/ /g, '-').replace(/([^0-9a-z-])/g,'');
	//		$(this).html('<img src="/assets/img/t/' + filename + '.png" alt="' + string + '" title="' + string + '" />');
	//	});
	//}
	
	//$.datepicker.setDefaults({
	//	changeYear:true//,
	//	//dateFormat:'dd/mm/yy'
	//});
						   
	//$(".datepick").datepicker();
	
	
	// Hide any text required for non JS browsers
	$('.hideJQ').hide();
	
	
	/* ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
	// SPLIT OR COMBINED LIFE COVER SWITCH
	/* ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
	// On load or submit
	if($('#jqChooser').val() == 'split'){
		$('#combinedAmounts').hide();
		$('#splitAmounts').show();
	} else {
		$('#combinedAmounts').show();
		$('#splitAmounts').hide();
	};
	$('#theQuestion').hide();
	// On Chancge 
	$('#coverType2').bind('click', function(){
		$('#theQuestion').slideDown();
		$('#jqChooser').val('combined');
	});
	$('.split').bind('click', function(){
		$('#combinedAmounts').slideUp('fast');
		$('#splitAmounts').slideDown('fast');
		$('#theQuestion').slideUp('fast');
		$('#jqChooser').val('split');
	});
	$('.combined, #coverType1').click(function(){
		$('#combinedAmounts').slideDown('fast');
		$('#splitAmounts').slideUp('fast');
		$('#theQuestion').slideUp('fast');
		$('#jqChooser').val('combined');
	});
	/* ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
	// END SPLIT OR COMBINED LIFE COVER SWITCH
	/* ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
	
	
	/* ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
	// SECOND APPLICANT SWITCH
	/* ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
	// On load or submit
	if($("input[name=uAppType]:checked").val() == 'single'){
		$('#secondPerson').hide();
	} else {
		$('#secondPerson').show();
	};
	// On Chancge 
	$('#uAppTypeJoint').click(function(){
		$('#secondPerson').slideDown('fast');
	});
	$('#uAppTypeSingle').click(function(){
		$('#secondPerson').slideUp('fast');
	});
	/* ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
	// END SECOND APPLICANT SWITCH
	/* ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- */


	/* ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
	// NO OF PENSIONS SWITCH
	/* ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
	// On load or submit
	if($("input[name=noOfPensions]:selected").val() == 1){
		$('#pensiontext1').show();
		$('#pensiontext2').hide();
		$('#pensiontext3').hide();
		$('#pension1').show();
		$('#pension2').hide();
		$('#pension3').hide();
	} else if($('#noOfPensions').val() == 2){
		$('#pensiontext1').hide();
		$('#pensiontext2').show();
		$('#pensiontext3').hide();
		$('#pension1').show();
		$('#pension2').show();
		$('#pension3').hide();
	} else if($('#noOfPensions').val() == 3){
		$('#pensiontext1').hide();
		$('#pensiontext2').show();
		$('#pensiontext3').hide();
		$('#pension1').show();
		$('#pension2').show();
		$('#pension3').show();
	} else if($('#noOfPensions').val() >= 4){
		$('#pensiontext1').hide();
		$('#pensiontext2').hide();
		$('#pensiontext3').show();
		$('#pension1').show();
		$('#pension2').show();
		$('#pension3').show();
	} else {
		$('#pensiontext1').hide();
		$('#pensiontext2').hide();
		$('#pensiontext3').hide();
		$('#pension1').hide();
		$('#pension2').hide();
		$('#pension3').hide();
	};
	// On Chancge 
	$('#noOfPensions').change(function(){
		if($('#noOfPensions').val() == 1){
			$('#pensiontext1').slideDown('fast');
			$('#pensiontext2').slideUp('fast');
			$('#pensiontext3').slideUp('fast');
			$('#pension1').slideDown('fast');
			$('#pension2').slideUp('fast');
			$('#pension3').slideUp('fast');
		} else if($('#noOfPensions').val() == 2){
			$('#pensiontext1').slideUp('fast');
			$('#pensiontext2').slideDown('fast');
			$('#pensiontext3').slideUp('fast');
			$('#pension1').slideDown('fast');
			$('#pension2').slideDown('fast');
			$('#pension3').slideUp('fast');
		} else if($('#noOfPensions').val() == 3){
			$('#pensiontext1').slideUp('fast');
			$('#pensiontext2').slideDown('fast');
			$('#pensiontext3').slideUp('fast');
			$('#pension1').slideDown('fast');
			$('#pension2').slideDown('fast');
			$('#pension3').slideDown('fast');
		} else if($('#noOfPensions').val() >= 4){
			$('#pensiontext1').slideUp('fast');
			$('#pensiontext2').slideUp('fast');
			$('#pensiontext3').slideDown('fast');
			$('#pension1').slideDown('fast');
			$('#pension2').slideDown('fast');
			$('#pension3').slideDown('fast');
		} else {
			$('#pensiontext1').slideUp('fast');
			$('#pensiontext2').slideUp('fast');
			$('#pensiontext3').slideUp('fast');
			$('#pension1').slideUp('fast');
			$('#pension2').slideUp('fast');
			$('#pension3').slideUp('fast');
		};
	});
	/* ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
	// END NO OF PENSIONS SWITCH
	/* ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- */

	
	
	
	
	/* ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
	// SICK PAY COVER SWITCH
	/* ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
	// On load or submit
	if($('#employmentType').val() == 'Employed'){
		$('#sickPay').show();
		$('#companyPay').hide();
	} else if ($('#employmentType').val() == 'Company Director'){
		$('#sickPay').hide();
		$('#companyPay').show();
	} else {
		$('#sickPay').hide();
		$('#companyPay').hide();
	};
	// On Chancge 
	$('#employmentType').bind('change', function(){
		if($('#employmentType').val() == 'Employed'){
			$('#sickPay').slideDown();
			$('#companyPay').slideUp();
		} else if($('#employmentType').val() == 'Company Director'){
			$('#companyPay').slideDown();
			$('#sickPay').slideUp();
		} else {
			$('#sickPay').slideUp();
			$('#companyPay').slideUp();
		}
	});
	
	/* ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
	// SICK PAY COVER SWITCH
	/* ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
	
	
	
	
	/* ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
	// CHOICE PAGES SWITCH
	/* ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
	
	// Hide all but the first section
	
	$('.option1').hide();
	$('.option2').hide();
	$('.option3').hide();
	$('.option4').hide();
	$('.option5').hide();
	$('.option6').hide();
	// Show option 1
	$('.show1').click(function(){
		$('.option1').slideDown();
		$('.link1').slideUp();
	});
	// Show option 2
	$('.show2').click(function(){
		$('.option2').slideDown();
		$('.link2').slideUp();
	});
	// Show option 3
	$('.show3').click(function(){
		$('.option3').slideDown();
		$('.link3').slideUp();
	});
	// Show option 4
	$('.show4').click(function(){
		$('.option4').slideDown();
		$('.link4').slideUp();
	});
	// Show option 5
	$('.show5').click(function(){
		$('.option5').slideDown();
		$('.link5').slideUp();
	});
	// Show option 6
	$('.show6').click(function(){
		$('.option6').slideDown();
		$('.link6').slideUp();
	});
	/* ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
	// END CHOICE PAGES SWITCH
	/* ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
	
});
