function raShowProductsSelector(xml, sessionId)
{
	jQuery('.raLoginForm').fadeOut('slow').remove();
	jQuery('.raLogin').append(jQuery('<div class="raProductSelector"><p>Please select product:</p><ul></ul></div>').mouseover(function(){
		jQuery(this).mouseleave(function(){
			jQuery(this).fadeOut('slow', function(){jQuery(this).remove();});
		});
	}).fadeIn('slow'));
	
	xml.find('shop').each(function(){
		raProductType 	= jQuery(this).attr('type');
		raProductId 	= jQuery(this).attr('id');
		raProductName 	= jQuery(this).text();
		jQuery('.raProductSelector ul').append('<li class="'+raProductType+'"><a href="http://redantenna.com/dashboard/Dashboard.action;jsessionid='+sessionId+'?shop='+raProductId+'">'+raProductName+'</a></li>');
	});
}

function crossDomainConnector(urlToConnect)
{
	return 'http://www.redantenna.com/wp-content/themes/coffeebreak/includes/raLoginCommunicator.php?url='+encodeURIComponent(urlToConnect);
}

jQuery(function(){
	jQuery('.raLogin a:first').click(function(){
		if(jQuery('.raLoginForm').length == 0)
		{
			var loginForm = jQuery('<div class="raLoginForm"><a href="#" class="close">X</a><form action="http://redantenna.com/user/Login.action?ajaxLogin" method="post" enctype="multipart/form-data"><p>E-mail:</p><input type="text" name="username"/> <p>Password:</p><input type="password" name="password"/><input type="image" src="http://blog.redantenna.com/wp-content/themes/coffeebreak/images/slider-arrow-right.png" class="submit"/></form></div>')
			jQuery(loginForm).find('a.close').click(function(){
								jQuery(this).parent().fadeOut('slow');
								if(jQuery('.statusMsg').length>0)
								{
									jQuery('.statusMsg').remove();
									jQuery('.raLoginForm form').show();
								}
								return false;
							});
			
			jQuery(loginForm).find('form').submit(function(){
						try{
				var theUrl = crossDomainConnector(jQuery(this).attr('action')
							+'&username='+jQuery(this).find('input[name=username]').attr('value')
							+'&password='+jQuery(this).find('input[name=password]').attr('value')
							+'&rand='+Math.random()+Math.random()+Math.random()+Math.random());

				jQuery.ajax({
					url:theUrl,
					cache:false,
					contentType:'xml',
					error:function(xhr, textStatus, errorThrown){
						alert("ERROR: Could not load the contents:\n"
							+xhr.statusText+'\n'
							+xhr.status+'\n'
							+xhr.responseText+'\n'
							+xhr.getAllResponseHeaders()+'\n'
							+textStatus+'\n'
							+errorThrown
							);
					},
					success:function(xml){
						if(jQuery(xml).find('login failures').length > 0)
						{
							statusMsg = jQuery(xml).find('login failures failure:first message:first').text();
							
							var raStatus = jQuery('<p class="statusMsg">'+statusMsg+' <a href="#">ok</a></p>');
							jQuery('.raLoginForm').find('form').hide().parent().append(raStatus).find('a:last').click(function(){
								jQuery('.statusMsg').remove();
								jQuery('.raLoginForm form').show();
								return false;
							});
						} else if(jQuery(xml).find('login success').length > 0) {
							var jsessionId = jQuery(xml).find('sessionid:first').text();
							if(jQuery(xml).find('login success shops shop').length > 1)
							{
								raShowProductsSelector(jQuery(xml).find('login success shops'), jsessionId);
							} else {
								var firstShop = jQuery(xml).find('login success shops shop:first');
								window.location='http://blue.redantenna.com/dashboard/Dashboard.action;jsessionid='+jsessionId+'?shop='+firstShop.attr('id');
							}
						} else {
							if(xml == '')
							{
								alert('couldn\'t receive the response xml');
							} else {
								alert('unknown failure');
							}
						}
					}
				});
						}catch(eg){alert(eg);}
				return false;
			});
			jQuery(this).parent().append(loginForm.fadeIn('slow'));
		} else {
			jQuery('.raLoginForm').fadeIn();
		}
		return false;
	});
						   });