var shadowGallery = {version: "0.1"}; // a combination of Shadowbox and Galleria

shadowGallery.inited = false;

shadowGallery.init = function() {
	Shadowbox.init({
		skipSetup: true,
		overlayOpacity: 0.9,
		continuous: true,
		animate: true,
		resizeDuration: 0.35, // affects animation resizing speed (only if animate is true)
		fadeDuration: 0.35, // affects fade speed (only if animate is true)
		onChange: shadowGallery.onChange, // allows us to add behavior to the new changed slide
		slideshowDelay: 3 // remove or set to 0 for no slideshow autoplay
	});
	shadowGallery.inited = true;
}

shadowGallery.addSlide = function(title, content, galleryName) {
	slidenum = 0;
	for (var key in Shadowbox.cache) {
		slidenum++;
	}
	Shadowbox.cache[slidenum] =
	{
		player: "img",
		title:		title,
		content:	content,
		gallery: 	galleryName
	};	
}

shadowGallery.showSlide = function(title, content, galleryName) {
	var image = {
			player:     "img",
			title:      title,
			content:    "http://www.cops4causes.org" + content, // needs full URL for some reason to determine current slide
			gallery:	galleryName
	};
	Shadowbox.open(image);		
}

shadowGallery.onChange = function(obj) {
	$('#sb-body').click(function(){
		Shadowbox.pause();
		Shadowbox.next();							 
	});	
}

shadowGallery.start = function(galleryName) {
	
	if (! shadowGallery.inited) {
		shadowGallery.init();
	}

	$('#' + galleryName).addClass('shadowGallery'); // adds new class name to maintain degradability
	
	// find all images and add them to Shadowbox; this MUST happen before the galleria call below
	$('ul#' + galleryName + ' li img').each(function(i,o) {
		shadowGallery.addSlide(o.title, o.src, galleryName);
	});
	
	// attach galleria functionality (always do AFTER initing Shadowbox)
	$('ul#' + galleryName).galleria(
	{
		history   : false, // activates the history object for bookmarking, back-button etc.
		clickNext : true, // helper for making the image clickable
		insert    : '#shadowGalleryImage', // the containing selector for our main image
		onImage   : function(image,caption,thumb) { // let's add some image effects for demonstration purposes
			
			image.attr('style', 'max-width: 462px; max-height: 350px;');
			
			// fade in the image & caption
			if(! ($.browser.mozilla && navigator.appVersion.indexOf("Win")!=-1) ) { // FF/Win fades large images terribly slow
				image.css('display','none').fadeIn(1000);
			}
			caption.css('display','none').fadeIn(1000);
			
			// fetch the thumbnail container
			var _li = thumb.parents('li');
			
			// fade out inactive thumbnail
			_li.siblings().children('img.selected').fadeTo(500,0.8);
			
			// fade in active thumbnail
			thumb.fadeTo('fast',1).addClass('selected');
			
			// add a title for the clickable image
			image.attr('title','Next image >>');
		},
		onThumb : function(thumb) { // thumbnail effects goes here
			
			// fetch the thumbnail container
			var _li = thumb.parents('li');
			
			// if thumbnail is active, fade all the way.
			var _fadeTo = _li.is('.active') ? '1' : '0.8';
			
			// fade in the thumbnail when finnished loading
			thumb.css({display:'none',opacity:_fadeTo}).fadeIn(1500);
			
			// hover effects
			thumb.hover(
				function() { thumb.fadeTo('fast',1); },
				function() { _li.not('.active').children('img').fadeTo('fast',0.8); } // don't fade out if the parent is active
			);
			
			// LARRY ADDED THIS
			thumb.click(
				function() { 
					shadowGallery.showSlide(thumb.attr("title"), thumb.attr("rel"), galleryName);
				}
			);
			
		}
			
	});
	
}
  
