/**
 * Custom effects
 * 
 * @version $Id$
 * @author M.Olszewski
 * @since 27 Mar 2009
 * @copyright Copyright (c) 2009 M.Olszewski. All rights reserved.
 */

function doResetOpacity(container)
{
  if (container == undefined)
  {
    return;
  }
  
  container.each(function(node) {
    node.setStyle({
      opacity: 0
    });
  });
}


function doQueuedOpacity(element, duration, scope, show)
{
  if (element == undefined)
  {
    return;
  }
  duration = typeof(duration) == 'number' ? duration : 2;
  scope    = typeof(scope)    == 'string' ? scope    : null;
  show     = typeof(show)     == 'boolean'? show     : true;
  
  if (show)
  {
    element.setStyle({
      visibility: 'visible'
    });
  }
  
  if (scope == null)
  {
    new Effect.Opacity(element, {
      duration: duration,
      from:     0,
      to:       1,
      queue:    'end'
    });
  }
  else
  {
    new Effect.Opacity(element, {
      duration: duration,
      from:     0,
      to:       1,
      queue: { position: 'end', scope: scope }
    });
  }
}

function doOpacity(element, duration, delay)
{
  if (element == undefined)
  {
    return;
  }
  
  duration = typeof(duration) == 'number' ? duration : 2;
  delay    = typeof(delay)    == 'number' ? delay    : 0;
  
  element.setStyle({
    opacity: 0
  });
  
  new Effect.Opacity(element, {
    delay:    delay,
    duration: duration,
    from:     0,
    to:       1
  });
}

function doAppear(element, duration, delay)
{
  if (element == undefined)
  {
    return;
  }
  
  duration = typeof(duration) == 'number' ? duration : 2;
  delay    = typeof(delay)    == 'number' ? delay    : 0;
  
  new Effect.Appear(element, {
    delay:    delay,
    duration: duration,
    from:     0,
    to:       1
  });
}

function addOnLoadFadeIn(elementId, duration)
{
  var element = $(elementId);
  element.setStyle({
    opacity: 0
  });
  
  document.observe('dom:loaded', function() {
    doOpacity(element, duration);
  });
}

function addOnLoadAppear(elementId, duration, delay)
{
  document.observe('dom:loaded', function() {
    doAppear($(elementId), duration, delay);
  });
}

function addOnLoadQueuedFadeIn(className, duration, scope)
{
  var frames = $$('.' + className);
  
  doResetOpacity(frames);
	
  document.observe('dom:loaded', function() {
    frames.each(function(node) {
      doQueuedOpacity(node, duration, scope);
    });
  });
}
