/*
Copyright (c) 2008, Ilikethis! Inc. All rights reserved.
Code licensed under the BSD License
version: 0.3
requires:
	yui-2.5.1/utilities.js
*/



/* Namespace Ilt.Effect
 * obsahuje tridy pro efekty nad elementy (shake, appear, move)
 */
Ilt.Effect = { };





/****************************
 * SLIDE - trida pro rozevirani a zavirani elementu
 ***************************/  
Ilt.Effect.Slide = function( boxId, oa )
{

    this.box = YAHOO.util.Dom.get( boxId );
    
    var o = oa || {};
    this.duration = o.duration || 1.0;
    this.height = o.height || 0;
    this.onComplete = o.onComplete || null;
    
    this.init();
}

Ilt.Effect.Slide.prototype = {

  init: function()
  {
    // vymenime noscreen za none
  	if ( YAHOO.util.Dom.hasClass(this.box,'noscreen') )
  	{
    	this.box.style.display = 'none';
    	YAHOO.util.Dom.removeClass(this.box,'noscreen');
  	}
   	this.setHeight();
   	// vymenine none za 0px
  	if ( YAHOO.util.Dom.getStyle(this.box,'display') == 'none' )
  	{
  		YAHOO.util.Dom.setStyle(this.box,'height','0px');
  		YAHOO.util.Dom.setStyle(this.box,'visibility','hidden');
  		YAHOO.util.Dom.setStyle(this.box,'display','');
  	}
   	this.status = 'visible';
  	if ( YAHOO.util.Dom.getStyle(this.box,'height') == 0 || YAHOO.util.Dom.getStyle(this.box,'visibility') == 'hidden' )
  	{
	   	this.status = 'hidden';
  	}
  },
  
  /********************
   * urci vysku elementu, pokud neni zrejma
   *******************/
  setHeight: function()
  {
        if ( this.height == 0 )
        {
            var oldDisplay = this.box.style.display;
            var oldVisibility = this.box.style.visibility;
        	//this.box.style.left = '-2000px';
        	this.box.style.display = '';
        	this.box.style.visibility = 'hidden';
            var r = YAHOO.util.Dom.getRegion( this.box );
            this.height = r.bottom - r.top;
            var a = YAHOO.util.Dom.getChildren( this.box );
            for ( var i=0; i<a.length; i++)
            {
            	var m = YAHOO.util.Dom.getStyle(a[i],'marginTop');
            	if ( !YAHOO.lang.isUndefined(m) && m!='0px' && m!='auto' )
            	{
            		this.height += parseInt(m);
            	}
            	m = YAHOO.util.Dom.getStyle(a[i],'marginBottom');
            	if ( !YAHOO.lang.isUndefined(m) && m!='0px' && m!='auto' )
            	{
            		this.height += parseInt(m);
            	}
            }
            this.box.style.display = oldDisplay;
			this.box.style.visibility = oldVisibility;
        }
  },
  
  /*********************
   * rozbali nebo sbali element
   ********************/
  toggle: function() {
  	
  	if ( this.status == 'hiding' || this.status == 'hidden' )
  	{
  		this.show();
  		this.show();
  	}
  	else
  	{
  		this.hide();
  		this.hide();
  	}
  },
  
  /*********************
   * rozbali element
   ********************/
  show: function() {
    if ( this.animation ) {
   		this.animation.onComplete.unsubscribeAll();
   		this.animation.stop(false);
    	this.animation = null;
    }
    var r = YAHOO.util.Dom.getRegion( this.box );
    var fromValue = r.bottom - r.top;
    var duration = this.duration * ( ( this.height - fromValue ) / this.height );
  	if ( YAHOO.util.Dom.getStyle(this.box,'visibility') == 'hidden' )
  	{
  		this.box.style.visibility = '';
  	}
    this.box.style.overflow = 'hidden';
    if ( fromValue < this.height )
    {
      this.status = 'showing';
      this.animation = new YAHOO.util.Anim( this.box
    	, { height: { from: fromValue, to:this.height } }
    	, duration
    	, YAHOO.util.Easing.easeNone
      );
  	  this.animation.onComplete.subscribe( function(e,o,c){ 
	  	  c.status = 'showed';
	  	  if (c.onComplete) c.onComplete();
	  }, this );
      this.animation.animate();
    }
    else
    {
      this.status = 'showed';
    }
  },
  
  /*********************
   * sbali element
   ********************/
  hide: function() {
    if ( this.animation ) {
   		this.animation.onComplete.unsubscribeAll();
   		this.animation.stop(false);
   		this.animation = null;
    }
    var r = YAHOO.util.Dom.getRegion( this.box );
    var fromValue = r.bottom - r.top;
    var duration = this.duration * ( 1 - ( this.height - fromValue ) / this.height );
    this.box.style.overflow = 'hidden';
    if ( fromValue > 0 )
    {
      this.status = 'hiding';
      this.animation = new YAHOO.util.Anim( this.box
    	, { height: { from: fromValue, to:0 } }
    	, duration
    	, YAHOO.util.Easing.easeNone
      );
	  this.animation.onComplete.subscribe( function(e,o,c){ 
	  	c.box.style.visibility = 'hidden'; 
	    c.status = 'hidden';
	  	if (c.onComplete) c.onComplete();
	  }, this );
      this.animation.animate();
    }
    else
    {
      this.status = 'hidden';
    }
  }
}

/******************
 * staticka metoda, ktera rozbali specifikovany element
 **************/
Ilt.Effect.Slide.show = function( id, oa ) {
	Ilt.Effect.Slide.get(id,oa).show();
}

/******************
 * staticka metoda, ktera sbali specifikovany element
 **************/
