﻿$(document).ready(function() {
//Helpful search text clearing
	var searchbox = $('#header ul#mainNav li input[type="text"]');
	var searchboxFieldValue = searchbox.attr('value');
	searchbox.focus(function() {
		if($(this).attr("value") == searchboxFieldValue) $(this).attr("value", "");
	});
	searchbox.blur(function(){
		if($(this).attr("value") == "") $(this).attr("value", searchboxFieldValue);  
    });

//greyBox height setting
    function equalHeight(group) {
        tallest = 0;
        group.each(function() {
        thisHeight = $(this).height();
        if(thisHeight > tallest) {
        tallest = thisHeight;
        }
    });
    group.height(tallest);
    }

equalHeight($(".lightGreyBox")); 
//Navigation
    $('.secondaryDropDown ul').hide();
    $('.secondaryDropDown ul li.currentPage').parent().show();
    $('.secondaryDropDown').click(function() {
		$('.secondaryDropDown ul:visible').slideUp(200);
		$(this).children('ul').slideDown(200);
	});
    
    $('#rotatingImageWrapper img').hide();
    
    $("ul#rotatorControl li").mouseenter(function () {
        var picPath = $(this).attr('id');
        
        if (('#rotatingImageWrapper img:hidden'))
        {
        $('#rotatingImageWrapper img').attr('src', '/img/frontPageRotator/' + picPath + '.gif');
        $('#rotatingImageWrapper img').fadeIn(300);
        }
        else
        {
        $('#rotatingImageWrapper img').attr('src', '/img/frontPageRotator/' + picPath + '.gif');
        $('#rotatingImageWrapper img').show();
        }
      });
      
      $("ul#rotatorControl").mouseleave(function () {
        $('#rotatingImageWrapper img').fadeOut(300);
      }
    );


});// End of the document.ready call

$(document).ready(function(){
// add a click event
	$(".lightbox").click(function(){
		overlayLink = $(this).attr("href");
		window.startOverlay(overlayLink);
		return false;
	});
});

function startOverlay(overlayLink) {
//add the elements to the dom
	$("body")
		.append('<div class="overlay"></div><div class="container"></div>')

//animate the semitransparant layer
	$(".overlay").animate({"opacity":"0.6"}, 400, "linear");

//add the lightbox image to the DOM
	$(".container").html('<p class="closeButton">CLOSE X</p><img src="'+overlayLink+'" alt="" />');
	var overlayHeight = $(document).height();
    $(".overlay")
			.css({			
				"height": overlayHeight
			});
//position it correctly after downloading
	$(".container img").load(function() {
		var imgWidth = $(".container img").width();
		var imgHeight = $(".container img").height();
		$(".container")
			.css({
				"top":        "50%",
				"left":       "50%",				
				"width":      imgWidth,
				"height":     imgHeight,
				"margin-top": -(imgHeight/2),
				"margin-left":-(imgWidth/2) //to position it in the middle
				
			})
			.animate({"opacity":"1"}, 400, "linear");

// you need to initiate the removeoverlay function here, otherwise it will not execute.
		window.removeOverlay();
	});
}
function removeOverlay() {
// allow users to be able to close the lightbox
	$(".overlay, .container p").click(function(){
		$(".container, .overlay").animate({"opacity":"0"}, 200, "linear", function(){
			$(".container, .overlay").remove();
		$("body")
		    .css({"overflow-y":"auto"});
		});
	});
}
/* fin. */