jQuery(document).ready(function() {

	/* apply slashes
	---------------------------------------------- */	
	var slash_wrapper = '<span class="slash_wrapper"></span>';
	var slash = '<span>/</span>';
	var elements_with_slash;
	
	elements_with_slash = jQuery( '.home #header .menu li' );	
	elements_with_slash.wrapInner( slash_wrapper );
	elements_with_slash.children( 'span' ).append( slash );
	
	elements_with_slash = jQuery( '#rollover-nav div' ).last().find( 'li' );
	elements_with_slash.wrapInner( slash_wrapper );
	elements_with_slash.children( 'span' ).append( slash );
		
	elements_with_slash = jQuery('#breadcrumbs h2 + h2').prev();
	elements_with_slash.wrapInner( slash_wrapper );
	elements_with_slash.children( 'span' ).append( slash );
	
	/* image handling
	---------------------------------------------- */
	
	/* resize the current featured image to fit within the container div, #featured */
	var fit_featured_image = function() {
				
		var currentImg = new Image();
		currentImg.onload = function() {
						
			// define a selector
			var current_img = '.current';
			if( jQuery('.current img').length ) {
				current_img = '.current img';
			}

			// compute scale factor			
			var scale_width = jQuery('#featured').width() / currentImg.width;
			var scale_height = jQuery('#featured').height() / currentImg.height;
			var scale_factor = scale_width;
			if ( scale_width > scale_height ) {
				scale_factor = scale_height;
			}
			
			// if the image doesn't need to be scaled
			if( scale_factor >= 1 ) {
				scale_factor = 1;
				
				// check if it has been scaled up
				if( jQuery( current_img ).hasClass( 'zoomed-out' ) ) {
				
					jQuery('.current img').removeClass('zoomed-out');
					jQuery('.current img').addClass('zoomed-in');	
				}
				
			// if the image does need to be scaled
			} else {
				if( scale_factor < 1.0 ) {
					jQuery( current_img ).addClass( 'zoomed-out' );
				}
			}
			
			// remove zoom cursor states, and zoom click handler from featured image on homepage
			jQuery( '.home #featured img').removeClass('zoomed-in');
			jQuery( '.home #featured img').removeClass('zoomed-out');
			jQuery( '.home #featured img').unbind('click');

			// scale the current image
			if( jQuery('.current img').length ) {
				jQuery('.current img').attr('width', currentImg.width * scale_factor);
				jQuery('.current img').attr('height', currentImg.height * scale_factor);
			} else {
				jQuery('.current').attr('width', currentImg.width * scale_factor);
				jQuery('.current').attr('height', currentImg.height * scale_factor);
			}
			
			// reset the #featured div height to auto
			jQuery('#featured').css('height', 'auto');
		};
		if( jQuery('.current img').length ) {
			currentImg.src = jQuery('.current img').attr('src');
		} else {
			currentImg.src = jQuery('.current').attr('src');
		}
	};
	
	/* utility function to resize container div, #featured */
	var resize_featured = function(width, height) {
		
		jQuery('#featured').css('width', width);
		jQuery('#featured').css('height', height);
		
		if( jQuery('#featured .current iframe').length ) {
			jQuery('#featured').css('height', 'auto');
		} else if( jQuery('#featured .current object').length ) {
			jQuery('#featured').css('height', 'auto');
		} else {
			/* after resizing, fit the current featured image to fit inside */
			fit_featured_image();
		}
	};
	
	/* resize the container div, #featured, to fit inside the window */
	var fit_featured = function() {
		// jQuery('#featured').hide();
		resize_featured(
			jQuery(window).width() - jQuery('#header').width() - 100,
			jQuery(window).height() - 120
		);
		// jQuery('#featured').fadeIn(100);
	};
	
	/* resize the #wrapper div to fit around #featured */
	var resize_wrapper = function() {
		jQuery('.home #wrapper').css('min-width', function() {
			var width = jQuery('#header').width() + jQuery('#featured').width() + 18;
			return width;
		});
		jQuery('.single #wrapper').css('min-width', function() {
			var width = jQuery('#header').width() + jQuery('#featured').width() + 18;
			return width;
		});
	};
	
	var show_default = function() {
		jQuery('#featured .default').addClass('current');
		jQuery('#featured').hide();
		fit_featured();
		jQuery('#featured').fadeIn(100);
		resize_wrapper();
	};

	/* re-position embedded videos into .default div (replacing featured image)
	---------------------------------------------- */
	jQuery(window).load(function() {
	
		if( jQuery('#caption iframe').length || jQuery('#caption object').length ) {
			jQuery('.default').html('');
			jQuery('.default').append( jQuery('iframe').detach() );
			jQuery('.default').append( jQuery('object').detach() );
		}
		if( jQuery('#featured').length ) {
			show_default();
		}
	});
	
	/* activate details
	---------------------------------------------- */
	/* if there are linked images in the caption, loop through them */
	if( jQuery('#caption a img').length ) {
		jQuery('#caption a img').each(function() {

			/* if this is not a link to an attachment page */
			if( jQuery(this).parent().attr('rel').indexOf('attachment') == -1 ) {

				/* grey out any images that link to the already visible featured image */
				if( jQuery(this).parent().attr('href') === jQuery('#featured .default img').attr('src') ) {
					if( !( jQuery('#caption iframe').length || jQuery('#caption object').length )) {
						jQuery(this).parent().addClass('selected');				
					}
				}

				/* preload linked images, and append to #featured */
				var detailImg = new Image();
				detailImg.onload = function()
				{
					jQuery('#featured').append('<img src="' + this.src + '">');
				};
				detailImg.src = jQuery(this).parent().attr('href');
				
				jQuery(this).click(function() {

					var detailSrc = jQuery(this).parent().attr('href');
					
					if( jQuery(this).parent().hasClass('selected') ) {
						jQuery('#caption a').removeClass('selected');
					} else {
						jQuery('#caption a').removeClass('selected');
						jQuery(this).parent().addClass('selected');						
					}
					
					jQuery('#featured img').each(function() {
											
						if( jQuery(this).attr('src') == detailSrc ) {
							
							/* if this is already selected */
							if( jQuery(this).hasClass('current') ) {
								jQuery('.current').removeClass('current');
								jQuery('.default').addClass('current');
							} else {
								jQuery('.current').removeClass('current');
								jQuery(this).addClass('current');
							}
							jQuery('#featured').hide();
							fit_featured();
							jQuery('#featured').fadeIn(100);

						}
					});
					
					return false;
				});	
			}
		});
	}
	jQuery(window).load(function() {
			
		// for each of the images in the #featured div
		jQuery('#featured img').each(function() {
			
			// add the zoom on click
			jQuery(this).click(function() {
						
				if( jQuery(this).hasClass('zoomed-in') ) {

					// if already zoomed out, zoom out by fitting #featured to the window
					fit_featured();
					resize_wrapper();
					
					jQuery(this).removeClass('zoomed-in');
					jQuery(this).addClass('zoomed-out');
					
				} else if( jQuery(this).hasClass('zoomed-out') ) {
									
					// if not, calculate the max zoom, and resize
					var zoomImg = new Image();
					zoomImg.onload = function() {
						resize_featured(
							zoomImg.width,
							zoomImg.height
						);
						resize_wrapper();
					};
					if( jQuery('.current img').length ) {
						zoomImg.src = jQuery('.current img').attr('src');
					} else {
						zoomImg.src = jQuery('.current').attr('src');
					}

					jQuery(this).addClass('zoomed-in');
					jQuery(this).removeClass('zoomed-out');
					
				}
			});
		});
		
		/* remove title attributes to prevent native rollover from text links
		---------------------------------------------- */
		jQuery('#header a').removeAttr('title');			
		jQuery('#footer a').removeAttr('title');
		jQuery('#featured img').removeAttr('title');
		jQuery('.thumbnail img').removeAttr('title');
		jQuery('#caption img').removeAttr('title');
		jQuery('.article-controls a').removeAttr('title');
		jQuery('.excerpt img').removeAttr('title');
	});

	/* add tooltips to thumbnails and system buttons
	---------------------------------------------- */
	jQuery('.thumbnail a img').each(function() {
		jQuery(this).parent().after('<span class="tooltip-label">' + jQuery(this).attr('title') + '</div>');
	});
	jQuery('.thumbnail').hoverIntent(function() {
		jQuery('.tooltip-label').hide();
		jQuery(this).children('.tooltip-label').show();
	}, function() {
		jQuery(this).children('.tooltip-label').hide();
	});
	jQuery('.tooltip-label').click(function() {
		window.location = jQuery(this).prev().attr('href');
	});
	jQuery('#controls a[title]').tooltip({
		tipClass: 'tooltip-system',
		predelay: 100,
		position: 'bottom left',
		offset: [-4, 4]
	});
	jQuery('.article-controls a[title]').tooltip({
		tipClass: 'tooltip-system',
		predelay: 100,
		position: 'bottom left',
		offset: [-4, 4]
	});
	jQuery('h3.subcategory a[title]').tooltip({
		tipClass: 'tooltip-system',
		predelay: 100,
		position: 'bottom left',
		offset: [-4, 4]
	});
	
	/* remove borders from linked images & pictograms
	---------------------------------------------- */
	jQuery('a img').parent().css('border', 'none');
	jQuery('.article a .flatfile-normal').parent().css('border', 'none');
	
	/* activate expanding descriptions
	---------------------------------------------- */
	jQuery('.description p').hide();
	jQuery('.description a').click(function() {
		jQuery(this).toggleClass('highlight');
		jQuery('#' + jQuery(this).attr('id') + '-description p').toggle();
	});
	
	/* overlay navigation
	---------------------------------------------- */
	jQuery( '#header h1' ).first().click(function() {
		jQuery( '#header h1' ).first().css( 'visibility', 'hidden' );
		jQuery( '#rollover-nav' ).show();
		jQuery( '#dim' ).show();
		return false;
	}, function() {});
	jQuery( '#dim' ).click(function() {
		jQuery( '#rollover-nav' ).hide();
		jQuery( '#dim' ).hide();
		jQuery( '#header h1' ).first().css( 'visibility', 'visible' );
	}, function() {});
	jQuery( '.home #header h1' ).unbind( 'click' );	// turn off rollover nav on homepage
	
});