Ilt.Effect.Slide.hide = function( id, oa ) {
	Ilt.Effect.Slide.get(id,oa).hide();
}

/******************
 * staticka metoda, ktera sbali specifikovany element
 **************/
Ilt.Effect.Slide.toggle = function( id, oa ) {
	Ilt.Effect.Slide.get(id,oa).toggle();
}

/******************
 * cache obsahujici otevirane elementy
 **************/
Ilt.Effect.Slide.elements = [];

/******************
 * static factory - udrzuje cache elementu
 **************/
Ilt.Effect.Slide.get = function( id, oa ) {
	var box = YAHOO.util.Dom.get( id );
	var o = oa || {};
	for ( var i=0; i<Ilt.Effect.Slide.elements.length; i++ )
	{
		if ( Ilt.Effect.Slide.elements[i].box == box )
		{
			if ( o.duration )
			{
				Ilt.Effect.Slide.elements[i].slider.duration = o.duration;
			}
			Ilt.Effect.Slide.elements[i].slider.onComplete = o.onComplete;
			return Ilt.Effect.Slide.elements[i].slider;
		}
	}
	var newElement = new Ilt.Effect.Slide( box, oa );
	Ilt.Effect.Slide.elements[Ilt.Effect.Slide.elements.length] = { box: box, slider: newElement };
	return newElement;
}















/****************************
 * FADE - pomalu zjevi nebo skryje element
 ***************************/  
Ilt.Effect.Fade = function( boxId, oa )
{

    this.box = YAHOO.util.Dom.get( boxId );
    
    var o = oa || {};
    this.duration = o.duration || 1.0;
    this.opacity = o.opacity || 1.0;
    this.onComplete = o.onComplete || null;
    
    this.init();
}

Ilt.Effect.Fade.prototype = {

  init: function()
  {
    // vymenime noscreen nebo none za invisible
  	if ( YAHOO.util.Dom.hasClass(this.box,'noscreen') || YAHOO.util.Dom.getStyle(this.box,'display') == 'none' )
  	{
  		YAHOO.util.Dom.setStyle(this.box,'visibility','hidden');
    	this.box.style.display = '';
    	YAHOO.util.Dom.removeClass(this.box,'noscreen');
  	}
   	//this.setHeight();
  },
  
  /*********************
   * zobrazi element
   ********************/
  show: function() {
    if ( this.animation ) {
   		this.animation.onComplete.unsubscribeAll();
   		this.animation.stop(false);
    	this.animation = null;
    }
    var fromValue = parseFloat( YAHOO.util.Dom.getStyle( this.box, 'opacity' ) );
    var duration = this.duration * ( ( this.opacity - fromValue ) / this.opacity );
  	if ( YAHOO.util.Dom.getStyle(this.box,'visibility') == 'hidden' )
  	{
  		this.box.style.visibility = '';
  	}
    this.box.style.overflow = 'hidden';
    if ( fromValue < this.opacity )
    {
      this.animation = new YAHOO.util.Anim( this.box
    	, { opacity: { from: fromValue, to:this.opacity } }
    	, duration
    	, YAHOO.util.Easing.easeNone
      );
      if (this.onComplete)
      {
	  	this.animation.onComplete.subscribe( function(e,o,c){ 
	  	  if (c.onComplete) c.onComplete();
	  	}, this );
      }
      this.animation.animate();
    }
  },
  
  /*********************
   * zmizi element
   ********************/
  hide: function() {
    if ( this.animation ) {
   		this.animation.onComplete.unsubscribeAll();
   		this.animation.stop(false);
   		this.animation = null;
    }
    var fromValue = parseFloat( YAHOO.util.Dom.getStyle( this.box, 'opacity' ) );
    var duration = this.duration * ( 1 - ( this.opacity - fromValue ) / this.opacity );
    if ( fromValue > 0 )
    {
      this.animation = new YAHOO.util.Anim( this.box
    	, { opacity: { from: fromValue, to:0.0 } }
    	, duration
    	, YAHOO.util.Easing.easeNone
      );
	  this.animation.onComplete.subscribe( function(e,o,c){ 
	  	c.box.style.visibility = 'hidden'; 
	  	if (c.onComplete) c.onComplete();
	  }, this );
      this.animation.animate();
    }
  }
}

/******************
 * staticka metoda, ktera rozbali specifikovany element
 **************/
Ilt.Effect.Fade.show = function( id, oa ) {
	Ilt.Effect.Fade.get(id,oa).show();
}

/******************
 * staticka metoda, ktera sbali specifikovany element
 **************/
Ilt.Effect.Fade.hide = function( id, oa ) {
	Ilt.Effect.Fade.get(id,oa).hide();
}

/******************
 * cache obsahujici otevirane elementy
 **************/
Ilt.Effect.Fade.elements = [];

/******************
 * static factory - udrzuje cache elementu
 **************/
Ilt.Effect.Fade.get = function( id, oa ) {
	var box = YAHOO.util.Dom.get( id );
	var o = oa || {};
	for ( var i=0; i<Ilt.Effect.Fade.elements.length; i++ )
	{
		if ( Ilt.Effect.Fade.elements[i].box == box )
		{
			if ( o.duration )
			{
				Ilt.Effect.Fade.elements[i].slider.duration = o.duration;
			}
			if ( o.opacity )
			{
				Ilt.Effect.Fade.elements[i].slider.opacity = o.opacity;
			}
			Ilt.Effect.Fade.elements[i].slider.onComplete = o.onComplete;
			return Ilt.Effect.Fade.elements[i].slider;
		}
	}
	var newElement = new Ilt.Effect.Fade( box, oa );
	Ilt.Effect.Fade.elements[Ilt.Effect.Fade.elements.length] = { box: box, slider: newElement };
	return newElement;
}








