$(function(){

	// christmas lights animation

	function animateLights() {
		
		setTimeout(function(){ 
				LightsOn(1);			
		}, 500);

		setTimeout(function(){ 
				LightsOn(2);			
		}, 1000);

		setTimeout(function(){ 
				LightsOn(3);			
		}, 1500);

		setTimeout(function(){ 
				LightsOn(4);			
		}, 2000);
		setTimeout(function(){ 
				animateLights();			
		}, 2000);
	}

	function LightsOn(lite) 
	{

			$('#lights' +lite).show()
				lite-= 1;
				$('#lights' +lite).hide();


	}
		
	// To turn lights off comment out this code
	//animateLights();


	//navigation hover 
	if ($('#serviceHover').length)
	{
		$('#serviceHover').hover(function(){
			element = $('.serviceHover');
			if (element.hasClass('hide'))
			{
				element.removeClass('hide');
				element.addClass('display');
			}

		});
		$('#serviceHover').mouseout(function(){
			element = $('.serviceHover');
			if (element.hasClass('display'))
			{
				element.removeClass('display');
				element.addClass('hide');
			}

		});
		$('.subNav').hover(function(){
			element = $('.serviceHover');
			if (element.hasClass('hide'))
			{
				element.removeClass('hide');
				element.addClass('display');
			}

		});
		$('.subNav').mouseout(function(){
			element = $('.serviceHover');
			if (element.hasClass('display'))
			{
				element.removeClass('display');
				element.addClass('hide');
			}

		});
	}

	//about us page
	if ($('.thumbs').length)
	{
		$('#Viewable').val('JT');
		$('.empOuter').hide();
		$('#JT').show();

		$('.thumbs,.thumbs2').click(function(){
			$('.thumbs').each(function(){
				if ($(this).hasClass('moveBackground'))
				{
					$(this).removeClass('moveBackground');
				}
				
			});
			if ($(this).hasClass('thumbs') && !$(this).hasClass('thumbs'))
			{
				$(this).addClass('moveBackground');
			}
			name = $(this).attr('data-name');
			oldName = $('#Viewable').val();

			//$('.'+oldName).hide("scale",600);
			//$('.'+name).show("scale",600);

			$('#'+oldName).hide("scale",600, function(){
				
				$('#'+name).show("scale",600);


			});
			
			$('#Viewable').val(name);
			return false;
		});
	}

	//service page
	/*
	if ($('.serviceTeaser').length)
	{
		$('#Viewable').val('Web');
		$('.serviceContent').hide();
		$('#Web').show();

		$('.serviceTeaser').click(function(){
			$('.serviceTeaser').each(function(){
				if ($(this).hasClass('moveServiceBackground'))
				{
					$(this).removeClass('moveServiceBackground');
				}
				
			});
			if ($(this).hasClass('serviceTeaser'))
			{
				$(this).addClass('moveServiceBackground');
			}
			name = $(this).attr('data-name');
			oldName = $('#Viewable').val();
			$('#'+oldName).stop().fadeOut(200,function() {
				$('#'+oldName).hide();
				$('#'+name).fadeIn(200);
			});
			$('#Viewable').val(name);
			return false;
		});
	}
	*/

	//insurance Systems
	if ($('.insuranceTeaser').length)
	{
		$('#Viewable').val('Ins');
		$('.insureContent').hide();
		$('#Ins').show();

		$('.insuranceTeaser').click(function(){
			$('.insuranceTeaser').each(function(){
				if ($(this).hasClass('moveInsuranceBackground'))
				{
					$(this).removeClass('moveInsuranceBackground');
				}
				
			});
			if ($(this).hasClass('insuranceTeaser'))
			{
				$(this).addClass('moveInsuranceBackground');
			}
			name = $(this).attr('data-name');
			oldName = $('#Viewable').val();
			$('#'+oldName).stop().fadeOut(200,function() {
				$('#'+oldName).hide();
				$('#'+name).fadeIn(200);
			});
			$('#Viewable').val(name);
			return false;
		});
	}

	//Core page
	if ($('.coreValuesLink').length)
	{
		$('#Current').val('Land');
		$('.coreTeaser').hide();
		$('.Land').show();

		$('.coreValuesLink').click(function(){
			name = $(this).attr('id');
			oldName = $('#Current').val();
			$('.'+oldName).hide("scale",600,function(){

				$('.'+name).show("scale",600);
			});



			//Possible values: 'blind', 'explode', 'fold', 'puff', 'scale', 'size', 'pulsate'.
			
			$('#Current').val(name);
			return false;
		});
	}

	//homepage function 
	function animateTeasers() {
		
		setTimeout(function(){ 
				changeTeaser(2);			
		}, 10000);

		setTimeout(function(){ 
				changeTeaser(3);			
		}, 14000);

		setTimeout(function(){ 
				changeTeaser(4);			
		}, 18000);

		setTimeout(function(){ 
				changeTeaser(5);			
		}, 22000);

		setTimeout(function(){ 
				changeTeaser(6);			
		}, 26000);

		setTimeout(function(){ 
				changeTeaser(1);			
		}, 30000);

		setTimeout(function(){ 
				animateTeasers();			
		}, 30000);

	}

	function changeTeaser(name)
	{
		if (bol)
		{
			oldName = $('#Current').val();
			$('.teaserDot').each(function(){
				if ($(this).hasClass('teaserSelected'))
				{
					$(this).removeClass('teaserSelected');
				}
				
			});

			$('.teaserDot').each(function(){
				if ($(this).attr('data-name') == name)
				{
					$(this).addClass('teaserSelected');
				}
				
			});
			$('#teaser'+oldName).hide("slide",{direction: 'left'},600);
			$('#teaser'+name).show("slide",{direction: 'right'},600);
			
			

			//Possible values: 'blind', 'clip', 'drop', 'explode', 'fold', 'puff', 'slide', 'scale', 'size', 'pulsate'.
			
			$('#Current').val(name);
		}

	}

	
	//Home page
	if ($('.teaserDot').length)
	{
		bol = true;
		$('#Current').val(1);
		animateTeasers();
		$('.teaserImage').hide();
		$('#teaser1').show();

		$('.teaserDot').click(function(e){
			bol = false;
			$('.teaserDot').each(function(){
				if ($(this).hasClass('teaserSelected'))
				{
					$(this).removeClass('teaserSelected');
				}
				
			});
			$(this).addClass('teaserSelected');
			name = $(this).attr('data-name');
			oldName = $('#Current').val();
			if (name != oldName)
			{
				if (name > oldName)
				{
					$('#teaser'+oldName).hide("slide",{direction: 'left'},600);
					$('#teaser'+name).show("slide",{direction: 'right'},600);
				}
				else
				{
					$('#teaser'+oldName).hide("slide",{direction: 'right'},600);
					$('#teaser'+name).show("slide",{direction: 'left'},600);

				}
			}
			
			//Possible values: 'blind', 'clip', 'drop', 'explode', 'fold', 'puff', 'slide', 'scale', 'size', 'pulsate'.
			
			$('#Current').val(name);
			return false;
		});
	}

	// news rotator
	$('#NewsRotator1').hide(); $('#NewsRotator2').hide(); $('#NewsRotator3').hide(); $('#NewsRotator4').hide();
	rotateNews();


	function rotateNews(){
			$('#NewsRotator1').show("scale",600, function(){
				$('#NewsRotator1').delay(1500).hide("scale",600, function(){
					$('#NewsRotator2').show("scale",600, function(){
						$('#NewsRotator2').delay(1500).hide("scale",600, function(){
							$('#NewsRotator3').show("scale",600, function(){
								$('#NewsRotator3').delay(1500).hide("scale",600, function(){
									$('#NewsRotator4').show("scale",600, function(){
										$('#NewsRotator4').delay(1500).hide("scale",600, function(){
											rotateNews();
										});
									});
								});
							});
						});
					});
				});
			});
	}

	//design page
	if ($('.designs').length)
	{
		$('.empText').hide();
		$('#WS').show();

		$('.designs').click(function(){
			$('.designs').each(function(){
				if ($(this).hasClass('moveDesBackground'))
				{
					$(this).removeClass('moveDesBackground');
				}
				
			});
			$(this).addClass('moveDesBackground');
			name = $(this).attr('data-name');
			oldName = $('#Viewable').val();
			$('#'+oldName).stop().fadeOut(200,function() {
				$('#'+oldName).hide();
				$('#'+name).fadeIn(200);
			});
			$('#Viewable').val(name);
			return false;
		});
	}

	//clients page
	if ($('.clients').length)
	{
		$('#current').val('insure');

		$('.bigClient').hide();
		$('#insure').show();

		$('.clients').click(function(){
			$('.clients').each(function(){
				if ($(this).hasClass('addBorder'))
				{
					$(this).removeClass('addBorder');
				}
				
			});
			$(this).addClass('addBorder');
			name = $(this).attr('data-name');
			oldName = $('#current').val();
			$('#'+oldName).stop().fadeOut(200,function() {
				$('#'+oldName).hide();
				$('#'+name).fadeIn(200);
			});
			$('#current').val(name);
			return false;
		});
	}
	
	//print route directions page
	$('#PrintWholePage').click(function()
	{
		window.print();
		return false;
	});

	$('#PrintRoute').click(function()
	{
		$('.Route').printArea();
		return false;
	});
	
	//focus change colour for all browsers
	if ($('#ContactPage').length)
	{
		$('input.text, textarea').focus(function() {
		  $(this).css({'background':'#c0d441','color':'#fff'}); 
		});
		$('input.text, textarea').blur(function() {
		  $(this).css({'background':'#fff','color':'#5a5a5a'}); 
		});
	}

	

	$('.clientLogin').click(function() {
		if ($(this).hasClass('closed'))
		{
			$('.loginFormContainer').animate({right : '0'}, function() {
				$('.clientLogin').removeClass('closed');
				$('.clientLogin').addClass('open');
			});
			
			
		}
		if ($(this).hasClass('open'))
		{
			$('.loginFormContainer').animate({right : '-215px'}, function() {
				$('.clientLogin').removeClass('open');
				$('.clientLogin').addClass('closed');
			});
		}
		
		return false;
	});

	if ($('#LoginForm').length)
	{
		$('#login').click(function(){
			$('.loginerror').remove();
			$('#LoginForm').append('<p class="loginerror error">Details not recognised</p>');

			return false;
		});
	}

});
