(function($) {
	// hide "View more" container
	$('.showMore').each(function() {
		$(this).hide()
	})
	
	/* show "View more" container */
	var showMore = function(a) {
		a.next().slideToggle();
		a.text( ( a.text() == 'View more' ) ? 'View less' : 'View more')
	}
	
	// on load
	if( h = window.location.hash ) {
		h = decodeURI( h.substr(1,h.length) )
		$('.showMoreLink').each(function(){
			console.log( h + ' | ' + $(this).attr('title') )
			if( $(this).attr('title') == h ) {
				showMore($(this))
			}
		})
	}
	
	// on click
	$('.showMoreLink').click(function(e){
		e.preventDefault()
		showMore($(this))

	});


	// initialize the slider
	$('#slideContainer')
		.prepend('<div id="navPrev">')
		//.append('<div class="clearfix" id="slideNavigation">')
		.append('<div id="navNext">')
		.children('a')
			.wrapAll('<div id="slides" />')
			.parent('#slides')
			.cycle({
				fx: 'fade',
				speed: 300,
				timeout: 0,
				//pager: '#slideNavigation',
				next: '#navPrev',
				prev: '#navNext',
				before: function(currSlideElement, nextSlideElement, options, forwardFlag) {
					sliderCaption( $(this) );
				}
			});


	// creates the markup for the image's caption
	$('.dm_widget_inner:not(.noCaption) #slideContainer')
			.append('<dl class="clearfix" id="slideCaption"><dt class="title">Title</dt><dd class="title"></dd><dt class="size">Size</dt><dd class="size"></dd><dt class="year">Year</dt><dd class="year"></dd><dt class="medium">Medium</dt><dd class="medium"></dd></dl>');

	// show the first slide's caption
	sliderCaption( $('#slideContainer').find('img:first') );

	// shows and updates the image's caption
	function sliderCaption($currSlideElement) {
		var data = {
				artist:	$currSlideElement.attr('data-artist') || '&nbsp;',
				title:	$currSlideElement.attr('data-title') || '&nbsp;',
				size:	$currSlideElement.attr('data-size') || '&nbsp;',
				year:	$currSlideElement.attr('data-year') || '&nbsp;',
				medium:	$currSlideElement.attr('data-medium') || '&nbsp;'
			};

		$('#slideContainer dl').show()
					.find('dd.title').html( data.title )
			.parent().find('dd.size').html( data.size )
			.parent().find('dd.year').html( data.year )
			.parent().find('dd.medium').html( data.medium )
		;
	}


	// initialize the fancybox
	$('.fancybox').fancybox({
		'overlayOpacity' : 1,
		'titleFormat'	 : function(title) {
				    return title;
			},
		'titleShow': true,
		'overlayColor': '#fff',
		'speedIn': 600,
		'speedOut': 200,
		'transitionIn': 'elastic',
		'transitionOut': 'elastic',
		'width': 853,
		'height': 510
	});
	
})(jQuery);
