/* @copyright Christian Jul Jensen 2007
 * http://jul.net :: http://mocsystems.com
 *
 * imageviewer for Sandvik Folkehøgskole
 * @requires jQuery (1.2.1)
 */

jQuery.noConflict();

var pictures;
var caption;

function init() {
	jQuery('a[target=thePicture]').each(
		function() {
			this.onclick = null;
		}

	).removeAttr('onClick').click(viewIt);
	
	jQuery('.gallery').hover(
		function() {
			jQuery('.gallery-description',this).show();
		},
		function() {
			jQuery('.gallery-description',this).hide();
		}
	);
}

function viewIt() {
	if(jQuery(this).parents('.gallery').length) {
		jQuery(this).parents('.gallery').find('.gallery-pictures a').clone().modal({onOpen: fadeIn});;
		jQuery('#modalContainer a[target=thePicture]').wrapAll('<ul id="smallImages"></ul>').wrap('<li></li>');
		jQuery('#modalContainer #smallImages').before('<ul id="bigImages"/><div class="description"></div><div class="caption"></div>');
		pictures = jQuery('#modalContainer a[target=thePicture]').each(function() {
				jQuery('<li />').load(this.href+' img').appendTo('#bigImages');
		}).click(update);
		jQuery('#bigImages').css('width',600*pictures.length);
		foo = jQuery('a[target=thePicture]',jQuery(this).parents('.gallery'));
		position = jQuery.inArray(this,jQuery.makeArray(foo));
		jQuery('#bigImages').css({'left':-600*position});
		caption = jQuery(this).parents('.gallery').find('.image-caption'); 
		jQuery('#modalContainer .caption').html(caption.eq(position).html())
		jQuery('#modalContainer .description').html(jQuery(this).parents('.gallery').find('.gallery-description').html());
	} else

	if(jQuery(this).parents('.rightcol-content-image').length) {
		jQuery(this).parents('table').find('a').clone().modal({onOpen: fadeIn});
		jQuery('#modalContainer a[target=thePicture]').wrapAll('<ul id="smallImages"></ul>').wrap('<li></li>');
		jQuery('#modalContainer #smallImages').before('<ul id="bigImages"/><div class="caption"></div>');
		pictures = jQuery('#modalContainer a[target=thePicture]').each(function() {
				jQuery('<li />').load(this.href+' img').appendTo('#bigImages');
		}).click(update);
		jQuery('#bigImages').css('width',600*pictures.length);
		foo = jQuery('a[target=thePicture]',jQuery(this).parents('table'));
		position = jQuery.inArray(this,jQuery.makeArray(foo));
		jQuery('#bigImages').css({'left':-600*position});
		caption = jQuery(this).parents('.imgtext-table').find('.image-caption'); 
		jQuery('#modalContainer .caption').html(caption.eq(position).html())
	} else {
		jQuery.get(this.href,function(data){
			jQuery(data).children('img').modal({onOpen: fadeIn});
		})
	}
	return false;
}

function update() {
	position = jQuery.inArray(this,pictures);
	jQuery('#bigImages').animate({'left':-600*position});
	if(position < caption.length) {
		jQuery('#modalContainer .caption').html(caption.eq(position).html())
	} else {
		jQuery('#modalContainer .caption').empty()
	}
	return false;
}

function fadeIn(dialog) {
	dialog.overlay.fadeIn('normal', function () {
		dialog.container.fadeIn('normal', function () {
			dialog.content.fadeIn('normal');
		})
	});
}


jQuery(init);
