var theLyteBox;
var has_gallery = { };
function getImageGallery (galleryName,callback,type) {
	if(!type) { type = 'lytebox'; }
	new Ajax.Request('ajax_receiver.php',{
		method:'POST',
		parameters:'action=imageList&source='+galleryName,
		evalJSON:'force',
		onSuccess:function(data) {
			for(i = 0; i < data.responseJSON.length; i++)
			{
				$("hidden_images").insert('<a href="'+data.responseJSON[i]+'" class="'+galleryName+'_'+type+'" rel="'+type+'['+galleryName+']"></a>');
			}
			theLyteBox.updateLyteboxItems();
			callback()
		}
	});
}
document.observe("dom:loaded", function() { 
	theLyteBox = new LyteBox();
	$$("body").first().insert('<div style="display:none" id="hidden_images"></div>');
	
	$$(".make_lyte, .make_lyte_show").each(function(node){
		node.observe('click',function(event){
			var galleryName = this.getAttribute('rel');
			if(node.hasClassName('make_lyte_show')) {
				var type = 'lyteshow';
			} else {
				var type = 'lytebox';
			}
			
			function callback(){
				$$("."+galleryName+'_'+type).first().onclick();
			}
			if(has_gallery[galleryName+type]) {
				callback();
			} else {
				has_gallery[galleryName+type] = true;
				getImageGallery(galleryName,callback,type);
			}
			Event.stop(event);
		});
	});
});