/**

LayerMagic.js v1.0, 17.09.2008
Copyright (c) 2007-2008 Roland Kujundzic (http://www.web-to-print.eu)

@desc
Layer Magic functions. 

@requires 
  script/scriptaculous/prototype.js,
  script/scriptaculous/scriptaculous.js?load=effects

@html
preserve status on submit with:
<input type="hidden" name="layer_magic" value="{get:layer_magic}">
You have to call layer_magic.init() too.

@provides

@variable duration = id_list
Default 0.6

@function showAnimated(id)
Show hidden layer (style="display:none"). Use drop down (blind-up) animation.

@function show_hide(id)
Show hidden layer (style="display:none"). Hide if already showing.
Use drop down (blind-up) animation.

 */


function LayerMagic() {
  this.duration = 0.6;
  this._last_id = '';
}


//-----------------------------------------------------------------------------
LayerMagic.prototype.showAnimated = function(id) {

  if (this._last_id == id) {
    return;
  }

  if (document.getElementById(id).style.display == 'none') {

    if (this._last_id) {
      new Effect.BlindUp(this._last_id, { 
        duration: this.duration, 
        queue: 'end',
        afterFinish: function() { 
          new Effect.BlindDown(id, { duration: this.duration}); 
        } 
      });
    }
    else {
      new Effect.BlindDown(id, { duration: this.duration});
    }
  }

  this._last_id = id;
}


//-----------------------------------------------------------------------------
LayerMagic.prototype.show_hide = function(id) {

  if (document.getElementById(id).style.display == 'none') {
    new Effect.BlindDown(id, { duration: this.duration / 4});
  }
  else {
    new Effect.BlindUp(id, { duration: this.duration / 4});
  }


  var el = document.getElementsByName('layer_magic');

  if (el) {
    el[0].value = 'show_hide:' + id;
  }
}


//-----------------------------------------------------------------------------
LayerMagic.prototype.init = function() {

  var el = document.getElementsByName('layer_magic');

  if (el && el[0]) {
    var val = el[0].value;

    if (val.substring(0, 10) == 'show_hide:') {
      this.show_hide(val.substring(10));
    }
  }
}


var layer_magic = new LayerMagic();

