//js file as at 19 April 2011  OLD FILE TO BE REPLACED ZONE ~(..,)~
$(function() {
	$('.nav_head').toggle(function() {
		$('#navBar').animate({height: 166}, 'slow');
						}, function(e) {
		$('#navBar').animate({height:18}, 'slow');
		e.stopPropagation();
	});
	$('.subNav a').click(function(){return true;});
	$('.screening_container').children('p').hide();
   	$('.screening_container h3').click(function(){
		$(this).parent().children('p').slideToggle(200);
		$(this).parent().children('.more_icon').toggleClass('more_hover');
	});
	
	//////////////////////// POPUP/TOOLTIP FUNCTION
	function toolTip(){
		
			var toolTips = $('.hover');
			var tips = $('.hover_content');
			var offsetX = 0;
     		var offsetY= 0;
			var id =0;
			tips.hide();
			
			toolTips.each(function(i){
					$(this).attr('id', 'tool_' + i);			   
			});
			
			tips.each(function(i){
					$(this).attr('id', 'tip_' + i);			   
			});
			
			toolTips.mouseover(function(event){
					event.stopPropagation();
					var coords = $(this).position();
					id = $(this).attr('id').slice(5);
					$(tips[id]).fadeIn();
									
			}).mouseout(function(){
						$(tips[id]).fadeOut(100);
					});
			
			
		
		}
		
	toolTip();
	
	
	//$('#vit_prem_red').hide(); interferes with table in vitality overview page
	$('#cont1').hide();
	$('.switcher').next().hide();
   	$('.switcher').click(function(){
		var next = $(this).next();
		var prev =$(this).prev();
		$(next).slideToggle();
		$(prev).toggleClass('more_hover');
	});
	$('.slider_close').click(function(){
		$(this).parent().slideUp();
	});
	
	//Carousel code
if($('#carousel_container').length){ //code will only run if carousel exists
		  var slides = $('.carousel_slide'); //create array of slides
		  var index = 0; //set count
		  var slideWidth = slides.attr("width");  
		  var direction = "right"; //initialise scroll direction
		  var $last_three = slides.slice(0,3) // for when image swaps from 1 to four when scrolling left
		  var delay = 6000;
		  //loop through slides and assign unique class and stack-height/z-index
		  for (i=0; i<slides.length; i++){ 
			  $(slides[i]).addClass("slide-"+i);
			  var new_index = (4 - i);
			  $(slides[i]).css("z-index", new_index);
		  }
		  
		  //click functions
		  $('#scroll_right').click(function(){direction = "right";clearInterval(auto);sift();});
		  $('#scroll_left').click(function(){direction = "left";clearInterval(auto);sift();});
		  
		  function sift(){
			  if (direction === "right"){
				  if (index<(slides.length-1)){
					  index +=1;
				  
				  } else {index=0;slides.fadeIn(400)} //resets after fourth image
					  show (index);
			  }
			  if (direction === "left"){
				  if (index>0){
					  index -=1;
				  
				  } else {index=3;$last_three.fadeOut(400);}//sets index to fourth image - reverse of the reset above ^
					  show (index);
			  }
		  }
	  
	  
		  function show(num){
			  var current = $(slides[num]);
			  if (direction === "left"){
				  $(slides[index]).fadeIn(400);//fade-in previous image
				  direction = "right";// reset scroll direction
		  
			  } else {
			  $(slides[num-1]).fadeOut(400); //fade-out current image to reveal next
			  }
		  }
		  		  var auto = setInterval(sift, delay);  //requires name so that clearInterval can cancel it

		  show (index);
	}

//Vitality rollovers
     var offsetX = -360;
     var offsetY= -200;
	 $('.vitality_steps > p').hide();
	  $('.trigger').hover(function(event){
		   var href=$(this).attr('href');
		   var thisPara=$(this).find('p');
		   $(thisPara).show();
		   $(thisPara)
		   .css('top', event.pageY + offsetY)
		   .css('left', event.pageX + offsetX)
	  }, function(){
		   //mouse off
		   $(this).find('p').hide();
	  });

	  
	  //News page
	  $(function(){
			var newsid = ('newsid');
			$('.article_body').hide();
			$('.article_body').each(function(index){
				$(this).attr("id", ['newsid' + index]);
				});
			$('.more').click(function(){
				newsid =$(this);
				var thisid = $(this).parent().next().attr('id');
				$(this).parent().next().slideDown();
				$(this).hide();
				return false;
				});
			$('.collapse').click(function(){
				$(this).parent().slideUp();
				$(newsid).show();
			});
			
			});
	  
  $(function(){
	  $('.faq_answer').hide()
	  $('.faq_question').click(function(){
			if ($(this).next().is(':hidden')){						
			  		$(this).next().slideDown();
			  		$(this).prev().addClass('more_hover');
					
			  } else if ($(this).next().is(':visible')){
				  	$(this).next().slideUp();
				  	$(this).prev().removeClass('more_hover');
			  }
			  return false;
	  });
  });
  
  
	  //tabbed nav
	if($('#tab_nav').length){
		$(function(){
				$('.tab_content').not(':first').hide();
				//$('#tab_nav #eighth_content').hide();
				$('#tab_nav li:eq(0)').addClass('current');
				$('a.tab').click(function(){
					$('a.tab').css({'color': '#ab218e', 'font-weight': 'normal'});					  
					$('.current').removeClass('current');
					$(this).parent().addClass('current');
					$(this).css({'color': '#ab218e', 'font-weight': 'bold'});
					$('.tab_content').hide();
					var content_show = $(this).attr('title');  
					$('#' + content_show).show();

					return false;
				});
			});
	}
	
	
	
	
});		
		//popup
