/**
 * @package    JMyLife
 * @subpackage Components
 * @version    1.0.12.1
 * @copyright  2010 Jeff Channell
 * components/com_jmylife/assets/js/commentHover.js
 * @license    

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
	
 * @since      1.0.10
 */
function commentHover( $base )
{
	if( typeof jQuery != 'undefined' )
	{
		jQuery(document).ready( function()
		{
			jQuery('.comJMyLifeItemInfo').css({
				'position': 'relative',
				'z-index': 1
			});
			jQuery('.comJMyLifeItemReadComments a').each( function()
			{
				var el = jQuery(this);
				if( el.hasClass( 'noballoon' ) ){ return; }
				var $w = parseInt( el.width() ) + 5;
				var $rel = el.attr('rel');
				var $href = $base + 'index.php?option=com_jmylife&controller=ajax&task=getlatestcomments&id=' + $rel;
				el.parent().css('position','relative');
				var $balloon = jQuery('<div></div>').css({
					'position': 'absolute',
					'display': 'none',
					'z-index': '99',
					'top': '0px',
					'left': $w+'px'
				}).addClass('jmylifeBalloon jmylifeBalloonLoading jmylifeRound');
				el.after( $balloon );
				el.hover(function()
				{
					$balloon.show().css('z-index','99');
					jQuery.ajax({
						type: "GET",
						url: $href,
						success: function( data, textStatus, XMLHttpRequest )
						{
							$balloon.removeClass('jmylifeBalloonLoading').css('background-image','none').html( data );
						}
					});
				},
				function()
				{
					$balloon.addClass('jmylifeBalloonLoading').hide().empty();
				});
			});
		});
	}
	else
	{
		window.addEvent( 'load', function()
		{
			$$('.comJMyLifeItemInfo').setStyles({'position':'relative','z-index':'1'});
			$$('.comJMyLifeItemReadComments a').each( function( el )
			{
				if( el.hasClass( 'noballoon' ) ){ return; }
				// get link width
				var $w = parseInt( el.getSize().size.x ) + 5;
				var $rel = el.getAttribute('rel');
				// change this !!
				var $href = $base + 'index.php?option=com_jmylife&controller=ajax&task=getlatestcomments&id=' + $rel;
				// set parent to position: relative
				el.getParent().setStyles({ 'position': 'relative' });
				// create and inject balloon
				var $balloon;
				$balloon = new Element('div');
				$balloon.setStyles({
					'position': 'absolute',
					'display': 'none',
					'z-index': '99',
					'top': '0px',
					'left': $w+'px'
				}).addClass('jmylifeBalloon').addClass('jmylifeBalloonLoading').addClass('jmylifeRound');
				$balloon.injectAfter( el );
				// add events to link
				el.addEvent( 'mouseenter', function( ev )
				{
					$balloon.setStyles({ 'display': 'block', 'z-index': '99' });
					// get comments via ajax
					try
					{
						new Ajax( $href,
						{
							method: 'get',
							update: $balloon,
							onComplete: function()
							{
								// set background image to none to make it work in IE pre-8
								$balloon.removeClass('jmylifeBalloonLoading').setStyles({'background-image':'none'});
							}
						}).request();
					}
					catch( err )
					{
						new Request({
							url: $href,
							method: 'get',
							onComplete: function( response )
							{
								$balloon.removeClass('jmylifeBalloonLoading').setStyles({'background-image':'none'}).set( 'html', response );
							}
						}).send();
					}
				});
				el.addEvent( 'mouseleave', function( ev )
				{
					$balloon.addClass('jmylifeBalloonLoading').setStyles({'display':'none'}).empty();
				});
			});
		});
	}
}
