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('.rateMe').length)
				{
					_content.find( '.buttonBlue' ).bind( 'click', formRatingTrigger );
					_type = 1;
				}else 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)
			{
				// Xigen - added some height to this div to see it loaded. Not part of fix
				_content.find('.alertNotificationPopupContent').wrapInner('<div class="content">');
				//_content.find('.alertNotificationPopupContent').wrapInner('<div class="content" style="height:150px;">');
				_content.find('.alertNotificationPopupContent .content').before('<div class="top"></div>').after('<div class="bottom"></div>');
				
			}			
			
			// Xigen - start of fix
			var x = 0, y = 0;
			
			// Iterate through all the parents until you find one without a static position. Record the offset of that parent
			_element.parents().each(function() {
				if ( $(this).css('position') != "static" ) {
					x = $(this).offset().left;
					y = $(this).offset().top;
					return false;
				}
			});
			
			// Re-write the top and left rules to re-position the popup
			_content.css( {
						 left: position.left - x + 'px',
						 top: position.top - y - _element.height() - 20 + 'px',
						 opacity:0
						 } );
			// Xigen - end of fix
			
			_content.animate( { opacity:1 }, 300, 'swing' );
		}

		e.preventDefault();
	}

		
	function formRatingTrigger(e)
	{
		var selectedVal = -1;
		
		_content.find( 'input' ).each( function() {
			if(this.checked){
				selectedVal = this.value;
			}
		});

		if (selectedVal != -1 )
		{
			loader();
			var data1="articleRating="+selectedVal+"";
			jQuery.ajax({ type: "POST", url: _url, data: data1, success: handleRatingResult, dataType: "json"}); //modified by avinash as part of 1.4 defect fix
			postRatingSubmit();
		}
		else
		{
			_content.find( 'p.txtError' ).show();
		}

		e.preventDefault();
	}

	function handleRatingResult( json )
	{
		hideLoader();
		if ( json && json.result )
		{
			setTimeout( function()
				{
					_content.animate( { opacity:0 }, 500, 'swing', function() { _content.remove() } );					
				}, 5000 );

			var orig_msg = json.result;

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

			var x,y;
			x = _content.parents('#ProductList, #Popup').length ? jQuery('#ProductList, #Popup').offset().left : 0;
			y = _content.parents('#ProductList, #Popup').length ? jQuery('#ProductList, #Popup').offset().top : 0;

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

	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 postRatingSubmit()
	{
		document.getElementById("ratingArticleLink").style.visibility="hidden";
		document.getElementById("ratingSubmit").style.visibility="visible";		
	}

	function handleResult( json )
	{
		hideLoader();

		if ( json && json.result )
		{
			// Xigen - removed self-removing function for ease of testing. Not part of fix
			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 ); } );
				}, 5000 );
			}

			var orig_msg = json.result.message;
			
			var orig_email = orig_msg.match(/([a-zA-Z0-9\.\-_]+)@([a-zA-Z0-9\.\-_]+)/)[0];
			var new_email = orig_email.split('').join(String.fromCharCode('8203'));
			orig_msg = orig_msg.replace(orig_email,new_email)

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

			var x,y;
			x = _content.parents('#ProductList, #Popup').length ? jQuery('#ProductList, #Popup').offset().left : 0;
			y = _content.parents('#ProductList, #Popup').length ? jQuery('#ProductList, #Popup').offset().top : 0;

			var position = _element.offset();
			_content.css( { top:position.top-_content.height()-y+'px', left:position.left-x+'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();
	}
}