$(document).ready(function(){
  
  $('.hidden').hide();
  $(".modal_trigger").click(function(e){
	  e.preventDefault();
	  var clicked = $(this).attr('title');
	  var popupcontent = $('#' + clicked);
	  var maskHeight = $(document).height();
	  var maskWidth = $(window).width();	
	  $('#dim').css({'width':maskWidth,'height':maskHeight});
	  var winH = $(window).height();
	  var winW = $(window).width();
	  $('#dim').fadeIn(1000);
	  $('#dim').fadeTo('slow', 0.8);
	  $(popupcontent).css('top', winH/2-$(popupcontent).height()/2);
	  $(popupcontent).css('top', winW/2-$(popupcontent).width()/2);
	  $(popupcontent).fadeIn(1000);
	  $(popupcontent).append('<br /><a href="" id="closelink" style="color:#f8981d;text-decoration:underline">Close window X</a><br /><br />	 ');

	  });
				
  $("#dim").live("click", function(){
	  $(this).remove()
	});
  $(".modal_close").live("click", function(e){
	  e.preventDefault();
	  $(this).parent().hide()
	  $('#dim').hide();
	});
});
// Common Conditions
$(function(){  
	$("#conditionId").change(onSelectChange);    
});  
function onSelectChange(){
	$('#common_condition').empty();
	var option = $("#conditionId option:selected:value");  
	var selected = option.attr('value') + '.htm';
	$('#common_condition').load(selected).hide();
	$('#common_condition').fadeIn('slow');
	
}


//New Customer Homepage
$(function(){
	
		var home_title = function(){
			  var delay = 5000;
			  var count = 0;
			  var copy_title = [];
			  copy_title.push("Competitive protection cover from 26p per day");
			  copy_title.push("Save money on your premiums each year, without reducing your cover*");
			  //copy_title.push("Save money on gyms, health screens, holidays and more");
			  copy_title.push("76% more likely to get a payout with our Serious Illness Cover*");
			  var rotate_headline = setInterval(rotate, delay, count);
			  $('#compliance_1').hide();
			  $('#compliance_2').hide();
			  function rotate(){
				  var $this = $('#banner_text');
				  if (count < copy_title.length - 1){
					  $this.fadeOut(function(){
											 $this.text(copy_title[count]);
											 if(count === 2){
												  $('#compliance_1').hide();	
											 } 
										   
								  });
					  count++;
					  $this.fadeIn(function(){
										if(count === 1){
											$('#compliance_1').show();	
										} else if (count === 2){
											$('#compliance_2').show();
										}
								  });
				  } else {
					  rotate(count=-1);
					  $('#compliance_2').hide();
				  }
			  }
	};
	
	if($('#banner_text').length){
		home_title();	
	}
	
	
	
	
	var vitality_links = function(){
			var copy = [];
			copy.push("Vitality is our unique health and wellness programme that encourages you to lead a healthier lifestyle and reduce your risk of suffering a serious illness.");
			copy.push("We encourage you to be healthy by offering discounts with a range of health partners.") ;
			copy.push("We track how well you are doing by giving you Vitality points.") ;
			copy.push("If you have the PruProtect Plan we also give you chance to reduce your premiums every year without reducing your cover. Premium discounts do not apply to the Essentials Plan.");
			copy.push("With Vitality Plus, we reward your healthy choices by offering savings with a range of reward partners.");
			
			$('#vitality_links li a').hover(function(){
					$this_id = $(this).attr('id').slice(1);
					$('#vitality_info').text(copy[$this_id]);
					
												  
			});
			
			$('#vitality_cust > img').hover(function(){
					$('#vitality_info').text(copy[0]);										
			});
			
			$('#vitality_links li a').click(function(){
				return false;										 
			});
		};
		
		if ($('#vitality_links').length){
		vitality_links();
		}
});

//Product Overview page
$(function(){
if($('#hover_trigger').length){ 
	var overview_box = $('#overview_box');
	var steps=$('#overview_box > div');
	var titles = $('#numbers_holder .numbers h3');
	var count = 0;
	var current = steps[count];
	steps.not(steps[count]).hide();
	titles.not(titles[count]).hide();
	$('#numbers_holder .numbers a').text('');
	//$('#stage_1').css("background", "url(images/numbers_master.gif) 0 0 no-repeat");
	
	$('#numbers_holder .numbers > a').click(function(e){
		$(steps).stop();
		var clicked = $(this).attr('id');
		var number = $(this);
		e.stopPropagation();
		$(steps).fadeOut("fast");
		count = clicked.slice(6);
		count = count - 1;
		switch(count){
			case 0:
			overview_box.css('border', '4px solid #f8981d');
			$('#stage_1').css("background", "url(/images/numbers_master.gif) 0 0 no-repeat")
			break;
			case 1:
			overview_box.css('border', '4px solid #ab218e');
			$('#stage_1').css("background", "url(/images/numbers_master.gif) 0 -94px no-repeat")
			break;
			case 2:
			overview_box.css('border', '4px solid #41c4dc');
			$('#stage_1').css("background", "url(/images/numbers_master.gif) 0 -94px no-repeat")
			break;
			case 3:
			overview_box.css('border', '4px solid #8cc63f');
			$('#stage_1').css("background", "url(/images/numbers_master.gif) 0 -94px no-repeat")
			break;
			default: overview_box.css('border', '4px solid #f8981d');
			$('#stage_1').css("background", "url(/images/numbers_master.gif) 0 0 no-repeat")
		}
		overview_box.css('border-top-width', '4px');
		$(steps[count]).fadeIn("slow",function(){$(this).stop()});
		$('#numbers_holder .numbers h3').hide();
		$(this).next().fadeIn("fast");
		return false;
	});
	$('a#stage_1').trigger('click');

	
};

});

