$(document).ready(function() {
	$('#media #photo_gallery_add_btn').click(function() {
		var target = $.wisdomcms_GetCurrentElement();
		var resizer = opts.site + opts.upload + 'resize.php/';
		var options = '?width=216&height=168&cropratio=216:168&image=/';

		$('#media_display .selected input').each(function() {
			var img = $(this).attr('name');
			var src = resizer + img + options + img;
			var href = opts.site + opts.upload + img;
			// var caption = prompt('Please enter a title for\n' + img);
			var rel = 'prettyPhoto[gallery]';

			$.wisdomcms_Command('insertimage', src);
			
			target.find('img[src$="' + img + '"]').each(function() {
				if($(this).hasClass('gallery-img')) {
					// do nothing
				} else {
					$(this)					
						.addClass('gallery-img')
						.wrap('<div class="gallery-img-div new-gallery-img"></div>');
				}
			});			
			
			target.find('.new-gallery-img').each(function() {
				$(this)
					.removeClass('new-gallery-img')					
					.append('<div class="gallery-img-tag-1">Image Title</div>')
					.append('<div class="gallery-img-tag-2">Date</div>')
					.append('<div class="gallery-img-tag-3">Description</div>')
					.append('<div class="gallery-img-tag-4">Description</div>');			
			});					
			
			target.find('.gallery-img-div').each(function() {					
				$image = $(this).find('.gallery-img').attr('src');			
				$images = $image.split('/');	
				$img_src = './uploads/' + $images[$images.length - 1];		
				$(this).wrap('<a href="' + $img_src + '" rel="prettyPhoto" style="padding: 0px;"></a>');				
			});	
			
			target.find('.gallery-img').attr('onmousedown', 'if (event.preventDefault) event.preventDefault()');		
		});
	});
	
	// clean up images that have been dragged!
	/*
	var gicount = 0;
	$('.gallery-img-div').each(function() {
		gicount = 0;
		$(this).find('img').each(function() {
			gicount++;
		});
		if(gicount == 0) $(this).remove();	
	});
	
	$('.gallery-img').each(function() {
		gicount = 0;
		$(this).parent('.gallery-img-div').each(function() {
			gicount++;
		});
		$(this).parent('.gallery-link').each(function() {
			gicount++;
		});
		
		if(gicount == 0) $(this).remove();	
	});
	*/
	
	$admin = (top.location.href.split('/').pop().match("^admin") == 'admin');
	
	// set up pretty photo	
	$("a[rel^='prettyPhoto']").unbind('click');
	if(!$admin) $("a[rel^='prettyPhoto']").prettyPhoto();
 	
});

