//plug-in to fix regex replace() in JQuery
jQuery.fn.replaceAttr = function(aName, rxString, repString) { 
    return this.attr( 
        aName, 
        function() { return jQuery(this).attr(aName).replace(rxString, repString); } 
    ); 
};

$(document).ready(function(){
	
	
	
	//hide Back To Top link for link for short pages
	var offset = $("p.back-to-top:first").offset();
	if ((offset.top) <= 600) {
		$("p.back-to-top").hide();
	}
	
	
		
	//change layout of trustees home page by separating image from link
	$("#trustees-inner").append("<ul class=\"thumbnails\"></ul>");
	$("ul.names li").clone().prependTo("ul.thumbnails");
	$("ul.names img").remove();
	
	$("ul.thumbnails li").each(function(i){
		$(this).replaceWith($("a",this));
	});

	$("ul.thumbnails a").wrap("<li></li>");
	$("ul.thumbnails span").remove();
	
	//SET HOVERING EFFECT ON LINKS AND IMAGES
	//set all thumbnail to opaque
	$("ul.thumbnails img").replaceAttr('src',/col/,'opaque');
	//set an incremental id for each name list item (used for hover)
	$("ul.names li").each(function(i){
		$(this).attr("class","img"+i);
	});
	//set an id incremental for each image list item (used for hover)
	$("ul.thumbnails li").each(function(i){
		$(this).attr("class","img"+i);
	});
	//hovering effect for links
	$("ul.names li").hover(function(){
		var img_class = $(this).attr("class");
		$("ul.thumbnails li[class='"+img_class+"'] a img").replaceAttr('src',/opaque/,'col');
			},function(){
		var img_class = $(this).attr("class");
		$("ul.thumbnails li[class='"+img_class+"'] a img").replaceAttr('src',/col/,'opaque');
	});
	//hovering effect for thumbnails
	$("ul.thumbnails li").hover(function(){
		var list_class = $(this).attr("class");
		$("ul.thumbnails li[class='"+list_class+"'] a img").replaceAttr('src',/opaque/,'col');
		//$("ul.names li[class='"+list_class+"'] a").css("text-decoration","underline");
			},function(){
		var list_class = $(this).attr("class");
		$("ul.thumbnails li[class='"+list_class+"'] a img").replaceAttr('src',/col/,'opaque');
		//$("ul.names li[class='"+list_class+"'] a").css("text-decoration","none");
	});
	
	
	
	//set vertical alignment of RH image in homepage
	var img_height = $("#content.two-cols").height();
	$("#decoration.image").css("line-height",img_height+"px");
	
	//set vertical alignment of RH blockquote
	var colours = ["blue","orange","purple"];
	$.each(
		colours,
		function(intIndex,objValue){
			var img_height_container = $("#two-cols-white-"+objValue+"").height();
			var img_height_quote = $("#two-cols-white-"+objValue+" blockquote").height();
			var img_height_block = ((img_height_container/2) - (img_height_quote/2));
			$("#two-cols-white-"+objValue+" blockquote").css("top",img_height_block+"px");
		}
	);
	
	//set vertical alignment of RH image in homepage
	var blue_container_height = $("#two-cols-white-blue").height();
	var blue_container_img_height = $("#two-cols-white-blue p.image img").height();
	var blue_container_img_block = ((blue_container_height/2) - (blue_container_img_height/2));
	$("#two-cols-white-blue p.image img").css("top",blue_container_img_block+"px");
	
	
	//set hovering effects for rounded-corners buttons
	var colours = ["blue","orange","purple"];
	$.each(
		colours,
		function(intIndex, objValue){
			$(".button-"+objValue+"-new a").hover().css("text-decoration","none"); //disable underline, used as a fallback for browsers with JS disabled
			$(".button-"+objValue+"-new").hover(function(){
				$(this).css("background-image","url(../images/buttons/btn-"+objValue+"-rb-hover.gif)");
				$(this).find(".button-"+objValue+"-new-inner").css("background-image","url(../images/buttons/btn-"+objValue+"-rt-hover.gif)");
				$(this).find(".button-"+objValue+"-new-inner a").css("background-image","url(../images/buttons/btn-"+objValue+"-lt-hover.gif)");
				$(this).find(".button-"+objValue+"-new-inner a span").css("background-image","url(../images/buttons/btn-arrow-"+objValue+"-hover.gif)");
				$(this).find(".button-"+objValue+"-new-lower").css("background-image","url(../images/buttons/btn-"+objValue+"-lb-hover.gif)");
					},function(){
				$(this).css("background-image","url(../images/buttons/btn-"+objValue+"-rb.gif)");
				$(this).find(".button-"+objValue+"-new-inner").css("background-image","url(../images/buttons/btn-"+objValue+"-rt.gif)");
				$(this).find(".button-"+objValue+"-new-inner a").css("background-image","url(../images/buttons/btn-"+objValue+"-lt.gif)");
				$(this).find(".button-"+objValue+"-new-inner a span").css("background-image","url(../images/buttons/btn-arrow-"+objValue+".gif)");
				$(this).find(".button-"+objValue+"-new-lower").css("background-image","url(../images/buttons/btn-"+objValue+"-lb.gif)");
			});
		}
	);
	
	//exceptions for dishomogeneous colours
	//grey buttons
	var shades = ["1","2","3","4"];
	$.each(
		shades,
		function(intIndex,objValue){
			$(".grey"+objValue+" .button-grey-new a").hover().css("text-decoration","none"); //disable underline, used as a fallback for browsers with JS disabled
			$(".grey"+objValue+" .button-grey-new").hover(function(){
				$(this).css("background-image","url(../images/buttons/btn-grey"+objValue+"-rb-hover.gif)");
				$(this).find(".button-grey-new-inner").css("background-image","url(../images/buttons/btn-grey"+objValue+"-rt-hover.gif)");
				$(this).find(".button-grey-new-inner a").css("background-image","url(../images/buttons/btn-grey"+objValue+"-lt-hover.gif)");
				$(this).find(".button-grey-new-inner a span").css("background-image","url(../images/buttons/btn-arrow-grey-hover.gif)");
				$(this).find(".button-grey-new-lower").css("background-image","url(../images/buttons/btn-grey"+objValue+"-lb-hover.gif)");
					},function(){
				$(this).css("background-image","url(../images/buttons/btn-grey"+objValue+"-rb.gif)");
				$(this).find(".button-grey-new-inner").css("background-image","url(../images/buttons/btn-grey"+objValue+"-rt.gif)");
				$(this).find(".button-grey-new-inner a").css("background-image","url(../images/buttons/btn-grey"+objValue+"-lt.gif)");
				$(this).find(".button-grey-new-inner a span").css("background-image","url(../images/buttons/btn-arrow-grey.gif)");
				$(this).find(".button-grey-new-lower").css("background-image","url(../images/buttons/btn-grey"+objValue+"-lb.gif)");
			});
		}
	);
	//grey on specific backgrounds
	var backgrounds = ["purple","orange"];
	$.each(
		backgrounds,
		function(intIndex,objValue){
			$(".button-grey-"+objValue+"-new a").hover().css("text-decoration","none"); //disable underline, used as a fallback for browsers with JS disabled
			$(".button-grey-"+objValue+"-new").hover(function(){
				$(this).css("background-image","url(../images/buttons/btn-grey-"+objValue+"-rb-hover.gif)");
				$(".button-grey-"+objValue+"-new-inner").css("background-image","url(../images/buttons/btn-grey-"+objValue+"-rt-hover.gif)");
				$(".button-grey-"+objValue+"-new a").css("background-image","url(../images/buttons/btn-grey-"+objValue+"-lt-hover.gif)");
				$(".button-grey-"+objValue+"-new a span").css("background-image","url(../images/buttons/btn-arrow-grey-hover.gif)");
				$(".button-grey-"+objValue+"-new-lower").css("background-image","url(../images/buttons/btn-grey-"+objValue+"-lb-hover.gif)");
					},function(){
				$(this).css("background-image","url(../images/buttons/btn-grey-"+objValue+"-rb.gif)");
				$(".button-grey-"+objValue+"-new-inner").css("background-image","url(../images/buttons/btn-grey-"+objValue+"-rt.gif)");
				$(".button-grey-"+objValue+"-new a").css("background-image","url(../images/buttons/btn-grey-"+objValue+"-lt.gif)");
				$(".button-grey-"+objValue+"-new a span").css("background-image","url(../images/buttons/btn-arrow-grey.gif)");
				$(".button-grey-"+objValue+"-new-lower").css("background-image","url(../images/buttons/btn-grey-"+objValue+"-lb.gif)");
			});
		}
	);

	
	
});