﻿var html;
jQuery.ajax({
	type: "POST",
	url: gallery_data.MAIN_SITE_ROOT_URL+"/cmtadmin/lib/gallery_get.php",
	data: { gallery: '' },
	datatype: "xml",
	success: function(data, textStatus){
		var gallery_div = jQuery("#gallery_listing");
		var d = jQuery(data);
		
		var re = function (id, flags) { 
			return new RegExp("\\{" + id + "\\}", flags ? flags : ""); 
		};

		html = '';
		d.find(".gallery").each( function() {
			var item = jQuery(this);
			var itemhtml = gallery_data.template.item;
			// get just the first img element
			var img = item.find(".gallery_item_img_thumb img:first").clone();
			// 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");
			}
			img = jQuery(document.createElement("div")).append(img);
			itemhtml = itemhtml.replace(re("first_thumb", "g"), img.html());
			itemhtml = itemhtml.replace(re("gallery_link", "g"), 
				gallery_data.MAIN_SITE_ROOT_URL +
				"/photo-gallery/index.php?gallery=" + 
				item.find(".gallery_id").text());
			itemhtml = itemhtml.replace(re("gallery_name", "g"), 
				item.find(".gallery_name").text());
//			itemhtml = itemhtml.replace(re("gallery_desc", "g"), 
//				item.find(".gallery_description").html());
			itemhtml = itemhtml.replace(re("gallery_desc", "g"), 
				item.find(".gallery_description").text());
			html += itemhtml;
		});
		html = gallery_data.template.gallery_list.replace(re("items"), html);
		gallery_div.html(html);
		
		gallery_div.show();
	},
	error: function (XMLHttpRequest, textStatus, errorThrown) {
		alert( "an error occurred: " + textStatus );
	}
});
