$(function(){
	window.makeMailLinks()?makeMailLinks():null;
	window.lightbox?lightbox($('a[rel=lightbox]')):null;
	window.movieLightbox?movieLightbox($('a[rel=movielightbox]')):null;
	window.scroller?scroller('#scrollnav>li>a'):null;
	$('html').addClass('js');
	window.castAccordion?castAccordion():null;
	});

function lightbox(e){
	$(e).click(function(){
		return showLightbox($(this),e);
		});
		function showLightbox(clicked,set){
			/* find out if this image is part of a set */
			var lightboxSet = [];
			var currentLightbox = 0;
			var clickedLightbox = $(clicked);
			$(set).each(function(index){
				lightboxSet.push($(this));
				if(clickedLightbox[0] == $(this)[0]){
					currentLightbox = index;
					}
				});
			if($('#overlay').size()>0){
				$('#overlay').append('<div class="lightboxcontainer" style="display:none"><div class="close" style="display: none;" title="click anywhere to close"></div><div class="lightboxcontent" style="display:none" ></div>');
			}else{
				$('html').addClass('lightbox');
				$('body').append('<div id="overlay" style="display:none;" title="click anywhere to close"></div><div class="lightboxcontainer" style="display:none"><div class="close" style="display: none;" title="click anywhere to close"></div><div class="lightboxcontent" style="display:none;"></div></div>');
			}
			$('#overlay').height($(document).height()).show();
			$('.close,#overlay,.lightboxcontent').click(function(){
				$('.lightboxcontainer,#overlay').remove();
				$('html').removeClass('lightbox');
				});
			var img = new Image();
			img.onload = function(){
					$('.lightboxcontainer').css({
						"margin-left": "-"+parseInt(this.width)/2+'px',
						"margin-top": "-"+parseInt(this.height)/2+'px'
					});
					/* exception for position absolute (IE6) */
					if($('.lightboxcontainer').css('position')=='absolute'){
						$('.lightboxcontainer').css({"margin-top": 0,"top": (parseInt($(window).scrollTop())+30)+'px'});
						}
					$('.lightboxcontainer').show();
					$('.lightboxcontent').append('<img src="'+$(this).attr('src')+'" />').fadeIn();
					$('.close').show();
					$('#overlay').height($(document).height());
				};
			img.src=$(clicked).attr('href');
			/* add nav (nex/prev) */
			if(set.length>1){
				var next = set.length>currentLightbox+1?true:false;
				var prev = currentLightbox>0?true:false;
				$('.lightboxcontent').append('<div class="lightboxnav"></div>');
				if(next){$('.lightboxnav').append('<span class="next">→</span>');}
				if(prev){$('.lightboxnav').append('<span class="prev">←</span>');}
				$('.lightboxcontent').hover(
					function(){$('.lightboxnav').fadeIn();},
					function(){$('.lightboxnav').fadeOut();}
					);
				$('.lightboxnav .next').click(function(){removeCurrentLightbox(set[currentLightbox+1],set,'left');return false;});
				$('.lightboxnav .prev').click(function(){removeCurrentLightbox(set[currentLightbox-1],set,'right');return false;});
				}
			return false;
		}
		function removeCurrentLightbox(nextelement,set,dir){
			$('.lightboxnav').empty();
			var pos = (dir=='left'?'-2000px':'1000px');
			$('.lightboxcontainer').animate({'margin-left': pos,'opacity': 0},'slow',function(){
				$('.lightboxcontainer').empty().remove();
				showLightbox(nextelement,set);
				
				});
			
			return false;
			}
		/* listen for keypresses */
	if ($.browser.mozilla) {
		$(document).keypress(checkKey);
	} else {
		$(document).keydown(checkKey);
	}
	function checkKey(e) {
		switch (e.keyCode) {
			case 37:
				/* lightbox nav */
				if($('.prev').length>0){
					$('.lightboxnav .prev:last').click();
					}
				break;
			case 39:
				if($('.next').length>0){
					$('.lightboxnav .next:last').click();
					}
				break;
			default:
				break;
		}
	}
}



/* <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()
	{
	if (!document.getElementsByTagName && !document.createElement && !document.createTextNode)
		return;
	var spans = document.getElementsByTagName("span");
	for(var i=0;i<spans.length;i++)
		{
		if (spans[i].className=="email")
			{
			var theNode = spans[i];
			var theAddress = theNode.firstChild.nodeValue;
			
			theAddress = theAddress.replace(/ \(at\) /, "@");
			theAddressNode = document.createElement('A');
			theAddressNode.setAttribute("href", "mailto:" + theAddress);
			theAddressNode.appendChild(document.createTextNode(theAddress));
			theNode.parentNode.replaceChild(theAddressNode, theNode);
			i--; //because we implicitly removed a <span> from the spans array by making it an <a>; Otherwise it'll skip the next span
			}
		}
	}

function scroller(e){
	$(e).click(function(){
		var thisOffset = $($(this).attr('href')).offset().left;
		var thisParentOffset = $($(this).attr('href')).parent().offset().left;
		var scrollAmount = thisOffset-thisParentOffset;
		$($(this).attr('href')).parent().parent().animate({scrollLeft: scrollAmount+'px'});
		return false;
		});
	}

function movieLightbox(e){
	$(e).click(function(){
		$('body').append('<div id="overlay"></div><div class="lightboxcontainer"><div class="close"></div><div class="lightboxcontent"></div></div>');
		$('html').addClass('lightbox');
		$('#overlay').height($(document).height()).show();
		$('.close,#overlay').click(function(){
			$('#overlay,.lightboxcontainer').remove();
			$('html').removeClass('lightbox');
			});
		var width = 720+20;
		var height = 404+20;
		$('.lightboxcontainer').css({
						"width": width+'px',
						"height": height+'px',
						"margin-left": "-"+parseInt(width)/2+'px',
						"margin-top": "-"+parseInt(height)/2+'px'
					});
					/* exception for position absolute (IE6) */
					if($('.lightboxcontainer').css('position')=='absolute'){
						$('.lightboxcontainer').css({"margin-top": 0,"top": (parseInt($(window).scrollTop())+30)+'px'});
						}
		$('.lightboxcontent').load($(this).attr('href'),{'lightbox':'true'});
		return false;
		});
	}
function castAccordion(){
	$('dd.bio').each(function(){
		$(this).prev().addClass('hasbio').click(function(){
					if($(this).next().hasClass('open')){
						$('dd.bio.open').slideUp().removeClass('open');
					}else{
						$('dd.bio.open').slideUp().removeClass('open');
						$(this).next().slideDown().addClass('open');
					}
				}
			
			);
		});

	}

