﻿var html;
jQuery.ajax({
	type: "POST",
	url: gallery_data.MAIN_SITE_ROOT_URL+"/cmtadmin/lib/gallery_get.php",
	data: { gallery: gallery_data.gallery_id, template: gallery_data.template },
	datatype: "xml",
	success: function(data, textStatus){
		var gallery_div = jQuery("#gallery");
		gallery_div.hide();
		gallery_div.empty();
		var d = jQuery(data);
		
		var re = function (id, flags) { 
			return new RegExp("\\{" + id + "\\}", flags ? flags : ""); 
		};
		
		html = '';
		d.find(".gallery_item").each( function() {
			var item = jQuery(this);
			// force height and width
			var img = item.find(".gallery_item_img_thumb img:first");
			// force height and width
			if (gallery_data.height || gallery_data.width) {
				img.css("height", gallery_data.height ? gallery_data.height : "auto");
				img.css("width", gallery_data.width ? gallery_data.width : "auto");
			}
			var itemhtml = gallery_data.template.item;
			itemhtml = itemhtml.replace(re("image_src", "g"), 
				item.find(".gallery_item_img img").attr("src"));
			itemhtml = itemhtml.replace(re("caption", "g"), 
				item.find(".gallery_item_caption").text());
			itemhtml = itemhtml.replace(re("thumb_image", "g"), 
				item.find(".gallery_item_img_thumb").html());
			if (html.search(re("item")) == -1) html += gallery_data.template.set;
			html = html.replace(re("item"), itemhtml);
		});
		if (html.search(re("item")) > -1) html = html.replace(re("item", "g"), "");
		html = gallery_data.template.gallery.replace(re("sets"), html);
		html = html.replace(re("gallery_name", "g"), d.find("#gallery_name").text());
//		html = html.replace(re("gallery_desc", "g"), d.find("#gallery_description").html());
		html = html.replace(re("gallery_desc", "g"), d.find("#gallery_description").text());
		gallery_div.html(html);
		// options commented out are defaults
		// see http://code.google.com/p/slimbox/wiki/jQueryManual#Setup 
		jQuery("#gallery a.gallery_item").slimbox({
			// loop: false;
			// overlayOpacity: 0.8;
			// overlayFadeDuration: 400;
			// resizeDuration: 400;
			// resizeEasing: "swing";
			// initialWidth: 250;
			// initialHeight: 250;
			// imageFadeDuration: 400;
			// captionAnimationDuration: 400;
			// counterText: "Image {x} of {y}";
			// closeKeys: [27, 88, 67];
			// previousKeys: [37, 80];
			// nextKeys: [39, 78];
		});
		gallery_div.show();
	},
	error: function (XMLHttpRequest, textStatus, errorThrown) {
		alert( "an error occurred: " + textStatus );
	}
});

