$(document).ready(function(){
	fader('#fader','../../../xml/fader.lasso');
	window.lightbox?lightbox($('a[rel=lightbox]')):null;
	});
jQuery.ajaxSetup({cache:false});

function fader(element,source){
	if($(element).size()>0){
		if($(element).children().size()==0){
			$(element).append('<p>loading...</p>');
			$(element).load(source,function(){
				$(this).children().fadeIn();
			});
		}
		$(element).children().hide();
		var fader = window.setInterval(function(){
			fadeThis(element,source)
			},30000);
		}
	}
function fadeThis(element,source){
	/* fade out what's in #fader, load new content, then fade #fader back in */
	$(element).children().fadeOut(1000,function(){
		$(this).parent().load(source,function(){
			$(this).fadeIn();
			});
		});
	}

function lightbox(e){
	
	$(e).click(function(){
		return showLightbox($(this),e);
		});
		function showLightbox(clicked,set){
			var lightboxBorder = 26;
			/* 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{
				$('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();
				});
			var img = new Image();
			img.onload = function(){
					$('.lightboxcontainer').css({
						"margin-left": "-"+((parseInt(this.width)/2)+lightboxBorder)+'px',
						"margin-top": "-"+((parseInt(this.height)/2)+lightboxBorder)+'px'
					});
					/* exception for position absolute (IE6) */
					if($('.lightboxcontainer').css('position')=='absolute'){
						$('.lightboxcontainer').css({"margin-top": 0,"top": (parseInt($(window).scrollTop())+lightboxBorder)+'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;
		}
	}
}

