function selectAll(obj) {
  obj.focus();
  obj.select();
}
function s(f) {
  f.d.value = 'i';
  f.target = '_blank';
  f.submit();
  f.d.value = 'a';
}
var page_fader = null;
var image_buttons = null;
var show_image_buttons = false;
var enlarge_last = false;
var full_image = null;
var full_width = 0;
var full_height = 0;
var max_width = 600;
var max_height = 600;
var image_buttons_timer = -1;

function init_enlarge(pic){
    if (full_image){
        enlarge(false);
        return;
    }
    var preview = $('preview');
    var ppos = preview.cumulativeOffset();
    var loader = new Element('div');
    loader.setStyle({
        background: '#fff url(/images/ajax-loader.gif) no-repeat scroll 50% 50%',
        opacity: '0.8',
        filter: 'alpha(opacity=80)',
        position: 'absolute',
        left: 1+ppos.left+'px',
        top: 1+ppos.top+'px',
        width: preview.offsetWidth-1+'px',
        height: preview.offsetHeight-1+'px'
    });
    document.body.appendChild(loader);
    var img = new Image();
    full_image = pic;
    img.onload = function(){
        if (full_width!=0)return;
        loader.hide();
        full_width = img.width;
        full_height = img.height;
       
        var thumb = $($('preview').firstChild);
        var tpos = thumb.cumulativeOffset();
        
        page_fader = new Element('div');
        page_fader.setStyle({
          position: 'absolute',
          display: 'none',
          opacity: '0.8',
          filter: 'alpha(opacity=80)'
        });
        page_fader.style.backgroundColor = '#000';
        document.body.appendChild(page_fader);
        
        div = new Element('div');
        div.setStyle({
            width: thumb.offsetWidth+'px',
            height: thumb.offsetHeight+'px',
            left: tpos.left+'px',
            top: tpos.top+'px',
            position: 'absolute',
            overflow: 'hidden'
        });
        div.setAttribute('id', 'thumb-enlarge');
        img = $(img);
        img.setStyle({
            width: '100%',
            height: '100%'
        });
        Event.observe(img, 'mouseover', function(){
          if (!show_image_buttons || enlarge_last)return;
          image_buttons.show();
          window.clearTimeout(image_buttons_timer);
        });
        Event.observe(img, 'mouseout', function(){
          if (!show_image_buttons)return;
          window.clearTimeout(image_buttons_timer);
          image_buttons_timer = window.setTimeout("image_buttons.hide();", 100);
        });
        
        div.appendChild(img);
        document.body.appendChild(div);
        Event.observe(div, 'click', function(e){
            Event.stop(e);
            $(this).hasClassName('enlarged')?(enlarge_last?enlarge(false):minimize()):enlarge(false);
        });
        
        image_buttons = new Element('div');
        image_buttons.setStyle({
          position: 'absolute',
          display: 'none'
        });
        image_buttons.setAttribute('id', 'image-buttons');
        var lnk = new Element('a');
        lnk.innerHTML = 'View Original';
        lnk.setAttribute('href', '#');
        lnk.className = 'show-original';
        image_buttons.appendChild(lnk);
        Event.observe(lnk, 'click', function(e){
          Event.stop(e);
          enlarge(true);
        });
        
        lnk = new Element('a');
        lnk.innerHTML = 'Close (x)';
        lnk.setAttribute('href', '#');
        lnk.className = 'image-close';
        image_buttons.appendChild(lnk);
        Event.observe(lnk, 'click', function(e){
          Event.stop(e);
          minimize();
        });
        
        document.body.appendChild(image_buttons);

        Event.observe(image_buttons, 'mouseover', function(){
          if (!show_image_buttons)return;
          window.clearTimeout(image_buttons_timer);
        });
        Event.observe(image_buttons, 'mouseout', function(){
          if (!show_image_buttons)return;
          window.clearTimeout(image_buttons_timer);
          image_buttons_timer = window.setTimeout("image_buttons.hide();", 100);
        });
        
        Event.observe(document, 'click', function(){
            minimize();
        });
        enlarge(false);
    }
    img.src = pic;
}

Event.observe(window, 'resize', function(){
  var div = $('thumb-enlarge');
  if (div.hasClassName('enlarged'))enlarge(enlarge_last);
});

