﻿// Initialize.
function init_modal() {
	// Does element exist?
	if (!jQuery('a.modal').length) {

		// If not, exit.
		return;
	}

	// Detect IE6 (boolean).
	var jQueryIE6 = typeof document.addEventListener !== 'function' && !window.XMLHttpRequest;

	// Do some math.
	function sizeModal() {

		// Modal dimensions.
		var jQuerymodal = jQuery('#modal_window');
		var jQuerymodal_width = jQuerymodal.outerWidth();
		var jQuerymodal_height = jQuerymodal.outerHeight();
		var jQuerymodal_top = '-' + Math.floor(jQuerymodal_height / 2) + 'px';
		var jQuerymodal_left = '-' + Math.floor(jQuerymodal_width / 2) + 'px';

		// Set modal.
		jQuery('#modal_window').css('margin-top', jQuerymodal_top).css('margin-left', jQuerymodal_left);
	}

	/* For IE6. */
	function positionModal() {

		// Force modal into place.
		jQuery('#modal_wrapper').css('top', jQuery(document).scrollTop() + 'px');
	}

	// Reveal the modal.
	function showModal() {

		// Bleh.
		if (jQueryIE6) {
			positionModal();
		}

		// Unveil the wrapper.
		jQuery('#modal_wrapper').show();

		// Size it.
		sizeModal();

		// Reveal modal window.
		jQuery('#modal_window').css('visibility', 'visible').show();

		// Resize as images load.
		jQuery('#modal_content img').each(function() {
			jQuery(this).load(function() {
				jQuery(this).removeClass('modal_placeholder').show();
				sizeModal();
			});
		});
	}

	// Insert modal at end of </body>.
	jQuery('body').append('<div id="modal_wrapper"><!--[if IE 6]><iframe id="modal_iframe" frameborder="0"></iframe><![endif]--><div id="modal_overlay"></div><div id="modal_window"><div id="modal_bar"><strong>Modal window</strong><a href="#" id="modal_close">Close</a></div><div id="modal_content"></div></div>');

	// Look for modal links.
	jQuery('a.modal').each(function() {
	// orig: (ap)
	// jQuery('a.modal').click(function() {

		// Check the href="..."
		var jQuerythe_link = jQuery(this).attr('href');

		// Determine link target.
		if (jQuerythe_link.match(/^#./)) {

			// Assume #anchor content.
			jQuery('#modal_content').html(jQuery(jQuery(this).attr('href')).html());
			showModal();

		} else if (jQuerythe_link.match(/.jpgjQuery/) || jQuerythe_link.match(/.pngjQuery/) || jQuerythe_link.match(/.gifjQuery/)) {

			// Assume image content.
			jQuery('#modal_content').html('<p id="modal_image_wrapper"><img src="' + jQuerythe_link + '" class="modal_placeholder" /></p>');
			showModal();

		} else {

			// Assume external Ajax content.
			jQuery('#modal_content').load(jQuery(this).attr('href').replace('#', ' #'), '', showModal);
		}

		// Determine modal title.
		if (jQuery(this).attr('title')) {

			// Insert title.
			jQuery('#modal_bar strong').html(jQuery(this).attr('title'));

		} else if (jQuery(this).html() !== '&nbsp;') {

			// Insert link text.
			jQuery('#modal_bar strong').html(jQuery(this).html());
		}

		// Nofollow.
		this.blur();
		return false;
	});
	// orig: (ap)
	// });

	// Hide modal elements.
	jQuery('#modal_overlay, #modal_close').click(function() {

		// Hide the modal.
		jQuery('#modal_wrapper').hide();

		// Hide, because images might load later.
		jQuery('#modal_window').css('visibility', 'hidden');

		// Unbind image listeners.
		jQuery('#modal_content img').each(function() {
			jQuery(this).unbind();
		});

		// Destroy modal content.
		jQuery('#modal_content').html('');

		// Reset modal title.
		jQuery('#modal_bar strong').html('Modal window');

		// Nofollow.
		this.blur();
		return false;
	});

	// Listen for browser scroll, if IE6.
	if (jQueryIE6) {
		jQuery(window).scroll(function() {
			if (jQuery('#modal_wrapper').is(':visible')) {
				positionModal();
			}
		});
	}
}

// Kick things off.
jQuery(document).ready(function() {
	init_modal();
});
