$(document).ready(function()
{
	var shadowbox_options = ''; // Will be appended to rel attribute for images and videos
	
	//---------------------------------------------START CUSTOM JS//
	
	// Preload images
	$.preloadImages.add(_root+'media/images/subnav_bg_who_we_are.png');
	$.preloadImages.add(_root+'media/images/subnav_bg_what_we_do.png');
	$.preloadImages.add(_root+'media/images/subnav_bg_portfolio.png');
	
	if(navigator.appVersion.indexOf("Mac")!=-1) // Sort out silly mac fat fonts
	{
		$('body').css('fontSize','64%');
	}
	
	/* Elastic Plugin - http://www.unwrongest.com/projects/elastic/ */
	(function(jQuery){jQuery.fn.extend({elastic:function(){var mimics=['paddingTop','paddingRight','paddingBottom','paddingLeft','fontSize','lineHeight','fontFamily','width','fontWeight'];return this.each(function(){if(this.type!='textarea'){return false;}
	var $textarea=jQuery(this),$twin=jQuery('<div />').css({'position':'absolute','display':'none','word-wrap':'break-word'}),lineHeight=parseInt($textarea.css('line-height'),10)||parseInt($textarea.css('font-size'),'10'),minheight=parseInt($textarea.css('height'),10)||lineHeight*3,maxheight=parseInt($textarea.css('max-height'),10)||Number.MAX_VALUE,goalheight=0,i=0;if(maxheight<0){maxheight=Number.MAX_VALUE;}
	$twin.appendTo($textarea.parent());var i=mimics.length;while(i--){$twin.css(mimics[i].toString(),$textarea.css(mimics[i].toString()));}
	function setHeightAndOverflow(height,overflow){curratedHeight=Math.floor(parseInt(height,10));if($textarea.height()!=curratedHeight){$textarea.css({'height':curratedHeight+'px','overflow':overflow});}}
	function update(){var textareaContent=$textarea.val().replace(/&/g,'&amp;').replace(/  /g,'&nbsp;').replace(/<|>/g,'&gt;').replace(/\n/g,'<br />');var twinContent=$twin.html();if(textareaContent+'&nbsp;'!=twinContent){$twin.html(textareaContent+'&nbsp;');if(Math.abs($twin.height()+lineHeight-$textarea.height())>3){var goalheight=$twin.height()+lineHeight;if(goalheight>=maxheight){setHeightAndOverflow(maxheight,'auto');}else if(goalheight<=minheight){setHeightAndOverflow(minheight,'hidden');}else{setHeightAndOverflow(goalheight,'hidden');}}}}
	$textarea.css({'overflow':'hidden'});$textarea.keyup(function(){update();});$textarea.live('input paste',function(e){setTimeout(update,250);});update();});}});})(jQuery);
	
	if($('form#quick_quote').length > 0)
	{
		$('form#quick_quote textarea').elastic();
		
		$('form#quick_quote textarea').val('put your name, number, email and message in this box (it will keep growing if you\'ve lots to say!)');
		
		$('form#quick_quote textarea').bind('focus',function()
		{
			if($(this).val() == 'put your name, number, email and message in this box (it will keep growing if you\'ve lots to say!)')
			{
				$(this).val('');
			}
		});
		
		$('form#quick_quote textarea').bind('blur',function()
		{
			if($(this).val() == '')
			{
				$(this).val('put your name, number, email and message in this box (it will keep growing if you\'ve lots to say!)');
			}
		});
		
		$('form#quick_quote').bind('submit',function()
		{
			if($('form#quick_quote textarea').val() == 'put your name, number, email and message in this box (it will keep growing if you\'ve lots to say!)')
			{
				return false;
			}
		});
	}
	
	if($('form#enquiry_form').length > 0 || $('form#careers_form').length > 0)
	{
		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 (id && (field = document.getElementById(id)))
						{
		                    field.focus();
		                }
		            });
		        }
		    });
		}
		
		$('label').each(function()
		{
			if(!$(this).parent().parent().hasClass('inputgroup') && !$(this).next().hasClass('file'))
			{
				$(this).addClass('overlabel');
			}
			else if($(this).next().hasClass('file'))
			{
				$(this).parent().addClass('file');
			}
			
			$('label.overlabel').overlabel();
		});
		
		$('select option:first-child').each(function()
		{
			var parent_id = $(this).parent().attr('id');
			
			var label_text = $('label[for="'+parent_id+'"]').html();
			
			if(label_text.search('<span') !== false)
			{
				label_text = label_text.split('>');
				
				$(this).html(label_text[0]);
			}
			else
			{
				$(this).html(label_text);
			}
			
			$('label[for="'+parent_id+'"]').removeClass('overlabel-apply').hide();
		});
	};
	
	//---------------------------------------------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
							});
		}
	}
});