$('head').append( '<link rel="stylesheet" type="text/css" href="css/screen_photos.css" media="screen" />' );
$('head').append( '<link rel="stylesheet" type="text/css" href="css/print_photos.css" media="print" />' );

var currentPosition;
var showDone = false;
var popDone = false;
var popOverlay, popFrame, frameHeight, frameWidth, id, borders, initialFrameHeight, showFrame, longTitle, titleSpace;
// var backgroundOpacity = ( navigator.appVersion.indexOf( "MSIE 6" ) > 0 ) ? 1 : 1 ;

var backOn = "<a onmouseover=\"$( 'img#back' ).attr ( 'src', 'img/back_over.png' )\" onmouseout=\"$( 'img#back' ).attr ( 'src', 'img/back_off.png' )\" onclick=\"back_thumb()\"><img id=\"back\" src=\"img/back_off.png\" alt=\"\" /></a>"
var backDim = "<img src=\"img/back_dim.png\" alt=\"\" />"
var nextOn = "<a onmouseover=\"$( 'img#next' ).attr ( 'src', 'img/next_over.png' )\" onmouseout=\"$( 'img#next' ).attr ( 'src', 'img/next_off.png' )\" onclick=\"next_thumb()\"><img id=\"next\" src=\"img/next_off.png\" alt=\"\" /></a>"
var nextDim = "<img src=\"img/next_dim.png\" alt=\"\" />"

// Code to manipulate thumbnail filmstrip

$( document ).ready ( function () {
	imageCount = $( "li.thumbCell" ).length;
	if ( imageCount > 5 ) {
		currentPosition = 0;
		$( "div#nextButton" ).html ( nextOn ).css ( "cursor", "pointer" );
	}
});

function next_thumb () {
	if ( currentPosition < 1 ) $( "div#backButton" ).html ( backOn ).css ( "cursor", "pointer" );
	currentPosition += 5;
	currentPosition = ( currentPosition < imageCount -5 ) ? currentPosition : imageCount -5;
	$( "div#thumbHolder" ).animate ({ "left": ( -136 * currentPosition ) + "px" }, 1000 );
	if ( currentPosition > imageCount - 6 ) $( "div#nextButton" ).html ( nextDim ).css ( "cursor", "none" );
}

function back_thumb () {
	if ( currentPosition > imageCount - 6 ) $( "div#nextButton" ).html ( nextOn ).css ( "cursor", "pointer" );
	currentPosition -= 5;
	currentPosition = ( currentPosition > 0 ) ? currentPosition : 0;
	$( "div#thumbHolder" ).animate ({ "left": ( -136 * currentPosition ) + "px" }, 1000 );
	if ( currentPosition < 1 ) $( "div#backButton" ).html ( backDim ).css ( "cursor", "none" );
}

// Code to display pop-up photograph details

$( document ).ready ( function () {
	$( "a.pop" ).click ( function () {
		if ( showDone ) return false;
		showFrame = "div#id_" +  $( "a.pop" ).attr ( "rel" );
		show_frame ();
		titleSpace = $( "span#pictureTitle");
		longTitle = $( titleSpace ).html ();
		$( titleSpace ).html ( "&#160;" );
		return false;
	});
});

function show_frame () {
	if ( !showDone ) {
 		$( showFrame ).children ( "div" ).click ( hide_frame );
 		$( showFrame ).children ( "p" ).css ( "margin", "10px 15px" );
		$( window ).resize ( show_position ).scroll ( show_position );
		showDone = true;
	}
	show_position ();
	$( showFrame ).css ({ display: "none", opacity: 1 });
	$( showFrame ).slideDown ( "fast" );
}

function hide_frame () {
	$( "div#id_" +  $( "a.pop" ).attr ( "rel" )).slideUp ( "fast" );
	$( titleSpace ).html ( longTitle );
	showDone = false;
}

// Code to switch which large picture is displayed

function show_position () {
	showImage = $( "a.pop" ).children ( "img" );
	showImageWidth = showImage.width ();
	showImageHeight = showImage.height ();
	showImageOffset = showImage.offset ();
	showBorders = parseInt ( $( showImage ).css ( "border-top-width" ));
	showPadding = parseInt ( $( "div.frame" ).css ( "padding-top" ));
	showOffset = showBorders - showPadding;
	$( showFrame ).css (
		{
			width: ( 2 * showOffset + showImageWidth ) + "px",
			height: ( 2 * showOffset + showImageHeight ) + "px",
			left: showImageOffset['left'] + "px",
			top: showImageOffset['top'] + "px"
		}
	);
}

