$(document).ready(function()
{
	
	function resizeWindow() {
		var vHeight = $(window).height();
		var vWidth = $(window).width();
		if (document.getElementById('t-content')) {
			$bgimage = $('div.bg-image:first', 'div#t-content');
			var iHeight = $bgimage.height();
			var iWidth = $bgimage.width();
			if (vHeight > iHeight) {
				$bgimage.css('bottom','0');
				$bgimage.css('top','');
			} else {
				$bgimage.css('top','50px');
				$bgimage.css('bottom','');
			}
			if (vWidth < 800) {
				$bgimage.css('right','-400px');
			} else if (vWidth < 900) {
				$bgimage.css('right','-300px');
			} else if (vWidth < 1000) {
				$bgimage.css('right','-200px');
			} else if (vWidth < 1100) {
				$bgimage.css('right','-100px');
			} else if (vWidth >= 1200){
				$bgimage.css('right','0');
			}
		}
		if (document.getElementById('t-profile')) {
			var nh = (vHeight-54);
			if ($('div.article:first', 'div#t-profile').height() + 100 < nh) {
				$('div.centre:first', 'div#t-profile').css('height', nh + 'px');
			} else {
				$('div.centre:first', 'div#t-profile').css('height', 'auto');
			}
		}
		if (document.getElementById('t-home')) {
			$bgimage = $('div.bg-image:first', 'div#t-home');
			var iHeight = $bgimage.height();
			var iWidth = $bgimage.width();
			if (vHeight > iHeight) {
				$bgimage.css('bottom','0');
				$bgimage.css('top','');
			} else {
				$bgimage.css('top','0');
				$bgimage.css('bottom','');
			}
		}
	}
	$(window).resize(function() {
		resizeWindow();
	});
	resizeWindow();	
	
	/*
	 * Fancy buttons
	 */
	
	$('.charcoal-button').wrapInner('<div></div>').addClass('cbutton');
	
	/*
	 * Gallery lightbox, and profile reflection and shadows.
	 */
	
	if (document.getElementById('t-sgallery') != null) {
		$('.gallery-photo').lightBox({
			imageLoading: '/content/themes/athertoncox/images/lightbox-loading.gif',
			imageBtnClose: '/content/themes/athertoncox/images/lightbox-close.png',
			imageBtnPrev: '/content/themes/athertoncox/images/lightbox-prev.png',
			imageBtnNext: '/content/themes/athertoncox/images/lightbox-next.png'
		});
	}
	
	if (document.getElementById('t-profile') != null)
	{
		$('.profile', '#t-profile #editable').reflect({height:0.20,opacity:0.7});
	}

	var team_urls = [];
	var team = { selected: null };
	if (document.getElementById('t-team') != null)
	{
		function selectNone()
		{
			if (team.selected) {
				team.selected.$link.removeClass('team-link-selected');
				team.selected.$head.removeClass('team-head-selected');
				team.selected = null;
			}
		}
		function onHoverTeamLink(i)
		{
			var t = team['t'+i];
			if (t == undefined || !t) return;
			if (team.selected) selectNone();
			team.selected = t;
			t.$link.addClass('team-link-selected');
			t.$head.addClass('team-head-selected');
		}
		var count = 0;
		$('li a', '#t-team #editable').each(function() {
			var $p = $(this).parent();
			var url = $(this).attr('href');
			if (url == '' || url == '#' || url == '/') return;
			if (!$.inArray(url, team_urls) > -1) {
				var i = count;
				team_urls.push(url);
				$p.attr('id', 'team-link-' + i).mouseover(function() {
					onHoverTeamLink(i);
				}).mouseleave(function() {
					selectNone();
				});
				count++;
			}
		});
		$('.headshot', '#t-team #editable').each(function() {
			var $p = $(this).parent();
			var url = $p.attr('href');
			if (url == '' || url == '#' || url == '/') return;
			var i = $.inArray(url, team_urls);
			if (i > -1) {
				$p.attr('id', 'team-head-' + i).mouseover(function() {
					onHoverTeamLink(i);
				}).mouseleave(function() {
					selectNone();
				});
				team['t'+i] = {
					index: i,
					$link: $('#team-link-'+i),
					$head: $('#team-head-'+i)
				};
			}
		});
	}
	
	var $cache = {};
	
	/*
	 * Search box interactivity
	 */
	$cache.searchFocus = false;
	$cache.searchHover = false;
	$cache.searchTimeout = false;
	$cache.criteria = $('#criteria');
	$cache.search = $('#search');
	$cache.searchform = $('#search-form');
	$cache.searchlink = $('#search-link');
	$cache.telephone = $('#telephone');
	
	function hideSearch() {
		$cache.searchHover = false;
		$cache.search.removeClass('search-hover');
		$cache.searchform.hide();
		$cache.searchlink.show();
		$cache.telephone.show();
	}
	
	$cache.criteria.focus(function()
	{
		$cache.searchFocus = true;
	}).blur(function()
	{
		$cache.searchFocus = false;
		if ($cache.searchHover) {
			hideSearch();
		}
	});
	
	$cache.search.live('mouseover', function(e)
	{
		//if ($cache.searchHover) return;
		if ($cache.searchFocus || e.target.id == 'search-link') {
			$cache.searchHover = true;
			$cache.search.addClass('search-hover');
			$cache.searchlink.hide();
			$cache.telephone.hide();
			$cache.searchform.show();
		}
	}).live('mouseout', function(e)
	{
		if (!$cache.searchFocus && $cache.searchHover && e.target.id == 'search') {
			$cache.searchHover = false;
			setTimeout(function(){
				if (!$cache.searchHover) {
					$cache.searchHover = false;
					hideSearch();
				}
			}, 250);
		}
	});
	
	$cache.criteria.addClass('placeholder').addClass('default').val('Search...');
	
	$('.placeholder').focus(function()
	{
		var $field = $(this);
		if ($field.val() == $field.attr('title')) {
			$field.removeClass('default');
			$field.val('');
		}
	}).blur(function()
	{
		var $field = $(this);
		if ($field.val() == '') {
			$field.addClass('default');
			$field.val($field.attr('title'));
		}
	});
	
});

