/*
 * TC Session @VERSION
 *
 * Copyright 2010, AUTHORS.txt (http://jqueryui.com/about)
 * Dual licensed under the MIT or GPL Version 2 licenses.
 * http://jquery.org/license
 *
 * http://docs.jquery.com/UI/Tabs
 *
 * Depends:
 *	jquery.ui.core.js
 *	jquery.ui.widget.js
 */
(function( $, undefined ) {

   $.fn.toggleBetween = function(state1, state2) {
    if ($(this).hasClass(state1)) {
      $(this).removeClass(state1);
      $(this).addClass(state2);
    } else {
      $(this).removeClass(state2);
      $(this).addClass(state1);
    }
   return $(this);
  };

$.widget( "ui.currentuser", {
	options: {
		disabled: [],
		enable: null,
		menuCloseAfter: null,
		idPrefix: "currentuser-"
	},

	_create: function() {
	  var self = this, el = self.element, input = el.clone(), headlink = null, panel = $('<div></div>');
	  self.timerClose = null;
	  el.empty();
	  el.addClass('ui-currentuser ui-layer-front');
	  headlink = el.html('<div><span>User:</span><a href="#" class="currentuser-name ui-state-default"> '+$(input).find('.user').contents().text()+' </a></div>').find('div');
	  $(headlink).addClass('ui-currentuser-titlebar ui-widget-header');
	  panel.hide().addClass('ui-currentuser-content ui-widget-content ui-state-default');
	  $('a', input).each( function(index) {
	    $(panel).append(
	      $(this).addClass('ui-state-default')
		.bind('mouseover', function() {
		$(this).removeClass('ui-state-default').addClass('ui-state-hover');
	      }).bind('mouseleave', function() {
		$(this).removeClass('ui-state-hover').addClass('ui-state-default');
	      })
	    );
	  });

	  if( this._getOption('menuCloseAfter')!==null) {
	    el.mouseleave( function(event) {
	      self._timeoutStart();
	      event.stopPropagation();
	      return false;
	    });
	    el.mouseover( function(event) {
	      self._timeoutReset();
	      event.stopPropagation();
	      return false;
	    });
	  }

	  el.append(panel);

	  $('a', headlink).click( function() {

	    $(this).toggleBetween('ui-state-default','ui-state-active');
	    $('.ui-currentuser-content', el)
	      .toggleBetween('ui-state-default','ui-state-active')
	      .animate( { height: 'toggle'}, 10 ); 
	  });
	},
	_timeoutStart: function () {
	  var timeArg = this._getOption('menuCloseAfter');
	  clearTimeout(this.timerClose);
	  this.timerClose = setTimeout( function() {
	  $('.ui-currentuser-content', this.element)
	    .toggleBetween('ui-state-default','ui-state-active')
	    .slideUp();
	  }, timeArg);
	},
	_timeoutReset: function () {
	  clearTimeout(this.timerClose);
	},
	_setOption: function( key, value ) {
	  this.options[ key ] = value;
	},
	_getOption: function( key ) {
	  return this.options[ key ];
	},
	destroy: function() {
	  // TODO: Remove classes and divs instead of remaking
	  var usr = $('a.currentuser-name', el.clone()).text().trim();
	  var prev = $('<div/>').html('<span class="user">'+usr+'</span>');
	  $('.ui-currentuser-content > a', el).each( function() {
	    $(prev).append('<a href="'+$(this).attr('href')+'">'+$(this).text()+'</a>');
	  });
	  el.removeClass('ui-currentuser ui-layer-front');
	  el.html(prev.html());
	  $.Widget.prototype.destroy.call( this );
	},

	enable: function() {
	  el.removeClass('ui-state-disabled');
	},

	disable: function() {
	  el.addClass('ui-state-disabled');
	}
});

$.extend( $.ui.currentuser, {
	version: "@VERSION"
});

/*
 * Widget Extensions
 */

})( jQuery );

