var slideshow;
$(document).ready(function() {
	slideShow('#photo');
	lightbox('.lightbox');
	window.makeMailLinks()?makeMailLinks():null;
	emptyInput($('#search input, #login input'));
	showLoginbox('#login h2');
});

function slideShow(element) {
	var e = $(element);
	e.children('img').not(':last').hide();
	if(e.length>0){
		slideshow = setInterval(function() {
			if(e.children('img:visible:eq(0)').prev().size()>0) {
				e.children('img:visible:eq(0)').prev().show(function(){
					e.children('img:visible:eq(1)').fadeOut(2000);
					});
			}
			else {
				e.children('img:last').fadeIn(2000, function() {
					e.children('img:visible').not(':last').hide();
				});
			}
		}, 6000);
	}
}
function lightbox(e){
	$(e).click(function(){
		clearInterval(slideshow); //stop slideshow
		$('body').append('<div id="overlay"></div><div id="lightboxcontainer"><div id="close"></div><div id="lightboxcontent"></div></div>');
		$('#overlay').height($(document).height());
		$('#close,#overlay').click(function(){
			$('#overlay,#lightboxcontainer').remove();
			slideShow('#photo'); //restart slideshow
			});
		$('#lightboxcontent').load($(this).attr('href'),{'lightbox':'true'});
		return false;
		});
	}
	
/* <span class="email">x (at) x.com</span> becomes <a href="x@x.com">x@x.com</a> 
* this is an anti-spam measure
*/
function makeMailLinks(){
		$('span.email').each(function(){
			var theAddress = $(this).text().replace(/ \(at\) /, "@");
			$(this).replaceWith($('<a href="mailto:'+theAddress+'">'+theAddress+'</a>'));
			});
	}

function emptyInput(element){
	element.each(function(){
		$(this).data('val',$(this).val());
		});
	element.focus(function(){
		defaultVal = $(this).val();
		if(defaultVal==$(this).data('val')){
			$(this).val('');
			}
	});
	
	element.focusout(function(){
		defaultVal = $(this).data('val');
		if($(this).val()==''){
			$(this).val(defaultVal);
		}
	});	
}

function showLoginbox(e){

	$(e).toggle(function(){
		$(this).parent().addClass('show');
		$(this).next().hide().slideDown(200);
		},function(){
		$(this).next().slideUp(200,function(){
			$(this).parent().removeClass('show');
			});
		
		});
	}

