// This requires jQuery...

function setPopups(){
	$("a.popup").click(function(){
		var ftw=window.open(this.href, 'popup', "width=800,height=600");
		return false;

	});
	$("a.popup-square").click(function(){
		var ftw=window.open(this.href, 'popup', "width=800,height=800");
		return false;

	});
	$("a.newwin").click(function(){
		var ftw=window.open(this.href, 'newwin',"");
		console.log(this.href);
		return false;

	});
}

$(function(){
	$("a.fastload").click(function(){
		var newhref = this.href+"?contentonly=1";
		var that = this;
		$.ajax({
			url: newhref,
			success: function(html){
				$("#content").replaceWith(html);
				$(that).parent().siblings('li').css({listStyleImage: "none"});
				$(that).parent().css({listStyleImage: "url(/images/thispage.png)"});
				setPopups();
  			}
		});
		return false;
	});
	if($('#contentFlow').length){
		var numimages = $('#contentFlow').children().length;
		var divider = 100/(numimages-1);
		$('#contentFlow').jcoverflip({
			current: Math.round(numimages/2),
			change: function(event, ui){
				$('#scrollbar').slider('value', ui.to*divider);
			},
			beforeCss: function( el, container, offset ){
				return [
					$.jcoverflip.animationElement( el, { left: ( container.width( )/2 - 340 - 175*offset + 20*offset )+'px', bottom: '20px' }, { } ),
					$.jcoverflip.animationElement( el.find( 'img' ), { width: Math.max(10,165-20*offset*offset) + 'px' }, {} )
				];
			},
			afterCss: function( el, container, offset ){
				return [
					$.jcoverflip.animationElement( el, { left: ( container.width( )/2 + 175 + 175*offset )+'px', bottom: '20px' }, { } ),
					$.jcoverflip.animationElement( el.find( 'img' ), { width: Math.max(10,165-20*offset*offset) + 'px' }, {} )
				];
			},
			currentCss: function( el, container ){
				return [
					$.jcoverflip.animationElement( el, { left: ( container.width( )/2 - 165 )+'px', bottom: 0 }, { } ),
					$.jcoverflip.animationElement( el.find( 'img' ), { width: '330px' }, { } )
				];
			}
		
		});
		$('#scrollbar').slider({
			value: Math.round(numimages/2)*divider,
			stop: function(event, ui) {
				if(event.originalEvent) {
					var newVal = Math.round(ui.value/divider);
					jQuery('#contentFlow' ).jcoverflip( 'current', newVal );
					jQuery('#scrollbar').slider('value', newVal*divider);
				}
			}
		});
	}

/*
	$('.scrollable-links a, .scrollable-link').click(function(event){
		$.scrollTo($(this).attr('href'), {duration:2000, easing: 'swing'});
		event.stopPropagation();
	});

	var photocount = $(".slide").size();
	var currentphoto = 1;
*/

/*
	$("#next-image").click(function(event){
		currentphoto++;
		if(currentphoto > photocount){
			currentphoto = 1;
		}
				
		$(this).attr('href', "#photo-"+currentphoto);
		$.scrollTo($(this).attr('href'), {duration:2000, easing: 'swing'});
		event.stopPropagation();
	});
*/
	
/*
	$("#slideshow-container").addClass("onready");
	var photocount = $(".slide").size();
	$("#photo-container").width(photocount*604+1000);
	
	$("#next-image").click(function(){
		
		var offset = parseInt($("#photo-container").css("left"));
		var currentphoto = -offset/604+1;
		var nextphoto    = 1;
		var left = 0;
		if(currentphoto != photocount){
			left = currentphoto*604;
			nextphoto    = currentphoto+1;
		}
		// Because IE is stupid.
		$("#photo-"+currentphoto).css({"visibility":"hidden"});
		$("#photo-container").css({"left":-left});
		$("#photo-"+nextphoto).css({"visibility":"visible"});
		return false;
	
	});
*/
	setPopups();
});
