/*
Copyright (c) 2006, Caridy Patiño (caridy@gmail.com). All rights reserved.
Code licensed under the BSD License:
http://developer.yahoo.net/yui/license.txt
Version 0.1b
*/
/**
* @class
* Liquid Panel is an implementation of a Multi Panel Class States.
* @param {Element}	el	        The element representing the MenuBar
* @param {object}	userConfig	The configuration object literal containing the configuration that should be set for this Overlay.
* @constructor
*/
YAHOO.namespace("CMS");

YAHOO.CMS.LiquidPanel = function(aElement, userConfig) {
    this.init(aElement, userConfig);
};

YAHOO.CMS.LiquidPanel.prototype = {

    element: null, // div element that represent the dynamic area (liquid of fixed)...
	ready: false,
	state: null,
	initConfig: null,
	cookie_handle: null,
	handle: null,

    // classes
	classes: ['open', 'close'],
	defaultClass: null,

	// utilities
	$E: YAHOO.util.Event,
	$D: YAHOO.util.Dom,
	$C: YAHOO.util.Connect,
	$:  YAHOO.util.Dom.get,

    // init...
    init:function (el, userConfig) {
      userConfig = userConfig || {};
      userConfig.element = userConfig.element || el || null;

	  // required values
	  this.element      = userConfig.element || el || null;
	  this.classes      = userConfig.classes || this.classes || [];
	  this.defaultClass = userConfig.defaultClass || this.classes[0] || null;
	  this.handle       = userConfig.handle  || null;

	  if (
			((typeof this.element == 'object') || (this.element = this.$( this.element ))) &&
			((typeof this.classes == 'array') || (typeof this.classes == 'object'))
		 ) {
	    this.ready = true;
        this.initConfig = userConfig;
		this.cookie_handle = this.$E.generateId (this.element) + '-liquidpanel';
		this.height = assetOffset( this.element, 'height' );
		this.width  = assetOffset( this.element, 'width' );
		var that = this;
		if (this.handle || (this.handle = getElementsByClass('control',this.element,'a')))
	 	  YAHOO.util.Event.addListener ( this.handle, 'click', this.takeaction, that, true );
		this.reset ( userConfig.discardCookie );
	  }
	},
	takeaction:function ( e ) {
	  this.next ();
	  this.$E.stopPropagation( e );
	  this.$E.stopEvent ( e );
	  return false;
	},
    /**
	* ---
	* @public
	* @param {bool}    discardCookie  True if we want to reset the ClassName without get or save the cookie value
	* @return void
	*/
	reset:function ( discardCookie ) {
	  if (this.ready) {
	  	// getting the information from the cookies... if not, then use a default class for this panel...
		if (!discardCookie)
	  	  this.status = getCookie ( this.cookie_handle ) || this.defaultClass;
	  	this.set ( discardCookie );
	  }
	},
    /**
	* ---
	* @public
	* @return {string}    Current ClassName
	*/
    get:function () {
  	  this.status;
	},
    /**
	* ---
	* @public
	* @param {bool}    d  True if we want to change de ClassName without save the cookie value
	* @return void
	*/
    set:function ( discardCookie ) {
	  if ((this.ready) && (this.status)) {
	  	for (var i=0; i<this.classes.length; i++)
	  	 if ((this.status != this.classes[i]) && (this.$D.hasClass(this.element, this.classes[i])))
	  	    this.$D.removeClass(this.element, this.classes[i]);
	  	if (!this.$D.hasClass(this.element, this.status))
	  	  this.$D.addClass(this.element, this.status);
	  	// saving the value into the cookies
	  	if (!discardCookie)
	  	  setCookie( this.cookie_handle, this.status );
	  }
	},
    /**
	* ---
	* @public
	* @param {string}  c  ClassName to set-up the panel
	* @return void
	*/
    setClassWithoutSave:function ( c ) {
      this.setClass ( c, true );
	},
    /**
	* ---
	* @public
	* @param {string}  c  ClassName to set-up the panel
	* @param {bool}    d  True if we want to change de ClassName without save the cookie value
	* @return void
	*/
    setClass:function ( c, d ) {
      if (this.ready && this.classes.inArray(c)) {
	    this.status = c;
	    this.set ( d );
	  }
	},
    /**
	* ---
	* @public
	* @param {bool}    d  True if we want to change de ClassName without save the cookie value
	* @return void
	*/
    next:function ( d ) {
	  if (this.ready) {
	  	var n = 0;
	  	for (var i=0; i<this.classes.length; i++)
	  	  if (this.status == this.classes[i])
	  	    n = i+1;
	  	if (this.classes.length == n) n = 0;
	    this.status = this.classes[n];
	    this.set ( d );
	  }
	},
    /**
	* ---
	* @public
	* @param {bool}    d  True if we want to change de ClassName without save the cookie value
	* @return void
	*/
    setDefault:function ( d ) {
	  if (this.ready) {
	    this.status = this.defaultClass;
	    this.set ( d );
	  }
	},
	/**
	* Returns a string representation of the object.
	* @type string
	*/
	toString: function() {
		return "Caridy´s Liquid Panel " + this.element.id;
	}
};

