// Image cache
var img_loading = new Image();
img_loading.src = '/images/loading.gif';

function update_links(container)
{
	$(container + ' a').each(function(){
		var href = this.href;
		if (href.substr(0, 1) != '#')
		{
			$(this).click(function(){
				var href = $(this).attr('href');
				var target = $(this).attr('class');
				
				if ((typeof(target) == 'undefined') || (target == ''))
				{
					target = '#content';
				}
				
				if ($(this).hasClass('_blank'))
				{
					window.open(href);
				}
				else if ($(this).hasClass('_image'))
				{
					$('#image_content').html('<table id="loading_anim"><tr><td><img src="/images/loading.gif" alt="Loading..."></td></tr></table>');
					$('#image_viewer').show();
					
					$('#image_content').append('<img id="viewer_image" src="' + href + '" alt="Image" style="display: none;">');
					$('#viewer_image').load(function(){
						$('#loading_anim').hide();
						$('#viewer_image').show();
						
						var img_width = $('#viewer_image').width();
						var img_height = $('#viewer_image').height();
						var vw_width = $('#image_viewer').innerWidth();
						var vw_height = $('#image_viewer').innerHeight();
						
						if (img_width < vw_width)
						{
							var left = Math.floor((vw_width - img_width) / 2);
							$('#viewer_image').css('margin-left', left + 'px');
						}
						
						if (img_height < vw_height)
						{
							var top = Math.floor((vw_height - img_height) / 2);
							$('#viewer_image').css('margin-top', top + 'px');
						}
						
						$('#image_viewer').scrollTop(0);
					});
				}
				else if (target == '_top')
				{
					location.href = href;
				}
				else
				{
					$('#loading_name').html($(this).html());
					scroll(0, 0);
					$.get(
						href,
						{},
						function(result)
						{
							$(target).html(result);
							update_links(target);
							if (href.substr(href.length - 15) == 'contact_us.html')
							{
								$('#from').focus();
								$('#mail_form').submit(function(){
									$.post(
										'send_mail.php',
										$('#mail_form').serialize(),
										function(result)
										{
											alert(result);
										}
									);
									return false;
								});
							}
						}
					);
				}
				return false;
			});
		}
	});
	
	$(container + ' a img').each(function(){
		$(this).attr('border', '0');
	});
	
	$(container + ' div.captioned_img').each(function(){
		$(this).height($(this).height() + 14);
	});
	
	$(container + ' div.captioned_img a').each(function(){
		var imgs = $(this).children('img');
		var caption = '';
		for (var i = 0; i < imgs.length; i++)
		{
			caption += (caption.length == 0 ? '' : ', ') + $(imgs[i]).attr('alt');
		}
		$(this).parent().append(caption);
	});
}

$(document).ready(function(){
	var bg_images = [
		'bg_bridge.jpg',
		'bg_engine300.jpg',
		'bg_riverwalk.jpg',
		'bg_banderacourthouse.jpg'
	];
	var img_idx = parseInt(Math.random() * 4);
	$('div#content').css('background-image', 'url(images/' + bg_images[img_idx] + ')');
	
	$('#loading').ajaxStart(function(){
		$(this).show();
	}).ajaxStop(function(){
		$(this).hide();
	}).hide();
	
	$('#image_new_window').click(function(){
		var img = $('#image_content').children('img');
		window.open($(img[0]).attr('src'));
		return false;
	});
	
	$('#image_close').click(function(){
		$('#image_viewer').hide();
		return false;
	});

	$.get(
		'menu.html',
		{},
		function(result)
		{
			$('#menu_loc').html(result);
			update_links('#menu_loc');
			$('a.head').click(function(){
				$($(this).next().children('li').children('a'))
					.fadeOut()
					.fadeIn();
				return false;
			});
			$('#menu a[href="news.html"]').click();
		}
	);
});

