﻿$(document).ready (function () {

//Toggling instructions
  $('h3, .h4, .h5, .h6').click(function(){
  	var $this = $(this);
    var $next = $(this).next();
    var descendants = $next.find('ul, ol');
      $next.toggle();
      descendants.hide();
		if ($next.is(':visible') ){
			$this.addClass('expand selected');
			$this.next().addClass('selected');
		}
		else {
			$this.removeClass('expand selected');
			$this.next().removeClass('expand selected');
		}
	}).hover(function () {
		$(this).toggleClass('mouseOver');
		$(this).find('span').toggleClass('path');
		
  });
	
	$('#search').focus();
	$('#search').click(function(){
		$(this).focus().select();
	});

	

		//Load info into Box Fixes-Enhancements Page
	var ul = $('#Release ul:first').clone();
	
	$('#Release ul').hide();
	var $FE = $('#Release h4:first, ul:first');
	$FE.clone().insertAfter('#current');
	$FE.appendTo('#FixText');
	ul.appendTo('#FixText');
	$('#Release h4').click (function () {
		var $heading = $(this);
		var $info = $(this).next().show();
		$('#FixText *').remove();
		$heading.clone()
			.appendTo('#FixText')
		$info.clone ()
			.appendTo ('#FixText')
		$info.hide();
	});


		//Back to Top
	$('a[href=#scrollUp]').click(function() {
		$.scrollTo(0, 'slow');
		return false;
		});
		
		//Scroll to highlighted text 
	$('#go').bind('focus click', function () {
		$.scrollTo('.highlight:first','slow');	
		});
	
	$('#search').keypress (function () {
		if(event.which == '13') {
		$.scrollTo('.highlight:first','slow'); }	
	});



		//Search Box
	var self = this;				
	self.input = $("#search");							    
		//handles searching the document				
	self.performSearch = function() {					
		
		//create a search string					
	var phrase = self.input.val().replace(/^\s+|\s+$/g, "");										
	//phrase = phrase.replace(/\s+/g, "|");										
		
		//make sure there are a couple letters					
	if (phrase.length < 3) { return; }													
		
		//append the rest of the expression
	phrase = ["\\b(", phrase, ")"].join("");
		
		//search for any matches
	var count = 0;
	$("#Help h4, #go").each(function(i, v) {
		//replace any matches
	var block = $(v);
	block.html(		
	block.text().replace(								
	new RegExp(phrase, "gi"), 								
	function(match) {									
	count++;									
	return ["<span class='highlight'>", match, "</span>"].join("");
	}));											
	});										
		//update the count					
	$(".result-count").text(count + " results on this page");										
		//clear this search attempt					
		//should be gone anyways...					
	self.search = null;								
	};							
	self.search;				
	self.input.keyup(function(e) {					
	if (self.search) { clearTimeout(self.search); }										
		//start a timer to perform the search. On browsers like					
		//Chrome, Javascript works fine -- other less performant					
		//browsers like IE6 have a hard time doing this					
	self.search = setTimeout(self.performSearch, 300);									
	});

		

		//Accordion
  $('.SideBarText ul > li ul')
    .click(function(e){
      e.stopPropagation();
    })
    //.filter(':not(:first)')
    .hide();
    
  $('.SideBarText ul > li').click(function(){
    var selfClick = $(this).find('ul:first').is(':visible');
    if(!selfClick) {
      $(this)
        .parent()
        .find('> li ul:visible')
        .slideToggle('fast');
    }
    
    $(this)
      .find('ul:first')
      .stop(true, true)
      .slideToggle('fast');
  });


var currentYear = (new Date).getFullYear();
	$('#year').text (currentYear);
	
	$('.box').hover (function() {
		$(this).toggleClass('boxHover');
	});
	$('#bottom li').mouseover(function () {
		var img = $(this).find('img');
		img.stop().animate({height: '96px', width: '96px'}, 200);
	});
	$('#bottom li').mouseout(function () {
		var img = $(this).find('img');
		img.stop().animate({height: '72px', width: '72px'}, 200);
	});
	
	
	$('#newsletter').hide();
	$('#newsletter li').mouseover(function () {
		var img = $(this).find('img');
		img.stop().animate({height: '60px', width: '60px'}, 200);
	});
	$('#newsletter li').mouseout(function () {
		var img = $(this).find('img');
		img.stop().animate({height: '48px', width: '48px'}, 200);
	});
	
	
	$('#join').click(function (e) {
		$('#newsletter').slideToggle();
		e.preventDefault();
	});
	$('#newsletter li').click(function (e) {
		$('#newsletter').slideUp();
	});
	
	$('#SixBoxes div:not("#perMonth, #eprescribe, #hl7-interface")').hover(function () {
		$(this).toggleClass('MidBoxHover');
	});
	
	$('#FQ').hide();
	$('#implementation').click(function (){
		//$(this).toggleClass('MidBoxHover');
		$('#FQ').show();
	});

  
//SlideShow
  rotateText(1);

function rotateText(currentText) {
  var numberOfText = $('#slideshow div').length;
  currentText = currentText % numberOfText;
	
  $('#slideshow div').eq(currentText).fadeOut(function() {
		// re-order the z-index
    $('#slideshow div').each(function(i) {
      $(this).css(
        'zIndex', ((numberOfText - i) + currentText) % numberOfText
      );
    });
    $(this).show();
    setTimeout(function() {rotateText(++currentText);}, 10000);
  });
}
  

//Lightbox
$('.formLink').click(function () {

var formData = $('#formData');

    $('body').css('overflow-y', 'hidden').css('overflow-x', 'hidden'); // hide scrollbars!
	$("#lightbox, #lightbox-panel").fadeIn(300);
	formData.clone().appendTo('#lightbox-panel');
 })
$("a#close-panel").click(function(){
	 $("#lightbox, #lightbox-panel").fadeOut(300);
     $('body').css('overflow-y', 'auto').css('overflow-x', 'auto'); // show scrollbars!
	 $(this).next().remove();
 });	

	
});






