/**
 * Togglable - make it easy to expand/collapse blocks.
 *   http://code.google.com/p/jquery-simpledialog/
 *
 * Copyright (c) 2009 Yusuke Horie
 *
 * Released under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 *
 * Since  : 0.01 - 07/06/2009
 * Version: 0.10 - 07/19/2009
 */
(function(jQuery){
  jQuery.fn.togglable = function (options) {
    var opts = jQuery.extend({}, $.fn.togglable.defaults, options);
    var $this = $(this);

    opts._effect = _effectPairs[opts.effect] || _effectPairs.slide;
    opts._easing = _easingPairs[opts.easing] || _easingPairs.other;

    $this.data('opts', opts);

    return this.each(function(i, e) {
      var $t = jQuery(this);
      if (!$t.is('a')) return;

      jQuery(e).bind('click.togglable', function (event) {
        event.preventDefault();

        var rel = $t.attr('rel');

        if (rel && typeof rel != 'undefined') {
			
          switch (rel) {
            case opts.toggleAll:
              _toggleAll($this, $t);
              break;
            case opts.showAll: case opts.hideAll:
              _all($this, $t, rel);
              break;
            default:
              _one($this, $t);
              break;
          }
        } else {
          _one($this, $t);
		  
        }
      });
    });
  };

  jQuery.fn.togglable.defaults = {
    showLabel: 'show',
    hideLabel: 'hide',
    showAllLabel: 'show all',
    hideAllLabel: 'hide all',
    toggleAll: 'toggleall',
    showAll: 'showall',
    hideAll: 'hideall',
    effect: 'sliding',
    speed: 'slow',
    easing: null,
    onShow: function () { return; },
    onHide: function () { return; }
  };

  /** private methods **/

  var _toggleAll = function ($this, $t) {
    var opts = $this.data('opts');
    $this.each(function (i, e) {
      _one($this, $(e));
    });
  };

  var _all = function ($this, $t, type) {
    var opts = $this.data('opts');
    var visible = !(type == opts.showAll);

    if (visible)
      $t.html(opts.showAllLabel).attr('rel', opts.showAll);
    else
      $t.html(opts.hideAllLabel).attr('rel', opts.hideAll);

    $this.each(function (i, e) {
      _one($this, $(e), visible);
    });
  };

  var _one = function ($this, $t, visible) {
    var
      opts = $this.data('opts'),
      href = $t.attr('href'),
      rel = $t.attr('rel'),
      txt = '';

    if (href.match(/^#/) && href.length > 1)
      var id = href.substr(1);
    else
      return;

    var obj = jQuery('#' + id);

    if (typeof visible != 'boolean')
      visible = obj.is(':visible');

    var changed = (visible != $t.data('visible'));
    $t.data('visible', visible);

    if (visible) {
      obj[opts._effect[1]](opts.speed, opts._easing[1], opts.onHide);
      txt = opts.showLabel || 'show';
	  //document.getElementById("tabs").style.backgroundColor="#fff";
	  $("#tabs").addClass('selected');
	  $(".togglable").addClass('selected2');
    } else {
      obj[opts._effect[0]](opts.speed, opts._easing[0], opts.onShow);
      txt = opts.hideLabel || 'hide';
	  $("#tabs").removeClass('selected');
	  $(".togglable").removeClass('selected2');
    }

    if (rel && typeof rel != 'undefined') {
      if (changed) $t.attr('rel', $t.html()).html(rel);
    } else {
      $t.html(txt);
    }
  };

  // effect pairs
  var _effectPairs = {
    basic: ['show', 'hide'],
    sliding: ['slideDown', 'slideUp'],
    fading: ['fadeIn', 'fadeOut']
  };

  // easing pairs
  var _easingPairs = {
    quad: ['easeOutQuad', 'easeInQuad'],
    cubic: ['easeInCubic', 'easeOutCubic'],
    quart: ['easeInQuart', 'easeOutQuart'],
    quint: ['easeInQuint', 'easeOutQuint'],
    sine: ['easeInSine', 'easeOutSine'],
    expo: ['easeInExpo', 'easeOutExpo'],
    circ: ['easeInCirc', 'easeOutCirc'],
    elastic: ['easeInElastic', 'easeOutElastic'],
    back: ['easeInBack', 'easeOutBack'],
    bounce: ['easeInBounce', 'easeOutBounce'],
    other: [null, null]
  };

})(jQuery);