/*

###############################################################################
#
# Copyright (c) 2009 Logimake, Inc.
#
# Orignially distributed by bugzappy at http://www.bugzappy.com
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
##############################################################################


#
# Bugzappy's "expandBox" jquery plugin
#


Please post questions and comments at:

	http://www.bugzappy.com/2009/04/02/expandbox-jquery-plugin/

or send email to:

	bugzappy@logimake.com

*/


//
// create closure
//
(function() {
  //
  // plugin definition
  //
	jQuery.fn.expandBox = function(expandButton, shrinkButton, options)
	{
		var opts = jQuery.extend({}, jQuery.fn.expandBox.defaults, options);
		$expandButton = jQuery(expandButton);
		$shrinkButton = jQuery(shrinkButton);

		opts.box = this;
		opts.expandButton = $expandButton;
		opts.shrinkButton = $shrinkButton;

		this.data('expandBox', opts);
		$expandButton.data('expandBox', opts);
		$shrinkButton.data('expandBox', opts);

		jQuery.fn.expandBox.setMaxHeight(this, opts.shrunkHeight);

		jQuery.fn.expandBox.disableShrink(this);
		jQuery.fn.expandBox.enableExpand(this);
		jQuery.fn.expandBox.disableExpandIfNeeded(this);

		return this;
	}

	jQuery.fn.expandBox.defaults = {
	  expandDelay: 200,
	  shrinkDelay: 200,
	  expandHeightInc: 200,
	  shrunkHeight: 200,
          expandText: 'more ...',
          expandNoText: '&nbsp;',
          shrinkText: 'close',
          shrinkNoText: '&nbsp;'
	};

	jQuery.fn.expandBox.expand = function() {
		$box = jQuery(this).data('expandBox').box;

		newMaxHeight = $box.height() + $box.data('expandBox').expandHeightInc;
		jQuery.fn.expandBox.setMaxHeight($box, newMaxHeight);

		$box.animate({maxHeight: newMaxHeight+'px'}, $box.data('expandBox').delay, 'linear',
			function() {
//				alert(jQuery(this));
//				alert(jQuery(this).data('expandBox'));
//				alert(jQuery(this).data('expandBox').box.html()); return;
				$box = jQuery(this).data('expandBox').box;
//				alert($box.height() + '   ' + $box.css("max-height"));
				jQuery.fn.expandBox.disableExpandIfNeeded($box);
				jQuery.fn.expandBox.enableShrink($box);
			}
		);
	}

	jQuery.fn.expandBox.shrink = function() {
		$box = jQuery(this).data('expandBox').box;
		$box.unbind("click", jQuery.fn.shrink);
		$box.css({overflow: "hidden"});
		newMaxHeight = $box.data('expandBox').shrunkHeight;
		$box.animate({maxHeight: newMaxHeight+'px'}, $box.data('expandBox').shrinkDelay, 'linear',
			function() {
				$box = jQuery(this).data('expandBox').box;
				jQuery.fn.expandBox.disableShrink($box);
				jQuery.fn.expandBox.enableExpandIfNeeded($box);
			}
		);
		jQuery.fn.expandBox.setMaxHeight($jqObj, newMaxHeight);

	}

	jQuery.fn.expandBox.setMaxHeight = function($jqObj, newMaxHeight) {
		$box = $jqObj.data('expandBox').box;
		$expandButton = $jqObj.data('expandBox').expandButton;
		$shrinkButton = $jqObj.data('expandBox').shrinkButton;
		$box.data('expandBox').maxHeight = newMaxHeight;
		$expandButton.data('expandBox').maxHeight = newMaxHeight;
		$shrinkButton.data('expandBox').maxHeight = newMaxHeight;
	}

	jQuery.fn.expandBox.enableExpandIfNeeded = function($jqObj) {
		$box = $jqObj.data('expandBox').box;
		if ($box.height() >= $box.data("expandBox").maxHeight)
		{
			jQuery.fn.expandBox.enableExpand($box);
		}
	}

	jQuery.fn.expandBox.disableExpandIfNeeded = function($jqObj) {
		$box = $jqObj.data('expandBox').box;
//		alert($box.height());
//		alert($box.data('expandBox').maxHeight);
		if ($box.data("expandBox").maxHeight > $box.height())
		{
			jQuery.fn.expandBox.disableExpand($box);
		}
	}

	jQuery.fn.expandBox.enableExpand = function($jqObj) {
		$button = $jqObj.data('expandBox').expandButton;
		$button.click(jQuery.fn.expandBox.expand);
		$button.html($jqObj.data('expandBox').expandText);
	}


	jQuery.fn.expandBox.disableExpand = function($jqObj) {
		$button = $jqObj.data('expandBox').expandButton;
		$button.unbind("click", jQuery.fn.expandBox.expand);
		$button.html($jqObj.data('expandBox').expandNoText);
	}

	jQuery.fn.expandBox.enableShrink = function($jqObj) {
		$button = $jqObj.data('expandBox').shrinkButton;
		$button.click(jQuery.fn.expandBox.shrink);
		$button.html($jqObj.data('expandBox').shrinkText);
	}

	jQuery.fn.expandBox.disableShrink = function($jqObj) {
//		alert('disable shrink ' + opts);
		$button = $jqObj.data('expandBox').shrinkButton;
		$button.unbind("click", jQuery.fn.expandBox.shrink);
		$button.html($jqObj.data('expandBox').shrinkNoText);
	}

})();

