var duration = 1000; // 8 seconds

//on load script
window.onload = function() {

	setTimeout( 'rotate_photo()', duration );
	$('top_header_photo2').setStyle( { opacity: 0 } );

	//preload images
	preload = new Array();
	for( var i=0; i<=number_of_photos; i++ ) {
		preload[i] = new Image();
		preload[i].src = 'images/header_photos/' + header_photos_names[i] + '.jpg';
	} //end for
	
} //end function

function rotate_photo() {

	//work out next photo number
	//do { var np = Math.floor( Math.random() * number_of_photos ); } while( np == cp ); //random
	np = cp+1; if( np >= number_of_photos ) np = 0; //incremental

	//set src
	$('top_header_photo2').setStyle( { opacity: 0 } );
	$('top_header_photo2').src = 'images/header_photos/' + header_photos_names[np] + '.jpg';

	//blend
	if( !is_ie ) $('top_header_photo2').onload = rotate_photo2;
	else rotate_photo2();

} //end function

function rotate_photo2() {

	//if in ie and not completely loaded, try again in 3 seconds
	if( is_ie && !$('top_header_photo2').complete ) {
		
		setTimeout( 'rotate_photo2()', 3000 );
		return;
		
	} //end if

	new Effect.Opacity( 'top_header_photo1', { duration: 2, from: 1, to: 0 } );
	new Effect.Opacity( 'top_header_photo2', { duration: 2, from: 0, to: 1 } );
	
	//when that is finished, swap photo 2 to photo 1
	setTimeout( 'swap_photos()', 2100 );
	
	setTimeout( 'rotate_photo()', duration + 2000 );

} //end function

function swap_photos() {
	
	//swap photo 2 to photo 1 
	$('top_header_photo1').setStyle( { opacity: 1 } );
	$('top_header_photo2').setStyle( { opacity: 0 } );
	$('top_header_photo1').src = $('top_header_photo2').src;

	cp = np;

} //end function

function show_large( elm ) {
	
	var src = 'images/thumbnails/' + elm.childNodes[0].src.replace( /^.+images\/thumbnails\/([a-z0-9\_\/]+)-thumb\.jpg$/, "$1" ) + '.jpg';
	
	var width = 600;
	var height = 300;
	var x = parseInt(screen.width  / 2) - (width / 2);
	var y = parseInt(screen.height / 2) - (height / 2);

	var win = window.open('image_viewer.html?src='+src, 'popupviewer', "top=" + y + ",left=" + x + ",scrollbars=no, minimizable=no, width=" + width + " ,height=" + height + ", resizable=yes");
	if (win == null) {
		alert("A popup blocker has stopped you opening this link.\n\nPlease disable your blocker and try again.");
		return;
	} //end if
	
}
