/*-------------------------------------------------------------

JQuery -   Brightlight Insurance
version:   1.0
author:    James Morrish
website:   http://jamesmorrish.co.uk/

-------------------------------------------------------------*/

DD_roundies.addRule('#form', '15px', true);
DD_roundies.addRule('.ticker', '15px', true);
DD_roundies.addRule('table', '15px', true);
DD_roundies.addRule('#BB_BuyButtonForm', '15px', true);
DD_roundies.addRule('#apply-by-phone form', '15px', true);
DD_roundies.addRule('#maintenance #main', '15px', true);
DD_roundies.addRule('#lng', '15px', true);

$(document).ready( function() {

	$('#applicant-2 div, #disabilities').hide();
	$('#second-applicant').change(function() {
		
		if ($('#applicant-2 div').is(':visible')) {
			
			$('#applicant-2 div').fadeOut('fast').find('input[type=text]').removeClass('required');
		} 
		else $('#applicant-2 div').fadeIn('fast').find('input[type=text]').addClass('required');
	});
	
	
	$('#day').change(function() {
		
		if ($(this).val() == 'Saturday') {
			$('#time').html('<option value="10:00-14:00">10:00-14:00</option>');
		} 
		else {
			$('#time').html('<option value="10:00-13:00">10:00-13:00</option><option value="13:00-16:00">13:00-16:00</option><option value="16:00-18:00">16:00-18:00</option><option value="18:00-20:00">18:00-20:00</option>');
		}
		
	});
	
	var validatedOnce = false;
	
	function checkPhone() {
		var error = true;
		$('#lng-form .phone').each(function() {
			if ($.trim($(this).val()) > '') error = false;
		});
		
		if (error) {
			$('#lng-form .phone').addClass('error');
			$('#lng-form #phone-error').show('fast');
		}
		else {
			$('#lng-form .phone').removeClass('error');
			$('#lng-form #phone-error').hide('fast');
		}
		
		return error;
	}
	
	function validate() {
		
		validatedOnce = true;
		var error = false;
			
			$('#lng-form .required').each(function() {

				$(this).removeClass('error');

				if ($.trim($(this).val()) == '') {

					error = true;
					$(this).addClass('error');
				}
				
			});
			
			error = checkPhone() && error;
			
			if (error) $('#lng-form .error-main').fadeIn('fast');
			else $('#lng-form .error-main').fadeOut('fast');
			
			return !error;	
	}
	
	$('#lng-form .date').focus(function() {
		if ($(this).val() == 'dd/mm/yyyy') $(this).val('');
	}).blur(function() {
		if ($(this).val() == '') $(this).val('dd/mm/yyyy');
	});
	
	if ($('#lng-form').length) {
		
		$('#lng-form input').change(function() {
			if (validatedOnce) validate();
		});
		
		$('#lng-form').submit(function() {
			
			return validate();
		});
	}
	
	$('#illness').change(function() {
		
		if ($('#disabilities').is(':visible')) {
			
			$('#disabilities').fadeOut('fast');		
		} 
		else $('#disabilities').fadeIn('fast');
	});

	$.preloadImages = function()
	{
		for(var i = 0; i<arguments.length; i++)
		{
			jQuery("<img>").attr("src", arguments[i]);
		}
	}

	$.preloadImages("/assets/images/btn-apply-down.gif", "/assets/images/btn-choose-down.gif", "/assets/images/btn-comparisons-down.gif", "/assets/images/btn-faqs-down.gif", "/assets/images/btn-glossary-down.gif", "/assets/images/btn-home-down.gif", "/assets/images/btn-quote-down.gif", "/assets/images/btn-underwriting-down.gif");



	if ($('.ticker', '#free-quote').length > 0)
	$().newsTicker({
		newsList: ".ticker",
		startDelay: 10,
		placeHolder1: " |",
		tickerRate: 40,
		loopDelay: 4000
	});

	var total = $('#total');
	$('#quantity').change(function() {

		total.html('Total: &#163;' + (parseInt($(this).val()) * 15)); 

	});
	total.html('Total: &#163;' + (parseInt($('#quantity').val()) * 15)); 

/*
	var form = $('form', '#free-quote');

	if (form.length > 0) {
			

		var tooltips = $('.tooltip', form);

		$('select, input:not(#submit)', form).live('focus', function() {
			var tip = $(this).closest('.cont').find('.tooltip');

			if (!tip.is(':visible')) {

				var showing = tooltips.filter(':visible');
				showing.fadeOut('fast', function() {
					tip.fadeIn('fast');
				});
				if (showing.length == 0) tip.fadeIn('fast');
			}
		});

		$("input.date").datepicker({ dateFormat: 'dd/mm/yy', changeYear: true, changeMonth: true, yearRange: '1941:1990' });

		var partnerForm = $('#partner, #you', form);
		partnerForm.css('display', 'none');

		$('#cover-type', form).bind('change', function() {
			if ($(this).val() == 'joint') partnerForm.fadeIn('fast');
			else partnerForm.fadeOut('fast');
		});

		$('.date', form).bind('click', function() {
			if ( $(this).val() == 'DD/MM/YYYY' ) $(this).val('');
		});
		$('.date', form).bind('blur', function() {
				if ( $(this).val() == '' ) $(this).val('DD/MM/YYYY');
		});

		function error(element, show, type) {
			if (show) element.parent().find('.' + type).fadeIn('fast');
			else element.parent().find('.' + type).fadeOut('fast');
		}

		function validate(element) {

			var hasError = false;
			var RegExPattern;

			if ( ( jQuery('#cover-type', form).val() != 'joint' ) && (element.parents('#partner').length > 0) ) return true;

			if (element.hasClass('required')) {

				if (element.val().length > 0) error(element, false, 'error');
				else {
					error(element, false, 'invalid');
					error(element, true, 'error');
					hasError = true;
					return hasError;
				}
			}
			
			if (element.hasClass('integer')) {

				if (parseInt(element.val()) == element.val()) error(element, false, 'error');
				else {
					error(element, false, 'invalid');
					error(element, true, 'error');
					hasError = true;
					return hasError;
				}
			}

			if (element.hasClass('email')) {

				RegExPattern = /^\w(?:\.?[\w%+-]+)*@\w(?:[\w-]*\.)+?[a-z]{2,}$/i;

				if ( element.val().match(RegExPattern) ) error(element, false, 'invalid');
				else {
					error(element, true, 'invalid');
					hasError = true;
				}
			}

			if (element.hasClass('date')) {

				RegExPattern = /^((((0?[1-9]|[12]\d|3[01])[\.\-\/](0?[13578]|1[02])[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|((0?[1-9]|[12]\d|30)[\.\-\/](0?[13456789]|1[012])[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|((0?[1-9]|1\d|2[0-8])[\.\-\/]0?2[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|(29[\.\-\/]0?2[\.\-\/]((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00)))|(((0[1-9]|[12]\d|3[01])(0[13578]|1[02])((1[6-9]|[2-9]\d)?\d{2}))|((0[1-9]|[12]\d|30)(0[13456789]|1[012])((1[6-9]|[2-9]\d)?\d{2}))|((0[1-9]|1\d|2[0-8])02((1[6-9]|[2-9]\d)?\d{2}))|(2902((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00))))$/;

				if ( element.val().match(RegExPattern) ) error(element, false, 'invalid');
				else {
					error(element, true, 'invalid');
					hasError = true;
				}
			}

			return !hasError;
		}

		var validatedOnce = false;
		var formElements = $("select, input[type != 'submit'][type != 'hidden']", form);

		$('#submit', form).bind('click', function(e) {

			validatedOnce = true;
			var validated = true;

			formElements.each(function() {
				validated = validate($(this)) && validated;
			});

			return validated;
		});
		formElements.bind('blur', function() {

			if (validatedOnce) validate($(this));
		});
	}
		*/

	var counter = 1;
	var container = $('#sidebar .container p');

	function changeSidebarText() {
		counter++;
		container.animate({'opacity': 0}, 600, function() {

			container.text(phrases[counter]).animate({'opacity': 1}, 600);
		});

		if (counter >= phrases.length) counter = 1;

		setTimeout(changeSidebarText, 5000);
	}

	setTimeout(changeSidebarText, 5000);

	var light = $('#light');
	var sideBar = $('#sidebar');

	light.removeClass('nojs');
	sideBar.hover(function() {
		light.stop(true, true).fadeIn('slow');
	}, function() {
		light.stop(true, true).fadeOut('slow');
	});


	var getQuote = $('#get-quote');
	var image = $('img.nojs', getQuote);
	image.css('display', 'none');
	image.removeClass('nojs');

	function pulsate() {
		image.fadeIn(600, function() { image.fadeOut(600, pulsate) });
	}
	pulsate();
});