YAHOO.CMS.SlidePanel = function(aElement, userConfig) {
    this.initSlide(aElement, userConfig);
};

YAHOO.extend ( YAHOO.CMS.SlidePanel, YAHOO.CMS.LiquidPanel );

YAHOO.CMS.SlidePanel.prototype.height = null;
YAHOO.CMS.SlidePanel.prototype.width = null;
YAHOO.CMS.SlidePanel.prototype.area = null;
YAHOO.CMS.SlidePanel.prototype.anim = null;

YAHOO.CMS.SlidePanel.prototype.initSlide = function (el, userConfig) {
  this.init (el, userConfig);
  if (this.ready) {
  	this.ready = false;
  	var a = getElementsByClass('slide',this.element,'div');
  	if (a.length > 0) {
	  this.ready  = true;
	  this.area = a[0];
	  this.height = assetOffset( this.area, 'height' );
	  this.width  = assetOffset( this.area, 'width' );
	  this.anim   = this.$E.generateId (this.area) + '-animation';
	  this.animate();
	}
  }
};
YAHOO.CMS.SlidePanel.prototype.animate = function () {
  if ((this.ready) && (this.status)) {
  	h = (this.status=='open'?this.height:0); var id = this.anim;
  	YAHOO.util.Dom.setStyle(this.area, 'overflow', 'hidden');
	if ((typeof YAHOO.tms.animations[id] == 'object') && (YAHOO.tms.animations[id].isAnimated())) { YAHOO.tms.animations[id].stop(); }
	YAHOO.tms.animations[id] = new YAHOO.util.Anim(this.area, { height: { to: h } }, 1.5, YAHOO.util.Easing.easeOutStrong);
	YAHOO.tms.animations[id].animate();
  }
};
YAHOO.CMS.SlidePanel.prototype.next = function ( d ) {
  if (this.ready) {
  	var n = 0;
  	for (var i=0; i<this.classes.length; i++)
  	  if (this.status == this.classes[i])
  	    n = i+1;
  	if (this.classes.length == n) n = 0;
    this.status = this.classes[n];
    this.set(d);
    this.animate ();
  }
}

// ******************************
// * expand panel class
// ******************************
YAHOO.CMS.ExpandPanel = function(p_oElement, p_oConfig) {
	arguments.callee.superclass.constructor.apply ( this, arguments );
};
YAHOO.extend ( YAHOO.CMS.ExpandPanel, YAHOO.CMS.SlidePanel );
YAHOO.CMS.ExpandPanel.prototype.animate = function () {
  if ((this.ready) && (this.status)) {
  	var w = (this.status=='open'?this.width:0); var id = this.anim;
  	YAHOO.util.Dom.setStyle(this.area, 'overflow', 'hidden');
	if ((typeof YAHOO.tms.animations[id] == 'object') && (YAHOO.tms.animations[id].isAnimated())) { YAHOO.tms.animations[id].stop(); }
	YAHOO.tms.animations[id] = new YAHOO.util.Anim(this.area, { width: { to: w } }, 1.5, YAHOO.util.Easing.easeOutStrong);
	YAHOO.tms.animations[id].animate();
  }
};