function switch_picture ( type, photographerID, imageID, matte, offset, title, cvStatus, name, origin ) {
	if ( showDone ) hide_frame ();
	fullPath = ( type == "p" ) ? "/pages/photos/" + photographerID + "_" + imageID + ".jpg" : "/pages/books/" + imageID + ".jpg" ;
	popImage = $( "a.pop" ).children ( "img" );
	$( popImage ).attr ( "src", fullPath );
	$( popImage ).css ( "border-color", matte );
	$( "a.pop" ).attr ( "rel", offset );
	$( "#pictureTitle" ).html ( title );
	$( "#picturePhotographer" ).attr ( "photographer", photographerID );
	cvCode = ( cvStatus ) ? '<a href="#" onclick="showCV ()" title="Read biographical information about ' + name + '">' + name + '</a>' : name ;
	$( "#picturePhotographer" ).html ( cvCode );
	( origin != "" ) ? $( "#pictureOrigin" ).html ( origin ) : $( "#pictureOrigin" ).html ( "" );
	( cvStatus ) ? $( "#cvInfoText" ).html ( " Click on the photographer&rsquo;s name to read additional biographical information." ) : $( "#cvInfoText" ).html ( "" ) ;
	popDone = ( cvStatus ) ? false : true ;
}

// Code to save an inquiry made from the pop-up details panel

function save_inquiry ( inventoryNumber, count ) {
	id = "#nav_" + count;
	$( id ).html ( "<img src=\"img/bullet.gif\" alt=\"\" />&#160;&#160;Photo <b>Added</b> to Inquiry List</a>&#160;&#160;<img src=\"img/bullet.gif\" alt=\"\" />" );
	show_button ();
	xajax_addToInquiry ( inventoryNumber );
}

function see_book ( bookID ) {
	location.replace ( "index.php?pg=book&id=" + bookID );
}

// Code to display pop-up biography

$( document ).ready ( function () {
	photographerID = $( "#picturePhotographer" ).attr ( "title" );
	name = $( "#picturePhotographer" ).html ();
	cvCode = ( $( "#cv_" + photographerID ).attr ( "class" ) == "details" ) ? '<a href="#" onclick="showCV ()" title="Read biographical information about ' + name + '">' + name + '</a>' : name ;
	$( "#picturePhotographer" ).html ( cvCode );
	$( "#picturePhotographer" ).attr ( "photographer", photographerID );
	$( "#picturePhotographer" ).removeAttr ( "title" );
});

function showCV () {
	if ( popDone ) return false;
	if ( showDone ) hide_frame ();
	popOverlay = $( "div#overlay" );
	popFrame = $( "#cv_" + $( "#picturePhotographer" ).attr ( "photographer" ));
	borders = parseInt ( popFrame.css( "borderTopWidth" )) * 2;
	initialFrameHeight = parseInt ( $( popFrame ).height ()) + borders;
	pop_show ();
	return false;
}

function pop_show () {
	if ( !popDone ) {
 		$( popFrame.children ( "div" )).click ( pop_hide );
		$( window ).resize ( pop_position ).scroll ( pop_position );
		popDone = true;
	}
  	var pageHeight = $( window ).height () + $( window ).scrollTop ();
 	$( popOverlay ).css ({
 			height: pageHeight + "px",
 			width: $( window ).width () + "px"
 		}).fadeIn ( "fast", function () {
		pop_position();
		$( popFrame ).css({ display: "none", opacity: 1 });
		$( popFrame ).slideDown ( "fast" );
	});
}

function pop_hide() {
	$( popFrame ).slideUp ( "fast", function () {
		$( popOverlay ).fadeOut ( "fast" );
		popDone = false;
	});
	return false;
}

function pop_position() {
	var windowWidth = $( window ).width ();
	var windowHeight = $( window ).height ();
	var scrollHeight = $( window ).scrollTop ();
	var frameWidth = parseInt ( popFrame.css( "width" )) + borders;
	if ( initialFrameHeight > windowHeight - 20 ) {
		frameHeight = windowHeight - 20;
		$( popFrame ).css ({ height: ( frameHeight - borders - 5 ) + "px", overflow: "auto" });
	} else {
		frameHeight = initialFrameHeight;
		$( popFrame ).css ({ height: ( frameHeight - borders ) + "px", overflow: "hidden" });
	}
	$( popFrame ).css ({ left: (( windowWidth - frameWidth ) / 2 ) + "px", top: ((( windowHeight - frameHeight ) / 2 ) + scrollHeight ) + "px" });
	$( popOverlay ).css ( "height", ( windowHeight + scrollHeight ) + "px" );
}

