var imageModule = function(opt) {
  var module = {};
  
  opt.ptarget = $(opt.target);
  opt.collection_name = (opt.ptarget.attributes['subdept'] != null) ? opt.ptarget.attributes['subdept'].value : opt.ptarget.id;
  opt.output = opt.output || 'stack';
  
  module.writeScroll = function(file) {
    file.hreftag == '' ? file.hrefend = '' : file.hrefend = '</a>';
    if (file.kind == 'text') {
      opt.ptarget.insert('<div class="ScrollImageContainer">' + file.hreftag + file.name + file.hrefend + '</div>');
    } else {
      opt.ptarget.insert('<span class="ScrollImageContainer">' + file.hreftag + '<img src="' + file.name + '" ' + 
                         file.heightattr + file.widthattr + file.altattr + 'border="0"/>' + file.hrefend + '</span>');
    };
  };
  
  module.doScroll = function() {
    var jtarget = $j('#' + opt.target);
    var jfirst = jtarget.find('.ScrollImageContainer:first');
    jfirst.slideUp('slow',function(){
      jfirst.appendTo(jtarget).slideDown();
    });
  };
  
  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;

    var innerH = file.hreftag + file.name + file.hrefend;
    if (file.kind != 'text') {
       innerH = file.hreftag + '<img src="' + file.name + '" ' + file.heightattr + file.widthattr + 'border="0"/>' + file.hrefend;
     };
     opt.ptarget.insert('<div style="margin-bottom: 4px; text-align: center;" ' + 
       'class="textsmall ModuleImageDisplay ModuleImageDisplay' + opt.collection_name + '">' +
        innerH + '</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.ptarget.innerHTML = '';
    var needsToScroll = false;
    var files = $A(r.responseXML.getElementsByTagName('file')).sort(function(a, b){ 
      var A = parseInt(a.attributes.getNamedItem('seq_no').value); var B = parseInt(b.attributes.getNamedItem('seq_no').value);
      return A - B;
    });
    files.each(function(f) {
      var file = {name: (f.hasChildNodes() ? f.firstChild.nodeValue : ''), hreftag: ''}
      if (file.name != '') {
        if (f.attributes.getNamedItem('href') != null) {
          var target = "_self";
          if(f.attributes.getNamedItem('target') != null) target = f.attributes.getNamedItem('target').value;
          file.hreftag = '<a href="' + f.attributes.getNamedItem('href').value + '" target="' + target + '">';
        }
        file.kind = (f.attributes.getNamedItem('kind') != null) ? f.attributes.getNamedItem('kind').value : '';
        file.altattr = ((f.attributes.getNamedItem('alt') != null) ? 'alt="' + f.attributes.getNamedItem('alt').value + '" ' : ' ');
        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);
          needsToScroll = true;
        } else {
          module.writeStack(file);
        }
      };
    });
    if (needsToScroll) {
      module.doScroll();
    };
  };
  
  return module;
};

