$(document).ready(function()
{
	var shadowbox_options = ''; // Will be appended to rel attribute for images and videos
	
	//---------------------------------------------START CUSTOM JS//
	jQuery.fn.overlabel = function()
	{
	    this.each(function(index)
		{
	        var label = $(this); var field;
	        var id = this.htmlFor || label.attr('for');
	        if (id && (field = document.getElementById(id)))
			{
	            var control = $(field);
	            label.addClass("overlabel-apply");
	            if (field.value !== '')
				{
	                label.css("text-indent", "-1000px");
	            }
	            control.focus(function () {label.css("text-indent", "-1000px");}).blur(function ()
				{
	                if (this.value === '')
					{
	                    label.css("text-indent", "0px");
	                }
	            });
	            label.click(function()
				{
	                var label = $(this); var field;
	                var id = this.htmlFor || label.attr('for');
					
					if(label.parent().hasClass('warning'))
					{
						label.find('span').remove();
						label.parent().removeClass('warning');
					}
					
	                if (id && (field = document.getElementById(id)))
					{
	                    field.focus();
	                }
	            });
	        }
	    });
	};
	
	$('label').overlabel();
	
	// Remove overlabel from select boxes as they cannot be selected properly then
	$('select option:first-child').each(function()
	{
		var parent_id = $(this).parent().attr('id');
		
		if($('label[for="'+parent_id+'"] span.warning').length > 0)
		{
			var warning = $('label[for="'+parent_id+'"] span.warning').html();
			
			$('label[for="'+parent_id+'"] span.warning').remove();
		}
		
		var label_text = $('label[for="'+parent_id+'"]').html();
		
		$(this).html(label_text);
		
		$('label[for="'+parent_id+'"]').removeClass('overlabel-apply').hide()
		
		if(warning != undefined)
		{
			$('select#'+parent_id).after('<span class="warning select">'+warning+'</span>');
		}
	});
	
	// hides the error message on subject select menu in contact page
	$('select#enquiry_subject').focus(function()
	{
		$('form#enquiry_form li.warning span.warning.select').css('visibility','hidden');
	});
	
	// rounded corners for the photo box
	if($('div.photo_box').length > 0)
	{
		$('div.photo_box').corner();
	}
	
	// size the left/right/centre columns, so that the three borders are bottom-aligned
	
	// first, get the position of the bottom border div in each of the three columns to compare
	// "stretchy_content_box_" is the name of the class applied to the side panel that is allowed to stretch
	rightCol = $('div.column.right div.stretchy_content_box_footer').position();
	leftCol  = $('div.column.left div.stretchy_content_box_footer').position();
	midPage  = $('div.page div.content_box_footer').position();

	if(leftCol.top > rightCol.top && leftCol.top > midPage.top)				// left column is tallest
	{
		var difference = leftCol.top - midPage.top;
		var page_height = $('div.page div.content_box').height();
		$('div.page div.content_box').css('height', page_height+difference);				
	}
	else if(rightCol.top > leftCol.top && rightCol.top > midPage.top)		// right column is tallest
	{
		var difference = rightCol.top - midPage.top;
		var page_height = $('div.page div.content_box').height();
		$('div.page div.content_box').css('height', page_height+difference);
	}
	else 																	//assume centre page is tallest of all (resize columns)
	{
		var diff_right   = midPage.top - rightCol.top;
		var diff_left    = midPage.top - leftCol.top;
		
		var height_left  = $('div.column.left div.stretchy_content_box').height();
		var height_right = $('div.column.right div.stretchy_content_box').height();
		
		$('div.column.left div.stretchy_content_box').css('height',height_left+diff_left);
		$('div.column.right div.stretchy_content_box').css('height',height_right+diff_right);
	}
	
	//---------------------------------------------END CUSTOM JS//
	
	$("li.subnav").hover(function()
	{
		$(this).addClass("active");
		
		if($("a.nav_search").hasClass("active"))
		{
			$("a.nav_search").removeClass("active");
			$("form.search").animate({ opacity: 0 }, 250);
			
			search_height_timeout = setTimeout(function()
			{
				$("form.search").css("height","0");
			}, 1000);
		}
	},
	function()
	{
		$(this).removeClass("active");
	});
	
	if($("ul#subnav").length > 0)
	{
		if(!$.browser.msie || $.browser.version != 5.5)
		{
			$("ul#subnav").css("display","none");
		}
	}
	
	$('a[rel*="external"]').click(function()
	{
		open($(this).attr('href'));
		return false;
	});
	
	$('a[rel*="shadowbox"]').each(function()
	{
		if(!$(this).hasClass('page_video'))
		{
			var enlarge_img = $(this).attr('rel').split(' ');
			
			if(enlarge_img != 'undefined' && enlarge_img != '')
			{
				$(this).removeAttr('rel');
				
				if(enlarge_img[1] != undefined)
				{
					$(this).attr('rel', enlarge_img[1]+shadowbox_options);
				}
				else
				{
					$(this).attr('rel', enlarge_img[0]+shadowbox_options);
				}
			}
		}
		else
		{
			var video_rel = $(this).attr('rel');
			
			$(this).removeAttr('rel');
			
			$(this).attr('rel', video_rel+shadowbox_options);
		}
	});
	
	$("ul.thumbs li a img").each(function()
	{
		$.preloadImages.add($(this).attr("src").replace("thumb", "mid"));
	});
	
	$("ul.thumbs li a").mouseover(function()
	{
		$("img#full").attr("src", ($(this).attr('href')).replace("full", "mid"));
		$("img#full").attr("alt", ($(this).attr('alt')));
		$("a#enlarge").attr("href", ($(this).attr('href')).replace("thumb", "full"));
		$("a#enlarge").attr("title", ($(this).attr('title')));
	});
	
	$("ul.thumbs li:first a").click(function()
	{
		$("a#enlarge").click();
	});
	
	$("ul.thumbs li a").click(function()
	{
		$("img#full").attr("src", ($(this).attr('href')).replace("full", "mid"));
		$("img#full").attr("alt", ($(this).attr('alt')));
		$("a#enlarge").attr("href", ($(this).attr('href')).replace("thumb", "full"));
		$("a#enlarge").attr("title", ($(this).attr('title')));
		return false;
	});
	
	if($('a.page_video').length > 0)
	{
		var page_video = $('a.page_video').attr('rel').split(' ');
		
		if(page_video != 'undefined' && page_video != '')
		{
			$('a.page_video').removeAttr('rel');
			$('a.page_video').attr('rel', page_video[1]);
		}
	}
	
	if($("li.help a").length > 0)
	{
		$("li.help a").click(function()
		{
			return false;
		});
		
		var help_timeout = 'undefined';
		
		$("li.help a").bind("mouseenter",function()
		{
			if(help_timeout != 'undefined')
			{
				clearTimeout(help_timeout);
			}
			$("li.help div").fadeOut();
			$(this).next().addClass('active').fadeIn();
		}).bind("mouseleave",function()
		{
			help_timeout = setTimeout(function()
			{
				$('li.help div.active').removeClass('active').fadeOut();
			},2000);
		});
		
		$('input').focus(function()
		{
			$('li.help div.active').removeClass('active').fadeOut();
		});
		
		$('select').focus(function()
		{
			$('li.help div.active').removeClass('active').fadeOut();
		});
		
		$('textarea').focus(function()
		{
			$('li.help div.active').removeClass('active').fadeOut();
		});
	}
	
	if($('a[rel*="shadowbox"]').length > 0)
	{
		if($('a.page_video').length > 0)
		{
			Shadowbox.init({
								continuous		:	true,
								ext				: 	{
										                img		:	['png', 'jpg', 'jpeg', 'gif', 'bmp'],
										                swf		:	['swf'],
										                flv		:	['flv', 'm4v', 'mp4'],
										                qt		:	['dv', 'mov', 'moov', 'movie'],
										                wmp		:	['asf', 'wm', 'wmv'],
										                qtwmp	:	['avi', 'mpg', 'mpeg']
										            },
								players			: 	["img", "flv", "iframe"],
								useSizzle		: 	false
							});
		}
		else
		{
			Shadowbox.init({
								continuous		: 	true,
								players			: 	["img"],
								useSizzle		: 	false
							});
		}
	}
});
