$(function(){

	//HOVER EFFECTS
	//H2 slide hover effects
	$(".paraSlide h2, .paraSlideH2only h2").hover(function(){
		$(this).addClass("hoverH2");
	},function(){
		$(this).removeAttr("class");
	});
	
	//Enquiries Submit Button Styling
	$("#submitButton").css({opacity:"0.9"});
	$("#submitButton").hover(function(){
		$(this).css({opacity:"1","cursor":"pointer"});
	},function(){
		$(this).css({opacity:"0.9"});
	});
	
	//H2 SLIDE EFFECTS
	//Instructions for slide functionality
	$("#faqs h1").after("<p class='pExpandMsg'>Please click a Question to view the Answer</p>");
	$("#doordrop h1").after("<p class='pExpandMsg'>Please click the headings below to view the full explanations</p>");
	$("#testimonials h1").after("<p class='pExpandMsg'>Please click on a persons below to view the full testimonial</p>");
	$("#toptips #content p:first").next().after("<p class='pExpandMsg'>Please click a Heading below to view further information</p>");
	
	//Get original paragraphs heights and convert them to ems, and then give each a unique id
	var paraHeights = new Array();
	$('.paraSlide p, .paraSlideH2only p').each(function(i) {
		var theHeight = $(this).height();
		var FontSize = parseInt($(this).css("font-size"));
		var theHeightEm = (1 / FontSize) * theHeight;
		var roundedHeightEm = parseFloat(theHeightEm).toFixed(4) + "em";
		paraHeights[i] = roundedHeightEm;
		$(this).attr("id",i);
	});
	
	//Hide all paragraphs
	$(".paraSlideH2only h2 + p").css({"overflow":"hidden", "height":"0.1em"});
	
	//Check to see what paragraphs heights need setting to 
	var defaultHeight = "3.5985em";
	if ($(".paraSlideH2only h2 + p").css("height") == "1px"){
		defaultHeight = "0.1em";
	}else{
		defaultHeight = "3.5985em";
	}
	
	//Reduce Paragraphs heights to default, and then hide any child images
	$(".paraSlide h2 + p").css({"overflow":"hidden", "height": defaultHeight});
	$(".paraSlide h2 + p").find("img").css({"opacity":0});
	
	//Slide to the original heights and back
	$(".paraSlide h2, .paraSlideH2only h2").click(function(){
		var paraID = $(this).next().attr("id");
		var classOpen = $(this).next().attr("class");
		var allParas = $(".paraSlide h2 + p, .paraSlideH2only h2 + p");
		var thisPara = $(this).next();
		if($(".paraSlide p img").css("opacity") == 0){
			checkOpen(paraID,classOpen,allParas,thisPara);
			$(this).next().find("img").animate({"opacity":1}, 1500);
		}else{
			if($(".paraSlide p, .paraSlideH2only p").find("img").length > 0){
			$(".paraSlide p img").animate({"opacity":0}, 700,function(){
				checkOpen(paraID,classOpen,allParas,thisPara);
			});
			}else{
				checkOpen(paraID,classOpen,allParas,thisPara);
			}
		};
	});

	//Check to see if paragraph is open then animate accoridingly
	function checkOpen(paraID,classOpen,allParas,thisPara){
		if(classOpen == "open"){
			allParas.animate({height:defaultHeight}).removeAttr("class");
		}else{
			allParas.animate({height:defaultHeight}).removeAttr("class");
			thisPara.animate({height:paraHeights[paraID]}).addClass("open");
		}
	}
	
	//Gallery hover effect
	$(".thumbImgs img").hover(function () {
		$(this).animate({opacity : 1}, "normal" );
	},function () {
		$(this).animate({opacity : .8}, "normal" );
	});
	
	//TOPTIPS
	$("#toptips #content img").next().addClass("firstPara");
	
	//CONTACTS
	//identify each area and get heights of the vCard divs
	var mapAreas = new Array();
	var vcards = new Array();
	var vcardHeights = new Array();
	$("#contacts #content area").each(function(i){
		theRef = $(this).attr("alt");
		mapAreas[i] = theRef;
		$("#contacts #content .vcard").each(function(i){
			$(this).attr("id",mapAreas[i]);
			theHeight = $(this).height();
			vcardHeights[i] = theHeight;
		});
	});
	
	//Display the instructions and hide the vCard divs
	$("#contacts #content .vcard").css({"overflow":"hidden", "height":"0.1em"});
	$("#contacts #content #contactsMap, #contacts #content .pExpandMsg").show();
	
	//Slide to the original heights and back
	$("#contacts #content map area").click(function(){
		$("#contacts #content .vcard").animate({height:"0.1em"});
		$.scrollTo(0, 1000);
		vcardid = $(this).attr("alt");
		vcard = $("#contacts #content").find("#" + vcardid);
		$(vcard).delay(500).animate({height:vcardHeights[vcardid]});
	});
	
});
