$(document).ready(function() {
	$('input.text').each(function() {
		$(this).val($(this).attr('title')).css('text-align','center');
	});
	
	$('input.text').blur(function() {
		if($(this).val() == '') {
			$(this).val($(this).attr('title')).css('text-align','center');
		}
	});
	
	$('input.text').focus(function() {
		if($(this).val() == $(this).attr('title')) {
			$(this).val('').css('text-align', 'left');
		}
	});
	
	$('form').submit(function() {
		$('input.text').each(function() {
			if($(this).val() == $(this).attr('title')) {
				$(this).val('').css('text-align', 'left');
			}
		});
	});	
	
	
	// about us history popup
	$('.history_popup').click(function() {
		$('#container').append('<div id="history_popup"><div id="timeline"><img src="/_images/loading.gif" alt="" /></div><a id="prev" href="#"></a><a id="next" href="#"></a><a id="close" href="#"><img src="/_images/close.png" alt="close" /></a></div>');
		$('#timeline').load("/about/popup-history.php", function() {
			$('#close').click(function() {
				$('#history_popup').remove();
				return false;
			});
			$('#next').click(function() {
				$('#timeline img').stop().animate({left:"-1130px"}, 2000, 'swing');
				return false;
			});
			$('#prev').click(function() {
				$('#timeline img').stop().animate({left:"0px"}, 2000, 'swing');
				return false;
			});
		});
		return false;
	});
	
	
	// team overlays
	$('#manuel, #david, #mario, #jaime').click(function() {
		$('#team_photos').hide();
		$('#main_image img').attr('src', '/_images/about-'+$(this).attr('id')+'.jpg');
		$('#container').remove('#team_popup').append('<div id="team_popup"></div>');
		$('#team_popup').load($(this).attr('href'), function() {
			$('#close').click(function() {
				$('#team_popup').remove();
				$('#main_image img').attr('src', '/_images/about-team.jpg');
				$('#team_photos').show();
				return false;
			});
		});
		return false;
	});
	
	
	// operations overlays
	$('#mines a, .mine_link, .silverstream_link').click(function() {
		$('#mine_info').hide();
		$('#container').append('<div id="overlay"></div><div id="mine_popup"><img src="/_images/loading.gif" alt="" /></div><a id="close_mine" href="#"><img src="/_images/close.png" alt="close" /></a>');
		$('#mine_popup').load($(this).attr('href'));
		$('#close_mine').click(function() {
			$('#overlay').remove();
			$('#mine_popup').remove();
			$('#close_mine').remove();
			$('#mine_info').show();
			return false;
		});
		return false;
	});
	
	$('#mines p').css('cursor','pointer').click(function() {
		$('#mines .selected').removeClass('selected')
		$(this).addClass('selected');
		$('.mine_link').hide();
		var type = $(this).attr('rel');
		$('.mine_link.'+type).show();
		$('#view-all').show();
	});
	
	$('#view-all').click(function() {
		$(this).hide();
		$('#mines .selected').removeClass('selected');
		$('.mine_link').show();
	});
	
	
	// environment popup
	$('#environment_popup').click(function() {
		$('#container').append('<img id="environment_popedup" src="/_images/environment-popup.png" /><a id="close_environment" href="#"><img src="/_images/close.png" alt="close" /></a>');
		$('#close_environment').click(function() {
			$(this).remove();
			$('#environment_popedup').remove();
			return false;
		});
		return false;
	});
	
	
	// social popup
	$('#social_popup').click(function() {
		$('#container').append('<div id="social_overlay"><div id="social_content"><img src="/_images/loading.gif" alt="" /></div><a id="close_social" href="#"><img src="/_images/close.png" alt="close" /></a></div>');
		loadSocial($(this));
		$('#close_social').click(function() {
			$('#social_overlay').remove();
			return false;
		});
		return false;
	});
	
	
	// performance popup
	$('.volume a').click(function() {
		$('#container').append('<div id="performance_overlay"><div id="performance_content"><img src="/_images/loading.gif" alt="" /></div><a id="close_performance" href="#"><img src="/_images/close.png" alt="close" /></a></div>');
		$('#performance_content').load($(this).attr('href'));
		$('#close_performance').click(function() {
			$('#performance_overlay').remove();
			return false;
		});
		return false;
	});

	// board popup
	$('a.board-link, .board_members a').click(function() {
		if($('#board-overlay')) { $('#board-overlay').remove(); }
		$('#container').append('<div id="board-overlay"><a id="close-board" href="#"><img src="/_images/board-close.gif" alt="close" /></a><div id="board-content"><img src="/_images/loading.gif" alt="" /></div></div>');
		$('#board-content').load($(this).attr('href'));
		$('#close-board').click(function() {
			$('#board-overlay').remove();
			return false;
		});
		return false;
	});
	
	// homepage dynamics
	if($('body').hasClass('home') || $('body').hasClass('investors')) {
		$('.date').html(dateddmmmyyyy);
		$('.time').html(positionat);
		$('.shareprice').html('<strong>FRES LSE</strong><br/>'+prices+' '+perc+'%<br/>Volume: '+dayvolume+'<br/><strong>15 minute delay</strong>');
		$('.gold').html('<strong>Gold</strong><br/>'+glapm_currentvalue+' dlls/oz.');
		$('.silver').html('<strong>Silver</strong><br/>'+slvpm_currentvalue+' dlls/oz.');
	}
	
	$('#search #query').attr('autocomplete', 'off');
	
	
	$('body.home .mines .fresnillo').hover(function() {
		$('body.home .mines img').attr('src','/_images/home-mine.jpg');
	});
	$('body.home .mines .cienega').hover(function() {
		$('body.home .mines img').attr('src','/_images/home-mine2.jpg');
	});
	$('body.home .mines .herradura').hover(function() {
		$('body.home .mines img').attr('src','/_images/home-mine3.jpg');
	});
	
	// tooltips
	$(".mine_link").tooltip({ 
	    showURL: false 
	});
	
});


function loadSocial(obj) {
	$('#social_content').load(obj.attr('href'), function() {
		$('#social_left a').click(function() {
			loadSocial($(this));
			return false;
		});
	});
}