/**
 * This file automatically adds a jQuery lightbox to all images which are
 * wrapped in a <div> with class="image_block".
 *
 * @author Tilman Blumenbach <tilman@ax86.net>
 * @copyright (c) 2009 Tilman Blumenbach
 * @license GNU General Public License 2 (GPL) - http://www.opensource.org/licenses/gpl-license.php
 */

jQuery.noConflict();
jQuery( function( $ )
{
	$( function()
	{
		$( '.image_block img' ).each( function()
		{
			var parent_elm = $( this ).parent( 'a' );

			if( parent_elm.length )
			{	// Check if the link points to an image
				var a_href = parent_elm.attr( 'href' );

				switch( a_href.substr( -4 ) )
				{
					case '.gif':
					case '.png':
					case '.jpg':
						// Extension is OK
						break;

					default:
						if( a_href.substr( -5 ) != '.jpeg' )
						{	// Invalid extension
							return;
						}

						// .jpeg is an allowed extension
						break;
				}
			}
			else
			{	// No link yet:
				//console.log( 'Adding link.' );
				parent_elm = $( this ).wrap( '<a></a>' ).parent();
				parent_elm.attr( 'href', $( this ).attr( 'src' ) );
			}

			if( parent_elm.attr( 'title' ) === '' )
			{	// Add a possible title from the image itself:
				parent_elm.attr( 'title', $( this ).attr( 'title' ) );
			}

			if( jq_lb_flavour == 'standard' )
			{
				parent_elm.lightBox( {
					imageLoading:  jq_lb_url + 'images/lightbox-ico-loading.gif',
					imageBtnPrev:  jq_lb_url + 'images/lightbox-btn-prev.gif',
					imageBtnNext:  jq_lb_url + 'images/lightbox-btn-next.gif',
					imageBtnClose: jq_lb_url + 'images/lightbox-btn-close.gif',
					imageBlank:    jq_lb_url + 'images/lightbox-blank.gif'
					} );
			}
			else
			{	// "balupton" version
				//console.log( 'balupton flavour chosen' );
				parent_elm.addClass( 'lightbox-enabled' )
						.attr( 'onclick', 'jQuery( this ).lightbox( { start: true, events: false } ); return false;' );
			}
		} );
	} );
} );
