var imageModule = function(opt) {
	var module = {};
	
	opt.target = $(opt.target);
	opt.collection_name = (opt.target.attributes['subdept'] != null) ? opt.target.attributes['subdept'].value : opt.target.id;
	opt.output = opt.output || 'stack';
	
	module.writeScroll = function(file) {
		
	};
	module.writeStack = function(file) {
		file.hreftag == '' ? file.hrefend = '' : file.hrefend = '</a><br/>';
		var imghtml = file.hreftag + '<img src="' + file.name + '" ' + file.heightattr + file.widthattr + 'border="0"/>' + file.hrefend;
		
		opt.target.insert('<div style="padding-bottom: 4px; text-align: center;" ' + 
			'class="textsmall ModuleImageDisplay ModuleImageDisplay' + opt.collection_name + '">' +
			 imghtml + '</div>');

		// %=assigned|sTempImageSRC|
		//   %=assigned|sTempImageHREF|<a href="%=sTempImageHREF=%" %=match|sTempImageTargetNew~-|target="_new"=%>=%<img src="%=sTempImageSRC=%" %=assigned|iTempImageWidth|width="%=iTempImageWidth=%"=% %=assigned|iTempImageHeight|height="%=iTempImageHeight=%"=% alt="%=sTempImageTitle=%" border="0"/>%=assigned|sTempImageHREF|</a><br/>=%
		// ||
		//   %=assigned|sTempImageHREF|<div class="textsmall" style="margin-top: 8px;"><a href="%=sTempImageHREF=%" %=match|sTempImageTargetNew~-|target="_new"=%>=%%=sTempImageTitle=%%=assigned|sTempImageHREF|</a></div>=%
		// =%

	};
	
	module.load = function() {
		new Ajax.Request("/_collections/" + opt.collection_name + "/manifest.xml?r=" + Math.round(Math.random()*10000), {
	      method: 'get',
	      asynchronous: false,
	      onCreate: function() { },
	      onSuccess: module.success, 
	      onFailure: function(){}
	    });
	};
	
	module.success = function(r) {
		opt.target.innerHTML = '';
	    var files = $A(r.responseXML.getElementsByTagName('file'));
			files.sort(function(a,b) {
			  try {
			    return parseInt(a.attributes.getNamedItem('seq_no').value) - parseInt(b.attributes.getNamedItem('seq_no').value);
			  } catch(e) {
			    return -1;
			  };
			});
	    files.each(function(f) {
				var file = {name: (f.hasChildNodes() ? f.firstChild.nodeValue : ''), hreftag: ''}
				if (file.name != '') {
					if (f.attributes.getNamedItem('href') != null) {
						var target = f.attributes.getNamedItem('target').value;
						if(window.location.protocol == "https:"){
							target = "/" + opts.jobid + target;
						};
						file.hreftag = '<a href="' + f.attributes.getNamedItem('href').value + '" target="' + target + '">';
					}
					file.heightattr = ((f.attributes.getNamedItem('height') != null) ? 'height="' + f.attributes.getNamedItem('height').value + '" ' : ' ');
					file.widthattr = ((f.attributes.getNamedItem('width') != null) ? 'width="' + f.attributes.getNamedItem('width').value + '" ' : ' ');
					if (opt.output == 'scroll') {
						module.writeScroll(file)
					} else {
						module.writeStack(file)
					}
				};
	    });
	};
	
	return module;
};

