//js file as at 12:2  June 2010  CUSTOMER ZONE ~(..,)~
$(function() {
	$('.nav_head').toggle(function() {
		$('#navBar').animate({height: 114}, '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');
	});
	
	$('#vit_prem_red').hide();
	$('#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();
			});
			
			});
	  
	  //FAQ Page
	 /* $(function(){
			$('.faq_answer').hide()
			$('.faq_question').click(function(){
					$(this).next().slideToggle();
					$(this).prev().toggleClass('more_hover');
					return false;
				});
			});*/
	   $(function(){
			$('.faq_answer').hide()
			$('.faq_question').toggle(function(){
					$(this).next().show('slow');
					$(this).prev().addClass('more_hover');
					}, function(){
						$(this).next().hide();
						$(this).prev().removeClass('more_hover');
					return false;
				});
			});
	  //tabbed nav
		$(function(){
			$('#second_content').hide();
			$('#third_content').hide();
			$('#fourth_content').hide();
			$('#fifth_content').hide();
			$('#sixth_content').hide();
			$('#seventh_content').hide();

			$('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').slideUp();
				var content_show = $(this).attr('title');  
				$('#' + content_show).slideDown();
				console.log(content_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');
	console.log(option);
	console.log(selected);
	
}
//L:iterature & Tools
$(function(){
	$('#tool_preview').hide();
		$('#second_main > li').hide();
		$('#first_main .menu_item').click(function(){
		$('#second_main > li').hide();
		$('#first_main .menu_item').removeClass('selected');
		var clicked = $(this).attr('id');
		$(this).addClass('selected');
		$('#second_main > li').each(function(index){
			var thisRow = $(this).attr('class');
			$(this).attr("id", index);
		if (thisRow == clicked){
			$(this).show();
		}
	});
	
	});
	$('#second_main > li').click(function(){
		$('.preview').hide().attr('src', '');
		$('#tool_preview').show();
		$('.toolTitle').remove();
		$('.tool_descr').remove();
		//$('#second_main > li').removeClass('selected');
		//$(this).addClass('selected');
		var thisLink = $(this).children().attr('href');
		var thisImg = thisLink.slice(10, -4) + '.gif'; //altered slice from 13 to 10 for SA
		var toolTitle = $(this).children('a').text();
		var toolText = $(this).children('.tool_description').html();
		$('<p class="tool_descr">' + toolText +'</p>').insertAfter('.preview');
		$('<h3 class="toolTitle greenone">' + toolTitle + '</h3>').insertBefore('.preview');
		$('.preview').show().attr('src', 'images/thnails/' + thisImg);  //modified 4/11/09 for SA's structure. Removed ../ before images
		$('a#download').attr('href', thisLink);
		console.log(thisImg);
		return false;
	
	});
});