/**
 * @package    JMyLife
 * @subpackage Components
 * @version    1.0.12.1
 * @copyright  2009-2010 Jeff Channell
 * components/com_jmylife/assets/js/ajaxClick.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
	
 */
if( typeof jQuery != 'undefined' )
{
	jQuery(document).ready( function()
	{
		jQuery('a.sendfriend_link').each(function()
		{
			var el = jQuery(this);
			el.click(function()
			{
				el.css({'display':'none'}).next().css({'display':'inline'});
				return false;
			});
		});
		jQuery('.comJMyLifeItemVoteLink, a.favorite_link, a.unsubscribe_link, a.reportstory_link').each(function()
		{
			var el = jQuery(this);
			el.click(function()
			{
				var href = el.attr( 'href' ) + '&ajax=1';
				jQuery.ajax({
					type: "GET",
					url: href,
					success: function( data, textStatus, XMLHttpRequest )
					{
						el.text( data );
					}
				});
				return false;
			});
		});
		jQuery('.comJMyLifeItemGraphicVoteLink').each( function()
		{
			var el = jQuery(this);
			el.click(function()
			{
				var href = el.attr( 'href' ) + '&ajax=1';
				jQuery.ajax({
					type: "GET",
					url: href,
					success: function( data, textStatus, XMLHttpRequest )
					{
						var r = eval( '('+data+')' );
						if( r.success )
						{
							el.find('.comJMyLifeItemGraphicVoteText').text( r.msg );
							el.find('.comJMyLifeItemGraphicVoteCount').text( r.count );
						}
						else
						{
							alert( r.msg );
						}
					}
				});
				return false;
			});
		});
		var jmlEmailSend;
		if( jmlEmailSend = jQuery('input.jmlEmailSend') )
		{
			jmlEmailSend.each( function()
			{
				var el = jQuery(this);
				el.click( function()
				{
					var email = el.prev().val();
					var href  = el.next().val();
					href = href + ( href.match(/\?/)?'&':'?' ) + 'email='+email + '&ajax=1';
					jQuery.ajax({
						type: "GET",
						url: href,
						success: function( data, textStatus, XMLHttpRequest )
						{
							el.parent().html( data );
						}
					});
					return false;
				});
			});
		}
	});
}
else
{
	window.addEvent( 'load', function()
	{
		/*
		 * ajax link submission
		 */
		$$('a.sendfriend_link').each(function( el )
		{
			el.addEvent( 'click', function( e )
			{
				new Event(e).stop();
				el.setStyles({'display':'none'});
				el.getNext().setStyles({'display':'inline'});
				return false;
			});
		});
		$$('.comJMyLifeItemVoteLink, a.favorite_link, a.unsubscribe_link, a.reportstory_link').each(function( el )
		{
			el.addEvent( 'click', function( e )
			{
				new Event(e).stop();
				var href = el.getProperty( 'href' ) + '&ajax=1';
				try
				{
					new Ajax( href,
					{
						method: 'get',
						evalScripts: true,
						update: el
					}).request();
				}
				catch( err )
				{
					new Request({
						url: href,
						method: 'get',
						onComplete: function( response )
						{
							el.set( 'html', response );
						}
					}).send();
				}
				return false;
			});
		});
		// graphic vote needs some extra special attention
		$$('.comJMyLifeItemGraphicVoteLink').each( function( el )
		{
			el.addEvent( 'click', function( e )
			{
				new Event(e).stop();
				var href = el.getProperty( 'href' ) + '&ajax=1';
				var graphicVoteComplete = function( response )
				{
					var r = eval( '('+response+')' );
					if( r.success )
					{
						el.getElement('.comJMyLifeItemGraphicVoteText').setText( r.msg );
						el.getElement('.comJMyLifeItemGraphicVoteCount').setText( r.count );
					}
					else
					{
						alert( r.msg );
					}
				};
				try
				{
					new Ajax( href,
					{
						method: 'get',
						evalScripts: true,
						onComplete: graphicVoteComplete
					}).request();
				}
				catch( err )
				{
					new Request({
						url: href,
						method: 'get',
						onComplete: graphicVoteComplete
					}).send();
				}
				return false;
			});
		});
		// email send
		var jmlEmailSend;
		if( jmlEmailSend = $$('input.jmlEmailSend') )
		{
			jmlEmailSend.each( function( el )
			{
				el.addEvent( 'click', function( ev )
				{
					var email;
					var href;
					try
					{
						email = el.getPrevious().getValue();
						href  = el.getNext().getValue();
					}
					catch( err )
					{
						email = el.getPrevious().get('value');
						href  = el.getNext().get('value');
					}
					href = href + ( href.match(/\?/)?'&':'?' ) + 'email='+email + '&ajax=1';
					try
					{
						new Ajax( href,
						{
							method: 'get',
							update: el.getParent()
						}).request();
					}
					catch( err )
					{
						new Request({
							url: href,
							method: 'get',
							onComplete: function( response )
							{
								el.getParent().set( 'html', response );
							}
						}).send();
					}
				});
			});
		}
	});
}