function enlarge(mode){
    var thumb = $($('preview').firstChild);
    var div = $('thumb-enlarge');
    var queue = Effect.Queues.get('thumb');
    queue.each(function(effect) { effect.cancel(); });

    div.addClassName('enlarged');
    if (enlarge_last && !mode){
      var div_pos = div.cumulativeOffset();
      window.scrollTo(0, div_pos.top);
    }
    enlarge_last = mode;

    var vd = document.viewport.getDimensions();
    var vs = document.viewport.getScrollOffsets();

    var large_width = full_width;
    var large_height = full_height;
    var max_width = Math.round(vd.width*0.9);
    var max_height = Math.round(vd.height*0.9);
    if (!mode){
      if (large_width>max_width){
        large_height = Math.round(large_height*max_width/large_width);
        large_width = max_width;
      }
      if (large_height>max_height){
        large_width = Math.round(large_width*max_height/large_height);
        large_height = max_height;
      }
    }

    var dx = (vd.width-large_width)/2;
    var dy = (vd.height-large_height)/2;
    if (dx<0)dx = 0;
    if (dy<0)dy = 0;
    
    dy += vs.top;
    
    image_buttons.style.width = large_width+'px';
    image_buttons.style.left = (vd.width-large_width)/2+'px';

    new Effect.Morph(div, {
        style: {
            left: dx+'px',
            top: dy+'px',
            width: large_width+'px',
            height: large_height+'px'
        }, // CSS Properties
        beforeStart: function(){
          $(div).show();
          page_fader.style.left = '0px';
          page_fader.style.top = '0px';
          page_fader.style.width = document.body.offsetWidth+'px';
          page_fader.style.height = Math.max(document.body.offsetHeight, vd.height)+25+'px';
          page_fader.show();
          image_buttons.addClassName('buttons-hidden');
        },
        afterFinish: function(){
          if (!enlarge_last)image_buttons.removeClassName('buttons-hidden');
        },
        afterUpdate: function(){
          image_buttons.style.top = $(div).offsetTop+'px';
        },
        duration: 0.5, // Core Effect properties,
        queue: {
            position: 'end',
            scope: 'thumb'
        }
    });
    if (mode || (large_width==full_width && large_height==full_height)){
      show_image_buttons = false;
    }
    else {
      show_image_buttons = true;
    }
}

function minimize(){
    image_buttons.hide();
    var queue = Effect.Queues.get('thumb');
    queue.each(function(effect) { effect.cancel(); });

    var div = $('thumb-enlarge');
    var thumb = $('preview').firstChild;
    var tpos = thumb.cumulativeOffset();
    div.removeClassName('enlarged');
    new Effect.Morph(div, {
        style: {
            width: thumb.offsetWidth+'px',
            height: thumb.offsetHeight+'px',
            left: tpos.left+'px',
            top: tpos.top+'px'
        }, // CSS Properties
        beforeStart: function(){
          image_buttons.addClassName('buttons-hidden');
        },
        afterFinish: function(){
          page_fader.hide();
          $(div).hide();
        },
        duration: 0.5, // Core Effect properties,
        queue: {
            position: 'end',
            scope: 'thumb'
        }
    });
}

Event.observe(window, 'load', function(){
    var n = $('preview');
    if (n){
        Event.observe(n.parentNode, 'click', function(e){
            Event.stop(e);
            init_enlarge($('preview').href);
        });
    }
});

Event.observe(window, 'load', function(){
    
    var folds = $$('.fold-box');
    for (var i=0;i<folds.length;i++){
        var fold = folds[i];
        fold_init(fold, i+1);
    }
    
});
function fold_init(fold, id){
    var options = fold.down('.fold-options');
    if (!options)return;
    
    var btn = fold.down('.fold-title a');
    if (!btn)return;
    
    var container = new Element('div');
    container.setStyle({
        overflow: 'hidden'
    });
    
    options.up().insertBefore(container, options);
    container.appendChild(options);
    if (btn.hasClassName('open')){
        container.setStyle({
            height: container.offsetHeight+'px'
        });
    }
    else {
        container.setStyle({
            height: '0px',
            display: 'none'
        });
    }
    
    Event.observe(btn, 'click', function(e){
        Event.stop(e);
        if (btn.hasClassName('open')){
            // minimize
            btn.removeClassName('open');
            new Effect.Morph(container, {
                style: {
                    height: '0px'
                }, // CSS Properties
                afterFinish: function(){
                    container.hide();
                },
                duration: 0.5, // Core Effect properties,
                queue: {
                    position: 'end',
                    scope: 'fold-'+id
                }
            });
        }
        else {
            // expand
            btn.addClassName('open');
            container.show();
            new Effect.Morph(container, {
                style: {
                    height: container.scrollHeight+'px'
                }, // CSS Properties
                duration: 0.5, // Core Effect properties,
                queue: {
                    position: 'end',
                    scope: 'fold-'+id
                }
            });
        }
        
    });
}

