Epson.ProductAlerts = function( element )
{
	var _element = element;
	var _url = null;
	var _type = 0;
	var _content;
	var _active = true;

	setup();

	function setup()
	{
		_url = _element[0].className.match( /an-url-([^\s]+)/ );

		if ( _url )
		{
			_url = _url[1];

			if ( _element.next( '.alertNotificationPopup' ).length )
			{
				_content = _element.next( '.alertNotificationPopup' );
				
				if(_content.find('.buttonBlue').length)
				{
					_content.find( '.buttonBlue' ).bind( 'click', formTrigger );
					_content.find( 'input' ).bind( 'keydown', function(e) { if ( e.keyCode == 13 ) { formTrigger(e); e.stopPropagation(); return false } } );
					_type = 1;
				}
			}
			else _content = jQuery( '<div class="alertNotificationPopup"></div>' );
	
			_element.bind( 'click', trigger );
		}
	}

	function trigger(e)
	{
		if ( _active )
		{
			_active = false;

			if ( _type == 0 )
			{
				// logged in
				_element.parent().append( _content );
				loader();
				jQuery.getJSON( _url, null, handleResult );
			}
			else
			{
				setTimeout( function() { jQuery( 'body' ).bind( 'click', close ) }, 100 );
				_content.bind( 'click', unclose );
			}

			var position = _element.offset();	
			
			if (!_content.find('.content').length)
			{
				_content.find('.alertNotificationPopupContent').wrapInner('<div class="content">');
				_content.find('.alertNotificationPopupContent .content').before('<div class="top"></div>').after('<div class="bottom"></div>');
			}			
			
			_content.css( { top:position.top-_content.height()+'px', left:position.left+'px', opacity:0 } );
			_content.animate( { opacity:1 }, 300, 'swing' );
		}

		e.preventDefault();
	}

	function formTrigger(e)
	{
		var email_address = _content.find( 'input' ).val();

		if ( email_address.length && email_address.match( /(^[a-zA-Z0-9\.\-_]+)@([a-zA-Z\.\-])+\.([a-zA-Z]+)$/ ) )
		{
			loader();
			jQuery.getJSON( _url, { email:email_address }, handleResult );
		}
		else
		{
			_content.find( 'p.txtError' ).show();
		}

		e.preventDefault();
	}

	function handleResult( json )
	{
		hideLoader();

		if ( json && json.result )
		{
			if ( json.result.success )
			{
				setTimeout( function()
				{
					_content.animate( { opacity:0 }, 500, 'swing', function() { _content.remove() } );					
					_element.animate( { opacity:0 }, 500, 'swing', function() { _element.remove(); jQuery( 'body' ).unbind( 'click', close ); _content.unbind( 'click', unclose ); } );
				}, 1500 );
			}

			// display result message
			var resultMsg = '<div class="top"></div><div class="content">' + json.result.message + '</div><div class="bottom"></div>' ;
			_content.find( '.alertNotificationPopupContent' ).append( resultMsg );

			var position = _element.offset();
			_content.css( { top:position.top-_content.height()+'px', left:position.left+'px'} );
		}
	}

	function close(e)
	{
		jQuery( 'body' ).unbind( 'click', close );
		_content.unbind( 'click', unclose );

		_content.css( { top:'-4000px', opacity:0 } );
		
		_active = true;
	}

	function unclose(e)
	{
		e.stopPropagation();
	}

	function loader()
	{
		_content.find( '.alertNotificationPopupContent' ).empty();
		_content.find( '.alertNotificationPopupContent' ).append( jQuery( '<div class="loader"></div>' ) );
	}

	function hideLoader()
	{
		_content.find( '.alertNotificationPopupContent' ).empty();
	}
}